Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL The Joy of Computing using Python Jan-2024 | Swayam

NPTEL The Joy of Computing using Python Jan-2024 | Swayam

 











NPTEL » The Joy of Computing using Python


  Please scroll down for latest Programs ðŸ‘‡ 




1. Programming Assignment | Week 2

Due on 2024-02-08, 23:59 IST
Accept two positive integers M and N as input. There are two cases to consider:
(1) If M < N, then print M as output.
(2) If M >= N, subtract N from M. Call the difference M1. If M1 >= N, then subtract N from M1 and call the difference M2. Keep doing this operation until you reach a value k, such that, Mk < N. You have to print the value of Mk as output.
Your last recorded submission was on 2024-02-08, 19:10 IST
Select the Language for this assignment. 
1
#Programming Assignment 1
2
M = int(input())
3
N = int(input())
4
if M<N :
5
  print(M, end='')
6
elif M>=N :
7
  M1=M-N
8
  while M1>=N :
9
    M1 = M1 - N
10
  print(M1, end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
2
1
1
Passed
Test Case 2
5
10
5
5
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed


2. Programming Assignment | Week 2

Due on 2024-02-08, 23:59 IST
Accept three positive integers as input and check if they form the sides of a right triangle. Print YES if they form one, and NO if they do not. The input will have three lines, with one integer on each line. The output should be a single line containing one of these two strings: YES or NO.
Your last recorded submission was on 2024-02-08, 19:21 IST
Select the Language for this assignment. 
1
#Programming Assignment 2 Week 2
2
a = int(input())
3
b = int(input())
4
c = int(input())
5
if a**2 + b**2 == c**2 :
6
  print("YES", end='')
7
elif b**2 + c**2 == a**2:
8
  print("YES", end='')
9
elif c**2 + a**2 == b**2:
10
  print("YES", end='')
11
else :
12
  print("NO", end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
2
3
NO
NO
Passed
Test Case 2
3
4
5
YES
YES
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed


3. Programming Assignment | Week 2

Due on 2024-02-08, 23:59 IST
Accept a string as input. Your task is to determine if the input string is a valid password or not. For a string to be a valid password, it must satisfy all the conditions given below:
(1) It should have at least 8 and at most 32 characters
(2) It should start with an uppercase or lowercase letter
(3) It should not have any of these characters: / \ = ' "
(4) It should not have spaces
It could have any character that is not mentioned in the list of characters to be avoided (points 3 and 4). Output True if the string forms a valid password and False otherwise.
Your last recorded submission was on 2024-02-08, 19:48 IST
Select the Language for this assignment. 
1
#3. Programming Assignment | Week 2
2
s = input()
3
if len(s)<8 or len(s)>32 :
4
  print("False", end='')
5
elif ( not (s[0].isalpha())) :
6
  print("False", end='')
7
elif any(c in s for c in ['/','\\','=',"'", '''"''']):
8
  print("False", end='')
9
elif ' ' in s:
10
  print("False", end='')
11
else :
12
  print("True", end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
abcd1234
True
True
Passed
Test Case 2
pass/word
False
False
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed









1. Programming Assignment | Week 3

Due on 2024-02-15, 23:59 IST

.     You are given a list marks that has the marks scored by a class of students in a Mathematics test. Find the median marks and store it in a float variable named median. You can assume that marks is a list of float values.


Procedure to find the median

(1) Sort the marks in ascending order. Do not try to use built-in methods.

(2) If the number of students is odd, then the median is the middle value in the sorted sequence. If the number of students is even, then the median is the arithmetic mean of the two middle values in the sorted sequence.


You do not have to accept input from the console as it has already been provided to you. You do not have to print the output to the console. Input-Output is the responsibility of the auto-grader for this problem.


FILL THE MISSING CODE

 

Your last recorded submission was on 2024-02-13, 08:25 IST
Select the Language for this assignment. 
1
def solution(marks):
2
    ### Enter your solution below this line
3
    ### Indent your entire code by one unit (4 spaces) to the right
4
    for i in range(len(marks)-1) :
5
        j = i + 1
6
        while (j<len(marks)) :
7
          if marks[i] > marks[j] :
8
            ( marks[i], marks[j] ) = ( marks[j], marks[i] )
9
          j = j + 1
10
    l = len(marks)
11
    if l%2 :
12
      median = marks[l//2]
13
    else :
14
      median = ( marks[l//2] + marks[l//2 - 1 ] )/2
0
### Enter your solution above this line
1
    return median
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
60,10,30,40,20,50
35.0
35.0\n
Passed after ignoring Presentation Error
Test Case 2
30,50,40,10,20
30.0
30.0\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed



2. Programming Assignment | Week 3

Due on 2024-02-15, 23:59 IST
Accept a string as input, convert it to lower case, sort the string in alphabetical order, and print the sorted string to the console. You can assume that the string will only contain letters.

Select the Language for this assignment. 
1
s = input()
2
s = s.lower()
3
l = len(s)
4
S=list(s)
5
for i in range(l-1) :
6
        j = i + 1
7
        while (j<l) :
8
          if S[i] > S[j] :
9
            ( S[i], S[j] ) = ( S[j], S[i] )
10
          j = j + 1
11
s = ''.join(S)
12
print(s, end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Bharatanatyam
aaaaabhmnrtty
aaaaabhmnrtty
Passed
Test Case 2
montypython
hmnnoopttyy
hmnnoopttyy
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed



3. Programming Assignment | Week 3

Due on 2024-02-15, 23:59 IST
You are given the dates of birth of two persons, not necessarily from the same family. Your task is to find the younger of the two. If both of them share the same date of birth, then the younger of the two is assumed to be that person whose name comes first in alphabetical order (names will follow Python's capitalize case format).

The input will have four lines. The first two lines correspond to the first person, while the last two lines correspond to the second person. For each person, the first line corresponds to the name and the second line corresponds to the date of birth in DD-MM-YYYY format. Your output should be the name of the younger of the two.
Your last recorded submission was on 2024-02-13, 09:38 IST
Select the Language for this assignment. 
1
import time
2
n1 = input()
3
date1 = input()
4
n2 = input()
5
date2 = input()
6
d1 = time.strptime(date1, "%d-%m-%Y")
7
d2 = time.strptime(date2, "%d-%m-%Y")
8
if d1 < d2 :
9
  print(n2, end='')
10
elif d2 < d1 :
11
  print(n1, end='')
12
elif n1<n2 :
13
  print(n1, end='')
14
else :
15
  print(n2, end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Harsh
10-03-1990
Sparsh
18-12-1987
Harsh
Harsh
Passed
Test Case 2
Harsh
18-01-2000
Sparsh
18-03-2000
Sparsh
Sparsh
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed






1. Programming Assignment | Week 4

Due on 2024-02-22, 23:59 IST

You are given the names and dates of birth of a group of people. Find all pairs of members who share a common date of birth. Note that this date need not be common across all pairs. It is sufficient if both members in a pair have the same date of birth.


The first line of input is a sequence of comma-separated names. The second line of input is a sequence of comma-separated positive integers. Each integer in the sequence will be in the range [1,365], endpoints inclusive, and stands for someday in the year.


Find all pairs of names that share a common date of birth and store them in a list called common. Each element of this list is itself a list and should be of the form [name1, name2], such that name1 comes before name2 in alphabetical order.


Notes

1)     You can assume that each test case will have at least one pair of members who share the same date of birth.

2)     You do not have to print the output to the console. This is the responsibility of the autograder. You just have to populate the list common in the required format.


Sample Input/Output

For example, consider the input:

sachin,ramesh,rohit,priya,saina,sandeep,stella

100,50,100,20,30,20,20

Your list common could look like this:

[['rohit', 'sachin'], ['priya', 'sandeep'], ['priya', 'stella'], ['sandeep', 'stella']]

 

 

Your last recorded submission was on 2024-02-15, 11:52 IST
Select the Language for this assignment. 
1
names = input().split(",")
2
d = input().split(",")
3
common = []
4
for i in range(len(names)-1):
5
  for j in range(i+1, len(names)):
6
    if d[i] == d[j]:
7
      if names[i] <= names[j] :
8
        common+= [[names[i],names[j]]]
9
      else :
10
        common+= [[names[j],names[i]]]
11
common.sort(key=lambda x: (names.index(x[0]), x[0], x[1]))
0
for pair in common:
1
    print(','.join(pair))
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
sachin,ramesh,kalam,priya,saina,sandeep,stella
100,50,100,20,30,20,20
kalam,sachin\n
priya,sandeep\n
priya,stella\n
sandeep,stella
kalam,sachin\n
priya,sandeep\n
priya,stella\n
sandeep,stella\n
Passed after ignoring Presentation Error
Test Case 2
newton,euler,gauss,pascal,fourier,leibniz,hardy,erdos
50,189,246,50,189,365,365,246
newton,pascal\n
euler,fourier\n
hardy,leibniz\n
erdos,gauss
newton,pascal\n
euler,fourier\n
hardy,leibniz\n
erdos,gauss\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




2. Programming Assignment | Week 4

Due on 2024-02-22, 23:59 IST

1.      Accept two square matrices A and B of dimensions n×n as input and compute their product AB.


The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.


Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denote a row of the matrix AB.

Your last recorded submission was on 2024-02-15, 14:21 IST
Select the Language for this assignment. 
1
n = int(input())
2
A = []
3
B = []
4
for i in range(n):
5
  A = A + [input().split(",")]
6
for i in range(n):
7
  B = B + [input().split(",")]
8
for i in range(n):
9
  for j in range(n):
10
    sum = 0
11
    for k in range(n):
12
      sum += int(A[i][k]) * int(B[k][j])
13
    if j == n-1:
14
      print(sum, end="")
15
    else :
16
      print(sum, end=",")
17
  if (i,j)!=(n-1,n-1):
18
    print()
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
1,2
3,4
5,6
7,8
19,22\n
43,50
19,22\n
43,50
Passed
Test Case 2
3
1,0,0
0,1,0
0,0,1
2,3,4
5,9,1
10,12,13
2,3,4\n
5,9,1\n
10,12,13
2,3,4\n
5,9,1\n
10,12,13
Passed






3. Programming Assignment | Week 4

Due on 2024-02-22, 23:59 IST

       Accept a square matrix A and an integer s as input and print the matrix sA as output. Multiplying a matrix by an integer s is equivalent to multiplying each element of the matrix by s. For example:



The first line of input is a positive integer, n, that denotes the dimension of the matrix A. Each of the next n lines contains a sequence of space-separated integers. The last line of the input contains the integer s.

Print the matrix sA as output. Each row of the matrix must be printed as a sequence of space-separated integers, one row on each line. There should not be any space after the last number on each line. If the expected output looks exactly like the actual output and still you are getting a wrong answer, it is because of the space at the end.

Your last recorded submission was on 2024-02-15, 12:40 IST
Select the Language for this assignment. 
1
n = int(input())
2
A = []
3
for i in range(n):
4
  A = A + [input().split(" ")]
5
s = int(input())
6
for i in range(n):
7
  for j in range(n):
8
    A[i][j] = int(A[i][j]) * s
9
    if j == n-1:
10
      print(A[i][j] , end="")
11
    else :
12
      print(A[i][j] , end=" ")
13
  if (i,j)!=(n-1,n-1):
14
    print()
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
1 2
3 4
2
2 4\n
6 8
2 4\n
6 8
Passed
Test Case 2
2
10 20
30 40
5
50 100\n
150 200
50 100\n
150 200
Passed









1. Programming Assignment | Week 5

Due on 2024-02-29, 23:59 IST

1.      Write a function insert that accepts a sorted list L of integers and an integer x as input. The function should return a sorted list with the element x inserted at the right place in the input list. The original list should not be disturbed in the process.


1)     The only built-in methods you are allowed to use are append and remove. You should not use any other method provided for lists.

2)     You do not have to accept input from the user or print output to the console. You just have to write the function definition.

 

Your last recorded submission was on 2024-02-17, 16:44 IST
Select the Language for this assignment. 
1
def insert(L, x):
2
    """
3
    insert an element x into a sorted list L
4
5
    Arguments:
6
        L: list
7
        x: integers
8
    Return:
9
        sorted_L: list
10
    """
11
    L.append(x)
12
    n = len(L)
13
    for i in range(n):
14
      for j in range(0,n-i-1):
15
        if(L[j]>L[j+1]):
16
          tmp = L[j]
17
          L[j] = L[j+1]
18
          L[j+1] = tmp
19
    return(L)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1,2,3,5
4
1,2,3,4,5
1,2,3,4,5\n
Passed after ignoring Presentation Error
Test Case 2
1,3,7,10,20
8
1,3,7,8,10,20
1,3,7,8,10,20\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




2. Programming Assignment | Week 5

Due on 2024-02-29, 23:59 IST

Accept a sequence of words as input. Create a dictionary named real_dict whose keys are the letters of the English alphabet. For each key (letter), the corresponding value should be a list of words that begin with this key (letter). For any given key, the words should be appended to the corresponding list in the order in which they appear in the sequence. You can assume that all words of the sequence will be in lower case.


You do not have to print the output to the console.

Your last recorded submission was on 2024-02-17, 15:47 IST
Select the Language for this assignment. 
1
real_dict = {}
2
A = []
3
A = A + input().split(",")
4
for i in range(len(A)):
5
  if A[i][0] in real_dict.keys() :
6
    real_dict[A[i][0]]+= [A[i]]
7
  else :
8
    real_dict[A[i][0]] = [A[i]]
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
apple,and,oranges,are,only,fruits
a:apple,and,are\n
o:oranges,only\n
f:fruits
a:apple,and,are\n
o:oranges,only\n
f:fruits\n
Passed after ignoring Presentation Error
Test Case 2
no,word,in,this,sentence,has,a,length,greater,than,ten
n:no\n
w:word\n
i:in\n
t:this,than,ten\n
s:sentence\n
h:has\n
a:a\n
l:length\n
g:greater
n:no\n
w:word\n
i:in\n
t:this,than,ten\n
s:sentence\n
h:has\n
a:a\n
l:length\n
g:greater\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




3. Programming Assignment | Week 5

Due on 2024-02-29, 23:59 IST

Consider a grid-world of size n×n. You are at the bottom-left corner, at the cell (1,1), to start with. A sample grid-world for the case of n=5 is given below for your reference:

You can move one step at a time in any one of the four directions: "North", "East", "West", "South". At every cell of the grid, all four moves are possible. The only catch is that if you make a move that will take you out of the grid at a border cell, you will stay put at the same cell. Concretely, if you are at the cell (1,4) and try to move west, you will remain at the same cell. Or if you are at (3,1) and try to move south, you will remain there.


Write a function named final that accepts a positive integer n and a sequence of moves (string) as arguments and returns the final position where you would end up in an n×n grid-world if you start at (1,1). You must return a tuple of size 2, where the first element is the x-coordinate and the second is the y-coordinate.


You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-02-17, 16:22 IST
Select the Language for this assignment. 
1
def final(n, moves):
2
    """
3
    Compute the final position
4
    
5
    Argument:
6
        n: int
7
        moves: string
8
    Return:
9
        (x, y): tuple
10
    """
11
    x=1
12
    y=1
13
    for i in range(len(moves)):
14
      if moves[i] == 'N' and y != n :
15
        y += 1
16
      if moves[i] == 'S' and y != 1 :
17
        y -= 1
18
      if moves[i] == 'E' and x != n :
19
        x += 1
20
      if moves[i] == 'W' and x != 1 :
21
        x -= 1
22
    return((x,y))
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
NEWS
(1, 1)
(1, 1)\n
Passed after ignoring Presentation Error
Test Case 2
7
NNNNNNNNNNEE
(3, 7)
(3, 7)\n
Passed after ignoring Presentation Error
Test Case 3
10
NNNEENSNENESNEWNSWENENSENWNSSSNWENESNESEW
(8, 7)
(8, 7)\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed
Test Case 6
Passed
Test Case 7
Passed







1. Programming Assignment | Week 6

Due on 2024-03-07, 23:59 IST

1.     The distance between two letters in the English alphabet is one more than the number of letters between them. Alternatively, it can be defined as the number of steps needed to move from the alphabetically smaller letter to the larger letter. This is always a non-negative integer. For example:


The distance between two words is defined as follows:

  • It is -1, if the words are f unequal lengths.
  • If the word-lengths are equal, it is the sum of the distances between letters at corresponding positions in the words. For example:

dword​(dog,cat)=dletter(d,c)+dletter​(o,a)+dletter​(g,t)=1+14+13=28


Write a function named distance that accepts two words as arguments and returns the distance between them.


You do not have to accept input from the user or print output to the console. You just have to write the function definition.

 


Your last recorded submission was on 2024-02-24, 09:52 IST
Select the Language for this assignment. 
1
def distance(word_1, word_2):   
2
    """
3
    calculate the distance between two words
4
    Argument:
5
        word1: str
6
        word2: str
7
    Return:
8
        dist: int
9
    """
10
    if  len(word_1) != len(word_2):
11
      dword = -1
12
    else:
13
      dword = 0
14
      for i in range(len(word_1)):
15
        dletter = ord(word_1[i])-ord(word_2[i])
16
        dword += abs(dletter)
17
    return(dword)
18
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
dog
cat
28
28\n
Passed after ignoring Presentation Error
Test Case 2
greet
great
4
4\n
Passed after ignoring Presentation Error






Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed





2. Programming Assignment | Week 6

Due on 2024-03-07, 23:59 IST

1.     P is a dictionary of father-son relationships that has the following structure: for any key in the dictionary, its corresponding value is the father of key. As an example:

P = {

    'Jahangir': 'Akbar',

    'Akbar': 'Humayun',

    'Humayun': 'Babur'   

}

If 'Jahangir' is the key, then 'Akbar', his father, is the value. This is true of every key in the dictionary.


Write a recursive function named ancestry that accepts the following arguments:

  • P: a dictionary of relationships
  • present: name of a person, string
  • past: name of a person, string

It should return the sequence of ancestors of the person named present, traced back up to the person named past. For example, ancestry(P, 'Jahangir', 'Babur') should return the list:

L = ['Jahangir', 'Akbar', 'Humayun', 'Babur']

In more Pythonic terms, L[i] is the father of L[i - 1], for 1≤ < len(L), with the condition that L[0] should be present and L[-1] should be past.


(1) You can assume that no two persons in the dictionary have the same name. However, a given person could either appear as a key or as a value in the dictionary.

(2) A given person could appear multiple times as one of the values of the dictionary. For example, in test-case-2, Prasanna has two sons, Mohan and Krishna, and hence appears twice (as a value).

(3) You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-02-24, 10:56 IST
Select the Language for this assignment. 
1
def ancestry(P, present, past):
2
    """
3
    A recursive function to compute the sequence of ancestors of person
4
5
    Arguments:
6
        P: dict, key and value are strings
7
        present: string
8
        past: string
9
    Return:
10
        result: list of strings
11
    """
12
    if P[present] == past :
13
      return [ present, P[present] ]
14
    else :
15
      return [ present ] + ancestry(P, P[present], past)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
{'Jahangir': 'Akbar', 'Akbar': 'Humayun', 'Humayun': 'Babur'}
Jahangir
Babur
['Jahangir', 'Akbar', 'Humayun', 'Babur']
['Jahangir', 'Akbar', 'Humayun', 'Babur']\n
Passed after ignoring Presentation Error
Test Case 2
{'Anil': 'Krishna', 'Mohan': 'Prasanna', 'Krishna': 'Prasanna', 'Prasanna': 'Mukesh'}
Anil
Prasanna
['Anil', 'Krishna', 'Prasanna']
['Anil', 'Krishna', 'Prasanna']\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




3. Programming Assignment | Week 6

Due on 2024-03-07, 23:59 IST

1.     Fibonacci

Fibonacci is a young resident of the Italian city of Pisa. He spends a lot of time visiting the Leaning Tower of Pisa, one of the iconic buildings in the city, that is situated close to his home. During all his visits to the tower, he plays a strange game while climbing the marble steps of the tower.


The Game

Fibonacci likes to climb the steps either one at a time, two at a time, or three at a time. This adds variety to the otherwise monotonous task of climbing. He wants to find the total number of ways in which he can climb n steps, if the order of his individual steps matters. Your task is to help Fibonacci compute this number.

For example, if he wishes to climb three steps, in the case of n=3, he could do it in four different ways:

  • (1,1,1) (1,1,1): do it in three moves, one step at a time
  • (1,2) (1,2): do it in two moves, first take a single step, then a double step
  • (2,1) (2,1): do it in two moves, first take a double step, then a single step
  • (3): do it in just one move, directly leaping to the third step

To take another example, if n=5, then some of the sequences could be:

 (1,3,1), (1,1,3), (3,1,1), (2,1,1,1), (1,2,1,1), (2,1,2)

Each sequence is one of the ways of climbing five steps. The point to note here is that each element of a sequence can only be 1, 2, or 3.


Write a recursive function named steps that accepts a positive integer n as the argument. It should return the total number of ways in which Fibonacci can ascend n steps. Note that the order of his steps is important.


You do not have to accept input from the user or print output to the console. You just have to write the function definition.


Your last recorded submission was on 2024-02-24, 11:12 IST
Select the Language for this assignment. 
1
def steps(n):
2
    """
3
    A recursive function to compute the number of ways to ascend steps 
4
5
    Argument:
6
        n: integer
7
    Return:
8
        result: integer
9
    """
10
    if n <= 1 :
11
      return 1
12
    if n == 2 :
13
      return 2
14
    if n == 3 :
15
      return 4
16
    else :
17
      return steps(n - 1) + steps(n - 2) + steps(n - 3)
0
# Basic idea behind the solution:
1
# The sum of all steps in a sequence should add up to n
2
# The last term in any sequence could be either 1, 2 or 3
3
# The number of sequences with last step as 1 is given by steps(n - 1)
4
# The number of sequences with last step as 2 is given by steps(n - 2)
5
# The number of sequences with last step as 3 is given by steps(n - 3)
6
# So, total number of sequences is steps(n - 1) + steps(n - 2) + steps(n - 3)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
1
1\n
Passed after ignoring Presentation Error
Test Case 2
2
2
2\n
Passed after ignoring Presentation Error






Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed
Test Case 6
Passed





1. Programming Assignment | Week 7

Due on 2024-03-14, 23:59 IST

In spreadsheets, columns are labelled as follows:


A is the first column, B is the second column, Z is the 26th column, and so on. Using the table given above, deduce the mapping between column labels and their corresponding numbers. Accept the column label as input and print the corresponding column number as output.

 


Your last recorded submission was on 2024-03-04, 19:01 IST
Select the Language for this assignment. 
1
column_label = input()
2
column_number = 0
3
for i in range(len(column_label)):
4
  column_number += (ord(column_label[-(i+1)])-64)*(26**i)
5
print(column_number, end="")
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
ABC
731
731
Passed
Test Case 2
ABCD
19010
19010
Passed
Test Case 3
ZYADBQRT
216563715388
216563715388
Passed



2. Programming Assignment | Week 7

Due on 2024-03-14, 23:59 IST

     queue at a fast-food restaurant operates in the following manner. The queue is a single line with its head at the left-end and tail at the right-end. A person can exit the queue only at the head (left) and join the queue only at the tail (right). At the point of entry, each person is assigned a unique token.

You have been asked to manage the queue. The following operations are permitted on it:

Operation

Meaning

JOIN,token

Add a person with this token number to the tail of the queue.

LEAVE

Remove the person from the head of the queue.

MOVE,token,HEAD

Move the person with this token number to the head of the queue.

MOVE,token,TAIL

Move the person with this token number to the tail of the queue.

PRINT

Print the queue as a sequence of comma-separated token numbers.
The head comes to the left, tail to the right.

The queue is empty to begin with. Keep accepting an operation as input from the user until the string "END" is entered.

Your last recorded submission was on 2024-03-04, 19:47 IST
Select the Language for this assignment. 
1
queue = []
2
op = input()
3
while(op != 'END'):
4
  if ',' in op:
5
    L=[]
6
    L = L + op.split(',')
7
    if( L[0] == 'JOIN'):
8
      queue.append(L[1])    
9
    elif ( L[0] == 'MOVE'):
10
      if( L[2] == 'HEAD'):
11
        queue.remove(L[1])
12
        queue.insert(0, L[1])
13
      elif ( L[2] == 'TAIL'):
14
        queue.remove(L[1])
15
        queue.append(L[1])
16
  elif ( op == 'PRINT'):
17
    print(','.join(map(str, queue)))
18
  elif ( op == 'LEAVE'):
19
    queue.pop(0)
20
  op = input()
21
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
JOIN,1
PRINT
JOIN,2
JOIN,3
LEAVE
PRINT
END
1\n
2,3
1\n
2,3\n
Passed after ignoring Presentation Error
Test Case 2
JOIN,1
JOIN,2
JOIN,3
PRINT
MOVE,3,HEAD
PRINT
END
1,2,3\n
3,1,2
1,2,3\n
3,1,2\n
Passed after ignoring Presentation Error
Test Case 3
JOIN,1
JOIN,2
JOIN,3
PRINT
MOVE,1,TAIL
PRINT
LEAVE
JOIN,4
PRINT
MOVE,4,HEAD
PRINT
LEAVE
PRINT
END
1,2,3\n
2,3,1\n
3,1,4\n
4,3,1\n
3,1
1,2,3\n
2,3,1\n
3,1,4\n
4,3,1\n
3,1\n
Passed after ignoring Presentation Error



3. Programming Assignment | Week 7

Due on 2024-03-14, 23:59 IST

Accept a string of lowercase letters from the user and encrypt it using the following image:


Each letter in the string that appears in the upper half should be replaced with the corresponding letter in the lower half and vice versa. Print the encrypted string as output.

Your last recorded submission was on 2024-03-04, 20:24 IST
Select the Language for this assignment. 
1
s = input()
2
S = ''
3
for i in range(len(s)):
4
  S += chr(219-ord(s[i]))
5
print(S, end='')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
apply
zkkob
zkkob
Passed
Test Case 2
secret
hvxivg
hvxivg
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed





1. Programming Assignment | Week 8

Due on 2024-03-21, 23:59 IST

A round-robin tournament is one in which each team competes with every other team. Consider a version of the IPL tournament in which every team plays exactly one game against every other team. All these games have a definite result and no match ends in a tie. The winning team in each match is awarded one point.

Eight teams participate in this round-robin cricket tournament: CSK, DC, KKR, MI, PK, RR, RCB and SH. You are given the details of the outcome of the matches. Your task is to prepare the IPL points table in descending order of wins. If two teams have the same number of points, the team whose name comes first in alphabetical order must figure higher up in the table.


There are eight lines of input. Each line is a sequence of comma-separated team names. The first team across these eight lines will always be in this order: CSK, DC, KKR, MI, PK, RR, RCB and SH. For a given sequence, all the other terms represent the teams that have lost to the first team. For example, the first line of input could be CSK, MI, DC, PK. This means that CSK has won its matches against the teams MI, DC, and PK and lost its matches against all other teams. If a sequence has just one team, it means that it lost all its matches.


Print the IPL points table in the following format — team: wins — one team on each line. There shouldn't be any spaces in any of the lines.

 


Your last recorded submission was on 2024-03-10, 12:05 IST
Select the Language for this assignment. 
1
def MyFn(s):
2
    return s[0]
3
A = []
4
for i in range(0,8):
5
  A = A + input().split("\n")
6
  A[i] = A[i].split(",")
7
A = sorted(A,key=MyFn)
8
A = sorted(A,key=len, reverse=True)
9
dict = {}
10
for i in range(0,8):
11
  dict[A[i][0]] =len(A[i])-1
12
for key, value in dict.items():
13
    print(f'{key}:{value}')
14
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
CSK,DC,KKR,MI,PK,RR,RCB,SH
DC,KKR,MI,PK,RR,RCB,SH
KKR,MI,PK,RR,RCB,SH
MI,PK,RR,RCB,SH
PK,RR,RCB,SH
RR,RCB,SH
RCB,SH
SH
CSK:7\n
DC:6\n
KKR:5\n
MI:4\n
PK:3\n
RR:2\n
RCB:1\n
SH:0
CSK:7\n
DC:6\n
KKR:5\n
MI:4\n
PK:3\n
RR:2\n
RCB:1\n
SH:0\n
Passed after ignoring Presentation Error
Test Case 2
CSK,DC,MI,PK
DC,RR,RCB,SH
KKR,CSK,DC,RR,MI
MI,DC,RCB,RR
PK,DC,KKR,MI,RCB
RR,CSK,PK,SH
RCB,CSK,KKR,RR
SH,CSK,KKR,MI,PK,RCB
SH:5\n
KKR:4\n
PK:4\n
CSK:3\n
DC:3\n
MI:3\n
RCB:3\n
RR:3
SH:5\n
KKR:4\n
PK:4\n
CSK:3\n
DC:3\n
MI:3\n
RCB:3\n
RR:3\n
Passed after ignoring Presentation Error
Test Case 3
CSK,RCB,MI,KKR
DC,CSK,KKR,MI,SH
KKR,PK,RR,RCB,SH
MI,KKR,PK,RR,SH,RCB
PK,CSK,DC,RCB
RR,CSK,DC,PK,SH
RCB,DC,RR
SH,CSK,PK,RCB
MI:5\n
DC:4\n
KKR:4\n
RR:4\n
CSK:3\n
PK:3\n
SH:3\n
RCB:2
MI:5\n
DC:4\n
KKR:4\n
RR:4\n
CSK:3\n
PK:3\n
SH:3\n
RCB:2\n
Passed after ignoring Presentation Error



2. Programming Assignment | Week 8

Due on 2024-03-21, 23:59 IST

1.     Given a square matrix M and two indices (i,j), Mij​ is the matrix obtained by removing the ith row and the jth column of M.


Write a function named minor_matrix that accepts three arguments:

  • M: a square matrix
  • i: a non-negative integer
  • j: a non-negative integer

The function should return the matrix Mij​ after removing the ith row and the jth column of M. Note that we use zero-based indexing throughout. That is, if the matrix M is of dimensions n×n, then we have 0 ≤ i≤ n−1.


(1) You can assume that the number of rows in M will be at least 3 in each test case.

(2) You do not have to accept input from the user or print the output to the console.

Your last recorded submission was on 2024-03-10, 12:38 IST
Select the Language for this assignment. 
1
def minor_matrix(M, i, j):
2
    """
3
    Compute the "minor matrix"
4
5
    Arguments:
6
        M: list of lists
7
        i: integer
8
        j: integer
9
    Return:
10
        M_ij: list of lists
11
    """
12
    del M[i]
13
    for m in range(len(M)):
14
      del M[m][j]
15
    return(M)
16
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
1,2,3
4,5,6
7,8,9
0
1
4,6\n
7,9
4,6\n
7,9\n
Passed after ignoring Presentation Error
Test Case 2
3
1,2,3
4,5,6
7,8,9
2
0
2,3\n
5,6
2,3\n
5,6\n
Passed after ignoring Presentation Error



3. Programming Assignment | Week 8

Due on 2024-03-21, 23:59 IST
Write a function named two_level_sort that accepts a list of tuples named scores as argument. Each element in this list is of the form (Name, Marks) and represents the marks scored by a student in a test: the first element is the student's name and the second element is his or her marks.

The function should return a list of tuples sorted that is sorted in two levels:

Level-1: ascending order of marks
Level-2: alphabetical order of names among those students who have scored equal marks
Each element in the returned list should also be of the form (Name, marks). Note that level-2 should not override level-1. That is, after the second level of sorting, the list should still be sorted in ascending order of marks. Additionally, the students having the same marks should appear in alphabetical order.

Sample input-output behaviour


(1) You should not use any built-in sort functions to solve this problem.
(2) You do not have to accept input from the user or print output to the console. You just have to write the function definition.
Your last recorded submission was on 2024-03-10, 12:57 IST
Select the Language for this assignment. 
1
def two_level_sort(scores):
2
  for i in range(len(scores)):
3
    for j in range(0,len(scores)-i-1):
4
      if(scores[j][0]>scores[j+1][0]):
5
        tmp = scores[j]
6
        scores[j] = scores[j+1]
7
        scores[j+1] = tmp
8
  for i in range(len(scores)):
9
    for j in range(0,len(scores)-i-1):
10
      if(scores[j][1]>scores[j+1][1]):
11
        tmp = scores[j]
12
        scores[j] = scores[j+1]
13
        scores[j+1] = tmp  
14
  return(scores)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
[('Harish', 80), ('Ram', 90), ('Harshita', 80)]
Harish:80\n
Harshita:80\n
Ram:90
Harish:80\n
Harshita:80\n
Ram:90\n
Passed after ignoring Presentation Error
Test Case 2
[('Sachin', 70), ('Sam', 69), ('Anirudh', 70), ('Anita', 80)]
Sam:69\n
Anirudh:70\n
Sachin:70\n
Anita:80
Sam:69\n
Anirudh:70\n
Sachin:70\n
Anita:80\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed

















































































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.