Found 10476 Articles for Python

Check if a number is Primorial Prime or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:55

812 Views

Suppose we have a number n, we have to check whether n is a primorial prime or not. A number is said to be a primorial prime when it is a prime number of the form pN# + 1 or pN# – 1 , where pN# indicates the primorial of pN such that the product of first N prime numbers.So, if the input is like 29, then the output will be True as 29 is Primorial prime of the form pN - 1 if N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.To solve this, we will follow these ... Read More

Check if a number is an Achilles number or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:38:56

279 Views

Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is ... Read More

Check if a number is a Trojan Numbers in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:35:01

144 Views

Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More

Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:26:52

400 Views

Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9

Check if a key is present in every segment of size k in an array in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:23:50

171 Views

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More

Check if a given string is a valid number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:20:44

744 Views

Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.To solve this, we will follow these steps −To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.ExampleLet us see the following implementation to get better understanding − Live Demodef isNumeric(s): ... Read More

Check if a given Binary Tree is Heap in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:13:42

593 Views

Suppose we have a binary tree; we have to check whether it is heap or not. The heap has following property: Heap will be a binary tree That tree should be a complete tree (So. all levels except last should be full). Every nodes value of that tree should be greater than or equal to its child node (max-heap).So, if the input is likethen the output will be trueTo solve this, we will follow these steps −Define a function number_of_nodes() . This will take rootif root is null, thenreturn 0otherwise, return(1 + number_of_nodes(root.left) + number_of_nodes(root.right))Define a function has_heap_property() . This ... Read More

Check if a given Binary Tree is height balanced like a Red-Black Tree in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:09:19

147 Views

Suppose there is a Red-Black Tree, here the largest height of a node is at most double the minimum height. If we have a binary search tree, we have to check the following property. With respect of every node, length of the longest leaf to node path has not more than double the nodes on shortest path from node to leaf.So, if the input is likethen the output will be True, as this is balanced.To solve this, we will follow these steps −Define a function solve() . This will take root, max_height, min_heightif root is null, thenmax_height := 0, min_height ... Read More

Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:04:35

460 Views

Suppose we have a string str containing these brackets '(', ')', '{', '}', '[' and ']', we have to check whether brackets are balanced or not. We can say brackets are balanced when Opening and closing bracket types are of same type. Brackets are closed in correct order.So, if the input is like {([])}, then the output will be True.To solve this, we will follow these steps −cnt := 0i := 0j := -1Define a function solve() . This will take s, tempcnt := cnt - 1s := a new list from sif j > -1 and s[j] is same ... Read More

Find the Largest Cube formed by Deleting minimum Digits from a number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:59:58

163 Views

Suppose we have a number N, we have to determine the largest perfect cube that can be generated by removing minimum digits (possibly 0) from the number. We can delete any digit from the given number to reach the target. As we know a number N is called a perfect cube if N = M^3 for some integer M.So, if the input is like 806, then the output will be 8, as we can delete 0 and 6 from the number, then we will get 8, this is perfect cube of 2.To solve this, we will follow these steps −Define ... Read More

Advertisements