Neural Sync Active
Jan 2026 - Python - Week 9 - WEEK9-GrPA 3
Registry Synced
Jan 2026 - Python - Week 9 - WEEK9-GrPA 3
862 words
4 min read
Graded Assignment
Course: Jan 2026 - Python
WEEK9-GrPA 3
Submission deadline has passed for this assignment
Due Apr 17, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
3/3 Passed
Submitted on Apr 17, 2026 at 5:36 AM IST
Private Tests
5/5 Passed
Submitted on Apr 17, 2026 at 5:36 AM IST
The Collatz function is defined for a positive integer n as follows.
We consider the repeated application of the Collatz function starting with a given integer n, which results in the following sequence:
f(n),f(f(n)),f(f(f(n))),…
It is conjectured that no matter which positive integer n you start from, the sequence will always reach 1. For example, if n=10, the sequence is:
.tg {border-collapse:collapse;border-spacing:0;} .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;} .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;} .tg .tg-0lax{text-align:left;vertical-align:top}
Thus, if you start from n=10, you need to apply the function f six times in order to first reach 1.
Write a recursive function named collatz that accepts a positive integer n as argument, where 1<n≤32,000, and returns the number of times f has to be applied repeatedly in order to first reach 1.
You do not have to accept input from the user or print output to the console. You just have to write the function definition.
Public Tests ( 3/3 )
Case 1
Input:
textpublic 7
Expected Output:
text16
Actual Output:
text16
Case 2
Input:
textpublic 10
Expected Output:
text6
Actual Output:
text6
Case 3
Input:
textpublic 101
Expected Output:
text25
Actual Output:
text25
Private Tests ( 5/5 )
Case 1
Input:
textprivate 30000
Expected Output:
text178 Good, your function is recursive
Actual Output:
text178 Good, your function is recursive
Case 2
Input:
textprivate 31999
Expected Output:
text98 Good, your function is recursive
Actual Output:
text98 Good, your function is recursive
Case 3
Input:
textprivate 2463
Expected Output:
text208 Good, your function is recursive
Actual Output:
text208 Good, your function is recursive
Case 4
Input:
textprivate 999
Expected Output:
text49 Good, your function is recursive
Actual Output:
text49 Good, your function is recursive
Case 5
Input:
textprivate 125
Expected Output:
text108 Good, your function is recursive
Actual Output:
text108 Good, your function is recursive
💻 IITM Official Solution
pythondef collatz(n): # base case of the recursion # if n = 1, you don't need to call the function at all if n == 1: return 0 # simple application of the piecewise defn of the # Collatz function if n % 2 == 0: return 1 + collatz(n // 2) else: return 1 + collatz(3 * n + 1)
💻 My Submitted Code
pythondef collatz(n): """ A recursive function to compute the number of calls to the Collatz function of n Argument: n: integer Assume that 1 < n <= 32,000 Returns: result: integer """ # Base case if n == 1: return 0 # Recursive case if n % 2 == 0: return 1 + collatz(n // 2) else: return 1 + collatz(3 * n + 1)