# Programming DSA using Python Nptel Week 4 Assignment Answers

Are you looking for the **Programming, Data Structures and Algorithms using Python NPTEL Week 4 Assignment Answers 2024**? You’re in the right place! This guide provides detailed solutions to the Week 4 assignment questions, covering key concepts in Python programming, data structures, and algorithms.

## Table of Contents

**Programming DSA using Python Week 4 Assignment Answers (July-Dec 2024)**

Q1Consider the following Python function.

def mystery(l):

if l == []:

return(l)

else:

return(mystery(l[1:])+l[:1])

What does mystery([22,14,19,65,82,55]) return?

**Answer: [55,82,65,19,14,22]**

**Q2.**What is the value of pairs after the following assignment?

pairs = [ (x,y) for x in range(4,1,-1) for y in range(5,1,-1) if (x+y)%3 == 0 ]

**Answer: [(4,5),(4,2),(3,3),(2,4)]**

**For answers or latest updates join our telegram channel: Click here to join **

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q3. **Consider the following dictionary.

wickets = {“Tests”:{“Bumrah”:[3,5,2,3],”Shami”:[4,4,1,0],”Ashwin”:[2,1,7,4]},”ODI”:{“Bumrah”:[2,0],”Shami”:[1,2]}}

Which of the following statements does not generate an error?

wickets[“ODI”][“Ashwin”][0:] = [4,4]

wickets[“ODI”][“Ashwin”].extend([4,4])

wickets[“ODI”][“Ashwin”] = [4,4]

wickets[“ODI”][“Ashwin”] = wickets[“ODI”][“Ashwin”] + [4,4]

**Answer: wickets[“ODI”][“Ashwin”] = [4,4]**

**Q4. **Assume that hundreds has been initialized as an empty dictionary:

hundreds = {}

Which of the following generates an error?

hundreds[“Tendulkar, international”] = 100

hundreds[“Tendulkar”] = {“international”:100}

hundreds[(“Tendulkar”,”international”)] = 100

hundreds[[“Tendulkar”,”international”]] = 100

**Answer: hundreds[[“Tendulkar”,”international”]] = 100**

**For answers or latest updates join our telegram channel: Click here to join **

**These are Programming DSA using Python Week 4 Assignment Answers**

Programming, Data Structures and Algorithms using Python NPTEL All weeks: Click Here

More Nptel Courses: https://progiez.com/nptel-assignment-answers

**Programming DSA using Python Week 4 Assignment Answers (JULY-DEC 2023)**

**Course Name: Programming Data Structure And Algorithms Using Python**

**Course Link: Click Here**

**These are Programming DSA using Python Week 4 Assignment Answers**

### Quiz

**Q1) 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: [46, 93, 58, 19, 35, 23]

**Q2) What is the value of pairs after the following assignment?pairs = [ (x,y) for x in range(6,1,-1) for y in range(3,1,-1) if (x+y)%2 == 0 ]**

Answer: [(6,2), (5,3), (4,2), (3,3), (2,2)]

**Q3) 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: goals[“Club”][“Messi”] = [496,71,145]**

**Q4) 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: wickets[[“Muralitharan”,”tests”]] = 800**

**Assignment**

**Question 1Write 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.**

Answer:

```
def histogram(l):
count,ans,k = 0,list(),[]
for i in range(len(l)):
index,count=i,0
for j in range(index,len(l)):
if l[index] == l[j] and l[index] not in k :
count =count + 1
k = k + [l[index]]
if (count != 0):
ans = ans + [(l[index], count)]
ans.sort()
ans=sorted(ans,key=lambda ans:ans[1])
return(ans)
def transcript(coursedetails, studentdetails, grades):
ans = list()
studentdetails.sort()
coursedetails.sort()
grades.sort()
for studentdet in studentdetails:
tuple,inlist = studentdet,list()
for grade in grades:
if studentdet[0] == grade[0]:
for cdetail in coursedetails:
if grade[1] == cdetail[0]:
intuple = cdetail
intuple = intuple + (grade[2],)
inlist.append(intuple)
tuple = tuple + (inlist,)
ans.append(tuple)
return(ans)
```

**These are Programming DSA using Python Week 4 Assignment Answers**

More Weeks of Programming Data Structure And Algorithms Using Python: Click here

More Nptel Courses: Click here

**Programming DSA using Python Week 4 Assignment Answers (JAN-APR 2023**)

**Course Name: Programming Data Structure And Algorithms Using Python**

**Course Link: Click Here**

**These are Programming DSA using Python Week 4 Assignment Answers**

### Quiz

**Q1. Consider the following Python function.**

```
def mystery(l):
if (l == []):
return(l)
else:
mid = len(l)//2
if (len(l)%2 == 0):
return l[mid-1:mid+1] + mystery(l[:mid-1]+l[mid+1:])
else:
return l[mid:mid+1] + mystery(l[:mid]+l[mid+1:])
```

**What does mystery([22,14,19,65,82,55]) return?**

**Answer: [19, 65, 14, 82, 22, 55]**

**Q2. What is the value of triples after the following assignment?**

```
triples = [ (x,y,z) for x in range(1,4) for y in range(2,5) for z in range(5,8) if x+y > z ]
```

**Answer: [(2, 4, 5), (3, 3, 5), (3, 4, 5), (3, 4, 6)]**

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q3. Consider the following dictionary.**

`marks = {"Quizzes":{"Mahesh":[3,5,7,8],"Suresh":[9,4,8,8],"Uma":[9,9,7,6]},"Exams":{"Mahesh":[37],"Uma":[36]}}`

**Which of the following statements does not generate an error?**

marks[“Exams”][“Suresh”].extend([44])

marks[“Exams”][“Suresh”] = [44]

marks[“Exams”][“Suresh”].append(44)

marks[“Exams”][“Suresh”][0:] = [44]

**Answer: b. marks[“Exams”][“Suresh”] = [44]**

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q4. Assume that actor has been initialized as an empty dictionary:**

```
actor = {}
```

**Which of the following generates an error?**

actor[“Star Wars”] = [“Rey”,”Ridley”]

actor[“Star Wars, Rey”] = “Ridley”

actor[[“Star Wars”, “Rey”]] = “Ridley”

actor[(“Star Wars”, “Rey”)] = “Ridley”

**Answer: c. actor[[“Star Wars”, “Rey”]] = “Ridley”**

**These are Programming DSA using Python Week 4 Assignment Answers**

### Programming Assignment

**Question**

**Write two Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.**

You may define additional auxiliary functions as needed.

In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.

For each function, there are normally some public test cases and some (hidden) private test cases.

“Compile and run” will evaluate your submission against the public test cases.

“Submit” will evaluate your submission against the hidden private test 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 see the actual test cases.

Ignore warnings about “Presentation errors”.

1. We have a list of annual rainfall recordings of cities. Each element in the list is of the form (c,r) where c is the city and r is the annual rainfall for a particular year. The list may have multiple entries for the same city, corresponding to rainfall recordings in different years.Write a Python function rainaverage(l) that takes as input a list of rainfall recordings and computes the avarage rainfall for each city.

The output should be a list of pairs (c,ar) where c is the city and ar is the average rainfall for this city among the recordings in the input list. Note that ar should be of type float. The output should be sorted in dictionary order with respect to the city name.Here are some examples to show how rainaverage(l) should work.

2. A list in Python can contain nested lists. The degree of nesting need not be uniform. For instance [1,2,[3,4,[5,6]]] is a valid Python list. Write a Python function flatten(l) that takes a nonempty list of lists and returns a simple list of all the elements in the nested lists, flattened out. You can make use of the following function that returns True if its input is of type list.Here are some examples to show how flatten(l) should work.

**Solution:**

```
def rainaverage(l):
raindata = {}
for (c,r) in l:
if c in raindata.keys():
raindata[c].append(r)
else:
raindata[c] = [r]
outputlist = []
for c in sorted(raindata.keys()):
thisaverage = sum(raindata[c])/len(raindata[c])
outputlist.append((c,thisaverage))
return(outputlist)
def flatten(l):
if not(listtype(l)):
return([l])
flatlist = []
for e in l:
flatlist.extend(flatten(e))
return(flatlist)
def listtype(l):
return(type(l) == type([]))
```

**These are Programming DSA using Python Week 4 Assignment Answers**

More Weeks of Programming Data Structure And Algorithms: Click Here

More Nptel courses: https://progiez.com/nptel

**Programming DSA using Python Week 4 Assignment Answers (JULY-DEC 2022**)

Course Name: Programming Data Structure And Algorithms Using Python

Link of Course: Click Here

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q1) Consider the following Python function.**def mystery (1):

if 1 == [] :

return (1)

else :

return(mystery (1[1 : ])+1[1])

What does mystery ([22, 14, 19, 65, 82,55]) return?

What does mystery ([22, 14, 19, 65, 82,55]) return?

**Answer: [55, 82, 65, 19, 14, 22]**

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q2) What is the value of pairs after the following assignment?**

pairs = [ (x,y) for x in range (4,1,-1) for y in range (5,1,-1) if (x+y)%3 ==0]

**Answer: [(4,5), (4,2), (3,3), (2,4)]**

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q3) Consider the following dictionary.wickets = {“Tests” : {“Bumrah” : [3,5,2,3], “Shami” : [4,4,1,0], “Ashwin” : [2,1,7,4]}, “ODI” : {“Bumrah” : [2, 0], “Shami” : [1,2]}}Which of the following statements does not generate an error?**

wickets [“ODI”][“Ashwin”][ : ] = [4,4]

wickets [“ODI”][“Ashwin”].extend ([4,4])

wickets [“ODI”][“Ashwin”] = [4,4]

wickets [“ODI”][“Ashwin”] = wickets [“ODI”][“Ashwin”] + [4,4]

**Answer: wickets [“ODI”][“Ashwin”] = [4,4]**

**These are Programming DSA using Python Week 4 Assignment Answers**

**Q4) Assume that hundreds has been initialized as an empty dictionary :hundreds = {}Which of the following generates an error?**

hundreds [“Tendulkar, international”] = 100

hundreds [“Tendulkar”] = {“international” : 100}

hundreds [(“Tendulkar”, “international”)] = 100

hundreds [[“Tendulkar”, “international”]] = 100

**Answer: hundreds [[“Tendulkar”, “international”]] = 100**

**These are Programming DSA using Python Week 4 Assignment Answers**

### Programming Assignment Solution

**Q1. Write two Python functions as specified below. Paste the text for both functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.**

**Solution:**

```
def frequency(l):
SET=set(l)
LIST=list(SET)
newl=list()
for a in LIST:
newl.append(l.count(a))
mi=min(newl)
ma=max(newl)
mil=[]
mal=[]
for b in range(len(newl)):
if newl[b]==mi:
mil.append(LIST[b])
if newl[b]==ma:
mal.append(LIST[b])
mil.sort()
mal.sort()
return(mil,mal)
def onehop(l):
ans=list()
l.sort()
for x in range(len(l)):
for y in range(len(l)):
if l[x]!=l[y]:
if l[x][1]==l[y][0]:
q=l[x][0]
w=l[y][1]
if q!=w:
t=[q,w]
t=tuple(t)
if t not in ans:
ans.append(tuple(t))
ans.sort()
return (ans)
```

**These are Programming DSA using Python Week 4 Assignment Answers**