Perform Excel Spreadsheet Operations in Python

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

380 Views

Suppose we have a 2D matrix representing an excel spreadsheet. We have to find the same matrix with all cells and formulas computed. An excel spreadsheet looks like belowB17035=A1+A2The columns are named as (A, B, C...) and rows are (1, 2, 3....) Each cell will either contain a value, a reference to another cell, or an excel formula for an operation with between numbers or cell reference. (Example. "=A1+5", "=A2+B2", or "=2+5")So, if the input is likeB17035=A1+A2then the output will be7703510as the B1 = 7 (The first row second column) and "=A1 + A2" is 7 + 3 = 10.To ... Read More

Interpolate Data Values into Strings in Python

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

250 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

Count Operations to Convert All Values to Same in Python

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

248 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

Check Two Partitions with Equal Sum in Python

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

261 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

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

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

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

617 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

307 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

210 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

Advertisements