0% found this document useful (0 votes)
17 views10 pages

Recursive Functions in Python

The document details a review of a coding quiz focused on functions and recursion, completed on September 24, 2025, with a perfect score of 100%. It includes five questions, each requiring the implementation of recursive functions for tasks such as calculating the greatest common divisor, counting digits, converting to binary, checking for palindromes, and determining if a number can be expressed as the sum of two primes. All answers were correct and passed the tests provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Recursive Functions in Python

The document details a review of a coding quiz focused on functions and recursion, completed on September 24, 2025, with a perfect score of 100%. It includes five questions, each requiring the implementation of recursive functions for tasks such as calculating the greatest common divisor, counting digits, converting to binary, checking for palindromes, and determining if a number can be expressed as the sum of two primes. All answers were correct and passed the tests provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Started on Wednesday, 24 September 2025, 10:07 PM

State Finished

Completed on Wednesday, 24 September 2025, 10:20 PM

Time taken 12 mins 52 secs

Marks 5.00/5.00

Grade 100.00 out of 100.00

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 1/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Question 1 Correct Mark 1.00 out of 1.00

Euclid was a Greek mathematician who lived approximately 2,300 years ago. His
algorithm for computing the greatest common divisor of two positive integers, a and
b, is both efficient and recursive. It is outlined below:

If b is 0 then
return a
Else
Set c equal to the remainder when a is divided by b
Return the greatest common divisor of b and c

Write a Recursive funtion that implements Euclid's algorithm and uses it to determine the
greatest common divisor of two integers entered by the user. Test your program with some very large integers. The result will be computed
quickly, even for huge numbers consisting of hundreds of digits, because Euclid's algorithm is extremely efficient.

For example:

Test Result

print(gcd(8, 12)) 4

print(gcd(720, 1000)) 40

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ def gcd(a,b):
2 ▼ if b == 0:
3 return a
4 ▼ else:
5 return gcd(b, a % b)

Test Expected Got

 print(gcd(8, 12)) 4 4 

 print(gcd(720, 1000)) 40 40 

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 2/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 3/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Question 2 Correct Mark 1.00 out of 1.00

Given an integer number and you have to count the digits using recursion using Python program. In
this program, you will be reading an integer number and counting the total digits, using a
function countDigits() which will take a number as an argument and return the count after
recursion process.
Input Format: The first and only line of the input contains a single integer n
Output Format: Output a single line denoting the number of digits in n.

For example:

Test Result

print(countDigits(800)) 3

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ def countDigits(n):
2 ▼ if n == 0:
3 return 0
4 ▼ else:
5 return 1 + countDigits(n // 10)

Test Expected Got

 print(countDigits(12345)) 5 5 

 print(countDigits(800)) 3 3 

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 4/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Question 3 Correct Mark 1.00 out of 1.00

Complete the recursive function to return Binary Equivalent of an Integer using Recursion.

Sample Test Cases

Test Case 1

Input

10

Output

1010

Test Case 2

Input

257

Output

100000001

For example:

Test Result

print(binayNumber(10)) 1010

print(binayNumber(257)) 100000001

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ def binayNumber(n):
2 ▼ if n == 0:
3 return ""
4 ▼ else:
5 return binayNumber(n // 2) + str(n % 2)

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 5/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Test Expected Got

 print(binayNumber(10)) 1010 1010 

 print(binayNumber(257)) 100000001 100000001 

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 6/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Question 4 Correct Mark 1.00 out of 1.00

The notion of a palindrome was introduced previously. In this exercise you will write a recursive function that determines whether or not a
string is a palindrome. The empty string is a palindrome, as is any string containing only one character. Any longer string is a palindrome if its
first and last characters match, and if the string formed by removing the first and last characters is also a palindrome.

Write a program that reads a string from the user and uses your recursive
function to determine whether or not it is a palindrome. Then your program should
display an appropriate message for the user.

Sample Input

malayalam

Sample Output

That was a palindrome!

Sample Input

madan

Sample Output

That is not a palindrome.

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ def isPalindrome(s):
2 ▼ if len(s) <= 1:
3 return True
4 ▼ if s[0] != s[-1]:
5 return False
6 return isPalindrome(s[1:-1])
7
8 line = input()
9 ▼ if isPalindrome(line):
10 print("That was a palindrome!")
11 ▼ else:
12 print("That is not a palindrome.")

Input Expected Got

 malayalam That was a palindrome! That was a palindrome! 

 madan That is not a palindrome. That is not a palindrome. 

Passed all tests! 

Correct

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 7/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review
Marks for this submission: 1.00/1.00.

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 8/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review

Question 5 Correct Mark 1.00 out of 1.00

Complete a Recursive Function to find if a given number N can be expressed as a sum of two prime numbers.

Note: YOU MUST OPTIMIZE the logic to find whether a number is prime or not, as very large prime numbers are provided as input. If the logic
is not optimized your program will NOT get executed within the given time limit.

Input Format:

First line contains number N.

Output Format:

Return either yes or no.

Boundary Conditions / Constraints:

3 <= N <= 10^9

Example Input/Output 1:

Input:

20

Output:

yes

Input:

23

Ouput:

no

Explanation:

20 can be expressed as 17+3

23 cannot be expressed as sum of two primes

For example:

Test Result

print(checkPrimeSum(20)) yes

print(checkPrimeSum(23)) no

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ def is_prime(x, d=2):


2 ▼ if x < 2:
3 return False
4 ▼ if d > int(x ** 0.5):
5 return True
6 ▼ if x % d == 0:
7 return False
8 return is_prime(x, d + 1)
9
10 ▼ def checkPrimeSum(n, i=2):
11 ▼ if i > n // 2:
[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 9/10
9/24/25, 10:21 PM Week-06-Coding-Functions-Recursion: Attempt review
12 return "no"
13 ▼ if is_prime(i) and is_prime(n - i):
14 return "yes"
15 return checkPrimeSum(n, i + 1)

Test Expected Got

 print(checkPrimeSum(20)) yes yes 

Passed all tests! 

Correct

Marks for this submission: 1.00/1.00.

[Link]/moodle/mod/quiz/[Link]?attempt=250644&cmid=2545 10/10

You might also like