Found 10784 Articles for Python

Program to check the string is repeating string or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:39:32

102 Views

Suppose we have a string, we have to check whether it's a repeating string or not.So, if the input is like string = "helloworldhelloworld", then the output will be TrueTo solve this, we will follow these steps −n := size of sDefine a function findFactors() . This will take nf := a new seti := 1while i * i

Program to count k length substring that occurs more than once in the given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:36:43

155 Views

Suppose we have a string s and a number k, we have to find the number of k-length substrings of s, that occur more than once in s.So, if the input is like s = "xxxyyy", k = 2, then the output will be 2To solve this, we will follow these steps −seen := a new listfor i in range 0 to size of s - k, dot := substring of s [from index i to i + k - 1]insert t at the end of seenmp := a map for all distinct element in seen and their occurrencesreturn sum ... Read More

Program to find sum of digits until it is one digit number in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:34:42

3K+ Views

Suppose we have a positive number n, we will add all of its digits to get a new number. Now repeat this operation until it is less than 10.So, if the input is like 9625, then the output will be 4.To solve this, we will follow these steps −Define a method solve(), this will take nif n < 10, thenreturn ns := 0l := the floor of (log(n) base 10 + 1)while l > 0, dos := s + (n mod 10)n := quotient of n / 10l := l - 1return solve(s)Let us see the following implementation to get ... Read More

Program to check one string can be converted to another by removing one element in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:33:00

126 Views

Suppose we have two strings s and t, we have to check whether we can get t by removing 1 letter from s.So, if the input is like s = "world", t = "wrld", then the output will be True.To solve this, we will follow these steps −i:= 0n:= size of swhile i < n, dotemp:= substring of s[from index 0 to i-1] concatenate substring of s[from index i+1 to end]if temp is same as t, thenreturn Truei := i + 1return FalseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s, t): ... Read More

Program to find duplicate elements and delete last occurrence of them in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:31:41

284 Views

Suppose we have a list of numbers A, we have to find all duplicate numbers and remove their last occurrences.So, if the input is like [10, 30, 40, 10, 30, 50], then the output will be [10, 30, 40, 50]To solve this, we will follow these steps −seen:= a new mapd:= a new mapfor i in range 0 to size of nums, doif nums[i] is not in d, thend[nums[i]]:= 1otherwise, d[nums[i]] := d[nums[i]] + 1i:= 0while i < size of nums, don:= d[nums[i]]if nums[i] is not in seen, thenseen[nums[i]]:= 1otherwise, seen[nums[i]] := seen[nums[i]] + 1if n is same as seen[nums[i]] ... Read More

Program to count number of elements present in a set of elements with recursive indexing in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:29:51

171 Views

Suppose we have a list of numbers called A and another number k, we have to make a new set of possible elements {A[k], A[A[k]], A[A[A[k]]], ... } stopping before it's out of index. We have to find the size of this set, otherwise -1 when there is a cycle.So, if the input is like A = [1, 2, 3, 4, 5, 6, 7], k = 1, then the output will be 6 as A[1] = 2, A[2] = 3, A[3] = 4, A[4] = 5, A[5] = 6, A[6] = 7, So the set is {2, 3, 4, 5, ... Read More

Program to find the index of first Recurring Character in the given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:28:37

315 Views

Suppose we have a string s, we have to find the index of the first recurring character in it. If we cannot find no recurring characters, then return -1.So, if the input is like "abcade", then the output will be 3, as 'a' is again present at index 3.To solve this, we will follow these steps −define a map charsfor i in range 0 to size of s, doif s[i] in chars, thenreturn iotherwise, chars[s[i]] := chars[s[i]] + 1return -1Let us see the following implementation to get better understanding −Example Live Demofrom collections import defaultdict class Solution:    def solve(self, s): ... Read More

Program to check two rectangular overlaps or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:26:20

3K+ Views

Suppose we have a rectangle that is represented as a list with four elements [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Two rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap.So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True.To solve this, we will follow these steps −if R1[0]>=R2[2] or R1[2]=R2[2]) or (R1[2]

Program to check if the given list has Pythagorean Triplets or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:24:11

989 Views

Suppose we have a list of numbers called nums, we have to check whether there exist three numbers a, b, and c such that a^2 + b^2 = c^2.So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8^2 + 6^2 = 64+36 = 100 = 10^2.To solve this, we will follow these steps −tmp := list of square of all numbers in nums in descending orderfor each index i and corresponding number n in tmp, dobase := nleft := i+1, right := size of tmp -1while left base, thenleft := ... Read More

Program to find all prime factors of a given number in sorted order in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:20:24

580 Views

Suppose we have a number n greater than 1, we have to find all of its prime factors and return them in sorted sequence. We can write out a number as a product of prime numbers, they are its prime factors. And the same prime factor may occur more than once.So, if the input is like 42, then the output will be [2, 3, 7].To solve this, we will follow these steps −res:= a new listwhile n mod 2 is same as 0, doinsert 2 at the end of resn := quotient of n/2for i in range 3 to (square ... Read More

Advertisements