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 - 2025 | Swayam

 

NPTEL » The Joy of Computing using Python



  Please scroll down for latest Programs 👇 



Week 2: Programming Assignment 1

Due on 2025-02-06, 23:59 IST

Create a Python program that calculates the sum of the squares of the first n natural numbers. The program should prompt the user to input a number n, then compute and print the sum of squares from 1 to n.

Input Format: The input consists of a single integer, n.

Output Format: The output consists of the sum of squares of the first n natural numbers.

Example:
Input:
5

Output:
55

Your last recorded submission was on 2025-01-30, 18:33 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
n = int(input())
3
print( n*(n+1)*(2*n+1)//6 , end="")
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
0
0
0
Passed
Test Case 2
3
14
14
Passed
Test Case 3
1
1
1
Passed



Week 2: Programming Assignment 2

Due on 2025-02-06, 23:59 IST

Write a Python program to calculate the arithmetic mean of n numbers, rounding the result to the nearest integer. The program should first take a single line of input containing n space-separated integer numbers, and then output the rounded mean of these numbers.

Input Format:
The input consists of a single line containing an integer n followed by n space-separated numbers.

Output Format:
The output is the arithmetic mean of the input numbers, rounded to the nearest integer.

Example:
Input:
5 10 15 20

Output:
13

Your last recorded submission was on 2025-01-30, 18:34 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
L = input().split(' ')
3
L = [int(num) for num in L]
4
sum = 0
5
for x in L:
6
  sum+=x
7
print(sum//len(L), end='')
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 3
3
3
Passed
Test Case 2
5 15 25
15
15
Passed
Test Case 3
100
100
100
Passed



Week 2: Programming Assignment 3

Due on 2025-02-06, 23:59 IST

Develop a Python program that determines the mode of a list of n numbers, assume only one mode exists in the provided test cases. The program should ask for a single line of input containing n, then n space-separated numbers, and output the mode.


Input Format:
The first line contains an integer n, followed by n space-separated numbers.

Output Format:
The output is the mode of the list.

Example:
Input:
5 1 2 2 3 4

Output:
2

Your last recorded submission was on 2025-01-30, 18:35 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
numbers = input().split(' ')
3
numbers = [int(num) for num in numbers]
4
counts = {}
5
for number in numbers:
6
  counts[number] = counts.get(number, 0) + 1
7
max_count = 0
8
mode = None
9
for number, count in counts.items():
10
  if count > max_count:
11
    max_count = count
12
    mode = number
13
print(mode, end='')
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 1 2 2 3 4
2
2
Passed
Test Case 2
3 1 1 1
1
1
Passed
Test Case 3
2 5 5
5
5
Passed





Week 3: Programming Assignment 1

Due on 2025-02-13, 23:59 IST

Create a Python program that finds the second smallest number in a list of positive integers (including zero). The program should prompt the user to input a list of numbers, then compute and print the second smallest number in that list.

  • Input Format:
    The input consists of a single list of numbers, separated by spaces.
    Hint: Use .split() function to convert input to list.

  • Output Format:
    The output consists of the second smallest number in the input list.

Example:

Input:
3 1 4 1 5 9 2 6 5 3 5

Output:
2

Your last recorded submission was on 2025-02-12, 16:04 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
L = input().split(' ')
3
L = [int(num) for num in L]
4
unique_numbers = sorted(list(set(L)))
5
print(unique_numbers[1], end='')
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 4 1 5 9 2 6 5 3 5
2
2
Passed
Test Case 2
0 2 2 2 2 10
2
2
Passed
Test Case 3
10 8 8 8 8 9
9
9
Passed



Week 3: Programming Assignment 2

Due on 2025-02-13, 23:59 IST

Create a Python program that removes even duplicate positive integer numbers(includes zero) from a list and prints the unique numbers in the order they first appeared.
The program should prompt the user to input a list of numbers, then process the list to remove duplicates and print the resulting list of unique numbers.

Input Format:
The input consists of a single list of numbers, separated by spaces.

Output Format:
The output consists of the unique numbers, separated by spaces, from the input list, in the order they first appeared.

Example:
Input:
3 1 4 1 5 9 2 6 5 3 5
Output:
3 1 4 1 5 9 2 6 5 3 5

Your last recorded submission was on 2025-02-12, 16:27 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
L = input().split(' ')
3
numbers = [int(num) for num in L]
4
unique_numbers = []
5
seen = set()
6
for num in numbers:
7
  if num >= 0 and num % 2==0:
8
    if num not in seen:
9
      unique_numbers.append(num)
10
      seen.add(num)
11
  else:
12
    unique_numbers.append(num)
13
print(' '.join(map(str, unique_numbers)), end='')
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 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
Passed
Test Case 2
0 2 2 1 3
0 2 1 3
0 2 1 3
Passed
Test Case 3
10 8 8 8 8 9 9
10 8 9 9
10 8 9 9
Passed



Week 3: Programming Assignment 3

Due on 2025-02-13, 23:59 IST

Create a Python program that takes a list of integers, reverses the list, adds the values at even indices from both the original and reversed lists, and creates a new list with the result. The new list should be printed in the end.

Input Format:
The input consists of a single list of integers, separated by spaces.

Output Format:
The output consists of the new list of values, separated by spaces, obtained by adding values at even indices from both the original and reversed lists.


Example:
Input:

1 2 3 4 5

Output:
6 2 6 4 6

Your last recorded submission was on 2025-02-12, 16:32 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
L = input().split(' ')
3
numbers = [int(num) for num in L]
4
reversed_numbers = numbers[::-1]
5
new_list = []
6
for i in range(len(numbers)):
7
  if i % 2 == 0:
8
    if i < len(reversed_numbers):
9
      new_list.append(numbers[i] + reversed_numbers[i])
10
    else:
11
      new_list.append(numbers[i])
12
  else:
13
    new_list.append(numbers[i])
14
print(' '.join(map(str, new_list)),end='')
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 4 5
6 2 6 4 6
6 2 6 4 6
Passed
Test Case 2
2 3 37 43 21
23 3 74 43 23
23 3 74 43 23
Passed
Test Case 3
0 1
1 1
1 1
Passed





Week 4: Programming Assignment 1

Due on 2025-02-20, 23:59 IST

Create a Python program that checks whether a given square matrix is diagonal. A diagonal matrix is a square matrix (same number of rows and columns) where all the entries outside the main diagonal are zero. The program should prompt the user to input the dimensions of the matrix and then input the matrix elements. The program should then determine whether the matrix is diagonal and print 1 if it is, otherwise print 0.

Input Format:
The first input is an integer r , the number of rows and columns in the matrix.
The next r lines each contain r integers, representing the elements of each row of the matrix.

Output Format:
The output is 1 if a matrix is diagonal, otherwise 0.


Example:

Input:

2
1 0
0 1

Output:
1

Your last recorded submission was on 2025-02-19, 14:38 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
r = int(input())
3
A = [ ]
4
for i in range(r):
5
  row = [ ]
6
  for x in input().split(' '):
7
    row.append(int(x))
8
  A.append(row)
9
flag = True
10
for i in range(r):
11
  for j in range(r):
12
    if i != j and A[i][j] !=0:
13
      flag = False
14
      break
15
    else:
16
      continue
17
if flag == True:
18
  print('1', end='')
19
else:
20
  print('0', end='')
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
2
1 0
0 1
1
1
Passed
Test Case 2
2
1 2
0 3
0
0
Passed
Test Case 3
3
5 0 0
0 3 0
0 0 9
1
1
Passed



Week 4: Programming Assignment 2

Due on 2025-02-20, 23:59 IST



Create a Python program that adds the transpose of a given matrix by a scalar. The program should prompt the user to input the dimensions of the matrix, the elements of the matrix, and the scalar value. The program should then compute the transpose of the matrix, add it by the scalar, and print the resulting matrix.



Input:
The first input is an integer 𝑟 , the number of rows in the matrix.
The second input is an integer 𝑐 , the number of columns in the matrix.
The next 𝑟 lines each contain 𝑐 integers, representing the elements of the matrix.
The final input is an integer 𝑠, representing the scalar value.

Output Format:
The output consists of 𝑐 lines, each containing 𝑟 integers, representing the elements of the resulting matrix after adding the transpose of the original matrix by the scalar.


Example:

Input:

2
3
1 2 3
4 5 6
2

Output:

3 6
4 7
5 8

Your last recorded submission was on 2025-02-19, 14:48 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
r = int(input())
3
c = int(input())
4
A = [ ]
5
for i in range(r):
6
  row = [ ]
7
  for x in input().split(' '):
8
    row.append(int(x))
9
  A.append(row)
10
s = int(input())
11
T = [ ]
12
for j in range(c):
13
  row = [ ]
14
  for i in range(r):
15
    row.append(A[i][j])
16
  T.append(row)
17
for i in range(c):
18
  for j in range(r):
19
    if j == r-1:
20
      print(T[i][j]+s, end="")
21
    else :
22
      print(T[i][j]+s, end=" ")
23
  if (i,j)!=(c-1,r-1):
24
    print()
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
2
3
1 2 3
4 5 6
2
3 6\n
4 7\n
5 8
3 6\n
4 7\n
5 8
Passed
Test Case 2
3
2
1 2
3 4
5 6
3
4 6 8\n
5 7 9
4 6 8\n
5 7 9
Passed
Test Case 3
2
2
-1 0
2 -3
4
3 6\n
4 1
3 6\n
4 1
Passed



Week 4: Programming Assignment 3

Due on 2025-02-20, 23:59 IST

Create a Python program that checks whether a given square matrix is symmetric. A matrix is symmetric if its transpose is equal to the matrix itself. The program should prompt the user to input the dimensions of the matrix and then input the matrix elements. The program should then determine whether the matrix is symmetric and print 1 if it is, otherwise print 0.

Input Format:
The first input is an integer r , the number of rows and columns in the matrix.
The next r lines each contain r integers, representing the elements of each row of the matrix.

Output Format:
The output is 1 if a matrix is symmetric, otherwise 0.


Example:

Input:

2
1 2
2 1


Output:
1

Your last recorded submission was on 2025-02-19, 14:56 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def is_symmetric(matrix, n):
3
    for i in range(n):
4
        for j in range(n):
5
            if matrix[i][j] != matrix[j][i]:
6
                return 0  # The matrix is not symmetric
7
    return 1  # The matrix is symmetric
8
def main():
9
    n = int(input())
10
    matrix = []
11
    for _ in range(n):
12
        row = list(map(int, input().split()))
13
        matrix.append(row)
14
    result = is_symmetric(matrix, n)
15
    print(result, end="")
16
main()
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
2
1 2
2 1
1
1
Passed
Test Case 2
2
2 3
4 5
0
0
Passed
Test Case 3
3
1 2 3
2 5 7
3 7 9
1
1
Passed



Week 5: Programming Assignment 1

Due on 2025-02-27, 23:59 IST

The Mystery of Word Frequencies

You are tasked with analyzing a mysterious message that was intercepted. The message seems to contain a series of repeated words, and the challenge is to count how many times each word appears. To solve this, you must write a Python program that takes a string of text and counts the frequency of each word.

Make sure your program is case-insensitive, as the secret message is a mix of upper and lower case letters.

After counting the occurrences of each word, display the results.

Input Format:  A line of text

Output format: For all the unique words in the sentence, word: followed by frequency.


Example :

Input :
The quick brown fox jumped over the lazy dog. The quick fox jumped over the dog."

Output:

the: 3

quick: 2

brown: 1

fox: 2

jumped: 2

over: 2

lazy: 1

dog: 2


Your last recorded submission was on 2025-02-23, 17:21 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def count_word_frequency(text):
3
    words = text.lower().split()# Convert to lowercase and split into words
4
    word_counts = {}
5
    for word in words:
6
        if word: # only count words that are not empty after removing punctuation.
7
            word_counts[word] = word_counts.get(word, 0) + 1
8
    return word_counts
9
input_text = input()
10
frequency = count_word_frequency(input_text)
11
for word, count in frequency.items():
12
  print(f"{word}: {count}")
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
There is always one more bug to fix
there: 1\n
is: 1\n
always: 1\n
one: 1\n
more: 1\n
bug: 1\n
to: 1\n
fix: 1
there: 1\n
is: 1\n
always: 1\n
one: 1\n
more: 1\n
bug: 1\n
to: 1\n
fix: 1\n
Passed after ignoring Presentation Error
Test Case 2
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
buffalo: 8
buffalo: 8\n
Passed after ignoring Presentation Error
Test Case 3
a a b b z b z c d e f y l
a: 2\n
b: 3\n
z: 2\n
c: 1\n
d: 1\n
e: 1\n
f: 1\n
y: 1\n
l: 1
a: 2\n
b: 3\n
z: 2\n
c: 1\n
d: 1\n
e: 1\n
f: 1\n
y: 1\n
l: 1\n
Passed after ignoring Presentation Error



Week 5: Programming Assignment 2

Due on 2025-02-27, 23:59 IST

Merging Dictionaries

You are required to write a Python function merge_dicts that takes two dictionaries as input and returns a new dictionary containing all the entries from both input dictionaries. The function should not modify the input dictionaries.

The dictionaries will have strings as keys and numbers as values. In the case where the same key appears in both dictionaries, the corresponding value in the output dictionary should be the sum of the values from both dictionaries.


You need to ensure the following:

If a key exists in only one of the dictionaries, its value should remain the same in the result.

If a key exists in both dictionaries, sum the values associated with that key.

The output dictionary should include all the keys from both input dictionaries, with updated values as described above.

 

Input Format:

Two dictionaries containing string keys and numerical values in two separate lines.


Output Format:

A new dictionary containing all the entries from both the dictionaries, with values summed for common keys.

 

Example :

Input:

{'Schuyler': 10, 'Brendan': 15}

{'Aaron': 5, 'Mansi': 20, 'Schuyler': -10}

 Output:

{'Schuyler': 0, 'Aaron': 5, 'Mansi': 20, 'Brendan': 15}


Your last recorded submission was on 2025-02-23, 17:26 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def merge_dicts(d1, d2):
3
    merged_dict = dict1.copy()  # Create a copy of the first dictionary
4
    for key, value in dict2.items():
5
        if key in merged_dict:
6
            merged_dict[key] += value  # Add values for common keys
7
        else:
8
            merged_dict[key] = value   # Add new keys and values
9
    return merged_dict
0
def parse_input(input_string):
1
    input_string = input_string.strip('{}').strip()
2
    parsed_dict = {}
3
    if input_string:
4
        items = input_string.split(',')
5
        for item in items:
6
            key, value = item.split(':')
7
            key = key.strip().strip("'").strip('"') 
8
            value = int(value.strip())  
9
            parsed_dict[key] = value    
10
    return parsed_dict
11
 
12
if __name__ == "__main__":
13
    dict1_input = input()
14
    dict2_input = input()
15
 
16
    dict1 = parse_input(dict1_input)
17
    dict2 = parse_input(dict2_input)
18
 
19
    merged_dict = merge_dicts(dict1, dict2)
20
    print("Merged Dictionary:", merged_dict)
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
{'Alice': 5, 'Bob': 7}
{}
Merged Dictionary: {'Alice': 5, 'Bob': 7}
Merged Dictionary: {'Alice': 5, 'Bob': 7}\n
Passed after ignoring Presentation Error
Test Case 2
{'X': -5, 'Y': -3}
{'X': 5, 'Y': 3}
Merged Dictionary: {'X': 0, 'Y': 0}
Merged Dictionary: {'X': 0, 'Y': 0}\n
Passed after ignoring Presentation Error
Test Case 3
{'A': 1000000000, 'B': 2000000000}
{'A': 500000000, 'C': 1000000000}
Merged Dictionary: {'A': 1500000000, 'B': 2000000000, 'C': 1000000000}
Merged Dictionary: {'A': 1500000000, 'B': 2000000000, 'C': 1000000000}\n
Passed after ignoring Presentation Error



Week 5: Programming Assignment 3

Due on 2025-02-27, 23:59 IST

Sorting the Royal Manuscripts

You are an apprentice in the Royal Library of the Vijayanagara Empire, home to centuries-old ancient manuscripts filled with knowledge on astronomy, Ayurveda, and warfare. However, a strong monsoon wind has scattered the manuscripts in random order!

The Rajguru (Royal Scholar) assigns you a crucial task: sort the manuscripts using the ancient "Bubble Sort" technique. But there’s a rule—you must record the order of manuscripts after every pass, as the Rajguru wishes to witness the sorting magic unfold gradually.

Your sorting spell, called "Grantha Sudharaka" (Sacred Manuscript Arranger), can swap two manuscripts at a time, ensuring the heaviest ones settle at the bottom after each pass. Your goal is to restore order to the library as quickly as possible. If there are no swaps, you can stop the process of arranging.

Can you help bring back order to the Royal Library of Vijayanagara?

Input Format :

List of integers separated by spaces.

Output Format :

Every pass output in a single line.

Example:

Input

700 200 500 300

Output

200 500 300 700 

200 300 500 700 

200 300 500 700  

Your last recorded submission was on 2025-02-23, 18:47 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def grantha_sudharaka(manuscripts):
3
    n = len(manuscripts)
4
    for i in range(n - 1):
5
        swapped = False
6
        for i in range(n - 1 -i):
7
            if manuscripts[i] > manuscripts[i + 1]:
8
                manuscripts[i], manuscripts[i + 1] = manuscripts[i + 1], manuscripts[i]
9
                swapped = True
10
        print(*manuscripts)
11
        if swapped == False:
12
          break
13
manuscripts = list(map(int, input().split()))
14
grantha_sudharaka(manuscripts)
0
#
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 4 1 5 9 2 6
1 3 1 4 5 2 6 9\n
1 1 3 4 2 5 6 9\n
1 1 3 2 4 5 6 9\n
1 1 2 3 4 5 6 9\n
1 1 2 3 4 5 6 9
1 3 1 4 5 2 6 9\n
1 1 3 4 2 5 6 9\n
1 1 3 2 4 5 6 9\n
1 1 2 3 4 5 6 9\n
1 1 2 3 4 5 6 9\n
Passed after ignoring Presentation Error
Test Case 2
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5\n
Passed after ignoring Presentation Error
Test Case 3
5 4 3 2 1
4 3 2 1 5\n
3 2 1 4 5\n
2 1 3 4 5\n
1 2 3 4 5
4 3 2 1 5\n
3 2 1 4 5\n
2 1 3 4 5\n
1 2 3 4 5\n
Passed after ignoring Presentation Error



Week 6: Programming Assignment 1

Due on 2025-03-06, 23:59 IST

The Treasure Hunt

You are part of an elite team of treasure hunters, and your latest mission is to find the hidden treasure in a mysterious ancient temple. Inside the temple, you discover a series of enchanted scrolls containing arrays of magical numbers. The numbers are encrypted in different arrays, and each scroll has a clue indicating which element in the array holds the key to the treasure.

You are given one of these arrays, and the clue on the scroll tells you that the treasure is hidden inside the kth smallest number in that array. Your task is to figure out the kth smallest number from the array, where k is smaller than the size of the array, and return it as your answer.

The treasure hunters in your team trust you with this task, and the sooner you find the key, the sooner they can open the vault. So, be efficient and find the answer quickly!

Example:

Imagine the scroll has the following magical array: 12 3 5 7 19 17 4

And the clue tells you that k = 4.

 Your mission is to find the 4th smallest number in the array. Upon scanning the array, you discover that the 4th smallest number is 7.

 Input:

A list of n integers

An integer k (where k <= n).

Output:

Return the kth smallest element from the array.

If k > n, then generate an error message, ‘The clue k should be smaller than or equal to the list size’


Your last recorded submission was on 2025-02-28, 11:51 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
L = input().strip(' ').split(' ')
3
k = int(input())
4
L = [int(num) for num in L]
5
if k > len(L):
6
  print("The clue k should be smaller than or equal to the list size", end='')
7
else:
8
  sorted_arr = sorted(L)
9
  print(sorted_arr[k - 1], end='')
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 10 2 5 8 7     
2
2
2
Passed
Test Case 2
1 2 3 4 5 6
9
The clue k should be smaller than or equal to the list size
The clue k should be smaller than or equal to the list size
Passed
Test Case 3
12 3 5 7 19 17 4
4
7
7
Passed



Week 6: Programming Assignment 2

Due on 2025-03-06, 23:59 IST

The Binary Quest

Alex, a young programmer, discovered a challenge: Convert a decimal number to binary using recursion! The rules were simple—no loops, no built-in functions —only the magic of recursion.

 Steps to convert a decimal number to binary:

·       Divide the number by 2.

·       Store the remainder (0 or 1).

·       Repeat with the quotient until it becomes 0.

·       Read the remainders in reverse order to get the binary number.

 For example, Consider a decimal number 13

13 ÷ 2 = 6, remainder 1

6 ÷ 2 = 3, remainder 0

3 ÷ 2 = 1, remainder 1

1 ÷ 2 = 0, remainder 1

Binary: 1101 (reading from bottom to top)

 Input Format : A positive integer, n

13

Output Format : Binary number

1101

Your last recorded submission was on 2025-02-28, 12:05 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def decimal_to_binary(n):
3
  if n == 0:
4
    return "0"
5
  if n == 1:
6
    return "1"
7
  else:
8
    return decimal_to_binary(n // 2) + str(n % 2)
9
n = int(input())
10
print(decimal_to_binary(n), end='')
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
16
10000
10000
Passed
Test Case 2
255
11111111
11111111
Passed
Test Case 3
0
0
0
Passed



Week 6: Programming Assignment 3

Due on 2025-03-06, 23:59 IST

Removing an element Recursively:

Write a recursive function delete_elements in Python that removes all occurrences of a specified item from a list, creating and returning a new list without modifying the input list.

 

Example :

Input:

A list of integers (can contain duplicates)

An integer item which represents the element to be removed from the list.

4 5 6

4

Output:

Original list

A new list with all occurrences of the item removed.

4 5 6

5 6

Your last recorded submission was on 2025-02-28, 12:28 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def delete_elements(lst, item):
3
    if not lst:
4
        return []
5
    if lst[0] == item:
6
        return delete_elements(lst[1:], item)
7
    else:
8
        return [lst[0]] + delete_elements(lst[1:], item)
9
lst = list(map(int, input().split(' ')))
10
item = int(input())
11
new_lst = delete_elements(lst, item)
12
print(*new_lst, end='')
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
4 5 6 7 4 8
4
5 6 7 8
5 6 7 8
Passed
Test Case 2
1 2 3 4 5
6
1 2 3 4 5
1 2 3 4 5
Passed
Test Case 3
1 2 3 2 4 2
2
1 3 4
1 3 4
Passed



Week 7: Programming Assignment 1

Due on 2025-03-13, 23:59 IST

Magic Square

A magic square of order n is a square arrangement of n2 numbers, typically distinct integers, where the sum of the n numbers in each row, each column, and both main diagonals is the same constant. It consists of the integers from 1 to .

The constant sum of each row, column, and diagonal in a magic square is known as the magic constant or magic sum, M. For a normal magic square, this constant depends solely on n and is given by the formula:

=((2+1))/2

For normal magic squares of order n = 3,4,5,...,

the magic constants are: 15,34,65,111,175,260,...

Given a matrix, check whether it’s Magic Square or not.

Input Format:
An integer value for n (n>2)
An nXn matrix

Output Format:
If the given matrix is magic square then print “Magic Matrix”, otherwise “Not a Magic Matrix

Example :
Input
3
2 7 6
9 5 1
4 3 8

Output
Magic Matrix

Your last recorded submission was on 2025-03-13, 09:39 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def is_magic_square(matrix, n):
3
    magic_constant = n * (n**2 + 1) // 2
4
    for row in matrix:
5
        if sum(row) != magic_constant:
6
            return "Not a Magic Matrix"
7
    for j in range(n):
8
        col_sum = 0
9
        for i in range(n):
10
            col_sum += matrix[i][j]
11
        if col_sum != magic_constant:
12
            return "Not a Magic Matrix"
13
    diag1_sum = 0
14
    for i in range(n):
15
        diag1_sum += matrix[i][i]
16
    if diag1_sum != magic_constant:
17
        return "Not a Magic Matrix"
18
    diag2_sum = 0
19
    for i in range(n):
20
        diag2_sum += matrix[i][n - 1 - i]
21
    if diag2_sum != magic_constant:
22
        return "Not a Magic Matrix"
23
    return "Magic Matrix"
0
n = int(input())
1
matrix = [list(map(int, input().split())) for _ in range(n)]
2
 
3
print(is_magic_square(matrix, n))
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
2 3 1
3 1 2
Not a Magic Matrix
Not a Magic Matrix\n
Passed after ignoring Presentation Error
Test Case 2
4
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Magic Matrix
Magic Matrix\n
Passed after ignoring Presentation Error
Test Case 3
3
2 7 6
9 5 1
4 3 8
Magic Matrix
Magic Matrix\n
Passed after ignoring Presentation Error



Week 7: Programming Assignment 2

Due on 2025-03-13, 23:59 IST

Median of Matrix

You are given a row-wise sorted matrix of size n×m, where both the number of rows and the number of columns are always odd. Your task is to find the median of the matrix.

The median is the middle number in a sorted list of numbers. In case the list has an even number of elements, the median is the leftmost of the two middle elements.

The matrix is sorted row-wise, but the entire matrix is not necessarily sorted.

You need to find the median from the matrix when all the elements are sorted in ascending order.

Input Format:

The first line contains two integers n and m, representing the number of rows and columns in the matrix.

The next n lines each contain m space-separated integers representing a row of the matrix.

Output Format:

Print the median of the matrix.

Constraints:
  • 3 ≤ n, m ≤ 100
  • n and m are always odd.
Example :
Input:
3 3
1 3 5
2 6 9
3 6 9
Output:
5

Explanation:
The elements in sorted order: [1,2,3,3,5,6,6,9,9].
The total number of elements is 9, so the median is the element at 5th location, which is 5.

Your last recorded submission was on 2025-03-13, 09:47 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def find_median(matrix, n, m):
3
    elements = []
4
    for row in matrix:
5
        elements.extend(row)
6
    elements.sort()
7
    total_elements = n * m
8
    median_index = total_elements // 2
9
    return elements[median_index]
10
n, m = map(int, input().split())
11
matrix = []
12
for _ in range(n):
13
  row = list(map(int, input().split()))
14
  matrix.append(row)
15
median = find_median(matrix, n, m)
16
print(median, end='')
0
#
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 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
8
8
Passed
Test Case 2
3 3
1 3 4
2 5 6
3 7 9
4
4
Passed
Test Case 3
1 1
7
7
7
Passed



Week 7: Programming Assignment 3

Due on 2025-03-13, 23:59 IST

Rotate a Rectangular Image by 90 Degrees Clockwise

You are given an image represented by an m x n matrix. Your task is to rotate the image by 90 degrees in the clockwise direction. After rotation, the dimensions of the result matrix will be n x m, as the number of rows and columns will be swapped.

[234567]..[642753]

Input Format

Two integers m and n

A matrix of integers of dimension m X n

Output Format

A matrix of integers of dimension n X m

Example :

Input:

4 4

1  2  3  4

5  6  7  8

9  10 11 12

13 14 15 16

 

Output:

13   9   5   1

14  10   6   2

15  11   7   3

16  12   8   4

Your last recorded submission was on 2025-03-13, 09:52 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def rotate_matrix(matrix, m, n):
3
    rotated_matrix = [[0 for _ in range(m)] for _ in range(n)]
4
    for i in range(m):
5
        for j in range(n):
6
            rotated_matrix[j][m - 1 - i] = matrix[i][j]
7
    return rotated_matrix
8
m, n = map(int, input().split())
9
matrix = []
10
for _ in range(m):
11
  row = list(map(int, input().split()))
12
  matrix.append(row)
13
rotated_matrix = rotate_matrix(matrix, m, n)
14
for row in rotated_matrix:
15
  print(*row)
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 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
21 16 11 6 1\n
22 17 12 7 2\n
23 18 13 8 3\n
24 19 14 9 4\n
25 20 15 10 5
21 16 11 6 1\n
22 17 12 7 2\n
23 18 13 8 3\n
24 19 14 9 4\n
25 20 15 10 5\n
Passed after ignoring Presentation Error
Test Case 2
3 3
1 2 3
4 5 6
7 8 9
7 4 1\n
8 5 2\n
9 6 3
7 4 1\n
8 5 2\n
9 6 3\n
Passed after ignoring Presentation Error
Test Case 3
2 3
1 2 3
4 5 6
4 1\n
5 2\n
6 3
4 1\n
5 2\n
6 3\n
Passed after ignoring Presentation Error



Week 8: Programming Assignment 1

Due on 2025-03-20, 23:59 IST

Remove Strings from a List of Tuples

You are given a list of tuples, where each tuple contains a mix of integers and strings. Your task is to remove all string elements from each tuple while keeping the original structure of the list.

·       If a tuple becomes empty after removing strings, it should be represented as an empty tuple ().

·       The number of tuples in the output list must be the same as in the input list.

Input Format:

A list of tuples

Output Format:

A list of tuples with all string elements removed.

 

Example :

Input:

[('string', ‘hello’), (2, 225), (3, '111')]

Output:

[(), (2, 225), (3,)]

Your last recorded submission was on 2025-03-16, 09:11 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
input_list = eval(input())
3
result = []
4
for tup in input_list:
5
  new_tuple = tuple(item for item in tup if not isinstance(item, str))
6
  result.append(new_tuple)
7
print(result, end='')
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
[(1000, 'hello', 2000, 'world', 3000)]
[(1000, 2000, 3000)]
[(1000, 2000, 3000)]
Passed
Test Case 2
[('apple', 1), (3, 'banana'), ('orange', 2)]
[(1,), (3,), (2,)]
[(1,), (3,), (2,)]
Passed
Test Case 3
[(1,'hello'), (4,5), ('ram','sai')]
[(1,), (4, 5), ()]
[(1,), (4, 5), ()]
Passed



Week 8: Programming Assignment 2

Due on 2025-03-20, 23:59 IST

Remove Anagram Duplicates and Sort

You are given a sequence of words. Two words are anagrams if they contain the same letters in a different order. Your task is to remove any word that is an anagram of an earlier word while keeping the first occurrence. Finally, print the remaining words in sorted order.

Input Format:

A single line containing space-separated words, where each word consists of lowercase letters only.

Output Format:

A single line containing the remaining words in sorted order, space-separated.

Example:

Input:

code doce ecod framer frame

Output:

code frame framer

Your last recorded submission was on 2025-03-16, 09:30 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def remove_anagram(words):
3
    seen_anagrams = set()
4
    result = []
5
    for word in words:
6
        sorted_word = "".join(sorted(word))
7
        if sorted_word not in seen_anagrams:
8
            seen_anagrams.add(sorted_word)
9
            result.append(word)
10
    result.sort()
11
    return " ".join(result)
12
input_words = input().split()
13
output_words = remove_anagram(input_words)
14
print(output_words, end='')
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
car race arc care
car race
car race
Passed
Test Case 2
apple elppa banana ananab cherry yrrehc date etad elderberry rrebdrele fig gif grape eparg
apple banana cherry date elderberry fig grape rrebdrele
apple banana cherry date elderberry fig grape rrebdrele
Passed
Test Case 3
listen silent enlist tinsel inlets
listen
listen
Passed



Week 8: Programming Assignment 3

Due on 2025-03-20, 23:59 IST

Multiply Adjacent Elements in a Tuple

In many data processing tasks, cumulative operations like summation or multiplication of adjacent elements are required. This problem focuses on adjacent element multiplication, where each pair of consecutive elements in a given tuple is multiplied to generate a new tuple. The input tuple can contain both positive and negative integers, and the goal is to compute a resultant tuple where each element is the product of two adjacent elements in the original tuple.

 

Input Format:

A tuple of integers T with n elements (n ≥ 2)

Output Format:

A new tuple R of size (n - 1) where each element R[i] is the product of T[i] and T[i+1].

 

Example:

Input:

(1, -5, 7, 8, -10)

Output:

(-5, -35, 56, -80)

Explanation:

·       1 × (-5) = -5

·       (-5) × 7 = -35

·       7 × 8 = 56

·       8 × (-10) = -80

 

Your last recorded submission was on 2025-03-16, 09:32 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def multiply_adjacent_elements(tuple_):
3
  result = []
4
  for i in range(len(tuple_) - 1):
5
    result.append(tuple_[i] * tuple_[i + 1])
6
  return tuple(result)
7
input_tuple = eval(input())
8
output_tuple = multiply_adjacent_elements(input_tuple)
9
print(output_tuple, end='')
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, -5, 7, 8, -10)
(-5, -35, 56, -80)
(-5, -35, 56, -80)
Passed
Test Case 2
(100000, 200000, 300000, 400000)
(20000000000, 60000000000, 120000000000)
(20000000000, 60000000000, 120000000000)
Passed
Test Case 3
(3, 3, 3, 3, 3)
(9, 9, 9, 9)
(9, 9, 9, 9)
Passed



Week 9: Programming Assignment 1

Due on 2025-03-27, 23:59 IST

       We represent scores of batsmen across a sequence of matches in a two-level dictionary as follows:

{'match1’: {'player1':57, 'player2':38}, 'match2’: {'player3':9, 'player1':42}, 'match3’: {'player2':41, 'player4':63, 'player3':91}

Each match is identified by a string, as is each player. The scores are all integers. The names associated with the matches are not fixed (here they are 'match1', 'match2', 'match3'), nor are the names of the players. A player need not have a score recorded in all matches.

Define a Python function orangecap(d) that reads a dictionary d of this form and identifies the player with the highest total score. Your function should return a pair (playername, topscore) where playername is a string, the name of the player with the highest score, and topscore is an integer, the total score of playername.

The input will be such that there are never any ties for highest total score.

For Example :

Input 

{'match1’: {'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}}

Output

('player3', 100)

Input
{'test1':{'Pant':84, 'Kohli':120}, 'test2':{'Pant':59, 'Gill':42}}

Output

('Pant', 143)


Your last recorded submission was on 2025-03-17, 11:45 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def orangecap(d):
3
    total={}
4
    l=[]
5
    for p in d.values():
6
        for ply in p.keys():
7
            if ply not in l:
8
                l=l+[ply]
9
    for n in l:
10
        total[n]=0
11
        for match in d.keys():
12
            if n in d[match].keys():
13
                total[n]=total[n]+d[match][n]
14
    topscore=0
15
    for player in total.keys():
16
            if total[player] > topscore:
17
                playername=player
18
                topscore=total[player]
19
    return (playername,topscore)
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
{'match1': {'player1': 57, 'player2': 38}, 'match2': {'player3': 9, 'player1': 42}, 'match3': {'player2': 41, 'player4': 63, 'player3': 91}}
('player3', 100)
('player3', 100)\n
Passed after ignoring Presentation Error
Test Case 2
{'test1': {'Pant': 84, 'Kohli': 120}, 'test2': {'Pant': 59, 'Gill': 42}}
('Pant', 143)
('Pant', 143)\n
Passed after ignoring Presentation Error
Test Case 3
{'match1': {'player1': 57, 'player2': 38}, 'match2': {'player3': 9, 'player1': 42}, 'match3': {'player2': 41, 'player4': 63, 'player3': 91}, 'match4': {'player2': 31, 'player4': 73, 'player3': 88}}
('player3', 188)
('player3', 188)\n
Passed after ignoring Presentation Error



Week 9: Programming Assignment 2

Due on 2025-03-27, 23:59 IST

    A positive integer m can be expressed as the sum of three squares if it is of the form p + q + r where p, q, r ≥ 0, and p, q, r are all perfect squares. For instance,
2 can be written as 0+1+1 but 7 cannot be expressed as the sum of three squares. The first numbers that cannot be expressed as the sum of three squares are 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, …(see Legendre's three-square theorem).

       Write a Python function threesquares(m) that takes an integer m as a parameter and returns True if m can be expressed as the sum of three squares and False otherwise.   (If m is not positive, your function should return False.)

Your last recorded submission was on 2025-03-17, 11:46 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def threesquares(m):
3
  if(m<0):
4
    return(False)
5
  l=[]
6
  for i in range(0, int(m**0.5)+1):
7
    l+=[i*i]
8
  for i in l:
9
    for j in l:
10
      for k in l:
11
        if(i+j+k==m):
12
          return (True)
13
  return(False)
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
8
True
True\n
Passed after ignoring Presentation Error
Test Case 2
191
False
False\n
Passed after ignoring Presentation Error
Test Case 3
1001
True
True\n
Passed after ignoring Presentation Error



Week 9: Programming Assignment 3

Due on 2025-03-27, 23:59 IST

 A list of numbers is said to be a hill if it consists of an ascending sequence followed by a descending sequence, where each of the sequences is of length at least two. Similarly, a list of numbers is said to be a valley if it consists of an descending sequence followed by an ascending sequence. You can assume that consecutive numbers in the input sequence are always different from each other.

Write a Python function hillvalley(l) that takes a list l of integers and returns True if it is a hill or a valley, and False otherwise.

Here are some examples to show how your function should work.

>>> hillvalley([1,2,3,5,4])
True

>>> hillvalley([1,2,3,4,5])
False

>>> hillvalley([5,4,1,2,3])
True

>>> hillvalley([5,4,3,2,1])
False

Your last recorded submission was on 2025-03-17, 11:47 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def hillvalley(l):
3
  if len(l)<4:
4
    return False
5
  if((l[1]-l[0])>0):
6
    v=1
7
  else:
8
    v=-1
9
  p=0
10
  for i in range(len(l)-1):
11
    if((l[i+1]-l[i])>0):
12
      d=1
13
    else:
14
      d=-1
15
    if (d!=v):
16
      p+=1
17
      v=d
18
  if(p==1):
19
    return (True)
20
  else:
21
    return(False)
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,3,2,1,2,3,5,4,3,2,1]
False
False\n
Passed after ignoring Presentation Error
Test Case 2
[1,2]
False
False\n
Passed after ignoring Presentation Error
Test Case 3
[5,4,3,2,1,0,-3,-2,-1]
True
True\n
Passed after ignoring Presentation Error




Week 10 : Programming Assignment 1

Due on 2025-04-03, 23:59 IST

Emma is a budding software developer working on a signal detection system. One day, her mentor gives her a special list called nums. This list only contains two types of signals:

  • 1 represents a strong signal.
  • 0 represents a weak signal.

Emma's task is to find out the longest streak of consecutive strong signals (1s) in the list, which will help her identify the best time period for stable communication.

Can you help Emma write a Python function that takes the list nums as input and returns the maximum number of consecutive strong signals in the list?


Your last recorded submission was on 2025-03-31, 18:14 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def findMaxConsecutiveOnes(nums):
3
    max_streak = 0
4
    current_streak = 0
5
    for num in nums:
6
        if num == 1:
7
            current_streak += 1
8
            max_streak = max(max_streak, current_streak)
9
        else:
10
            current_streak = 0
11
    return max_streak
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,0,1,1,1]
3
3\n
Passed after ignoring Presentation Error
Test Case 2
[1,0,1,1,0,1]
2
2\n
Passed after ignoring Presentation Error
Test Case 3
[0,0,0,0,0]
0
0\n
Passed after ignoring Presentation Error




Week 10: Programming Assignment 2

Due on 2025-04-03, 23:59 IST

In the ancient land of Lexiconia, hidden messages are locked behind a common magical prefix. Only those who can decipher the longest common prefix will unveil the wisdom of the lost texts.

Your task is to create a function that accepts a list of words, finds the longest common prefix, and prints the result.

Write a function longest_common_prefix(st) that:

  • Accepts a list of strings as input.
  • Returns the longest common prefix among all the strings.
  • If there is no common prefix, return an empty string "".
Your last recorded submission was on 2025-04-01, 14:32 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def longestCommonPrefix(st):
3
    if not st:
4
        return ""
5
    prefix = st[0]
6
    for i in range(1, len(st)):
7
        while not st[i].startswith(prefix):
8
            prefix = prefix[:-1]
9
            if not prefix:
10
                return "\"\""
11
    return (f"\"{prefix}\"")
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", "apricot", "apartment"]
"ap"
"ap"\n
Passed after ignoring Presentation Error
Test Case 2
["banana", "band", "banner"]
"ban"
"ban"\n
Passed after ignoring Presentation Error
Test Case 3
["home", "honey", "horse"]
"ho"
"ho"\n
Passed after ignoring Presentation Error




Week 10: Programming Assignment 3

Due on 2025-04-03, 23:59 IST

Once upon a time in the land of Isomorphia, there were two ancient scrolls written in different languages. The wise king of Isomorphia wanted to know if these scrolls conveyed the same message or if they were completely different. He summoned his greatest coder, who had the magical power of Isomorphic Translation.

The challenge was to determine if every symbol in the first scroll could be mapped to exactly one symbol in the second scroll, without any two symbols sharing the same mapping. However, the order of symbols in both scrolls had to be preserved.

The coder had to write a function to check if the two scrolls were isomorphic or not.

Examples:

Input : s = "egg" , t = "add"

Output : true

Explanation : The 'e' in string s can be replaced with 'a' of string t.

The 'g' in string s can be replaced with 'd' of t.

Hence all characters in s can be replaced to get t.

Input : s = "apple" , t = "bbnbm"

Output : false

Explanation : One possible try can be:

The 'a' in string s can be replaced with 'n' of string t.

The 'p' in string s can be replaced by 'b' of string t.

The 'l' in string s can be replaced by 'm' of string t.

The 'e' in string s cannot be replaced by any character of string t as all the characters in string t are already mapped.

Hence all characters in s cannot be replaced to get t by this try. It can be proved that no solution exists for this example.


Your last recorded submission was on 2025-03-31, 18:18 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def isIsomorphic(s,t):
3
    if len(s) != len(t):
4
        return False
5
    s_to_t = {}
6
    t_to_s = {}
7
    for i in range(len(s)):
8
        char_s = s[i]
9
        char_t = t[i]
10
        if char_s in s_to_t:
11
            if s_to_t[char_s] != char_t:
12
                return False
13
        else:
14
            if char_t in t_to_s:
15
                return False
16
            else:
17
                s_to_t[char_s] = char_t
18
                t_to_s[char_t] = char_s
19
    return True
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
"egg"
"add"
True
True\n
Passed after ignoring Presentation Error
Test Case 2
"apple"
"bbnbm"
False
False\n
Passed after ignoring Presentation Error
Test Case 3
"paper"
"title"
True
True\n
Passed after ignoring Presentation Error



Week 11 : Programming Assignment 1

Due on 2025-04-10, 23:59 IST
You are given a list of n distinct integers in the range [0, n] (inclusive). This means exactly one number from this range is missing. Your task is to determine and return the missing number.
Input: A list nums containing n distinct integers from 0 to n.
Output: The missing integer in the range [0, n]
Examples:
Input: nums = [0, 2, 3, 1, 4]
Output: 5
The list contains {0, 1, 2, 3, 4}, so 5 is missing.

Input:
 nums = [0, 1, 2, 4, 5, 6]
Output: 3
The list contains {0, 1, 2, 4, 5, 6}, so 3 is missing.




Your last recorded submission was on 2025-04-10, 09:48 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def missingNumber(nums):
3
    n = len(nums)
4
    expected_sum = n * (n + 1) // 2
5
    actual_sum = sum(nums)
6
    return expected_sum - actual_sum
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
[0, 2, 3, 1, 4]
5
5\n
Passed after ignoring Presentation Error
Test Case 2
[0, 1, 2, 4, 5, 6]
3
3\n
Passed after ignoring Presentation Error
Test Case 3
[1, 3, 6, 4, 2, 5]
0
0\n
Passed after ignoring Presentation Error



Week 11 : Programming Assignment 2

Due on 2025-04-10, 23:59 IST
Given a list of integers nums and an integer k, write a Python function to check whether there exists a subsequence (a non-contiguous subset of the list) such that the sum of its elements equals k. Return True if such a subsequence exists, otherwise False.
Example 1 :
Input:
nums = [1, 2, 3, 4, 5]
k = 8
Output:
True

Example 2 : 
Input:
nums = [4, 3, 9, 2]
k = 10

Output:
False

Constraints :

  • 1<=len(nums)<=20
  • 1<=nums[i]<=100
  • 1<=k<=2000


  • Your last recorded submission was on 2025-04-10, 09:51 IST
    Select the Language for this assignment. 
    1
    ~~~THERE IS SOME INVISIBLE CODE HERE~~~
    2
    def checkSubsequenceSum(nums, k):
    3
        n = len(nums)
    4
        dp = [False] * (k + 1)
    5
        dp[0] = True
    6
        for num in nums:
    7
            for i in range(k, num - 1, -1):
    8
                dp[i] = dp[i] or dp[i - num]
    9
        return dp[k]
    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, 4, 5]
    8
    True
    True\n
    
    Passed after ignoring Presentation Error
    Test Case 2
    [4, 3, 9, 2]
    10
    False
    False\n
    
    Passed after ignoring Presentation Error
    Test Case 3
    [1, 10, 4, 5]
    16
    True
    True\n
    
    Passed after ignoring Presentation Error



    Week 11 : Programming Assignment 3

    Due on 2025-04-10, 23:59 IST

    Given a string s representing a large integer, return the largest-valued odd integer (as a string) that is a substring of s.

    Note:

    • The result should not contain any leading zeros.
    • The input string s may contain leading zeros.
    Constraints :
    1 <= s.length <= 10^3
    '0' <= s[i] <= '9'

    Examples:

    Example 1:
    Input:
    s = "5347"

    Output:
    "5347"

    Explanation:

    All possible odd numbers: {5, 3, 53, 347, 5347}
    The largest odd number is "5347".


    Example 2:
    Input:
    s = "0214638"

    Output:
    "21463"

    Explanation:
    Valid odd numbers: {1, 3, 21, 63, 463, 1463, 21463}
    Leading zeros cannot be included.
    The largest odd number is "21463".


    Example 3:
    Input:
    s = "0032579"

    Output:
    "32579"



    Your last recorded submission was on 2025-04-10, 09:58 IST
    Select the Language for this assignment. 
    1
    ~~~THERE IS SOME INVISIBLE CODE HERE~~~
    2
    def largeOddNum(s):
    3
        for i in range(len(s) - 1, -1, -1):
    4
            if int(s[i]) % 2 != 0:
    5
                first_non_zero = 0
    6
                while first_non_zero < len(s[:i+1]) - 1 and s[first_non_zero] == '0':
    7
                    first_non_zero += 1
    8
                return (f"\"{s[first_non_zero:i+1]}\"")
    9
        return "\"\""
    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
    "987654321"
    "987654321"
    "987654321"\n
    
    Passed after ignoring Presentation Error
    Test Case 2
    "2468024680"
    ""
    ""\n
    
    Passed after ignoring Presentation Error
    Test Case 3
    "0012345"
    "12345"
    "12345"\n
    
    Passed after ignoring Presentation Error




    Week 12 : Programming Assignment 1

    Due on 2025-04-17, 23:59 IST
    Given a sorted list nums in non-decreasing order, where every element appears twice except for one unique element, find and return the single number.
    Example 1:
    Input
    nums = [1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6]
    Output
    4
    Example 2 :
    Input 
    nums = [1, 1, 3, 5, 5]
    Output
    3

    Constraints:
    n==len(nums)
    1<=n<=10
    4


     

     

    Your last recorded submission was on 2025-04-11, 15:26 IST
    Select the Language for this assignment. 
    1
    ~~~THERE IS SOME INVISIBLE CODE HERE~~~
    2
    def singleNonDuplicate(nums):
    3
        left, right = 0, len(nums) - 1
    4
        while left < right:
    5
            mid = (left + right) // 2
    6
            if mid % 2 == 1:
    7
                mid -= 1  # Ensure mid is always even
    8
            if nums[mid] != nums[mid + 1]:
    9
                right = mid
    10
            else:
    11
                left = mid + 2
    12
        return nums[left]
    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, 2, 2, 3, 3, 4, 5, 5, 6, 6]
    4
    4\n
    
    Passed after ignoring Presentation Error
    Test Case 2
    [1, 1, 3, 5, 5]
    3
    3\n
    
    Passed after ignoring Presentation Error
    Test Case 3
    [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7]
    7
    7\n
    
    Passed after ignoring Presentation Error




    Week 12 : Programming Assignment 2

    Due on 2025-04-17, 23:59 IST

    You are given a string s consisting of only lowercase English letters. Your task is to return a list of unique characters from the string, sorted in descending order based on their frequency of occurrence.

    • If two or more characters have the same frequency, they should be sorted in alphabetical order.

    Input:

    • A string s
    • The string consists of only lowercase English characters ('a' to 'z').

    Output:

    • A list of unique characters sorted by frequency (highest to lowest).
    • If characters have the same frequency, they should appear in alphabetical order.

    Example 1:

    Input
    : s = "tree"

    Output: ['e', 'r', 't']

    Example 2 :

    Input: s = "bbccddaaa"

    Output: ['a', 'b', 'c', 'd']

    Explanation:

    The frequency of each character:

    • 'a' → 3
    • 'b' → 2
    • 'c' → 2
    • 'd' → 2

    Since 'b''c', and 'd' have the same frequency, they are sorted alphabetically.








    Your last recorded submission was on 2025-04-11, 15:35 IST
    Select the Language for this assignment. 
    1
    ~~~THERE IS SOME INVISIBLE CODE HERE~~~
    2
    def frequencySort(s):
    3
        char_freq = {}
    4
        for char in s:
    5
            char_freq[char] = char_freq.get(char, 0) + 1
    6
        unique_chars = sorted(char_freq.keys(), key=lambda char:(-char_freq[char], char))
    7
        return unique_chars
    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
    tree
    ['e', 'r', 't']
    ['e', 'r', 't']\n
    
    Passed after ignoring Presentation Error
    Test Case 2
    raaaajj
    ['a', 'j', 'r']
    ['a', 'j', 'r']\n
    
    Passed after ignoring Presentation Error
    Test Case 3
    bbccddaaa
    ['a', 'b', 'c', 'd']
    ['a', 'b', 'c', 'd']\n
    
    Passed after ignoring Presentation Error




    Week 12 : Programming Assignment 3

    Due on 2025-04-17, 23:59 IST

    Write a Python function subsequence(l1,l2) that takes two sorted lists as arguments and returns True if the the first list is a subsequence of the second list, and returns False otherwise.

    A subsequence of a list is obtained by dropping some values. Thus, [2,3,4] and [2,2,5] are subsequences of [2,2,3,4,5], but [2,4,4] and [2,4,3] are not.

    For Example : 

    Input :


    subsequence([2,2,5],[2,2,3,4,5])

    Output :

    True 



    Your last recorded submission was on 2025-04-11, 15:37 IST
    Select the Language for this assignment. 
    1
    ~~~THERE IS SOME INVISIBLE CODE HERE~~~
    2
    def subsequence(l1, l2):
    3
        i = 0 
    4
        j = 0
    5
        while i < len(l1) and j < len(l2):
    6
            if l1[i] == l2[j]:
    7
                i += 1
    8
            j += 1
    9
        return i == len(l1)
    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
    subsequence([2,2,5],[2,2,3,4,5])
    True
    True\n
    
    Passed after ignoring Presentation Error
    Test Case 2
    subsequence([2,3,4],[2,2,3,4,5])
    True
    True\n
    
    Passed after ignoring Presentation Error
    Test Case 3
    subsequence([2,4,4],[2,2,3,4,5])
    False
    False\n
    
    Passed after ignoring Presentation Error











    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.