Skip to content

Commit 28ae6dd

Browse files
authored
Update README.md
1 parent 3897e48 commit 28ae6dd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
11
# NPTEL-Programming-Data-Structures-and-Algorithms-using-Python-Week-4-Programming-Assignment
22
NPTEL Programming, Data Structures and Algorithms using Python Week 4 Programming Assignment
3+
4+
# Week 4 Programming Assignment
5+
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.
6+
7+
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.
8+
9+
Here are some examples to show how rainaverage(l) should work.
10+
```python
11+
>>> rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)])
12+
[(1, 2.0), (2, 3.0), (3, 8.0)]
13+
14+
>>> rainaverage([('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)])
15+
[('Bangalore', 201.0), ('Bombay', 885.5), ('Madras', 115.5)]
16+
```
17+
18+
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.
19+
```python
20+
def listtype(l):
21+
return(type(l) == type([]))
22+
```
23+
Here are some examples to show how flatten(l) should work.
24+
```python
25+
>>> flatten([1,2,[3],[4,[5,6]]])
26+
[1, 2, 3, 4, 5, 6]
27+
28+
>>> flatten([1,2,3,(4,5,6)])
29+
[1, 2, 3, (4, 5, 6)]
30+
```
31+
32+
# Sample Test Cases
33+
| | Input | Output |
34+
|--------------|------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
35+
| Test Case 1 | rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)]) | [(1, 2.0), (2, 3.0), (3, 8.0)] |
36+
| Test Case 2 | rainaverage([('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)]) | [('Bangalore', 201.0), ('Bombay', 885.5), ('Madras', 115.5)] |
37+
| Test Case 3 | flatten([1,2,[3],[4,[5,6]]]) | [1, 2, 3, 4, 5, 6] |
38+
| Test Case 4 | flatten([1,2,3,(4,5,6)]) | [1, 2, 3, (4, 5, 6)] |
39+
| Test Case 5 | flatten(["hello",True,3]) | ['hello', True, 3] |
40+
| Test Case 6 | flatten([1,2,[3,["hello",True]],[4,[5,6]]]) | [1, 2, 3, 'hello', True, 4, 5, 6] |
41+
| Test Case 7 | rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)]) | [(1, 2.0), (2, 3.0), (3, 8.0)] |
42+
| Test Case 8 | rainaverage([(1,0),(1,3),(2,3),(1,1),(3,8),(3,-8)]) | [(1, 1.3333333333333333), (2, 3.0), (3, 0.0)] |
43+
| Test Case 9 | rainaverage([('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)]) | [('Bangalore', 201.0), ('Bombay', 885.5), ('Madras', 115.5)] |
44+
| Test Case 10 | rainaverage([('Bombay',1848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128),('Madras',103),('Bombay',948),('Bangalore',323)]) | [('Bangalore', 262.0), ('Bombay', 1239.6666666666667), ('Madras', 111.33333333333333)] |
45+
| Test Case 11 | rainaverage([('Bombay',1848),('Bombay',923),('Bombay',201),('Bombay',128),('Bombay',103),('Bombay',948),('Bangalore',323)]) | [('Bangalore', 323.0), ('Bombay', 691.8333333333334)] |
46+
| Test Case 12 | flatten([1,2,3,(4,5,6)]) | [1, 2, 3, (4, 5, 6)] |
47+
| Test Case 13 | flatten(["hello",True,3]) | ['hello', True, 3] |
48+
| Test Case 14 | flatten([1,2,[3,["hello",True]],[4,[5,6]]]) | [1, 2, 3, 'hello', True, 4, 5, 6] |
49+
| Test Case 15 | flatten([[1,2,[3],[4,[5,6]]]]) | [1, 2, 3, 4, 5, 6] |
50+
| Test Case 16 | flatten([[[]]]) | [] |

0 commit comments

Comments
 (0)