Found 10476 Articles for Python

Check whether sum of digits at odd places of a number is divisible by K in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:48:35

128 Views

Suppose we have a number n and another number k, we have to check whether the sum of digits of n at it's odd places (from right side to left side) is divisible by k or not.So, if the input is like n = 2416 k = 5, then the output will be True as sum of odd placed numbers from right to left is 4 + 6 = 10. Which is divisible by 5.To solve this, we will follow these steps −total := 0, pos := 1while n > 0 , doif pos is odd, thentotal := total + ... Read More

Check whether second string can be formed from characters of first string in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:46:51

495 Views

Suppose we have two strings s and t. We have to check whether t can be formed using characters of s or not.So, if the input is like s = "owleh" t = "hello", then the output will be True.To solve this, we will follow these steps −freq := a map containing all characters and their frequenciesfor i in range 0 to size of t - 1, doif freq[t[i]] is 0, thenreturn Falsefreq[t[i]] := freq[t[i]] - 1return TrueLet us see the following implementation to get better understanding −Example CodeLive Demofrom collections import defaultdict   def solve(s, t):    freq = ... Read More

Check if a number can be expressed as a^b in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:46:15

370 Views

Suppose we have a number n. We have to check whether we can make express it like a^b or not.So, if the input is like 125, then the output will be True as 125 = 5^3, so a = 5 and b = 3To solve this, we will follow these steps −if num is same as 1, then:return truefor initialize i := 2, when i * i

Check whether right angled triangle is valid or not for large sides in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:45:16

746 Views

Suppose we have three sides in a list. We have to check whether these three sides are forming a right angled triangle or not.So, if the input is like sides = [8, 10, 6], then the output will be True as (8^2 + 6^2) = 10^2.To solve this, we will follow these steps −sort the list sidesif (sides[0]^2 + sides[1]^2) is same as sides[2]^2, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(sides):    sides.sort()    if (sides[0]*sides[0]) + (sides[1]*sides[1]) == (sides[2]*sides[2]):       return True    return False     sides = [8, 10, 6] print(solve(sides))Input[8, 10, 6] OutputTrue

Check whether product of integers from a to b is positive, negative or zero in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:43:56

200 Views

Suppose we have lower limit and upper limit of a range [l, u]. We have to check whether the product of the numbers in that range is positive or negative or zero.So, if the input is like l = -8 u = -2, then the output will be Negative, as the values in that range is [-8, -7, -6, -5, -4, -3, -2], then the product is -40320 so this is negative.To solve this, we will follow these steps −if l and u both are positive, thenreturn "Positive"otherwise when l is negative and u is positive, thenreturn "Zero"otherwise, n := ... Read More

Check whether product of digits at even places of a number is divisible by K in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:42:45

138 Views

Suppose we have a number n, and another number k, we have to check whether the product of digits at even places of n is divisible by k or not. Places are started counting from right to left. Right most is at place 1.So, if the input is like n = 59361, then the output will be True as (1*3*5) is divisible by 3.To solve this, we will follow these steps −digit_count := digit count of given number nprod := 1while n > 0, doif digit_count is even, thenprod := prod * last digit of nn := quotient of (n ... Read More

Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:42:05

577 Views

Suppose we have a number n, we have to check whether the product of digits at even places of n is divisible by sum of digits at odd place of n or not. Places are started counting from right to left. Right most is at place 1.So, if the input is like n = 59361, then the output will be True as (1*3*5) = (6+9).To solve this, we will follow these steps −digit_count := digit count of given number ntotal := 0, prod := 1while n > 0, doif digit_count is even, thenprod := prod * last digit of notherwise, ... Read More

Check whether product of \\'n\\' numbers is even or odd in Python

Akshitha Mote
Updated on 17-Dec-2024 15:57:36

589 Views

In this article, we try to find different methods to solve a problem to check whether the product of n numbers is even or odd. A number that is divisible by 2 is known as an even number. otherwise; it is an odd number. For example, 14 and 12 are two even numbers, the product is 168 is an even number. 9 and 5 are two odd numbers, the product is 45 is an odd number. Consider one even number 2 and one odd number 3, product of these two numbers is 6 an even number. Facts to Check if ... Read More

Check whether N is a Dihedral Prime Number or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:38:01

237 Views

Suppose we have a number n. We have to check whether n is dihedral prime or not. A number is said to be dihedral prime when that number is itself prime and also shown same number or any other prime number using 7-segment display regardless the orientation of the display (normal or up-side down).So, if the input is like n = 1181, then the output will be Truesecond one is up-side down format of the first one and both are prime.To solve this, we will follow these steps −Define a function up_side_down() . This will take ntemp := n, total ... Read More

Check whether K-th bit is set or nots in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:36:45

3K+ Views

Suppose we have a number n and another value k. We have to check whether the kth bit in n is set (1) or not. The value of k is considered from right hand side.So, if the input is like n = 23, k = 3, then the output will be True as binary form of 23 is 10111 so the third last bit is 1 (set).To solve this, we will follow these steps −temp := n after shifting bits (k - 1) times to the rightif temp AND 1 is 1, thenreturn Truereturn FalseLet us see the following implementation ... Read More

Advertisements