### Programming Data Structure And Algorithms Using Python Assignment 2

Course Name: Programming Data Structure And Algorithms Using Python

#### These are the solutions of Programming Data Structure And Algorithms Using Python Week 2

Q1. One of the following 10 statements generates an error. Which one? (Your answer should be a number between 1 and 10.)

``````x = [[3,5],"mimsy",2,"borogove",1]  # Statement 1
y = x[0:50]                          # Statement 2
z = y                                # Statement 3
w = x                                # Statement 4
x = x[:5] + 'ery'              # Statement 5
y = 4                             # Statement 6
w[:3] = 'fea'                     # Statement 7
z = 42                            # Statement 8
x = 5555                       # Statement 9
a = (x == 1)                   # Statement 10``````

Q2. Consider the following lines of Python code.

Which of the following holds at the end of this code?

``````b = [43,99,65,105,4]
a = b[2:]
d = b[1:]
c = b
d = 95
b = 47
c = 73``````

Answer: c – a == 65, b == 73, c == 73, d == 95

Q3. What is the value of endmsg after executing the following lines?

``````startmsg = "anaconda"
endmsg = ""
for i in range(1,1+len(startmsg)):
endmsg = endmsg + startmsg[-i]``````

Q4. What is the value of mylist after the following lines are executed?

``````def mystery(l):
l = l[2:]
return(l)

mylist = [7,11,13,17,19,21]
mystery(mylist)``````

These are the solutions of Programming Data Structure And Algorithms Using Python Week 2

## Programming Data Structures And Algorithms Using Python Coding Assignment 2 Answers

Q1. Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n. Write a function matched(s) that takes as input a string s and checks if the brackets “(” and “)” in s are matched: that is, every “(” has a matching “)” after it and every “)” has a matching “(” before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not. Write a function sumprimes(l) that takes as input a list of integers l and retuns the sum of all the prime numbers in l.

Sol:-

``````def intreverse(n):
ans = 0
while n > 0:
(d,n) = (n%10,n//10)
ans = 10*ans + d
return(ans)

def matched(s):
nested = 0
for i in range(0,len(s)):
if s[i] == "(":
nested = nested+1
elif s[i] == ")":
nested = nested-1
if nested < 0:
return(False)
return(nested == 0)

def factors(n):
factorlist = []
for i in range(1,n+1):
if n%i == 0:
factorlist = factorlist + [i]
return(factorlist)

def isprime(n):
return(factors(n) == [1,n])

def sumprimes(l):
sum = 0
for i in range(0,len(l)):
if isprime(l[i]):
sum = sum+l[i]
return(sum)``````

These are the solutions of Programming Data Structure And Algorithms Using Python Week 2

All weeks solution of Programming Data Structure And Algorithms Using Python: Click Here

More NPTEL Solutions: Click me

* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.

### Problem Solving Through Programming In C NPTEL Assignment 9