# Programming Data Structure And Algorithms Using Python | Week 4

**Session: JAN-APR 2023**

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

**Course Link: Click Here**

**These are Programming Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 Answers**

## Programming Data Structure and Algorithms 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 Data Structure And Algorithms Assignment 4 Answers**

More Weeks of Programming Data Structure And Algorithms: Click Here

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

**Session: JULY-DEC 2022**

Course Name: Programming Data Structure And Algorithms Using Python

Link of Course: Click Here

**These are Programming Data Structure And Algorithms Assignment 4 Answers**

**These are Programming Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 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 Data Structure And Algorithms Assignment 4 Answers**

All weeks solution of Programming Data Structure And Algorithms Using Python: https://progies.in/answers/nptel/programming-data-structure-and-algorithms-using-python

More NPTEL Solutions: https://progies.in/answers/nptel

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