Python Articles - Page 397 of 1048

Program to count number of BST with n nodes in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:46:13

351 Views

Suppose we have n different nodes. All are distinct. We have to find how many number of ways we can arrange them to form Binary search tree. As we know for binary search trees, the left subtree always hold smaller values and right subtrees hold the greater values.To solve this, we shall find the Catalan number. The Catalan number C(n) represents the binary search trees with n different keys. The formula is like$$C(n)=\frac{(2n)!}{(n+1)!\times n!}$$So, if the input is like n = 3, then the output will be 5 becauseTo solve this, we will follow these steps −Define a function ncr() ... Read More

Program to filter all values which are greater than x in an array

Arnab Chakraborty
Updated on 12-Oct-2021 09:38:55

716 Views

Suppose we have a list of numbers called nums. We also have another number x. We have to find all numbers from nums which are less than x by filtering. In we use python there is one filter() method that takes function as argument and filter using this function.So, if the input is like nums = [1, 5, 8, 3, 6, 9, 12, 77, 55, 36, 2, 5, 6, 12, 87] x = 50, then the output will be [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]To solve this, we will follow these steps −define a ... Read More

Program to check points are forming concave polygon or not in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:40:56

834 Views

Suppose we have outer points of a polygon in clockwise order. We have to check these points are forming a convex polygon or not. A polygon is said to be concave if any one of its interior angle is greater than 180°.From this diagram it is clear that for each three consecutive points the interior angle is not more than 180° except CDE.So, if the input is like points = [(3, 4), (4, 7), (7, 8), (8, 4), (12, 3), (10, 1), (5, 2)], then the output will be True.To solve this, we will follow these steps −n := size ... Read More

Program to find replicated list by replicating each element n times

Arnab Chakraborty
Updated on 12-Oct-2021 09:36:55

569 Views

Suppose we have a list of n elements; we have to repeat each element in the list n number of times.So, if the input is like nums = [1,5,8,3], then the output will be [1, 1, 1, 1, 5, 5, 5, 5, 8, 8, 8, 8, 3, 3, 3, 3]To solve this, we will follow these steps −n := size of numsret := a new listfor each num in nums, doret := ret concatenate a list with n number of numsreturn retExampleLet us see the following implementation to get better understandingdef solve(nums):    n = len(nums)    ret = []    for num in nums:       ret += [num]*n    return ret nums = [1,5,8,3] print(solve(nums))Input[1,5,8,3] Output[1, 1, 1, 1, 5, 5, 5, 5, 8, 8, 8, 8, 3, 3, 3, 3]

Program to find super digit of a number in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:36:13

3K+ Views

Suppose we have a number n. We have to find the super digit of this number. The super digit of a single digit number is the digit itself but for multi-digit numbers super digit is the sum of all digits repeatedly until the sum is a single digit number.So, if the input is like n = 513682, then the output will be 7 because (5+1+3+6+8+2) = 25, (2 + 5) = 7.To solve this, we will follow these steps −s := 0while n > 0 or s > 9, doif n is same as 0, thenn := ss := 0s ... Read More

Program to find number of values factors of two set of numbers

Arnab Chakraborty
Updated on 12-Oct-2021 09:33:33

109 Views

Suppose we have two arrays called nums1 and nums2. We have to find the number of values that satisfy the following conditions −The elements in nums1 are the factors of the elements which are being selectedThe elements which are selected is a factor of all of the elements of nums2So, if the input is like nums1 = [3, 9] nums2 = [27, 81], then the output will be 2 because the numbers are 9 and 27, because9 mod 3 = 09 mod 9 = 027 mod 9 = 081 mod 9 = 027 mod 3 = 027 mod 9 = ... Read More

Program to check points are forming convex hull or not in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:30:12

817 Views

Suppose we have outer points of a polygon in clockwise order. We have to check these points are forming a convex hull or not.From this diagram it is clear that for each three consecutive points the interior angle is not more than 180°. So if all angles are not more than 180° then the polygon is convex hull.So, if the input is like points = [(3, 4), (4, 7), (7, 8), (11, 6), (12, 3), (10, 1), (5, 2)], then the output will be True.To solve this, we will follow these steps −n := size of pointsfor i in range ... Read More

Program to find ith element by rotating k times to right

Arnab Chakraborty
Updated on 12-Oct-2021 09:17:26

165 Views

Suppose we have an array nums, and a value k and another value i. We have to find the element at index i after rotating elements of nums, k number of times to the right.So, if the input is like nums = [2, 7, 9, 8, 10] k = 3 i = 2, then the output will be 10 because after 3rd rotation array will be [9, 8, 10, 2, 7], so now the ith element will be nums[2] = 10.To solve this, we will follow these steps −for r in range 0 to k, dodelete last element from nums ... Read More

Program to count number of common divisors of two numbers in Python

Arnab Chakraborty
Updated on 12-Oct-2021 14:04:06

651 Views

Suppose we have two numbers a and b. We have to find how many positive integers are there, that are divisors to both a and b.So, if the input is like a = 288 b = 240, then the output will be 10 because the common divisors are [1, 2, 3, 4, 6, 8, 12, 16, 24, 48].To solve this, we will follow these steps −res := 0for i in range 1 to gcd(a, b) + 1, doif (a mod i) is 0 and (b mod i) is 0, thenres := res + 1return resExampleLet us see the following implementation ... Read More

Python program to validate postal address format

Arnab Chakraborty
Updated on 12-Oct-2021 09:15:26

1K+ Views

Suppose we have a postal code we have to check whether it is valid or not. A valid postal code has following criteriaIt must be a number in the range from 100000 to 999999 (both inclusive).It must not contain more than one alternating repetitive digit pair.So, if the input is like s = "700035", then the output will be True as this is in range 100000 to 999999 and there are no consecutive digits either.To solve this, we will follow these steps −n := size of snb := 0ok := Truefor i in range 0 to n - 1, dook ... Read More

Advertisements