#### Programming, Data Structures And Algorithms Using Python - Week 4 Programming Assignment | NPTEL Jan 2023 | Answer with Explanation

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

>>> rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)]) [(1, 2.0), (2, 3.0), (3, 8.0)] >>> rainaverage([('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)]) [('Bangalore', 201.0), ('Bombay', 885.5), ('Madras', 115.5)]

## Answer

def rainaverage(l): city_rainfall = {} for city, rainfall in l: if city not in city_rainfall: city_rainfall[city] = [rainfall] else: city_rainfall[city].append(rainfall) result = [(city, sum(rainfall) / len(rainfall)) for city, rainfall in city_rainfall.items()] result.sort(key=lambda x: x[0]) return result

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

def listtype(l): return(type(l) == type([]))

## Here are some examples to show how flatten(l) should work.

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

## Answer

def flatten(l): result = [] for item in l: if listtype(item): result.extend(flatten(item)) else: result.append(item) return result

Disclaimer:

"This page contains Programming Assignment related to Programming, Data Structures And Algorithms Using Python. The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."

## 0 Comments