NPTEL » The Joy of Computing using Python
Please scroll down for latest Programs 👇
Week 2: Programming Assignment 1
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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}
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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’
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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:
For normal magic squares of order n = 3,4,5,...,
the magic constants are: 15,34,65,111,175,260,...
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
- 3 ≤ n, m ≤ 100
- n and m are always odd.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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,)]
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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)
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.)
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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?
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
""
.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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:The list contains {0, 1, 2, 3, 4}, so 5 is missing.
Input: nums = [0, 1, 2, 4, 5, 6]
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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 :
Example 2 :
Constraints :
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
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"
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
nums
in non-decreasing order, where every element appears twice except for one unique element, find and return the single number.Example 1:
Input
Example 2 :
Input
Output
3
Constraints:
n==len(nums)
1<=n<=104
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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.