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

admin
11 Min Read

NPTEL Programming, Data Structures And Algorithms Using Python Assignment Solutions 2023:

Programming Data Structures And Algorithms Using Python

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

1. Consider the following Python function.

def mystery(l):
if l == []:
return(l)
else:
return(l[-1:]+mystery(l[:-1]))

What does mystery([23,35,19,58,93,46]) return?

Answer :- For Answer Click Here

2. Consider the following Python function.

def mystery(l):
if l == []:
return(l)
else:
return(l[-1:]+mystery(l[:-1]))

What does mystery([23,35,19,58,93,46]) return?

Answer :- For Answer Click Here

3. Consider the following dictionary.

goals = {“Country”:{“Ronaldo”:123,”Messi”:103,”Pele”:83},”Club”:{“Ronaldo”:[512,51,158],”Pele”:[604,49,26]}}

Which of the following statements does not generate an error?

goals[“Club”][“Messi”][0:] = [496,71,145]
goals[“Club”][“Messi”].extend([496,71,145])
goals[“Club”][“Messi”] = [496,71,145]
goals[“Club”][“Messi”] = goals[“Club”][“Messi”] + [496,71,145]

Answer :- For Answer Click Here

4. Assume that wickets has been initialized as an empty dictionary:

wickets = {}

Which of the following generates an error?

wickets[“Muralitharan, tests”] = 800
wickets[“Muralitharan”] = {“tests”:800}
wickets[(“Muralitharan”,”tests”)] = 800
wickets[[“Muralitharan”,”tests”]] = 800

Answer :- 

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

1. 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)]
Answer :- For Answer Click Here

2. 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')])]
Answer :- For Answer Click Here
Course NameProgramming, Data Structures And Algorithms Using Python
CategoryNPTEL Assignment Answer
Home Click Here
Join Us on TelegramClick Here

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

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]

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

>>> transpose([[3]])
[[3]]

Solution:-

def expanding(l):
    if len(l) <= 2:
        return(True)
    diff = abs(l[1]-l[0])
    return(diff < abs(l[2]-l[1]) and expanding(l[1:]))

def expanding_loop(l):
    if len(l) <= 2:
        return(True)
    olddiff = abs(l[1]-l[0])
    for i3 in range(2,len(l)):
        newdiff = abs(l[i3]-l[i3-1])
        if newdiff <= olddiff:
            return(False)
        olddiff = newdiff
    return(True)

  
def sumsquare(l):
  (odd_sum,even_sum)=(0,0)
  for aa in l:
    if aa%2!=0:
      odd_sum+=aa*aa
    else:
      even_sum+=aa*aa
  return([odd_sum,even_sum])
 
def transpose(m):
  ans1=list()
  for ip in range(len(m[0])):
    a=list()
    for j in range(len(m)):
      a.append(m[j][ip])
    ans1.append(a)
  return(ans1)     
                
Course NameProgramming, Data Structures And Algorithms Using Python
CategoryNPTEL Assignment Answer
Home Click Here
Join Us on TelegramClick Here

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[0] = x[0][:5] + 'ery'              # Statement 5
y[2] = 4                             # Statement 6
z[4] = 42                            # Statement 7
w[0][:3] = 'fea'                     # Statement 8
x[1][0] = 5555                       # Statement 9
a = (x[4][1] == 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[0] = 97
c[2] = 77

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

  • a[1] == 77, b[2] == 77, c[2] == 77, d[0] == 97
  • a[1] == 87, b[2] == 87, c[2] == 77, d[0] == 97
  • a[1] == 87, b[2] == 77, c[2] == 77, d[0] == 97
  • a[1] == 97, b[2] == 77, c[2] == 77, d[0] == 97
Answer:-Click Here 

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:- Click Here 

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:- 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.

Note:

  • If the question asks about a value of type string, remember to enclose your answer in single or double quotes.
  • 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.

NPTEL Programming, Data Structures And Algorithms Using Python Week 1 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 :- Click Here

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 :- Click Here

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 :- Click Here

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 :- Click Here

Share This Article
Leave a comment