Python Program to Solve Rod Cutting Problem using Dynamic Programming with Memoization

This is a Python program to solve the rod-cutting problem using dynamic programming with top-down approach or memoization. Problem Description In the rod-cutting problem, we are given a rod of length n inches and a table of prices p[i] for i = 1, 2, …, n. Here p[i] is the price of a rod of … Read more

advertisement

Python Program to Convert Binary to Gray Code

This is a Python program to convert binary to Gray code. Problem Description We are given a binary number. We have to find the associated Gray codeword. Problem Solution 1. The function binary_to_gray is defined. 2. It takes the binary number as a string as argument. 3. It returns its associated Gray codeword as a … Read more

advertisement

Python Program to Convert Gray to Binary Code

This is a Python program to convert Gray code to binary. Problem Description We are given a Gray codeword. We have to find the associated binary number. Problem Solution 1. The function gray_to_binary is defined. 2. It takes the Gray codeword string as argument. 3. It returns its associated binary number as a string. 4. … Read more

advertisement

Python Program to Generate Gray Codes using Recursion

This is a Python program to generate all gray codes using recursion Problem Description The number of bits n is given. The problem is to generate the n-bit Gray code. The Gray code is an ordering of the binary numbers such that two successive codewords differ in only one bit. Problem Solution 1. The function … Read more

advertisement

Python Program to Clear the Rightmost Set Bit of a Number

This is a Python program to clear the rightmost set bit of a number. Problem Description A number is given. We have to clear the rightmost set bit of the number. That is, we have to flip the rightmost 1 in the binary representation of n. Problem Solution 1. The function clear_rightmost_set_bit is defined. 2. … Read more

advertisement

Python Program to Find Whether a Number is a Power of Two

This is a Python program to find whether a number is a power of two. Problem Description A number is given. The problem is to determine whether the number is a power of two. Problem Solution 1. The function is_power_of_two is defined. 2. It takes a number n as argument and returns True if the … Read more

advertisement

Python Program to Count Set Bits in an Integer

This is a Python program to count set bits in a number. Problem Description The program finds the number of ones in the binary representation of a number. Problem Solution 1. Create a function count_set_bits that takes a number n as argument. 2. The function works by performing bitwise AND of n with n – … Read more

advertisement

Python Program to Test Collatz Conjecture for a Given Number

This is a Python program to test Collatz conjecture for a given number. Problem Description The Collatz conjecture is a conjecture that a particular sequence always reaches 1. The sequence is defined as: start with a number n. The next number in the sequence is n/2 if n is even and 3n + 1 if … Read more

advertisement

Python Program to Solve n-Queen Problem without Recursion

This is a Python program to solve the n-queen problem without recursion. Problem Description The n-queen problem is the problem of placing n queens on an n x n chessboard such that no queen can attack another queen. Problem Solution 1. Create a class QueenChessBoard. 2. The board configuration is stored in a list called … Read more

advertisement

Celebrity Problem in Python

This is a Python program to solve the celebrity problem. Problem Description The celebrity problem is the problem of finding the celebrity among n people. A celebrity is someone who doesn’t know anyone (including themselves) but is known by everyone. The problem is to find who the celebrity is by asking people questions of the … Read more

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.