Registry Synced

Jan 2026 - Python - Week 8 - GrPA 2

683 words
3 min read

Graded Assignment

Course: Jan 2026 - Python
GrPA 2
Submission deadline has passed for this assignment
Due Apr 08, 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 08, 2026 at 5:46 AM IST
Private Tests
5/5 Passed
Submitted on Apr 08, 2026 at 5:46 AM IST

**Change in eligibility criteria to write oppe2 exam: A5>=40/100 AND A6>=40/100 AND A7>=40/100 AND A8>=40/100. and becoming eligible to give the end term exam. ** You are given two non-empty text files file1 and file2 that have f1f_1 and f2f_2 lines respectively. Each file is a collection of ten-digit phone numbers, one number per line. It is also known that 0<f1f20 < f_1 \leq f_2.
The following relations are defined on these two files:
  • Subset: file1 is a subset of file2 if the phone number in the ithi^{th} line of file1 is equal to the number in the ithi^{th} line of file2, for 1if11 \leq i \leq f_1, and f1<f2f_1 < f_2.
  • Equal: file1 is equal to file2 if the phone number in the ithi^{th} line of file1 is equal to the number in the ithi^{th} line of file2 for 1if11 \leq i \leq f_1, and f1=f2f_1 = f_2.

Write a function named relation that accepts these two text files as arguments. It should return the string Subset if file1 is a subset of file2. It should return Equal if file1 is equal to file2. If both these conditions are not satisfied, it should return the string No Relation.

(1) file1 and file2 are string variables that hold the names of the two files. For example, in the first test case, it is file1 = 'public_1_1.txt' and file2 = 'public_1_2.txt'.
(2) You do not have to accept input from the console or print the output to the console. You just have to write the function definition.
(3) The strings '9876543210' and '9876543210\n' are not the same, though the phone numbers are the same. So, strip the strings of all new lines using the strip method before checking for equality of two lines across the files.

Public Tests ( 3/3 )

Case 1

Input:
text
100
public_1_1.txt
public_1_2.txt
Expected Output:
text
Subset
Actual Output:
text
Subset

Case 2

Input:
text
300
public_2_1.txt
public_2_2.txt
Expected Output:
text
No Relation
Actual Output:
text
No Relation

Case 3

Input:
text
500
public_3_1.txt
public_3_2.txt
Expected Output:
text
Equal
Actual Output:
text
Equal

Private Tests ( 5/5 )

Case 1

Input:
text
700
private_1_1.txt
private_1_2.txt

Case 2

Input:
text
900
private_2_1.txt
private_2_2.txt

Case 3

Input:
text
1100
private_3_1.txt
private_3_2.txt

Case 4

Input:
text
1300
private_4_1.txt
private_4_2.txt

Case 5

Input:
text
2341
private_5_1.txt
private_5_2.txt

💻 IITM Official Solution

python
def relation(file1, file2):
    f1 = open(file1, 'r')
    L1 = f1.readlines()
    for i in range(len(L1)):
        L1[i] = L1[i].strip()
    f1.close()

    f2 = open(file2, 'r')
    L2 = f2.readlines()
    for i in range(len(L2)):
        L2[i] = L2[i].strip()
    f2.close()

    if L1 == L2:
        return 'Equal'

    for i in range(len(L1)):
        if L1[i] != L2[i]:
            return 'No Relation'
    return 'Subset'


💻 My Submitted Code

python
def relation(file1, file2):
    f1 = open(file1, 'r')
    f2 = open(file2, 'r')

    # Read all lines into lists for easier comparison
    lines1 = [line.strip() for line in f1.readlines()]
    lines2 = [line.strip() for line in f2.readlines()]

    f1.close()
    f2.close()

    count1 = len(lines1)
    count2 = len(lines2)

    # Check if lines in file1 match the corresponding lines in file2
    # The constraint says count1 <= count2
    is_match = True
    for i in range(count1):
        if lines1[i] != lines2[i]:
            is_match = False
            break

    if is_match:
        if count1 == count2:
            return 'Equal'
        else:
            return 'Subset'
    else:
        return 'No Relation'

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.