Found 10784 Articles for Python

Program to find length of contiguous strictly increasing sublist in Python

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

161 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

467 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

462 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

167 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

Program to make all elements equal by performing given operation in Python

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

420 Views

Suppose we have given a list of numbers called nums, we want to make the values equal. Now let an operation where we pick one element from the list and increment every other value. We have to find the minimum number of operations required to make element values equal.So, if the input is like [2, 4, 5], then the output will be 5.To solve this, we will follow these steps −min_val := minimum of numss := 0for each num in nums, dos := s + (num - min_val)return sLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Program to perform given operation with each element of a list and given value in Python

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

274 Views

Suppose we have a list of numbers called nums, we also have another string op representing operator like "+", "-", "/", or "*", and another value val is also given, we have to perform the operation on every number in nums with val and return the result.So, if the input is like [5, 3, 8], then the output will be [15, 9, 24]To solve this, we will follow these steps −res:= a new listfor each i in nums, doif op is same as '+', theninsert i+val at the end of resotherwise when op is same as '-', theninsert i-val at ... Read More

Program to convert Linked list representing binary number to decimal integer in Python

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

1K+ Views

Suppose we have a singly linked list. the linked list is representing a binary number with most significant digits first, we have to return it as decimal number.So, if the input is like [1, 0, 1, 1, 0], then the output will be 22To solve this, we will follow these steps −l := a new listwhile node is not null, doinsert value of node at the end of lnode:= next of nodek := 0, v:= 0for i in range size of l - 1 to 0, decrease by 1, doif l[i] is same as 1, thenv := v + 2^kk ... Read More

Program to remove all nodes of a linked list whose value is same as in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:08:18

331 Views

Suppose we have a singly linked list, and one target, we have to return the same linked after deleting all nodes whose value is same as target.So, if the input is like [5, 8, 2, 6, 5, 2, 9, 6, 2, 4], then the output will be [5, 8, 6, 5, 9, 6, 4, ]To solve this, we will follow these steps −head := nodewhile node and node.next are not null, dowhile value of next of node is same as target, donext of node := next of next of nodenode := next of nodeif value of head is same as ... Read More

Program to find Lexicographically Smallest String With One Swap in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:06:36

3K+ Views

Suppose we have a string s, we have to find the lexicographically smallest string that can be made if we can make at most one swap between two characters in the given string s.So, if the input is like "zyzx", then the output will be "xyzz"To solve this, we will follow these steps −temp := an array of size s and fill with 0m:= size of s - 1for i in range size of s -1 to -1, decrease by 1, doif s[i] < s[m], thenm := itemp[i] := mfor i in range 0 to size of s, doa := ... Read More

Advertisements