Found 10476 Articles for Python

Program to find an element in list whose value is same as its frequency in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:34:55

196 Views

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency in the list is same as its value or not.So, if the input is like [2, 4, 8, 10, 4, 4, 4], then the output will be TrueTo solve this, we will follow these steps −res := a new map to store value wise frequencyfor each key value pair (k, v) in res, doif k is same as v, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):     ... Read More

Program to find the minimum cost to arrange the numbers in ascending or descending order in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:32:43

858 Views

Suppose we have a list of numbers called nums, we have to find the minimum cost to sort the list in any order (Ascending or Descending). Here the cost is the sum of differences between any element's old and new value.So, if the input is like [2, 5, 4], then the output will be 2.To solve this, we will follow these steps −temp:= copy the array numssort the list tempc1:= 0, c2:= 0n:= size of numsfor i in range 0 to n, doif nums[i] is not same as temp[i], thenc1 := c1 + |nums[i]-temp[i]|if nums[i] is not same as temp[n-1-i], ... Read More

Program to merge two sorted list to form larger sorted list in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:31:29

224 Views

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −x:= a new listi:= 0, j:= 0while i < size of (lst0) and j < size of (lst1), doif lst0[i] > lst1[j], theninsert lst1[j] at the end of xj:= j+1otherwise when lst0[i]

Program to split a list of numbers such that the absolute difference of median values are smallest in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:29:13

159 Views

Suppose we have a list of numbers called nums, we have to divide it into two parts of same size where the absolute difference between each list's median is as small as possible and we have to find this difference. We have to keep in mind that here length of nums / 2 will be odd.So, if the input is like [2, 10, 8, 5, 4, 7], then the output will be 2, as we can make two lists like [2, 5, 10] and [4, 7, 8], then the medians are 5 and 7, their difference is 2.To solve this, ... Read More

Program to count how many times we can find "pizza" with given string characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:27:07

183 Views

Suppose we have a lowercase string s, we have to find how many "pizza" strings we can make using the characters present in s. We can use the characters in s in any order, but each character can be used once.So, if the input is like "ihzapezlzzilaop", then the output will be 2.To solve this, we will follow these steps −p_freq := Frequency of 'p' in si_freq := Frequency of 'i' in sz_freq := Frequency of 'z' in sa_freq := Frequency of 'a' in sreturn minimum of (p_freq, i_freq, z_freq/2 and a_freq)Let us see the following implementation to get better ... Read More

Program to find length of contiguous strictly increasing sublist in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:37:26

211 Views

Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist when we can remove one or zero elements from the list.So, if the input is like nums = [30, 11, 12, 13, 14, 15, 18, 17, 32], then the output will be 7, as when we remove 18 in the list we can get [11, 12, 13, 14, 15, 17, 32] which is the longest, contiguous, strictly increasing sublist, and its length is 7.To solve this, we will follow these steps−n := size of numspre := a list ... Read More

Program to equal two strings of same length by swapping characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:25:37

604 Views

Suppose we have two strings s and t of length n. We can take one character from s and another from t and swap them. We can make unlimited number of swaps; we have to check whether it's possible to make the two strings equal or not.So, if the input is like s = "xy", t = "yx", then the output will be TrueTo solve this, we will follow these steps −st:= sort the string after concatenating s and tfor i in range 0 to size of st - 1, increase by 2, doif st[i] is not same as st[i+1], ... Read More

Program to find length of substring with consecutive common characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:23:26

539 Views

Suppose we have a string s, we have to find the length of the longest substring with same characters.So, if the input is like "abbbaccabbbba", then the output will be 4, as there are four consecutive b's.To solve this, we will follow these steps −if size of s is 0, thenreturn 0s := s concatenate blank spacect:= 1, tem:= 1for i in range 0 to size of s -2, doif s[i] is same as s[i+1], thentem := tem + 1otherwise, ct:= maximum of tem and cttem:= 1return ctLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find longest common prefix from list of strings in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:21:16

2K+ Views

Suppose we have a list of lowercase strings, we have to find the longest common prefix.So, if the input is like ["antivirus", "anticlockwise", "antigravity"], then the output will be "anti"To solve this, we will follow these steps −sort the list words alphabeticallyprefix := a new listflag := 0for i in range 0 to size of words[0], dofor each j in words, doif j[i] is not same as last element of prefix, thendelete last element from prefixflag := 1come out from the loopif flag is same as 1, thencome out from the loopreturn string after concatenating all elements present in prefix ... Read More

Program to replace each element by smallest term at left side in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:19:25

247 Views

Suppose we have a list of numbers called nums, we have to replace every nums[i] with the smallest element left of i. We have to replace nums[0] with 0.So, if the input is like [15, 7, 9, 16, 12, 25], then the output will be [0, 15, 7, 7, 7, 7]To solve this, we will follow these steps −if nums is empty, thenreturn a new listj:= nums[0]nums[0]:= 0for i in range 1 to size of nums - 1, dok:= nums[i]nums[i]:= jj:= minimum of j, kreturn numsLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def ... Read More

Advertisements