Check If Number Has Only First and Last Bits Set in Python

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

176 Views

Suppose we have a number n. We have to check whether the number has only two set bits at first and last position or not.So, if the input is like n = 17, then the output will be True as the binary representation of n is 10001, there is only two 1's at first and last position.To solve this, we will follow these steps −if n is same as 1, thenreturn Truereturn true if n - 1 is power of 2, otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef is_pow_of_two(n):    return (n & ... Read More

Check if Concatenated Number is a Perfect Square in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:59:00

180 Views

Suppose we have two numbers x and y. We have to concatenate them and check whether the resultant number is perfect square or not.So, if the input is like x = 2 y = 89, then the output will be True as after concatenating the number will be 289 which is 17^2.To solve this, we will follow these steps −first_num := x as stringsecond_num := y as stringres_num := concatenate first_num and second_num then convert to integersqrt_val := integer part of square root of(res_num)if sqrt_val * sqrt_val is same as res_num, thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Check If Number Can Be Made Perfect Square After Adding 1 in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:58:32

560 Views

Suppose we have a number n. We have to check whether the number can be a perfect square number by adding 1 with it or not.So, if the input is like n = 288, then the output will be True as after adding 1, it becomes 289 which is same as 17^2.To solve this, we will follow these steps −res_num := n + 1sqrt_val := integer part of square root of(res_num)if sqrt_val * sqrt_val is same as res_num, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demofrom math import sqrt def solve(n):   ... Read More

Check Length of Linked List in Python: Even or Odd

Arnab Chakraborty
Updated on 16-Jan-2021 04:57:08

205 Views

Suppose we have a linked list, we have to check whether the length of it is odd or even.So, if the input is like head = [5, 8, 7, 4, 3, 6, 4, 5, 8], then the output will be Odd.To solve this, we will follow these steps −while head is not null and next of head is not null, dohead := next of next of headif head is null, thenreturn "Even"return "Odd"Let us see the following implementation to get better understanding −Example CodeLive Democlass ListNode:    def __init__(self, data, next = None):       self.val = data   ... Read More

Check Valid Identifier in Python

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

857 Views

Suppose we have a string representing an identifier. We have to check whether it is valid or not. There are few criteria based on which we can determine whether it is valid or not.It must start with underscore '_' or any uppercase or lowercase lettersIt does not contain any whitespaceAll the subsequent characters after the first one must not consist of any special characters like $, #, % etc.If all of these three are valid then only the string is valid identifier.So, if the input is like id = "_hello_56", then the output will be True.To solve this, we will ... Read More

Check if Given Numbers are Cousin Prime in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:55:30

335 Views

Suppose we have a pair of integers. We have to check whether they are cousin primes or not. Two numbers are said to be cousin primes when both are primes and differ by 4.So, if the input is like pair = (19, 23), then the output will be True as these are two primes and their difference is 4 so they are cousin primes.To solve this, we will follow these steps −if difference between two elements is not 4, thenreturn Falsereturn true when both are prime, otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef ... Read More

Check If a Number is Wagstaff Prime in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:53:55

140 Views

Suppose we have a number n. We have to check whether n is Wagstaff prime or not. As we know Wagstaff prime is a prime number which is in the following form.where q is an odd prime number.So, if the input is like n = 683, then the output will be True n can be represented asSo here q = 11. And q is odd prime.To solve this, we will follow these steps −if num is prime and (num*3 - 1) is also prime, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef isPrime(num): ... Read More

Check if a Number is Euclid Number in Python

Arnab Chakraborty
Updated on 16-Jan-2021 04:52:49

234 Views

Suppose we have a number n. We have to check whether n is Euclid number or not. As we know Euclid numbers are integer which can be represented as n= Pn+1where  is product of first n prime numbers.So, if the input is like n = 211, then the output will be True n can be represented as 211=(2×3×5×7)+1To solve this, we will follow these steps −MAX := 10000primes := a new listDefine a function generate_all_primes() . This will takeprime := a list of size MAX and fill with Truex := 2while x * x < MAX, doif prime[x] is True, thenfor i ... Read More

Check Character Frequencies for Primality in Python

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

218 Views

Suppose we have a string s. We have to check whether the occurrences of each character in s is prime or notSo, if the input is like s = "apuuppa", then the output will be True as there are two 'a's, three 'p's and two 'u's.To solve this, we will follow these steps −freq := a map containing all characters and their frequenciesfor each char in freq, doif freq[char] > 0 and freq[char] is not prime, thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example CodeLive Demofrom collections import defaultdict def isPrime(num):    if num ... Read More

Check Average Character Presence in String using Python

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

122 Views

Suppose we have a string s that contains alphanumeric characters, we have to check whether the average character of the string is present or not, if yes then return that character. Here the average character can be found by taking floor of average of each character ASCII values in s.So, if the input is like s = “pqrst”, then the output will be 'r' because the average of character ASCII values are (112 + 113 + 114 + 115 + 116)/5 = 570/5 = 114 (r).To solve this, we will follow these steps −total := 0for each ch in s, ... Read More

Advertisements