NPTEL » The Joy of Computing using Python
Please scroll down for latest Programs 👇
Programming Assignment 1
The program should prompt the user to input a number, then compute and print the absolute value of that number.
Input Format-
The input consists of single number.
Output Format-
The output consists of absolute value of the input.
Example-
Input:
-6
Output:
6
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 | 1 | 1 | Passed |
Test Case 2 | -1 | 1 | 1 | Passed |
Test Case 3 | -6 | 6 | 6 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Programming Assignment 2
Input Format:
The input consists of a single integer n.
Output Format:
The output consists of the nth element of the arithmetic sequence.
Example:
Input:
3
Output:
29
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 | 29 | 29 | Passed |
Test Case 2 | 1 | 7 | 7 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Programming Assignment 3
Question:
Create a Python program to compute the sum of the first N natural numbers {1,2,3...N}. The program should prompt the user to input the value of N, then compute and print the sum of the first N natural numbers.
Input Format:
The input consists of a single integer .
Output Format:
The output consists of the sum of the first natural numbers.
Example:
Input-
5
Output-
15
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 5 | 15 | 15 | Passed |
Test Case 2 | 4 | 10 | 10 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Programming Assignment 1
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 largest number in the input list.
Example:
Input:
3 1 4 1 5 9 2 6 5 3 5
Output:
6
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 1 4 1 5 9 2 6 5 3 5 | 6 | 6 | Passed |
Test Case 2 | 1 1 0 | 0 | 0 | Passed |
Test Case 3 | 11 12 10 33 | 12 | 12 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Programming Assignment 2
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 5 9 2 6
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 5 9 2 6 | 3 1 4 5 9 2 6 | Passed |
Test Case 2 | 2 3 4 2 5 6 1 | 2 3 4 5 6 1 | 2 3 4 5 6 1 | Passed |
Test Case 3 | 2 3 45 55 55 2 | 2 3 45 55 | 2 3 45 55 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Programming Assignment 3
Create a Python program that takes a list of integers, reverses the list, adds the values at odd 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 odd indices from both the original and reversed lists.
Example:
Input:
1 2 3 4 5
Output:
1 6 3 6 5
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 2 3 4 5 | 1 6 3 6 5 | 1 6 3 6 5 | Passed |
Test Case 2 | 2 3 37 43 21 | 2 46 37 46 21 | 2 46 37 46 21 | Passed |
Test Case 3 | 1 0 | 1 1 | 1 1 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 4: Programming Assignment 1
1
; otherwise, it should print 0
.Input Format:
The first input is an integer r , the number of rows in the matrix.
The second input is an integer c, the number of columns in the matrix.
The next r lines each contain c integers, representing the elements of each row of the matrix.
Output Format:
The output is 1
if a saddle point exists, otherwise 0
.
Example:
Input:
3
3
2 0 3
2 1 4
4 2 6
Output:
1
Reason:
Element 2 in third row and second column is minimum in its row and maximum in its column.
So, it is a saddle point.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3
3
2 2 3
2 1 4
4 0 6 | 0 | 0 | Passed |
Test Case 2 | 3
3
2 0 3
2 1 4
4 2 6 | 1 | 1 | Passed |
Test Case 3 | 4
4
7 3 5 8
4 2 9 1
2 2 10 11
14 1 7 3 | 1 | 1 | Passed |
Week 4: Programming Assignment 2
Create a Python program that multiplies 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, multiply 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 multiplying the transpose of the original matrix by the scalar.
Example:
Input:
2
3
1 2 3
4 5 6
2
Output:
2 8
4 10
6 12
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2
3
1 2 3
4 5 6
2 | 2 8\n
4 10\n
6 12 | 2 8\n
4 10\n
6 12 | Passed |
Test Case 2 | 3
2
1 2
3 4
5 6
3 | 3 9 15\n
6 12 18 | 3 9 15\n
6 12 18 | Passed |
Test Case 3 | 2
2
-1 0
2 -3
4 | -4 8\n
0 -12 | -4 8\n
0 -12 | Passed |
Week 4: Programming Assignment 3
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 skew-symmetric, otherwise 0
.
Example:
Input:
3
0 2 -1
-2 0 -4
1 4 0
Output:
1
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3
0 2 -1
-2 0 -4
1 4 0 | 1 | 1 | Passed |
Test Case 2 | 3
0 2 -1
-2 0 4
1 4 0 | 0 | 0 | Passed |
Test Case 3 | 4
0 2 -3 4
-2 0 5 -6
3 -5 0 7
-4 6 -7 0 | 1 | 1 | Passed |
Week 5: Programming Assignment 1
Create a Python program that performs a binary search on a sorted list of integers using only loops. The program should prompt the user to input a sorted list of integers and a target number to search for. The program should then search for the target number in the list using the binary search algorithm and print the index of the target if found. If the target is not found, the program should print -1
.
Input Format:
- The first line of input consists of a space-separated sorted list of integers.
- The second line of input consists of a single integer, representing the target number.
Output Format:
- The output consists of the index of the target number in the list if found. If the target number is not found, the output should be
-1.
Example:
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 10 20 30 40 50
30 | 2 | 2 | Passed |
Test Case 2 | 1 2 3 4 5 6 7 8 9
5 | 4 | 4 | Passed |
Test Case 3 | 2 3 5 6 8 9 10 12 13
14 | -1 | -1 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 5: Programming Assignment 2
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 10 20 30 40 50
5 70 90 100 | 5 10 20 30 40 50 70 90 100 | 5 10 20 30 40 50 70 90 100 | Passed |
Test Case 2 | 1 3 7 9
2 4 6 8 | 1 2 3 4 6 7 8 9 | 1 2 3 4 6 7 8 9 | Passed |
Test Case 3 | 1 3 5 6 8 9 10 12 17
1 7 20 100 | 1 1 3 5 6 7 8 9 10 12 17 20 100 | 1 1 3 5 6 7 8 9 10 12 17 20 100 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 5: Programming Assignment 3
- 1 if the k-th largest and k-th smallest elements are the same and are at the middle of the sorted list.
- -1 if the k-th largest and k-th smallest elements are the same but are not in the middle of the sorted list.
- 0 if the k-th largest and k-th smallest elements are different.
- The first line of input consists of a space-separated list of integers.
- The second line of input consists of a single integer k.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 8 7 5 9 1
2 | 0 | 0 | Passed |
Test Case 2 | 2 1 1 3
2 | 0 | 0 | Passed |
Test Case 3 | 700
1 | 1 | 1 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 6: Programming Assignment 1
Question:
Write a Python program that takes two positive integers a
and b
as input and returns their product using a recursive function. The function should only use the +
and -
operators to calculate the product.
Input Format:
The first line of input consists of two space-separated positive integers, a
and b
.
Output Format:
The output consists of a single integer representing the product of a
and b
.
Example:
Input:
3 4
Output:
12
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 1 | 1 | 1 | Passed |
Test Case 2 | 3 4 | 12 | 12 | Passed |
Test Case 3 | 999 1 | 999 | 999 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Week 6: Programming Assignment 2
Question:
Write a Python program that takes a positive integer x
as input and returns the logarithm of x
to the base 2 using a recursive function. The logarithm of x
to the base 2, denoted by log₂(x)
, is the number of times 2
has to be multiplied by itself to get x
. Assume that x
is a power of 2.
Input Format:
The input consists of a single positive integer x
which is a power of 2.
Output Format:
The output consists of a single integer representing log₂(x)
.
Example:
Input:
8
Output:
3
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2 | 1 | 1 | Passed |
Test Case 2 | 4 | 2 | 2 | Passed |
Test Case 3 | 16 | 4 | 4 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 6: Programming Assignment 3
Question
Write a Python program that includes a recursive function named non_decreasing
which takes a non-empty list L
of integers as input. The function should return True
if the elements in the list are sorted in non-decreasing order from left to right, and False
otherwise.
Input Format:
The input consists of a single line containing space-separated integers that form the list L
.
Output Format:
The output consists of a single boolean value (True
or False
) indicating whether the list is sorted in non-decreasing order.
Example:
Input:
1 2 2 3 4
Output:
True
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 2 2 3 4 | True | True | Passed |
Test Case 2 | 3 2 1 | False | False | Passed |
Test Case 3 | 11 22 45 89 100 483 | True | True | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 7: Programming Assignment 1
Question
You are given a board game scenario with ladders and snakes. A player starts at a given position on the board, and you are provided with the result of a die roll. You need to determine whether the player lands on a ladder, a snake, or a normal block after moving.
Write a Python function named move_player
that takes four inputs:
- A list
ladders
containing the indices of blocks with ladders. - A list
snakes
containing the indices of blocks with snakes. - An integer
current_position
representing the player's current position on the board. - An integer
die_roll
representing the result of a die roll.
The function should return:
1
if the player lands on a block with a ladder,-1
if the player lands on a block with a snake,0
if the player lands on a block with neither.
Input Format
The input consists of four lines:
- The first line contains space-separated integers representing the ladder indices.
- The second line contains space-separated integers representing the snake indices.
- The third line contains a single integer representing the current position of the player.
- The fourth line contains a single integer representing the die roll result.
Output Format
The output consists of a single integer (1
, -1
, or 0
) as per the conditions mentioned above.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 8 12
6 14 18
5
3 | 1 | 1 | Passed |
Test Case 2 | 4 8 12 11 67 43
2 15 18 19 56 9
10
6 | 0 | 0 | Passed |
Test Case 3 | 2 6 14 19 26 67 5
7 12 17 98 3 76 4 18
3
4 | -1 | -1 | Passed |
Week 7: Programming Assignment 2
Question
Write a Python program that includes a recursive function named is_palindrome
which takes a non-empty string s
as input. The function should return 1 if the string is a palindrome (reads the same backward as forward), and 0 otherwise.
Take necessary inputs, pass the inputs to the function, and print the return value of the function.
Input Format:
The input consists of a single line containing the string s
.
Output Format:
The output consists of a single integer value (1 or 0) indicating whether the string is a palindrome.
Example:
Input:
racecar
Output
1
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | racecar | 1 | 1 | Passed |
Test Case 2 | level | 1 | 1 | Passed |
Test Case 3 | dinosaur | 0 | 0 | Passed |
Week 7: Programming Assignment 3
Question
Write a Python program that takes a matrix of size r x c
as input, where r
is the number of rows and c
is the number of columns. The program should check if the matrix is binary (i.e., all elements are either 0 or 1) and if it is symmetric (i.e., the matrix is equal to its transpose).
The program should output:
11
if the matrix is both binary and symmetric,10
if the matrix is binary but not symmetric,01
if the matrix is not binary but symmetric,00
if the matrix is neither binary nor symmetric.
Input Format:
- The first line contains an integer
r
representing the number of rows. - The second line contains an integer
c
representing the number of columns. - The next
r
lines each containc
space-separated integers representing the elements of the matrix.
Output Format:
- The output consists of a single string (either
11
,10
,01
, or00
) as per the conditions mentioned above.
Example:
Input:
3
3
1 0 1
0 1 0
1 0 1
Output:
11
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3
3
1 0 1
0 1 0
1 0 1 | 11 | 11 | Passed |
Test Case 2 | 3
3
1 0 2
0 1 0
2 0 1 | 01 | 01 | Passed |
Test Case 3 | 3
3
1 0 1
1 1 0
1 0 1 | 10 | 10 | Passed |
Test Case 4 | 3
3
1 0 2
3 1 0
2 0 1 | 00 | 00 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Test Case 5 | Passed |
Test Case 6 | Passed |
Week 8: Programming Assignment 1
Write a function that takes an integer n
followed by n
lines of space-separated integers, where each line represents a tuple. The function should return the sum of the first elements of all tuples.
Input Format:
- The first line contains a single integer
n
, which represents the number of tuples. - The next
n
lines each contain two space-separated integers representing the values of the tuple.
Output Format:
- A single integer representing the sum of the first elements of all tuples.
Example:
Input:
3
3 4
1 2
5 6
Output:
9
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2
1 10
2 20 | 3 | 3 | Passed |
Test Case 2 | 4
5 12
7 8
1 5
3 9 | 16 | 16 | Passed |
Test Case 3 | 1
9 10 | 9 | 9 | Passed |
Week 8: Programming Assignment 2
Question:
Write a function that checks if two given strings are anagrams of each other considering only alphabetic characters and ignoring case. Additionally, the function should handle cases where the strings may contain spaces or punctuation, and should return the result as 1
or 0
.
Input Format:
- The first line contains a string
str1
which may include alphabetic characters, spaces, and punctuation. - The second line contains a string
str2
which may also include alphabetic characters, spaces, and punctuation.
Output Format:
- Print 1 if the two strings are anagrams of each other, considering only alphabetic characters and ignoring case, otherwise print
0
.
Example:
Input:
Astronomer
Moon starer
Output:
1
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | The Morse Code
Here come dots | 1 | 1 | Passed |
Test Case 2 | A gentleman
Elegant man | 1 | 1 | Passed |
Test Case 3 | Claremont
Montreal | 0 | 0 | Passed |
Week 8: Programming Assignment 3
Question:
Write a function that takes a list of integers and an integer k
. The function should multiply each integer in the list by k
and then return the maximum value from the resulting list.
Input Format:
- The first line contains space-separated integers representing the list of numbers.
- The second line contains a single integer
k
, the multiplier.
Output Format:
- Print a single integer representing the maximum value after multiplying each number by
k
.
Example:
Input:
1 5 3 9
2
Output:
18
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 5 3 9
2 | 18 | 18 | Passed |
Test Case 2 | 4 7 2 8
3 | 24 | 24 | Passed |
Test Case 3 | 8 15 4 10
6 | 90 | 90 | Passed |
Week 9: Programming Assignment 1
Write a function that takes a string and counts the number of vowels in it. The function should return the total number of vowels.
Input Format:
The input consists of a single string. The string can contain both uppercase and lowercase letters, as well as spaces and punctuation marks.
Output Format:
A single integer representing the total number of vowels in the string.
Example:
Input:
Hello, World!
Output:
3
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | Hello, World | 3 | 3 | Passed |
Test Case 2 | Python Programming | 4 | 4 | Passed |
Test Case 3 | AEIOU | 5 | 5 | Passed |
Test Case 4 | The quick brown fox jumps over the lazy dog | 11 | 11 | Passed |
Week 9: Programming Assignment 2
Write a function that takes a string and determines if the average length of the words in the string is greater than 4. If the average word length is greater than 4, the function should return 1
; otherwise, it should return 0
.
Input Format:
The input consists of a single string. The string contains words separated by spaces.
Output Format:
A single integer: 1
if the average word length is greater than 4, 0
otherwise.
Example:
Input:
The quick brown fox jumps over the lazy dog
0
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | The quick brown fox jumps over the lazy dog | 0 | 0 | Passed |
Test Case 2 | Hello world | 1 | 1 | Passed |
Test Case 3 | Average word length calculation | 1 | 1 | Passed |
Test Case 4 | Data science is fun0 | 1 | 1 | Passed |
Week 9: Programming Assignment 3
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 4 | 6 | 6 | Passed |
Test Case 2 | 5 | 10 | 10 | Passed |
Test Case 3 | 6 | 15 | 15 | Passed |
Test Case 4 | 9 | 36 | 36 | Passed |
Week 10: Programming Assignment 1
Question:
Write a program that accepts the string `para` and a positive integer `n` as input. The program should print `1` if there is at least one word in `para` that occurs exactly `n` times, and `0` otherwise.
Input Format:
- The input consists of two lines.
- The first line contains the string `para`, which is a sequence of space-separated words.
- The second line contains a positive integer `n`.
Output Format:
- A single integer, either `1` or `0`.
- Print `1` if there is at least one word that occurs exactly `n` times.
- Print `0` otherwise.
Example:
Input:
apple orange apple banana orange banana banana
2
Output:
1
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | apple orange apple banana orange banana banana
2 | 1 | 1 | Passed |
Test Case 2 | cat dog elephant cat dog elephant lion
3 | 0 | 0 | Passed |
Test Case 3 | test test test example example
2 | 1 | 1 | Passed |
Test Case 4 | sun moon stars sun sun
1 | 1 | 1 | Passed |
Week 10: Programming Assignment 2
Question:
Write a program that accepts a string of space-separated float numbers. The program should print the number of long tail numbers, where a float number is said to have a long tail if the number of digits after the decimal point is greater than the number of digits before the decimal point.
Input Format:
- The input consists of a single line containing space-separated float numbers.
Output Format:
- A single integer representing the number of long tail numbers.
Example:
Input:
12.3214 123.56 3.14159 100.1 45.6789
Output:
3
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 12.3214 123.56 3.14159 100.1 45.6789 | 3 | 3 | Passed |
Test Case 2 | 12345.678 1234.5 12.34 1.234 0.1234 | 2 | 2 | Passed |
Test Case 3 | 0.0 0.0 | 0 | 0 | Passed |
Week 10: Programming Assignment 3
Question:
Write a program that accepts a space-separated string of integers representing a sequence of even length. The program should determine whether the sequence is left-heavy, right-heavy, or balanced. It should print:
-1
if the sequence is left-heavy (left half sum > right half sum).- 1 if the sequence is right-heavy (right half sum > left half sum).
0
if the sequence is balanced (both sums are equal).
Input Format:
- The input consists of a single line containing space-separated integers.
Output Format:
- A single integer: -
1
for left-heavy, 1 for right-heavy, or 0
for balanced.
1
for left-heavy, 1 for right-heavy, or 0
for balanced.Example:
Input:
1 2 3 4 5 6
Output:
1
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 2 3 4 5 | 1 | 1 | Passed |
Test Case 2 | 1 2 2 2 | 1 | 1 | Passed |
Test Case 3 | 10 5 1 1 2 2 | -1 | -1 | Passed |
Test Case 4 | 2 4 6 8 10 1 3 5 7 9 | -1 | -1 | Passed |
Week 11: Programming Assignment 1
Question:
Write a program that accepts a date in MM/DD/YYYY
format as input and prints the date in DD-MM-YY
format. The program should retain only the last two digits of the year, replace the forward slash (/
) with a dash (-
), and swap the order of the month and date.
Input Format:
The input consists of a single line.
- The line contains a date string in
MM/DD/YYYY
format.
- The line contains a date string in
Output Format:
A single string in
DD-MM-YY
format.- Print the date with the day and month swapped, the year truncated to the last two digits, and all separators replaced by dashes (
-
).
- Print the date with the day and month swapped, the year truncated to the last two digits, and all separators replaced by dashes (
Example:
Input:
12/25/2024
Output:
25-12-24
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 12/25/2024 | 25-12-24 | 25-12-24 | Passed |
Test Case 2 | 01/05/2001 | 05-01-01 | 05-01-01 | Passed |
Test Case 3 | 10/15/1999 | 15-10-99 | 15-10-99 | Passed |
Test Case 4 | 07/04/1776 | 04-07-76 | 04-07-76 | Passed |
Week 11: Programming Assignment 2
Question:
Write a program that accepts a sequence of numbers separated by colons as input, and prints the common factors of all the numbers in ascending order.
Input Format:
The input consists of a single line.
The line contains a sequence of integers separated by colons.
Output Format:
Print the common factors of all the numbers, separated by spaces, in ascending order.
Example:
Input:
12:18:24
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 12:18:24 | 1 2 3 6 | 1 2 3 6 | Passed |
Test Case 2 | 20:30:50 | 1 2 5 10 | 1 2 5 10 | Passed |
Test Case 3 | 15:25:35 | 1 5 | 1 5 | Passed |
Test Case 4 | 8:12:16 | 1 2 4 | 1 2 4 | Passed |
Week 11: Programming Assignment 3
Question:
You are given a specific mapping between random lowercase alphabet letters and digits. Your task is to decode a date provided in the format `DD-MM-YYYY`, where each digit is represented by a corresponding letter, and convert it back to its standard decimal format.
The mapping is as follows:
a → 0
k → 1
x → 2
y → 3
s → 4
m → 5
b → 6
d → 7
p → 8
z → 9
Input Format:
- A single line containing a date in the format `DD-MM-YYYY`, where each digit is encoded using the given letter-to-number mapping.
Output Format:
- A single line containing the decoded date in standard `DD-MM-YYYY` format.
Example:
Input:
mk-ya-kzma
Output:
15-03-1750
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | km-ay-kdma | 15-03-1750 | 15-03-1750 | Passed |
Test Case 2 | xx-ad-kdzz | 22-07-1799 | 22-07-1799 | Passed |
Test Case 3 | ap-kx-kpkx | 08-12-1812 | 08-12-1812 | Passed |
Test Case 4 | ak-ak-kpma | 01-01-1850 | 01-01-1850 | Passed |
Week 12: Programming Assignment 1
Question:
Write a recursive function named collatz
that accepts a positive integer n
as an argument, where 1 < n ≤ 32,000
, and returns the number of times the Collatz function has to be applied repeatedly in order to first reach 1.
The Collatz function is defined as follows:
- If
n
is even, dividen
by 2. - If
n
is odd, multiplyn
by 3 and add 1.
The function should continue applying this process until n
becomes 1, and return the number of steps taken.
Input Format:
A single integer n
where 1 < n ≤ 32,000
.
Output Format:
A single integer representing the number of steps required for n
to reach 1.
Example:
Input:
12
9
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 12 | 9 | 9 | Passed |
Test Case 2 | 27 | 111 | 111 | Passed |
Test Case 3 | 19 | 20 | 20 | Passed |
Test Case 4 | 1000 | 111 | 111 | Passed |
Week 12: Programming Assignment 2
Question:
There are `n` people who start in a happy state. The following rules determine how people move between states after each iteration:
- A person in the happy state has a 70% chance of moving to a sad state, and a 30% chance of staying in the happy state.
- A person in the sad state has a 50% chance of moving to a happy state, and a 50% chance of staying in the sad state.
Write a program that computes the number of people in the happy and sad states after 3 iterations.
Input Format:
A single integer `n` representing the number of people who start in the happy state.
Output Format:
Two integers separated by a space, where the first integer is the number of people in the happy state and the second is the number of people in the sad state after the 3rd iteration.
Example:
Input:
1000
Output:
412 588
Explanation:
State 0: 1000 0
State 1: 300 700
State 2: 440 560
State 3: 412 588
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1000 | 412 588 | 412 588 | Passed |
Test Case 2 | 2000 | 824 1176 | 824 1176 | Passed |
Test Case 3 | 3000 | 1236 1764 | 1236 1764 | Passed |
Week 12: Programming Assignment 3
Problem Statement:
Write a program that accepts a positive integer n
and prints the sum of all prime numbers in the range [1, n]
, including both endpoints. If there are no prime numbers in the given range, the program should print 0.
Input Format:
- A single integer
n
, representing the upper limit of the range.
n
, representing the upper limit of the range.Output Format:
- A single integer representing the sum of all prime numbers in the range
[1, n]
. If no prime numbers are found, output 0
.
[1, n]
. If no prime numbers are found, output 0
.Example:
Input:
10
Output:
17
Explanation:
The prime numbers between 1 and 10 are 2, 3, 5, and 7. Their sum is 2 + 3 + 5 + 7 = 17.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 6 | 10 | 10 | Passed |
Test Case 2 | 12 | 28 | 28 | Passed |
Test Case 3 | 13 | 41 | 41 | Passed |
Test Case 4 | 14 | 41 | 41 | Passed |
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.