NPTEL » Programming, Data Structures and Algorithms using Python
Please scroll down for latest Programs 👇
Week 2 Programming Assignment
- You may define additional auxiliary functions as needed.
- In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
- For each function, there are normally some public test cases and some (hidden) private test cases.
- "Compile and run" will evaluate your submission against the public test cases.
- "Submit" will evaluate your submission against the hidden private test cases. There are 12 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
- Ignore warnings about "Presentation errors".
A positive integer m is a prime product if it can be written as p×q, where p and q are both primes. .
Write a Python function primeproduct(m) that takes an integer m as input and returns True if m is a prime product and False otherwise. (If m is not positive, your function should return False.)
Here are some examples of how your function should work.
>>> primeproduct(6) True >>> primeproduct(188) False >>> primeproduct(202) True
Write a function delchar(s,c) that takes as input strings s and c, where c has length 1 (i.e., a single character), and returns the string obtained by deleting all occurrences of c in s. If c has length other than 1, the function should return s
Here are some examples to show how your function should work.
>>> delchar("banana","b") 'anana' >>> delchar("banana","a") 'bnn' >>> delchar("banana","n") 'baaa' >>> delchar("banana","an") 'banana'
Write a function shuffle(l1,l2) that takes as input two lists, 11 and l2, and returns a list consisting of the first element in l1, then the first element in l2, then the second element in l1, then the second element in l2, and so on. If the two lists are not of equal length, the remaining elements of the longer list are appended at the end of the shuffled output.
Here are some examples to show how your function should work.
>>> shuffle([0,2,4],[1,3,5]) [0, 1, 2, 3, 4, 5] >>> shuffle([0,2,4],[1]) [0, 1, 2, 4] >>> shuffle([0],[1,3,5]) [0, 1, 3, 5]
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | primeproduct(6) | True | True\n
| Passed after ignoring Presentation Error |
Test Case 2 | primeproduct(188) | False | False\n
| Passed after ignoring Presentation Error |
Test Case 3 | primeproduct(202) | True | True\n
| Passed after ignoring Presentation Error |
Test Case 4 | delchar("banana","b") | anana | anana\n
| Passed after ignoring Presentation Error |
Test Case 5 | delchar("banana","a") | bnn | bnn\n
| Passed after ignoring Presentation Error |
Test Case 6 | delchar("banana","n") | baaa | baaa\n
| Passed after ignoring Presentation Error |
Test Case 7 | delchar("banana","an") | banana | banana\n
| Passed after ignoring Presentation Error |
Test Case 8 | shuffle([0,2,4],[1,3,5]) | [0, 1, 2, 3, 4, 5] | [0, 1, 2, 3, 4, 5]\n
| Passed after ignoring Presentation Error |
Test Case 9 | shuffle([0,2,4],[1]) | [0, 1, 2, 4] | [0, 1, 2, 4]\n
| Passed after ignoring Presentation Error |
Test Case 10 | shuffle([0],[1,3,5]) | [0, 1, 3, 5] | [0, 1, 3, 5]\n
| Passed after ignoring Presentation Error |
Week 4 Programming Assignment
Write two Python functions as specified below. Paste the text for both functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.
- You may define additional auxiliary functions as needed.
- In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
- For each function, there are normally some public test cases and some (hidden) private test cases.
- "Compile and run" will evaluate your submission against the public test cases.
- "Submit" will evaluate your submission against the hidden private test cases. There are 10 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
- Ignore warnings about "Presentation errors".
Write a Python function histogram(l) that takes as input a list of integers with repetitions and returns a list of pairs as follows:.
for each number n that appears in l, there should be exactly one pair (n,r) in the list returned by the function, where r is the number of repetitions of n in l.
the final list should be sorted in ascending order by r, the number of repetitions. For numbers that occur with the same number of repetitions, arrange the pairs in ascending order of the value of the number.
For instance:
>>> histogram([13,12,11,13,14,13,7,7,13,14,12]) [(11, 1), (7, 2), (12, 2), (14, 2), (13, 4)] >>> histogram([7,12,11,13,7,11,13,14,12]) [(14, 1), (7, 2), (11, 2), (12, 2), (13, 2)] >>> histogram([13,7,12,7,11,13,14,13,7,11,13,14,12,14,14,7]) [(11, 2), (12, 2), (7, 4), (13, 4), (14, 4)]
A college maintains academic information about students in three separate lists
Course details: A list of pairs of form (coursecode,coursename), where both entries are strings. For instance,
[ ("MA101","Calculus"),("PH101","Mechanics"),("HU101","English") ]Student details: A list of pairs of form (rollnumber,name), where both entries are strings. For instance,
[ ("UGM2018001","Rohit Grewal"),("UGP2018132","Neha Talwar") ]A list of triples of the form (rollnumber,coursecode,grade), where all entries are strings. For instance,
[ ("UGM2018001", "MA101", "AB"), ("UGP2018132", "PH101", "B"), ("UGM2018001", "PH101", "B") ]. You may assume that each roll number and course code in the grade list appears in the student details and course details, respectively.
Your task is to write a function transcript (coursedetails,studentdetails,grades) that takes these three lists as input and produces consolidated grades for each student. Each of the input lists may have its entries listed in arbitrary order. Each entry in the returned list should be a tuple of the form
(rollnumber, name,[(coursecode_1,coursename_1,grade_1),...,(coursecode_k,coursename_k,grade_k)])
where the student has grades for k ≥ 1 courses reported in the input list grades.
The output list should be organized as follows.
The tuples shold sorted in ascending order by rollnumber
Each student's grades should sorted in ascending order by coursecode
For instance
>>>transcript([("MA101","Calculus"),("PH101","Mechanics"),("HU101","English")],[("UGM2021001","Rohit Grewal"),("UGP2021132","Neha Talwar")],[("UGM2021001","MA101","AB"),("UGP2021132","PH101","B"),("UGM2021001","PH101","B")]) [('UGM2021001', 'Rohit Grewal', [('MA101', 'Calculus', 'AB'), ('PH101', 'Mechanics', 'B')]), ('UGP2021132', 'Neha Talwar', [('PH101', 'Mechanics', 'B')])] >>>transcript([("T1","Test 1"),("T2","Test 2"),("T3","Test 3")],[("Captain","Rohit Sharma"),("Batsman","Virat Kohli"),("No3","Cheteshwar Pujara")],[("Batsman","T1","14"),("Captain","T1","33"),("No3","T1","30"),("Batsman","T2","55") ,("Captain","T2","158"),("No3","T2","19"), ("Batsman","T3","33"),("Captain","T3","95"),("No3","T3","51")]) [('Batsman', 'Virat Kohli', [('T1', 'Test 1', '14'), ('T2', 'Test 2', '55'), ('T3', 'Test 3', '33')]), ('Captain', 'Rohit Sharma', [('T1', 'Test 1', '33'), ('T2', 'Test 2', '158'), ('T3', 'Test 3', '95')]),('No3', 'Cheteshwar Pujara', [('T1', 'Test 1', '30'), ('T2', 'Test 2', '19'), ('T3', 'Test 3', '51')])]
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 |
Test Case 7 | Passed |
Test Case 8 | Passed |
Test Case 9 | Passed |
Test Case 10 | Passed |
Week 8 Programming Assignment - Dividing Sequences
For this assignment, you have to write a complete Python program. Paste your code in the window below.
- You may define additional auxiliary functions as needed.
- There are some public test cases and some (hidden) private test cases.
- "Compile and run" will evaluate your submission against the public test cases
- "Submit" will evaluate your submission against the hidden private test cases. There are 10 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
- Ignore warnings about "Presentation errors".
Dividing Sequences
(IARCS OPC Archive, K Narayan Kumar, CMI)This problem is about sequences of positive integers a1,a2,…,aN. A subsequence of a sequence is anything obtained by dropping some of the elements. For example, 3,7,11,3 is a subsequence of 6,3,11,5,7,4,3,11,5,3 , but 3,3,7 is not a subsequence of 6,3,11,5,7,4,3,11,5,3 .
A fully dividing sequence is a sequence a1,a2,…,aN where ai divides aj whenever i < j. For example, 3,15,60,720 is a fully dividing sequence.
Given a sequence of integers your aim is to find the length of the longest fully dividing subsequence of this sequence.
Consider the sequence 2,3,7,8,14,39,145,76,320
It has a fully dividing sequence of length 3, namely 2,8,320, but none of length 4 or greater.
Consider the sequence 2,11,16,12,36,60,71,17,29,144,288,129,432,993 .
It has two fully dividing subsequences of length 5,
- 2,11,16,12,36,60,71,17,29,144,288,129,432,993 and
- 2,11,16,12,36,60,71,17,29,144,288,129,432,993
and none of length 6 or greater.
Solution hint
Let the input be a1, a2, …, aN. Let us define Best(i) to be the length of longest dividing sequence in a1,a2,…ai that includes ai.
Write an expression for Best(i) in terms of Best(j) with j<i, with base case Best(1) = 1. Solve this recurrence using dynamic programming.
.Input format
The first line of input contains a single positive integer N indicating the length of the input sequence. Lines 2,…,N+1 contain one integer each. The integer on line i+1 is ai.
Output format
Your output should consist of a single integer indicating the length of the longest fully dividing subsequence of the input sequence.
Test Data
You may assume that N ≤ 2500.
Example:
Here are the inputs and outputs corresponding to the two examples discussed above.
Sample input 1:
9 2 3 7 8 14 39 145 76 320
Sample output 1:
3
Sample input 2:
14 2 11 16 12 36 60 71 17 29 144 288 129 432 993
Sample output 2:
5
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 9
2
3
7
8
14
39
145
76
320 | 3 | 3 | Passed |
Test Case 2 | 14
2
11
16
12
36
60
71
17
29
144
288
129
432
993 | 5 | 5 | Passed |
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.