Registry Synced

Jan 2026 - Python - Week 8 - GrPA 4

837 words
4 min read

GrPA 4

Course: Jan 2026 - Python
GrPA 4
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
2/2 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. ** Write a function named num_to_words that accepts a square matrix of single digit numbers as argument. Within the function, create a file named words.csv. Write the matrix to the file by replacing the digits with their corresponding words. For example, num_to_words([[1, 2], [3, 4]]) should create the file words.csv with the following contents:
one,two
three,four
Note that the matrix will only have integers from 00 to 99, endpoints inclusive.

(1) You do not have to accept input from the user or print the output to the console. You just have to write the function definition. However, within the function, you have to create a file named words.csv and write to it.
(2) The last line of the file should not end with a '\n'. The last character of every other line in the file should end with a '\n'. This is a convention that we will be following in all questions that ask you to write to a file.

Public Tests ( 2/2 )

Case 1

Input:
text
public
[1, 2], [3, 4](/viewer?path=1, 2], [3, 4)
Expected Output:
text
one,two

three,four
Actual Output:
text
one,two

three,four

Case 2

Input:
text
public
[9, 7, 2], [1, 0, 5], [5, 2, 6](/viewer?path=9, 7, 2], [1, 0, 5], [5, 2, 6)
Expected Output:
text
nine,seven,two

one,zero,five

five,two,six
Actual Output:
text
nine,seven,two

one,zero,five

five,two,six

Private Tests ( 5/5 )

Case 1

Input:
text
private
[1, 2, 3], [4, 5, 6], [7, 8, 9](/viewer?path=1, 2, 3], [4, 5, 6], [7, 8, 9)
Expected Output:
text
I am reading a file

one,two,three

four,five,six

seven,eight,nine

Just printing the contents will not work
Actual Output:
text
I am reading a file

one,two,three

four,five,six

seven,eight,nine

Just printing the contents will not work

Case 2

Input:
text
private
[0, 0, 1], [0, 9, 3], [5, 8, 6](/viewer?path=0, 0, 1], [0, 9, 3], [5, 8, 6)
Expected Output:
text
I am reading a file

zero,zero,one

zero,nine,three

five,eight,six

Just printing the contents will not work
Actual Output:
text
I am reading a file

zero,zero,one

zero,nine,three

five,eight,six

Just printing the contents will not work

Case 3

Input:
text
private
[9, 0, 3, 1], [0, 1, 3, 5], [6, 7, 2, 5], [0, 8, 4, 2](/viewer?path=9, 0, 3, 1], [0, 1, 3, 5], [6, 7, 2, 5], [0, 8, 4, 2)
Expected Output:
text
I am reading a file

nine,zero,three,one

zero,one,three,five

six,seven,two,five

zero,eight,four,two

Just printing the contents will not work
Actual Output:
text
I am reading a file

nine,zero,three,one

zero,one,three,five

six,seven,two,five

zero,eight,four,two

Just printing the contents will not work

Case 4

Input:
text
private
[0, 9], [5, 7](/viewer?path=0, 9], [5, 7)
Expected Output:
text
I am reading a file

zero,nine

five,seven

Just printing the contents will not work
Actual Output:
text
I am reading a file

zero,nine

five,seven

Just printing the contents will not work

Case 5

Input:
text
private
[1, 0, 4, 3], [5, 6, 8, 1], [9, 8, 3, 4], [6, 7, 2, 9](/viewer?path=1, 0, 4, 3], [5, 6, 8, 1], [9, 8, 3, 4], [6, 7, 2, 9)
Expected Output:
text
I am reading a file

one,zero,four,three

five,six,eight,one

nine,eight,three,four

six,seven,two,nine

Just printing the contents will not work
Actual Output:
text
I am reading a file

one,zero,four,three

five,six,eight,one

nine,eight,three,four

six,seven,two,nine

Just printing the contents will not work

💻 IITM Official Solution

python
def num_to_words(mat):
    P = {0: 'zero', 1: 'one', 2: 'two',
    3: 'three', 4: 'four', 5: 'five',
    6: 'six', 7: 'seven', 8: 'eight',
    9: 'nine'}
    f = open('words.csv', 'w')
    n = len(mat)
    for i in range(n):
        for j in range(n):
            line = f'{P[mat[i][j]]}'
            # do not add a comma for the elements in the last column
            if j != n - 1:
                line += ','
            f.write(line)
        # go to the next line as long as you are not in the last line
        if i != n - 1:
            f.write('\n')
    f.close()


💻 My Submitted Code

python
def num_to_words(mat):
    # Mapping dictionary for digits 0-9
    mapping = {
        0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four',
        5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'
    }

    f = open('words.csv', 'w')
    n = len(mat)

    for i in range(n):
        # Convert digits in the row to words using list comprehension
        word_row = [mapping[digit] for digit in mat[i]]

        # Join words with commas
        line = ",".join(word_row)

        # Convention: Add newline to all lines EXCEPT the last one
        if i < n - 1:
            f.write(line + "\n")
        else:
            f.write(line)

    f.close()

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.