1967 words
10 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Week 9 - Graded Assignment 9 > **Course:** Jan 2026 - Python > Week 9 - Graded Assignment 9 > **Last Submitted:** You have last submitted on: 2026-04-17, 05:44 IST --- ## Introduction **Common data for questions 1, 2, 3 and 4** Consider the following functions: Common Data for questions (5) and (6) Two lists are e...

Visual strip

Relevant recall pieces for this note

Open gallery

Week 9 - Graded Assignment 9

Course: Jan 2026 - Python
Week 9 - Graded Assignment 9
Last Submitted: You have last submitted on: 2026-04-17, 05:44 IST

Introduction

Common data for questions 1, 2, 3 and 4
Consider the following functions:
python
def f(n):
    if n < 10:
        return 1
    return 1 + f(n // 10)

def g(n):
    if n < 10:
        return n
    return n % 10 + g(n // 10)

def h(n):
    if n <= 1:
        return n
    return n * h(n - 1)
Common Data for questions (5) and (6)
Two lists are equal if and only if they satisfy both the following conditions:
(1) They have the same number of elements. Call this the size of the list.
(2) The ithi^{th} element in the first list is the same as the ithi^{th} element in the second list for 0i<size0 \leq i < \text{size}. We are using zero-indexing here.
If both lists are empty, then they are assumed to be equal.
equality is a function that accepts two lists P and Q as arguments and returns True if the lists are equal and False otherwise. Questions (5) and (6) discusses multiple ways of implementing this function.

Question 1

If n is a positive integer, what does f(n) return? Note that your answer should be applicable for any positive integer n, and not just for a specific value.
  • Sum of digits in n.
  • Number of digits in n.
  • Product of digits in n.
  • First digit in n.
  • Factorial of n.
Status: Yes, the answer is correct. Score: Score: 2
Feedback: f: Returns the number of digits in n
def f(n): if n < 10: return 1 return 1 + f(n // 10)
In the recursive call, we are passing n // 10 to f. n // 10 is the quotient obtained when n is divided by 10, which is the same as the number obtained by removing the last digit from n. For example, if n = 1234, then n // 10 is 123.
Line-4 should be read like this:
  • 1 stands for the units digit of n. One digit in n has been accounted for.
  • f(n // 10) stands for the number of digits in n // 10.
If we add these two, we get the total number of digits in n.
Accepted Answers:
Number of digits in n.

Question 2

if n is a positive integer, what does g(n) return? Note that your answer should be applicable for any positive integer n, and not just for a specific value.
  • Sum of digits in n.
  • Number of digits in n.
  • Product of digits in n.
  • First digit in n.
  • Factorial of n.
Status: Yes, the answer is correct. Score: Score: 2
Feedback: g: Returns the sum of the digits in n.
def g(n): if n < 10: return n return n % 10 + g(n // 10)
This is very similar to f. n % 10 gives the last digit in n. So, line-4 should be read like this:
  • n % 10 is the last digit of n
  • g(n // 10) is the sum of the digits in n // 10.
Summing these two, we get the sum of the digits in n.
Accepted Answers:
Sum of digits in n.

Question 3

Which of the following function calls returns the sum of the digits in 100!100!, where, n!n! is the product of the first nn positive integers?
  • f(g(100))
  • f(h(100))
  • g(h(100))
  • h(g(100))
  • h(f(100))
Status: Yes, the answer is correct. Score: Score: 2
Feedback: h is the familiar factorial function. h(100) is 100!100!
g(h(100)) gives the sum of the digits in 100!100!
Accepted Answers:
g(h(100))

Question 4

Which of the following function calls returns the number of digits in 100!100!, where, n!n! is the product of the first nn positive integers?
  • f(g(100))
  • f(h(100))
  • g(h(100))
  • h(g(100))
  • h(f(100))
Status: Yes, the answer is correct. Score: Score: 2
Feedback: h is the familiar factorial function. h(100) is 100!100!
f(h(100)) gives the number of the digits in 100!100!
Accepted Answers:
f(h(100))

Question 5

Select all correct implementations of the function equality. [MSQ]
  • ```python def equality(P, Q): if len(P) != len(Q): return False size = len(P) for i in range(size): if P[i] != Q[i]: return False
  • ```python def equality(P, Q): if len(P) != len(Q): return False size = len(P) for i in range(size): if P[i] != Q[i]: return False return True
  • ```python def equality(P, Q): size = len(P) for i in range(size): if P[i] != Q[i]: return False return True
  • ```python def equality(P, Q): for elem in P: if elem not in Q: return False return True
  • ```python def equality(P, Q): if len(P) != len(Q): return False for elem in P: if elem not in Q: return False return True
  • ```python def equality(P, Q): return P == Q
Status: Yes, the answer is correct. Score: Score: 4
Feedback:
  • Option (a) is wrong because it never returns True at the end.
  • Option (b) is correct because, whenever P[i] is not equal to Q[i] it returns False. It returns True right at the end of the function at line-8. We hit line-8 only if P[i] == Q[i] for every value of i.
  • Option (c) doesn't check if the two lists are of equal length.
  • Option (d) has the same complaint as option (c).
  • Option (e) is not bothered about the order of elements. So, [1, 2, 3, 4] and [4, 3, 2, 1] would be deemed equal by this implementation, which is not true.
  • Option (f) is correct. It uses Python's native way of checking equality of two lists.
Accepted Answers:
python
def equality(P, Q):
    if len(P) != len(Q):
        return False
    size = len(P)
    for i in range(size):
        if P[i] != Q[i]:
            return False
    return True
python
def equality(P, Q):
	return P == Q

Question 6

Select all the correct recursive implementation of the function equality. [MSQ]
  • ```python def equality(P, Q): if len(P) != len(Q): return False if P[-1] != Q[-1]: return False return equality(P[:-1], Q[:-1])
  • ```python def equality(P, Q): if len(P) != len(Q): return False if len(P) == 0: return True if P[0] != Q[0]: return False return equality(P[:-1], Q[:-1])
  • ```python def equality(P, Q): if len(P) != len(Q): return False if len(P) == 0: return True if P[0] != Q[0]: return False return equality(P[1: ], Q[1: ])
  • ```python def equality(P, Q): if len(P) != len(Q): return False if len(P) == 0: return True if P[-1] != Q[-1]: return False return equality(P[: -1], Q[: -1])
Status: Yes, the answer is correct. Score: Score: 4
Feedback: The basic idea behind the correct solutions is as follows. That the lists should be of the same size is obvious. So, let us not spend too much time on that. Given two lists of equal size, there are two ways of testing equality:
  • We check the first element of the P with the first element of Q; if they are unequal, then the two lists are unequal, if they are equal, then we could drop the first element and only look at the rest of the elements in both the lists. This is where the recursive call comes in. Option-(c) implements this logic.
  • Option-(d) starts from the end. We check the last elements of P and Q respectively. If they are unequal, then we can return False then and there. If not, we ignore the last element from both the lists and continue checking with the help of the recursive call.
The base case for both these solutions is the same. When we hit the empty list, then it means that the original lists P and Q are equal. Try to play around with different lists and unroll the recursion. That will convince you about the correctness of the base case.
  • Option-(a) is wrong as it is missing the base case.
  • To see why option-(b) is wrong, execute the following call: equality([1, 2, 3, 4], [1, 2, 3, 5]).
Accepted Answers:
python
def equality(P, Q):
    if len(P) != len(Q):
        return False
    if len(P) == 0:
        return True
    if P[0] != Q[0]:
        return False
    return equality(P[1: ], Q[1: ])
python
def equality(P, Q):
    if len(P) != len(Q):
        return False
    if len(P) == 0:
        return True
    if P[-1] != Q[-1]:
        return False
    return equality(P[: -1], Q[: -1])

Question 7

A binary number consists of a string of ones and zeros. For example, 101101 and 110101110101 are binary numbers. Every binary number can be converted into its corresponding decimal number. If a1a2ana_1a_2 \dots a_n is an nn-digit binary number, then its decimal counterpart is given by:
a1×2n1+a2×2n2++an1×21+an×20a_1 \times 2^{n - 1} + a_2 \times 2^{n - 2} + \cdots + a_{n - 1} \times 2^1 + a_n \times 2^{0}
For example, the decimal representation of the binary number 10111011 is:
1×23+0×22+1×21+1×20=111 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 11
Write a function named bin_to_dec that accepts a binary number bin as a non-empty string. It should return its decimal representation as an integer. Some sample instances are given below for your reference.
<table class="tg" style="border-collapse:collapse;border-spacing:0;;" border="1"><thead><tr><th class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:700;font-style:normal">bin_to_dec(bin)</span></th><th class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:700;font-style:normal">Returns</span></th></tr></thead><tbody><tr><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">bin_to_dec('101')</span></td><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;">5</td></tr><tr><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">bin_to_dec('0')</span></td><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;">0</td></tr><tr><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">bin_to_dec('1')</span></td><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;">1</td></tr><tr><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;"><span style="font-weight:400;font-style:normal">bin_to_dec('101101')</span></td><td class="tg-0pky" style="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;;border-color:inherit;text-align:left;vertical-align:top;">45</td></tr></tbody></table>
  • ```python def bin_to_dec(bin): if len(bin) == 1: return int(bin) init = bin[:-1] last = bin[-1] return bin_to_dec(init) * 2 + int(last)
  • ```python def bin_to_dec(bin): if len(bin) == 1: return int(bin) init = bin[:-1] last = bin[-1] return bin_to_dec(init) + int(last) * 2
  • ```python def bin_to_dec(bin): if bin == 0 or bin == 1: return bin init = bin[:-1] last = bin[-1] return bin_to_dec(init) + int(last) * 2
  • ```python def bin_to_dec(bin): if len(bin) == 1: return int(bin) init = bin[:-1] last = bin[-1] return bin_to_dec(init) * 2 + last
Status: Yes, the answer is correct. Score: Score: 4
Feedback: The solutions rests on this simple arithmetic idea:
a1×2n1+a2×2n2++an1×21+an×20=(a1×2n2+a2×2n2++an1)×2+ana_1 \times 2^{n - 1} + a_2 \times 2^{n - 2} + \cdots + a_{n - 1} \times 2^1 + a_n \times 2^{0} =\\ (a_1 \times 2^{n - 2} + a_2 \times 2^{n - 2} + \cdots + a_{n - 1}) \times 2 + a_n
Separate the last binary digit and consider the first n1n - 1 digits. The expression on the LHS is the decimal representation of the binary string a1a2ana_1a_2 \dots a_n. In the expression on the RHS, the first part is two times the decimal representation of the binary string of a1a2an1a_1a_2 \dots a_{n-1}. To this, ana_n is added. The base case is simple enough as it just checks if the binary string has just one digit in it, in which case, it should return the corresponding integer.
Accepted Answers:
python
def bin_to_dec(bin):
    if len(bin) == 1:
        return int(bin)
    init = bin[:-1]
    last = bin[-1]
    return bin_to_dec(init) * 2 + int(last)

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.