# [Week 3] NPTEL Programming Data Structures And Algorithms Using Python Assignment Answers 2023

NPTEL Programming Data Structures And Algorithms Using Python Assignment Solutions 2023

## Programming, Data Structures And Algorithms Using Python Week 3 Quiz Assignment Answers 2023

1. Write a function expanding(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly increases.

Here are some examples of how your function should work.

>>> expanding([1,3,7,2,9])
True

Explanation: Differences between adjacent elements are 3-1 = 2, 7-3 = 4, 7-2 = 5, 9-2 = 7.

>>> expanding([1,3,7,2,-3]) False

Explanation: Differences between adjacent elements are 3-1 = 2, 7-3 = 4, 7-2 = 5, 2-(-3) = 5, so not strictly increasing.

>>> expanding([1,3,7,10]) False

`Answer :-For Answer Click Here`

2. Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

Here are some examples to show how your function should work.

>>> sumsquare([1,3,5])
[35, 0]

>>> sumsquare([2,4,6])
[0, 56]

>>> sumsquare([-1,-2,3,7])
[59, 4]

`Answer :- For Answer Click Here`

3. A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix

1 2 3 4
5 6 7 8

would be represented as [[1, 2, 3, 4], [5, 6, 7, 8]].

The transpose of a matrix converts each row into a column. The transpose of the matrix above is:

1 5
2 6
3 7
4 8

which would be represented as [[1, 5], [2, 6], [3, 7], [4, 8]].
Write a Python function transpose(m) that takes as input a two dimensional matrix m and returns the transpose of m. The argument m should remain undisturbed by the function.

Here are some examples to show how your function should work. You may assume that the input to the function is always a non-empty matrix.

>>> transpose([[1,2,3],[4,5,6]])
[[1, 4], [2, 5], [3, 6]]

>>> transpose([,,])
[[1, 2, 3]]

>>> transpose([])
[]

`Answer :- For Answer Click Here`

All questions carry equal weightage. All Python code is assumed to be executed using Python3. You may submit as many times as you like within the deadline. Your final submission will be graded.Programming, Data Structures And Algorithms Using Python Week 2 Quiz Assignment Answers 2023

Note:

• If the question asks about a value of type list, remember to enclose your answer in square brackets and use commas to separate list items.

## Programming, Data Structures And Algorithms Using Python Week 2 Quiz Assignment Answers 2023

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

``````x = ["slithy",[7,10,12],2,"tove",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
z = 42                            # Statement 7
w[:3] = 'fea'                     # Statement 8
x = 5555                       # Statement 9
a = (x == 1)                   # Statement 10``````
`Answer:- 8`

2. Consider the following lines of Python code.

``````b = [23,44,87,100]
a = b[1:]
d = b[2:]
c = b
d = 97
c = 77``````

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

• a == 77, b == 77, c == 77, d == 97
• a == 87, b == 87, c == 77, d == 97
• a == 87, b == 77, c == 77, d == 97
• a == 97, b == 77, c == 77, d == 97
`Answer:- c) a == 87, b == 77, c == 77, d == 97`

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

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

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

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

mylist = [7,11,13]
mystery(mylist)``````
`Answer:- [7, 11, 13]`

## Programming, Data Structures And Algorithms Using Python Week 2 Programming Assignment Answers 2023

1. 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``````

2. 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'``````

3. 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],)
[0, 1, 2, 4]

>>> shuffle(,[1,3,5])
[0, 1, 3, 5]```

``````def primeproduct(m):
P=list()
if m>=2:
for k in range(2,m):
if m%k==0:
P=P+[k]
if len(P)==2:
if m==P*P:
if P%P==0:
return(False)
return(True)
elif len(P)==1:
if m==P*P:
return(True)
else:
return(False)
else:
return(False)

def delchar(s,c):
if len(c)==1:
s=s.replace(c,'')
ans=s
else:
ans=s
return(ans)

def shuffle(l1,l2):
ans=[]
if len(l1)!=0 and len(l2)!=0:
for q in range(min(len(l1), len(l2))):
ans.extend([l1[q],l2[q]])
ans.extend(l1[q+1:] or l2[q+1:])
else:
ans.extend(l1[0:] or l2[0:])
return(ans)``````

## NPTEL Programming, Data Structures And Algorithms Using Python Week 1 Quiz Assignment Answers 2023

1. What is the value of g(728) for the function below?

``````def g(y):
b = 0
while y >= 3:
(y,b) = (y/3,b+1)
return(b)``````
`Answer :-  5`

2. What is f(90)-f(89), given the definition of f below?

```def f(n):
s = 0
for i in range(2,n):
if n%i == 0 and i%2 == 1:
s = s+1
return(s)```
`Answer :- 5`

3. Consider the following function h.

```def h(n):
s = True
for i in range(1,n+1):
if i*i == n:
s = False
return(s)
```

The function h(n) given above returns False for a positive number n if and only if:

• n is an odd number.
• n is a prime number.
• n is a perfect square.
• n is a composite number.
`Answer :- c (n is a perfect square.)`

4. Consider the following function foo.

```def foo(m):
if m == 0:
return(0)
else:
return(m+foo(m-1))
```

Which of the following is correct?

• The function always terminates with foo(n) = factorial of n
• The function always terminates with foo(n) = n(n+1)/2
• The function terminates for non­negative n with foo(n) = factorial of n
• The function terminates for non­negative n with foo(n) = n(n+1)/2
`Answer :- d  (The function terminates for non­negative n with foo(n) = n(n+1)/2)`

1. 