Found 10476 Articles for Python

Program to check whether we can make k palindromes from given string characters or not in Python?

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

195 Views

Suppose we have a string s and another number k, we have to check whether we can create k palindromes using all characters in s or not.So, if the input is like s = "amledavmel" k = 2, then the output will be True, as we can make "level" and "madam".To solve this, we will follow these stepsd := a map where store each unique characters and their frequencycnt := 0for each key in d, doif d[key] is odd, thencnt := cnt + 1if cnt > k, thenreturn Falsereturn TrueLet us see the following implementation to get better understandingExamplefrom collections ... Read More

Program to find minimum number of Fibonacci numbers to add up to n in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:28:33

353 Views

Suppose we have a number n; we have to find the minimum number of Fibonacci numbers required to add up to n.So, if the input is like n = 20, then the output will be 3, as We can use the Fibonacci numbers [2, 5, 13] to sum to 20.To solve this, we will follow these stepsres := 0fibo := a list with values [1, 1]while last element of fibo n, dodelete last element from fibon := n - last element of fibores := res + 1return resLet us see the following implementation to get better understandingExampleclass Solution:   ... Read More

How to create charts in excel using Python with openpyxl?

Kiran P
Updated on 10-Nov-2020 09:32:41

2K+ Views

In this post, I will show you how to create charts in excel using Python - Openpyxl module. We will create an excel spreadsheet from scratch with Tennis players grandslam titles as the data for creating bar charts using the openpyxl module.Introduction..Microsoft office has started providing a new extension to Microsoft Excel sheets, which is .xlsx, from Office 2007 to support storing more rows and columns.This change had moved Excel sheets to a XML based file format with ZIP compression. The world is ruled by Microsoft spreadsheets, people have been using spreadsheets for various purposes and one of the use ... Read More

Program to perform excel spreadsheet operation in Python?

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

366 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

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

234 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

241 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

250 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

912 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

382 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

593 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

Advertisements