Check Two Partitions with Equal Sum in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:16:24

251 Views

Suppose we have a list of numbers called nums, we have to check whether we can partition nums into two groups where the sum of the elements in both groups are same.So, if the input is like nums = [2, 3, 6, 5], then the output will be True, as we can make groups like: [2, 6] and [3, 5].To solve this, we will follow these stepstotal := sum of all elements in numsif total is odd, thenreturn Falsehalf := integer part of total / 2dp := a list of size half + 1 and fill with falsedp[0] := truefor ... Read More

Add Command Line Arguments in Python

Kiran P
Updated on 10-Nov-2020 09:15:18

917 Views

Introduction..Python has a very powerful argparse module which provides functions for parsing command line arguments. If we want to get the user input from the OS command line without a lot of interaction or code a program that accepts parameters from the command line e.g. Provide a URL to parse or accepts the file to upload to a S3 bucket then argparse can be used with minimal effort.Basic UsageDefine the arguments that your code is going to accept.Call the argument parser to return the results object.Use the arguments.In short, the structure of argument parser looks some thing like below.def main( ... Read More

Enclose Pattern into Bold Tag in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:14:34

386 Views

Suppose we have a text and a list of strings called patterns, we have to define an embolden function where all substrings in text that match any string in the given patterns are wrapped in  and  tags. If any two patterns are adjacent or overlap, they should be merged into one tag.So, if the input is like text = "thisissampleline" patterns = ["this", "issam", "sample"], then the output will be "abcdefg", as bc and ef match the text and are wrapped in and tags.To solve this, we will follow these stepsn := size of textbold := a list ... Read More

Restrict Argument Values Using Choice Options in Python

Kiran P
Updated on 10-Nov-2020 09:13:26

3K+ Views

Introduction..Assume you are asked to code a program to accept the number of tennis grandslam titles from the user and process them. We already know, Federer and Nadal share the maximum grandslam titles in Tennis which is 20 (As of 2020) while the minimum is 0, lot of players are still fighting to get their first grandslam title.Let us create a program to accept the titles.Note - Execute the program from terminal.Exampleimport argparse def get_args(): """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, ... Read More

Find Number of Distinct Coin Sums with Given Coins in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:12:25

599 Views

Suppose we have a list of values called coins and another list called quantities of the same length. The value of ith coin is coins[i] and we currently have quantities[i] number of ith coin. We have to find number of distinct coin sum values we can get by using non-empty group of these coins.So, if the input is like coins = [1, 2, 5] quantities = [1, 2, 1], then the output will be 10, as we can have the following distinct coin sums [1] = 1, [2] = 2, [1, 2] = 3, [2, 2] = 4, [5] = ... Read More

Find Pair (i, j) to Maximize nums[i] + nums[j] in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:10:15

296 Views

Suppose we have a list of numbers called nums, we have to find a pair (i, j) where i < j, and nums[i] + nums[j] + (i - j) is maximized.So, if the input is like nums = [6, 6, 2, 2, 2, 8], then the output will be 11, as if we pick the two 6 then its score is 6 + 6 + 0 - 1 = 11.To solve this, we will follow these steps:large := nums[0]maxi := 0for i in range 1 to size of nums, dolarge := large - 1maxi := maximum of large + nums[i] ... Read More

Find Length of Longest Diminishing Word Chain in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:08:50

197 Views

Suppose we have a list of valid words, and have a string s also, we have to find the length of the longest chain of diminishing words that can be generated by starting at s and removing single letters and still make valid words.So, if the input is like words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"] s = "limit", then the output will be 4, as we can make the chain, starting from the word "limit", "limit" -> "limi" -> "lii" -> "li".To solve this, we will follow these stepsDefine a function solve() . This will take ... Read More

Find Cost to Remove Consecutive Duplicate Characters in C++

Arnab Chakraborty
Updated on 10-Nov-2020 09:07:01

465 Views

Suppose we have a string with lowercase letters and we also have a list of non-negative values called costs, the string and the list have the same length. We can delete character s[i] for cost costs[i], and then both s[i] and costs[i] is removed. We have to find the minimum cost to delete all consecutively repeating characters.So, if the input is like s = "xxyyx" nums = [2, 3, 10, 4, 6], then the output will be 6, as we can delete s[0] and s[3] for a total cost of 2 + 4 = 6.To solve this, we will follow ... Read More

Print Maximum Number of Characters by Copy-Pasting in N Steps in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:00:23

115 Views

Suppose we have a number n; we have to find the maximum number of characters we can enter using n operations where each operation is likeInserting the character "x".Copy all characters.PasteSo, if the input is like n = 12, then the output will be 81.To solve this, we will follow these stepsif n

Count Minimum Operations to Make Numbers Non-Coprime in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:57:53

323 Views

Suppose we have two numbers A and B. Now in each operation, we can select any one of the number and increment it by 1 or decrement it by 1. We have to find the minimum number of operations we need such that the greatest common divisor between A and B is not 1.So, if the input is like A = 8, B = 9, then the output will be 1, as we can select 9 then increase it to 10, so 8 and 10 are not coprime.To solve this, we will follow these steps:if gcd of a and b ... Read More

Advertisements