Registry Synced

Jan 2026 - Python - Week 9 - WEEK9-GrPA 2

518 words
3 min read

WEEK9-GrPA 2

Course: Jan 2026 - Python
WEEK9-GrPA 2
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
2/2 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

Write a recursive function named linear that accepts the following arguments:
  • P: a non-empty list of positive integers
  • Q: a non-empty list of positive integers
  • k: a positive integer
It should return True only if both the conditions given below are satisfied:
  • PP and QQ are of same length.
  • P[i]=kQ[i]P[i] = k \cdot Q[i], for every integer ii in the range [0,len(P)1][0, \text{len}(P) - 1], endpoints inclusive.

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 ( 2/2 )

Case 1

Input:
text
public
[2, 4, 6, 8]
[1, 2, 3, 4]
2
Expected Output:
text
True
Actual Output:
text
True

Case 2

Input:
text
public
[10, 20, 30, 40, 50]
[1, 2, 3, 4, 6]
10
Expected Output:
text
False
Actual Output:
text
False

Private Tests ( 5/5 )

Case 1

Input:
text
private
[10, 40, 30, 10, 20, 90, 80]
[2, 8, 6, 2, 4, 18, 16]
5
Expected Output:
text
True

Good job! Your function is recursive
Actual Output:
text
True

Good job! Your function is recursive

Case 2

Input:
text
private
[1, 2, 3, 4, 5, 6, 7]
[3, 6, 9, 12, 15, 18, 21]
3
Expected Output:
text
False

Good job! Your function is recursive
Actual Output:
text
False

Good job! Your function is recursive

Case 3

Input:
text
private
[1]
[1]
1

Case 4

Input:
text
private
[10, 20, 30]
[1, 2, 3, 4]
10

Case 5

Input:
text
private
[10, 100, 200, 300, 400]
[2, 10, 20, 30, 40]
10

💻 IITM Official Solution

python
def linear(P, Q, k):
    if len(P) != len(Q):
        return False
    if len(P) == 0:
        return True
    if P[0] / Q[0] != k:
        return False
    return linear(P[1: ], Q[1: ], k)


💻 My Submitted Code

python
def linear(P, Q, k):
    """
    A recursive function to determine if a list is scalar multiple of the other

    Arguments:
        P: list of integers
        Q: list of integers
        k: integer
    Return:
        result: bool
    """
    # Length mismatch
    if len(P) != len(Q):
        return False

    # Base case
    if len(P) == 0:
        return True

    # Check current element
    if P[0] != k * Q[0]:
        return False

    # Recursive case
    return linear(P[1:], Q[1:], k)

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.