Found 10476 Articles for Python

Check whether it is possible to make both arrays equal by modifying a single element in Python

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

223 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 whether given three numbers are adjacent primes in Python

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

180 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 whether given string can be generated after concatenating given strings in Python

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

153 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 whether given 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 whether given degrees of vertices represent a Graph or Tree in Python

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

240 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 whether given circle resides in boundary maintained by two other circles in Python

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

176 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

Check if the given number K is enough to reach the end of an array in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:52:11

163 Views

Suppose we have an array nums and another value k. We have to check whether it is possible to reach the end of the array by performing these operations or not Operation: Traverse nums and, if any non-prime value is there then decrement the value of k by 1. Now if any value is prime then refill the value of k to its initial value.So, if the input is like nums = [8, 5, 6, 7, 8], k = 2, then the output will be True as nums[0] is not prime, then make k = 1, then nums[1] is prime ... Read More

Check if the given number is Ore number or not in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:50:43

348 Views

Suppose we have a number n. We have to check whether n is an ore number or not. As we know an ore number is a number whose divisors have an integer harmonic value.So, if the input is like 28, then the output will be True as there are six divisors of 28: [1, 2, 4, 7, 14, 28], soAs 3 is an integer so 28 is an ore number.To solve this, we will follow these steps −Define a function get_all_div() . This will take ndiv := a new listfor i in range 1 to integer part of (square root ... Read More

Check if the given decimal number has 0 and 1 digits only in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:48:54

563 Views

Suppose we have a number num. We have to check whether num is consists of only 0s and 1s or not.So, if the input is like num = 101101, then the output will be True.To solve this, we will follow these steps −digits_set := a new set with all elements digits of numdelete 0 from digits_setdelete 1 from digits_setif size of digits_set is same as 0, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(num):    digits_set = set()    while num > 0:       digit = num % 10 ... Read More

Check if the given array contains all the divisors of some integer in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:47:59

109 Views

Suppose we have an array nums, we have to check whether this array is containing all of the divisors of some integer or not.So, if the input is like nums = [1, 2, 3, 4, 6, 8, 12, 24], then the output will be True as these are the divisors of 24.To solve this, we will follow these steps −maximum := maximum of numstemp := a new listfor i in range 1 to square root of maximum, doif maximum is divisible by i, theninsert i at the end of tempif quotient of (maximum / i) is not same as i, ... Read More

Advertisements