AmitDiwan has Published 10744 Articles

Python Program to Convert Binary to Gray Code

AmitDiwan

AmitDiwan

Updated on 19-Apr-2021 10:52:13

2K+ Views

When it is required to convert binary code to gray code, a method is defined that performs the ‘xor’ operation.Below is the demonstration of the same −Example Live Demodef binary_to_gray_op(n):    n = int(n, 2)    n ^= (n >> 1)    return bin(n)[2:] gray_val = input('Enter the binary ... Read More

Python Program to Convert Gray Code to Binary

AmitDiwan

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 = "" ... Read More

Python Program to Generate Gray Codes using Recursion

AmitDiwan

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 ... Read More

Python Program to Clear the Rightmost Set Bit of a Number

AmitDiwan

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 ... Read More

Python Program to Search the Number of Times a Particular Number Occurs in a List

AmitDiwan

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, ... Read More

Python Program to Determine all Pythagorean Triplets in the Range

AmitDiwan

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, ... Read More

Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!

AmitDiwan

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):     ... Read More

Python Program to Print Numbers in a Range (1,upper) Without Using any Loops

AmitDiwan

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):     ... Read More

Convert Nested Tuple to Custom Key Dictionary in Python

AmitDiwan

AmitDiwan

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

681 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], ... Read More

Flatten tuple of List to tuple in Python

AmitDiwan

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 ... Read More

Advertisements