noc20-cs26_Week_02_Assignment_02
noc20-cs26_Week_02_Assignment_02
(https://swayam.gov.in) (https://swayam.gov.in/nc_details/NPTEL)
Announcements (announcements)
Week 2
Programming 1. Write a function intreverse(n) that takes as input a positive integer n
Assignment and returns the integer obtained by reversing the digits in n.
(/noc19_cs40/progassignment?
name=90)
Here are some examples of how your function should work.
https://onlinecourses.nptel.ac.in/noc19_cs40/progassignment?name=90 1/4
02/07/2020 Programming, Data Structures And Algorithms Using Python - Course
>>> intreverse(3)
definitions,
3
sorting
Week 3 2. Write a function matched(s) that takes as input a string s and checks if
Programming the brackets "(" and ")" in s are matched: that is, every "(" has a
Assignment matching ")" after it and every ")" has a matching "(" before it. Your
function should ignore all other symbols that appear in s. Your function
Week 4: Sorting, should return True if s has matched brackets and False if it does not.
Tuples,
Here are some examples to show how your function should work.
Dictionaries,
Passing
Functions, List
>>> matched("zb%78")
Comprehension
True
>>> matched("(7)(a")
Week 4 Quiz
False
>>> matched("a)*(?")
Week 4
False
Programming
>>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
Assignment
True
Week 5:
Exception 3. Write a function sumprimes(l) that takes as input a list of integers l and
handling, retuns the sum of all the prime numbers in l.
input/output, file Here are some examples to show how your function should work.
handling, string
processing >>> sumprimes([3,3,1,13])
19
Week 5 >>> sumprimes([2,4,6,9,11])
Programming 13
Assignment
>>> sumprimes([-3,1,6])
0
Week 6:
Backtracking,
scope, data
Sample Test Cases
structures;
stacks, queues Input Output
and heaps Test Case
intreverse(31511) 11513
1
Week 6 Quiz Test Case
intreverse(4) 4
2
Week 7:
Test Case 5324324235
Classes, objects intreverse(15135324234235)
3 3151
and user
defined
datatypes Test Case matched("a3qw3;4w3(aasdgsd)((agadsgd
True
4 sgag)agaga)")
Week 7 Quiz
Test Case matched("(ag(Gaga(agag)Gaga)GG)a)33)
False
Week 8: 5 cc(")
Dynamic
programming, Test Case
matched("(adsgdsg(agaga)a") False
wrap-up 6
Test Case
matched("15ababa.agaga[][[[") True
Week 8 7
Programming
https://onlinecourses.nptel.ac.in/noc19_cs40/progassignment?name=90 2/4
02/07/2020 Programming, Data Structures And Algorithms Using Python - Course
https://onlinecourses.nptel.ac.in/noc19_cs40/progassignment?name=90 3/4
02/07/2020 Programming, Data Structures And Algorithms Using Python - Course
35 return(sum)
36
37
38 import ast
39
40 def tolist(inp):
41 inp = "["+inp+"]"
42 inp = ast.literal_eval(inp)
43 return (inp[0],inp[1])
44
45 def parse(inp):
46 inp = ast.literal_eval(inp)
47 return (inp)
48
49 fncall = input()
50 lparen = fncall.find("(")
51 rparen = fncall.rfind(")")
52 fname = fncall[:lparen]
53 farg = fncall[lparen+1:rparen]
54
55 if fname == "intreverse":
56 arg = parse(farg)
57 print(intreverse(arg))
58 elif fname == "matched":
59 arg = parse(farg)
60 print(matched(arg))
61 elif fname == "sumprimes":
62 arg = parse(farg)
63 print(sumprimes(arg))
64 else:
65 print("Function", fname, "unknown")
66
67
https://onlinecourses.nptel.ac.in/noc19_cs40/progassignment?name=90 4/4