# Python Competitive Programming Questions for Practice

This space is to list Python competitive programming questions.

You can use questions for coding practice. As we progress, I will keep adding more coding questions here.

This initiate is a part of our FREE Python online tutorial.

### 0. Count Common Factor

This problem is asked in one of the HackerEarth contest.

Problem Statement: Little Robert likes mathematics. Today his teacher has given him two integers and asked to find out how many integers can divide both the numbers. Would you like to help him in completing his school assignment?

**Input Formatting:** Thre is two integers, a and b as input to the program.

**Output Formatting:** Print the number of common factors of a and b. Both the input value should be in a range of 1 to 10^12.

**Example:**

Input: 10 15 Output: 2

**Explanation:** The common factors of 10 and 15 are 1 and 5. So the answer will be 2.

**Python Code:**

data = input() li = data.split() a = int(li[0]) b = int(li[1]) def gcd(a, b): if (a == 0): return b; return gcd(b%a, a); if (a>0 and a<(10**12+1) and b>=1 and b<(10**12+1)): count = 1 for i in range(2, gcd(a, b)+1): if a%i==0 and b%i==0: count = count+1 print(count)

**Explanation:**

- We are reading two integers as single input and then splitting it using the split() method.
- The function gcd() is to find the greatest common divisor.
- To know how to find the GCD of two numbers, you can go through the function gcd() which uses recursion. It is self-explanatory.

### 1. Does it Divide?

This problem is asked in the HackerEath contest.

**Problem Statement: **Consider a permutation of numbers from 1 to N written on a paper. Let’s denote the product of its element as ‘prod’ and the sum of its elements as ‘sum’. Given a positive integer N, your task is to determine whether ‘prod’ is divisible by ‘sum’ or not.

**Input Format: **First input will be an integer T. It depicts a number of test cases. Followed by value f0r each test cases. Each test case will contain an integer N (1<= N <=10^9). It is nothing but the length of the permutation.

**Output Format: **For each test case, print “YEAH” if ‘prod’ is divisible by ‘sum’, otherwise print “NAH”.

**Python Code:**

testSize = int(input()) nArr=[] for i in range(1,testSize+1): nArr.append(int(input())) for n in nArr: if n>=1 and n<=(10**9): prod = 1 sum = 0 for i in range(1, n+1): prod = prod*i sum = sum+i if prod%sum==0: print("YEAH") else: print("NAH")

**Input:**

2 2 3

**Output:**

YEAH NAH

**Explanation:**

- The first input is a number of test cases.
- Iterate to read all the inputs and store it in the Python list.
- For each element in the list, calculate prod and sum.
- Print “YEAH” if the prod is divided by the sum. Otherwise, print “NAH”.

#### 3. Sum of sub-arrays

**Python Code:**

This question is actually asked in Cohesity coding round on Hackerearth.

I hope that this code is self-explanatory.

### Thought…

I will keep adding more Python Competitive Programming Questions for Practice. Also, I explain each Python tutorial.

Follow this page to help yourself and to find some important tips for competitive programming.