Found 26504 Articles for Server Side Programming

Check whether the vowels in a string are in alphabetical order or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:05:30

419 Views

Suppose we have a string s. We have to check whether the vowels present in s are in alphabetical order or not.So, if the input is like s = "helloyou", then the output will be True as vowels are e, o, o, u all are in alphabetical order.To solve this, we will follow these steps −character := character whose ASCII is 64for i in range 0 to size of s - 1, doif s[i] is any one of ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'), thenif s[i] < character, thenreturn Falseotherwise, character := s[i]return TrueLet us see ... Read More

Check whether the two numbers differ at one-bit position only in Python

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

494 Views

Suppose we have two numbers x and y. We have to check whether these two numbers differ at one-bit position or not.So, if the input is like x = 25 y = 17, then the output will be True because x = 11001 in binary and y = 10001. Only one-bit position is different.To solve this, we will follow these steps −z = x XOR yif number of set bits in z is 1, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example CodeLive Demodef bit_count(n):    count = 0    while n:       ... Read More

Check whether the sum of prime elements of the array is prime or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:03:01

176 Views

Suppose we have an array nums. We have to check whether the sum of all prime elements in the given array is also prime or notSo, if the input is like nums = [1, 2, 4, 5, 3, 3], then the output will be True as sum of all primes are (2+5+3+3) = 13 and 13 is also prime.To solve this, we will follow these steps −MAX := 10000sieve := a list of size MAX and fill with trueDefine a function generate_list_of_primes()sieve[0] := False, sieve[1] := Falsefor i in range 2 to MAX - 1, doif sieve[i] is true, thenfor ... Read More

Check whether the sum of absolute difference of adjacent digits is Prime or not in Python

Arnab Chakraborty
Updated on 16-Jan-2021 05:01:44

216 Views

Suppose we have a number n. We have to check whether the sum of the absolute difference of adjacent digit pairs is prime or not.So, if the input is like n = 574, then the output will be True as |5-7| + |7-4| = 5, this is prime.To solve this, we will follow these steps −num_str := n as stringtotal := 0for i in range 1 to size of num_str - 1, dototal := total + |digit at place num_str[i - 1] - digit at place num_str[i]|if total is prime, thenreturn Truereturn FalseLet us see the following implementation to get ... Read More

Check whether the point (x, y) lies on a given line in Python

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

2K+ Views

Suppose we have a straight line in the form y = mx + b, where m is slope and b is y-intercept. And have another coordinate point (x, y). We have to check whether this coordinate point lies on that straight line or not.So, if the input is like m = 3 b = 5 point = (6, 23), then the output will be True as if we put the given x and y coordinate values on the straight line equation then it will satisfy.To solve this, we will follow these steps −if y of point is same as (m ... Read More

Check whether the number has only first and last bits set in Python

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

178 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 whether the number formed by concatenating two numbers is a perfect square or not in Python

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

183 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 whether the number can be made perfect square after adding 1 in Python

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

564 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 whether the length of given linked list is Even or Odd in Python

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

208 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 whether the given string is a valid identifier in Python

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

862 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

Advertisements