0% found this document useful (0 votes)
5 views

noc20-cs26_Week_02_Assignment_02

The document outlines a programming course focused on Data Structures and Algorithms using Python, including assignments and quizzes for various weeks. It details specific programming tasks such as writing functions to reverse integers, check for matched brackets, and sum prime numbers from a list. Sample test cases and solutions are provided to assist students in completing their assignments.

Uploaded by

chittoras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

noc20-cs26_Week_02_Assignment_02

The document outlines a programming course focused on Data Structures and Algorithms using Python, including assignments and quizzes for various weeks. It details specific programming tasks such as writing functions to reverse integers, check for matched brackets, and sum prime numbers from a list. Sample test cases and solutions are provided to assist students in completing their assignments.

Uploaded by

chittoras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

02/07/2020 Programming, Data Structures And Algorithms Using Python - Course

(https://swayam.gov.in) (https://swayam.gov.in/nc_details/NPTEL)

[email protected]

NPTEL (https://swayam.gov.in/explorer?ncCode=NPTEL) » Programming, Data Structures And Algorithms

Using Python (course)

Announcements (announcements)

About the Course (https://swayam.gov.in/nd1_noc19_cs40/preview) Ask a Question (forum)

Progress (student/home) Mentor (student/mentor)

Week 2 Programming Assignment


Course Due on 2019-08-22, 23:59 IST
outline
Write three Python functions as specified below. Paste the text for all three
functions together into the submission window. Your function will be called
How to access automatically with various inputs and should return values as specified. Do not
the portal write commands to read any input or print any output.
You may define additional auxiliary functions as needed.
Week 1: In all cases you may assume that the value passed to the function is of
Introduction the expected type, so your function does not have to check for malformed
inputs.
Week 1 Quiz For each function, there are normally some public test cases and some
(hidden) private test cases.
Week 2: Basics "Compile and run" will evaluate your submission against the public test
of Python cases.
"Submit" will evaluate your submission against the hidden private test
Week 2 Quiz 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
Week 2 see the actual test cases.
Programming Ignore warnings about "Presentation errors".
Assignment

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.

Week 3: Lists, >>> intreverse(783)


inductive 387
function >>> intreverse(242789)
987242
>>> intreverse(3)

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

Assignment Test Case


sumprimes([101,93,97,44]) 198
8
Download Test Case
videos sumprimes([1001,393,743,59]) 802
9
Test Case
Text Transcripts sumprimes([11,11,11,13,11,-11]) 57
10
Test Case
Online intreverse(368) 863
Programming 11
Test - Sample Test Case
intreverse(798798) 897897
12
Online Test Case
intreverse(7) 7
Programming 13
Test 1, 26 Sep Test Case
2019, 09:30- matched("(7)(a") False
14
11:30
Test Case
matched("a)*(?") False
15
Online
Programming Test Case matched("((jkl)78(A)&l(8(dd(FJ
Test 2, 26 Sep True
16 I:),):)?)")
2019, 20:00-
22:00 Test Case
sumprimes([17,51,29,39]) 46
17
Test Case
sumprimes([-3,-5,3,5]) 8
18
Test Case
sumprimes([4,6,15,27]) 0
19

The due date for submitting this assignment has passed.


As per our records you have not submitted this assignment.
Sample solutions (Provided by instructor)
1 def intreverse(n):
2 ans = 0
3 while n > 0:
4 (d,n) = (n%10,n//10)
5 ans = 10*ans + d
6 return(ans)
7
8 def matched(s):
9 nested = 0
10 for i in range(0,len(s)):
11 if s[i] == "(":
12 nested = nested+1
13 elif s[i] == ")":
14 nested = nested-1
15 if nested < 0:
16 return(False)
17 return(nested == 0)
18
19 def factors(n):
20 factorlist = []
21 for i in range(1,n+1):
22 if n%i == 0:
23 factorlist = factorlist + [i]
24 return(factorlist)
25
26 def isprime(n):
27 return(factors(n) == [1,n])
28
29
30 def sumprimes(l):
31 sum = 0
32 for i in range(0,len(l)):
33 if isprime(l[i]):
34 sum = sum+l[i]

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

You might also like