# Python Challenges: Exercises, Practice, Solution

## Python Challenges - 1 [32 Challenges with solution]

[*An editor is available at the bottom of the page to write and execute the scripts.*]

**1.** Write a Python program to check if a given positive integer is a power of two. Go to the editor

Input : 4

Output : True

Click me to see the sample solution

**2.** Write a Python program to check if a given positive integer is a power of three. Go to the editor

Input : 9

Output : True

Click me to see the sample solution

**3.** Write a Python program to check if a given positive integer is a power of four. Go to the editor

Input : 4

Output : True

Click me to see the sample solution

**4. ** Write a Python program to check if a number is a perfect square. Go to the editor

Input : 9

Output : True

Click me to see the sample solution

**5.** Write a Python program to check if an integer is the power of another integer.Go to the editor

Input : 16, 2

Output : True

Click me to see the sample solution

**6.** Write a Python program to check if a number is a power of a given base.Go to the editor

Input : 128,2

Output : True

Click me to see the sample solution

**7.** Write a Python program to find a missing number from a list. Go to the editor

Input : [1,2,3,4,6,7,8]

Output : 5

Click me to see the sample solution

**8. ** Write a Python program to find missing numbers from a list. Go to the editor

Input : [1,2,3,4,6,7,10]

Output : [5, 8, 9]

Click me to see the sample solution

**9. ** Write a Python program to find three numbers from an array such that the sum of three numbers equal to zero. Go to the editor

Input : [-1,0,1,2,-1,-4]

Output : [[-1, -1, 2], [-1, 0, 1]]

Note : Find the unique triplets in the array.

Click me to see the sample solution

**10. ** Write a Python program to find three numbers from an array such that the sum of three numbers equal to a given number.Go to the editor

Input : [1, 0, -1, 0, -2, 2], 0)

Output : [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]

Click me to see the sample solution

**11. ** Write a Python program to compute and return the square root of a given 'integer'. Go to the editor

Input : 16

Output : 4

Note : The returned value will be an 'integer'

Click me to see the sample solution

**12. ** Write a Python program to find the single number in a list that doesn't occur twice.Go to the editor

Input : [5, 3, 4, 3, 4]

Output : 5

Click me to see the sample solution

**13. ** Write a Python program to find the single element in a list where every element appears three times except for one.Go to the editor

Input : [5, 3, 4, 3, 5, 5, 3]

Output : 4

Click me to see the sample solution

**14. ** Write a Python program to find the single element appears once in a list where every element appears four times except for one. Go to the editor

Input : [1, 1, 1, 2, 2, 2, 3]

Output : 3

Click me to see the sample solution

**15. ** Write a Python program to find two elements appear twice in a list where all the other elements appear exactly twice in the list. Go to the editor

Input : [1, 2, 1, 3, 2, 5]

Output :[5, 3]

Click me to see the sample solution

**16. ** Write a Python program to add the digits of a positive integer repeatedly until the result has a single digit. Go to the editor

Input : 48

Output : 3

For example given number is 59, the result will be 5.

Step 1: 5 + 9 = 14

Step 1: 1 + 4 = 5

Click me to see the sample solution

**17. ** Write a Python program to find whether it contains an additive sequence or not. Go to the editor

The additive sequence is a sequence of numbers where the sum of the first two numbers is equal to the third one.

Sample additive sequence: 6, 6, 12, 18, 30

In the above sequence 6 + 6 =12, 6 + 12 = 18, 12 + 18 = 30....

Also, you can split a number into one or more digits to create an additive sequence.

Sample additive sequence: 66121830

In the above sequence 6 + 6 =12, 6 + 12 = 18, 12 + 18 = 30....

Note : Numbers in the additive sequence cannot have leading zeros.

Click me to see the sample solution

**18. ** Write a Python program to reverse the digits of an integer. Go to the editor

Input : 234

Input : -234

Output: 432

Output : -432

Click me to see the sample solution

**19. ** Write a Python program to reverse the bits of an integer (32 bits unsigned). Go to the editor

Input : 1234

Output : 1260388352

For example, 1234 represented in binary as 10011010010 and returns 1260388352 which represents in binary as 1001011001000000000000000000000.

Click me to see the sample solution

**20. ** Write a Python program to check a sequence of numbers is an arithmetic progression or not. Go to the editor

Input : [5, 7, 9, 11]

Output : True

In mathematics, an arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.

For example, the sequence 5, 7, 9, 11, 13, 15 ... is an arithmetic progression with common difference of 2.

Click me to see the sample solution

**21. ** Write a Python program to check a sequence of numbers is a geometric progression or not. Go to the editor

Input : [2, 6, 18, 54]

Output : True

In mathematics, a geometric progression or geometric sequence is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio.

For example, the sequence 2, 6, 18, 54, ... is a geometric progression with common ratio 3. Similarly, 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.

Click me to see the sample solution

**22. ** Write a Python program to compute the sum of the two reversed numbers and display the sum in reversed form. Go to the editor

Input : 13, 14

Output : 27

Note : The result will not be unique for every number for example 31 is a reversed form of several numbers of 13, 130, 1300 etc. Therefore all the leading zeros will be omitted

Click me to see the sample solution

**23. ** Write a Python program where you take any positive integer n, if n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process until you reach 1. Go to the editor

Input : 12

Output : [12, 6.0, 3.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0]

According to Wikipedia, the Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture.

The conjecture can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One") indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.

Example :

For instance, starting with n = 12, one gets the sequence 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.

n = 19, for example, takes longer to reach 1: 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

Click me to see the sample solution

**24. ** Write a Python program to check whether a given number is an ugly number. Go to the editor

Input : 12

Output : True

Ugly numbers are positive numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...

shows the first 10 ugly numbers.

Note: 1 is typically treated as an ugly number

Click me to see the sample solution

**25. ** Write a Python program to get the Hamming numbers upto a given numbers also check whether a given number is an Hamming number. Go to the editor

Input : 7

Output : 0

Hamming numbers are numbers of the form

H = 2^{i} x 3^{j} x 5^{k}

Where i, j, k = 0

The sequence of Hamming numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27. . . consists of all numbers of the form 2i.3j.5k where i, j and k are non-negative integers.

Click me to see the sample solution

**26. ** Write a Python program to check if a given string is an anagram of another given string. Go to the editor

Input : 'anagram','nagaram'

Output : True

According to Wikipedia an anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into nag-a-ram

Click me to see the sample solution

**27. ** Write a Python program to push all zeros to the end of a list. Go to the editor

Input : [0,2,3,4,6,7,10]

Output : [2, 3, 4, 6, 7, 10, 0]

Click me to see the sample solution

**28. ** Write a Python program to the push the first number to the end of a list. Go to the editor

Click me to see the sample solution

**29. ** Write a Python program to find majority element in a list. Go to the editor

Input : [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6]

Output : 5

Note: The majority element is the element that appears more than n/2 times where n is the number of elements in the list.

Click me to see the sample solution

**30. ** Write a Python program to find the length of the last word. Go to the editor

Input : Python Exercises

Output : 9

Click me to see the sample solution

**31. ** Write a Python program to add two binary numbers. Go to the editor

Input : ('11', '1')

Output : 100

Click me to see the sample solution

**32. ** Write a Python program to find the single number which occurs odd numbers and other numbers occur even number. Go to the editor

Click me to see the sample solution

**Python Code Editor:**

**More to Come !**