Server Side Programming Articles - Page 1471 of 2650

Program to check whether we can make group of two partitions with equal sum or not in Python?

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

260 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

How to add command line arguments in Python?

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

943 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

Program to enclose pattern into bold tag in Python?

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

397 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

Program to find number of distinct coin sums we can make with coins and quantities in Python?

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

615 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

Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?

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

305 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

Program to find length of longest diminishing word chain in Python?

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

207 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

Program to find cost to remove consecutive duplicate characters with costs in C++?

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

478 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

Program to print maximum number of characters by copy pasting in n steps in Python?

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

122 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

Program to count minimum number of operations required to make numbers non coprime in Python?

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

331 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

Program to find next state of next cell matrix state in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:54:17

191 Views

Suppose we have a 2D binary matrix where a 1 means a live cell and a 0 means a dead cell. A cell's neighbors are its immediate horizontal, vertical and diagonal cells. We have to find the next state of the matrix using these rulesAny living cell with two or three living neighbors lives.Any dead cell with three living neighbors becomes a live cell.All other cells die.So, if the input is like1100010001011101then the output will be1100010001001100To solve this, we will follow these steps:n := row size of matrix, m := column size of matrixres := a matrix of size n ... Read More

Advertisements