Found 26504 Articles for Server Side Programming

Program to count number of operations required to convert all values into same in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:18:22

240 Views

Given a list of integers nums, you can perform the following operation: pick the largest number in nums and turn it into the second largest number. Return the minimum number of operations required to make all integers the same in the list.So, if the input is like nums = [5, 9, 2], then the output will be 3, as pick 9 first, then make it 5, so array is [5, 5, 2], then pick 5 and make 2, [5, 2, 2], again pick 5 and convert into 2, [2, 2, 2].To solve this, we will follow these stepsvals := sort ... Read More

How to interpolate data values into strings in Python?

Kiran P
Updated on 10-Nov-2020 09:19:45

244 Views

We can interpolate data values into strings using various formats. We can use this to dedug code, produce reports, forms, and other outputs. In this topic, We will see three ways of formatting strings and how to interpolate data values into strings.Python has three ways of formatting strings:% - old school (supported in Python 2 and 3)() - new style (Python 2.6 and up){} - f-strings (Python 3.6 and up)Old style: %The old style of string formatting has the form format_string % data. The format strings are nothing but interpolation sequences.syntaxdescription%sstring%ddecimal%xhexa-int%ooctal-int%fdecimal-float%eexponential-float%gdecimal or exponential-float%%literal %# printing integer with % style type. ... Read More

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

254 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

919 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

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

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

602 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

297 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

198 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

466 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

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

Advertisements