Check Divisibility of Product of Digits at Even Places in Python

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

152 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 Divisibility of Product of Even Place Digits by Sum of Odd Place Digits in Python

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

612 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 If n is a Dihedral Prime Number in Python

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

265 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 Not 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

Make Both Arrays Equal by Modifying a Single Element in Python

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

246 Views

Suppose we have two arrays nums1 and nums2 and another value k. We have to check whether both arrays can be made equal by modifying any one element from the nums1 in the following way (only once): We can add any value from the range [-k, k] to any element of nums1.So, if the input is like nums1 = [5, 7, 11] nums2 = [5, 5, 11] k = 8, then the output will be True as we can add -2 (in range [-8, 8]) with nums1[1] to make it 5 then it will be same as nums2.To solve this, ... Read More

Check Adjacent Primes in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:34:09

203 Views

Suppose we have three numbers and we have to check whether they are adjacent primes are not. The adjacent primes are prime numbers where no other prime is present between them.So, if the input is like nums = [5, 7, 11], then the output will be True.To solve this, we will follow these steps −if any one of these three numbers is not prime themreturn Falseif next prime of x is not same as y, thenreturn Falseif next prime of y is not same as z, thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example CodeLive ... Read More

Check If String Can Be Generated by Concatenating Given Strings in Python

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

179 Views

Suppose we have two strings s and t and r, we have to check whether r = s | t or r = t + s where | denotes concatenation.So, if the input is like s = "world" t = "hello" r = "helloworld", then the output will be True as "helloworld" (r) = "hello" (t) | "world" (s).To solve this, we will follow these steps −if size of r is not same as the sum of the lengths of s and t, thenreturn Falseif r starts with s, thenif r ends with t, thenreturn Trueif r starts with t, ... Read More

Check If Floating Point Number is Even or Odd in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:29:44

1K+ Views

Suppose we have a floating point number; we have to check whether the number is odd or even. In general, for integer it is easy by dividing the last digit by 2. But for floating point number it is not straight forward like that. We cannot divide last digit by 2 to check if it is odd or even.So, if the input is like n = 200.290, then the output will be Odd though the last digit is divisible by 2.To solve this, we will follow these steps −s := convert number as stringflag := Falsefor i in range size ... Read More

Check Degrees of Vertices for Graph or Tree in Python

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

257 Views

Suppose we have a list of degrees of some vertices. We have to check whether it is forming graph or tree.So, if the input is like deg = [2,2,3,1,1,1], then the output will be TreeTo solve this, we will follow these steps −vert := number of verticesdeg_sum := sum of all degree values of all verticesif 2*(vert-1) is same as deg_sum, thenreturn 'Tree'return 'Graph'Let us see the following implementation to get better understanding −Example CodeLive Demodef solve(deg):    vert = len(deg)    deg_sum = sum(deg)          if 2*(vert-1) == deg_sum:       return 'Tree'    return 'Graph' deg = [2,2,3,1,1,1] print(solve(deg))Input[2,2,3,1,1,1] OutputTree

Check Circle Residing in Boundary of Two Other Circles in Python

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

195 Views

Suppose we have two radius values r1 and r2 of two concentric circles. We have another input coordinate coord and a radius value r. We have to check whether the circle whose center is placed at coord and it fits inside the boundary of two given concentric circles.So, if the input is like r1 = 4 r2 = 2 coord = (3, 0) r = 1, then the output will be True.To solve this, we will follow these steps −val := square root of(x^2 + y^2)if val + r = r1 - r2, thenreturn Truereturn FalseLet us see the following ... Read More

Advertisements