Convert Gray Code to Binary in Python

AmitDiwan
Updated on 19-Apr-2021 10:49:47

1K+ Views

When it is required to convert gray code to binary code, a method is defined, that checks to see if the number is 0 or not.Below is the demonstration of the same −Example Live Demodef flip_num(my_nu):    return '1' if(my_nu == '0') else '0'; def gray_to_binary(gray):    binary_code = ""    binary_code += gray[0]    for i in range(1, len(gray)):       if (gray[i] == '0'):          binary_code += binary_code[i - 1]       else:          binary_code += flip_num(binary_code[i - 1])    return binary_code gray_code = "01101001" print("The gray code ... Read More

Generate Gray Codes Using Recursion in Python

AmitDiwan
Updated on 19-Apr-2021 10:49:28

423 Views

When it is required to generate gray codes with the help of recursion, a method is defined, that creates an empty list and appends values 0 and 1 to it. Multiple ‘for’ loops are used to generate the gray code within the function.Below is the demonstration of the same −Example Live Demoimport math as mt def generate_gray_list(my_val):    if (my_val = 1

Clear the Rightmost Set Bit of a Number in Python

AmitDiwan
Updated on 19-Apr-2021 10:48:49

417 Views

When it is required to clear the rightmost bit of a number which was previously set, the ‘&’ operator can be used.Below is the demonstration of the same −Example Live Demodef clear_right_bit(my_val):    return my_val & (my_val-1) n_val = 6 print("The vlaue of n is :") print(n_val) print("The number after unsetting the rightmost set bit is ") print(clear_right_bit(n_val))OutputThe vlaue of n is : 6 The number after unsetting the rightmost set bit is 4ExplanationA method is defined that takes an integer as a parameter.It computes the ‘&’ operation between the number and the number decremented by 1.Outside the method, an integer ... Read More

Search Number of Occurrences in a List using Python

AmitDiwan
Updated on 19-Apr-2021 10:48:29

386 Views

When it is required to search the frequency of a number in a list, a method is defined, that takes a list and the number. It iterates through the list, and every time the number is encountered, the counter is incremented.Below is the demonstration of the same −Example Live Demodef count_num(my_list, x_val):    my_counter = 0    for elem in my_list:       if (elem == x_val):          my_counter = my_counter + 1    return my_counter my_list = [ 66, 26, 48, 140, 66, 20, 1, 96, 86] print("The list is :") print(my_list) occ_number = 66 ... Read More

Determine All Pythagorean Triplets in the Range Using Python

AmitDiwan
Updated on 19-Apr-2021 10:40:17

2K+ Views

When it is required to determine the Pythagorean triplets within a given range, a method is defined, that helps calculate the triplet values.Below is the demonstration of the same −Example Live Demodef pythagorean_triplets(limits) :    c, m = 0, 2    while c < limits :    for n in range(1, m) :       a = m * m - n * n       b = 2 * m * n       c = m * m + n * n       if c > limits :          break   ... Read More

Compute the Value of Euler's Number e in Python

AmitDiwan
Updated on 19-Apr-2021 10:39:18

1K+ Views

When it is required to implement Euler’s number, a method is defined, that computes the factorial.Another method is defined that find the sum of these factorial numbers.Below is the demonstration of the same −Example Live Demodef factorial_result(n):    result = 1    for i in range(2, n + 1):       result *= i    return result def sum_result(n):    s = 0.0    for i in range(1, n + 1):       s += 1.0 / factorial_result(i)    print(s) my_value = 5 print("The value is :") print(my_value) print("The result is :") sum_result(my_value)OutputThe value is : ... Read More

Print Numbers in a Range Without Using Loops in Python

AmitDiwan
Updated on 19-Apr-2021 10:39:00

549 Views

When it is required to print the numbers in a given range without using any loop, a method is defined, that keeps displays numbers from higher range by uniformly decrementing it by one after every print statement.Below is the demonstration of the same −Example Live Demodef print_nums(upper_num):    if(upper_num>0):       print_nums(upper_num-1)       print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim)OutputThe upper limit is : 6 The numbers are : 1 2 3 4 5 6ExplanationA method named ‘print_nums’ is defined.It checks if the upper limit is greater than 0.If ... Read More

Convert Nested Tuple to Custom Key Dictionary in Python

AmitDiwan
Updated on 19-Apr-2021 10:38:39

682 Views

When it is required to convert a nested tuple into a customized key dictionary, the list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_tuple = ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) print("Thw tuple is : ") print(my_tuple) my_result = [{'key': sub[0], 'value': sub[1], 'id': sub[2]}    for sub in my_tuple] print("The converted dictionary is : ") print(my_result)OutputThw tuple is : ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) The converted dictionary is : [{'key': 6, 'value': 'Will', 'id': 13}, {'key': 2, 'value': 'Mark', 'id': 15}, {'key': 9, 'value': 'Rob', ... Read More

Flatten Tuple of List to Tuple in Python

AmitDiwan
Updated on 19-Apr-2021 10:38:15

317 Views

When it is required to flatten tuple of a list to tuple, a method is defined, that takes input as tuple.The tuple is iterated over, and the same method is called on it over and over again until the result is obtained.Below is the demonstration of the same −Example Live Demodef flatten_tuple(my_tuple):    if isinstance(my_tuple, tuple) and len(my_tuple) == 2 and not isinstance(my_tuple[0], tuple):       my_result = [my_tuple]       return tuple(my_result)    my_result = []    for sub in my_tuple:       my_result += flatten_tuple(sub)    return tuple(my_result) my_tuple = ((35, 46), ((67, ... Read More

Find the Nth Palindrome Number in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:37:23

526 Views

ProblemWe are required to write a JavaScript function that takes in number n. Our function should return the nth palindrome number if started counting from 0.For instance, the first palindrome will be 0, second will be 1, tenth will be 9, eleventh will be 11 as 10 is not a palindrome.ExampleFollowing is the code − Live Democonst num = 31; const findNthPalindrome = (num = 1) => {    const isPalindrome = (num = 1) => {       const reverse = +String(num)       .split('')       .reverse()       .join('');       return reverse === num;    };    let count = 0;    let i = 0;    while(count < num){       if(isPalindrome(i)){          count++;       };       i++;    };    return i - 1; }; console.log(findNthPalindrome(num));OutputFollowing is the console output −212

Advertisements