## Problems

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 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)]```

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”)]
• Grades: 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 should be sorted in ascending order by rollnumber.
-> Each student’s grades should sorted in ascending order by coursecode

## Solution

Download Python File:- Raw FileDownload (Please rename it to .py file once downloaded, for it to run)

Special thanks to Yash Mhadgut for commenting and helping us in the second solution.

```def histogram(l):
d={}
for i in l:
if i in d.keys():
d[i] +=1
else:
d[i] = 1
ans = []
for a in d.keys():
number_of_times = d[a]
ans.append((a, number_of_times))

ans = sorted(ans, key=lambda x : x[0])
ans = sorted(ans, key=lambda x : x[1])
return ans
#print(ans)

def transcript(coursedetails, studentdetails, grades):
list = []
studentdetails.sort()
coursedetails.sort()
grades.sort()
for studentdet in studentdetails:
tuple = studentdet
inlist = []
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,)
list.append(tuple)
return list```

Explanation :

Tracing:- (Looping through each element in the list passed in histogram)
`{13: 1}`
`{13: 1, 12: 1}`
`{13: 1, 12: 1, 11: 1}`
`{13: 2, 12: 1, 11: 1}`
`{13: 2, 12: 1, 11: 1, 14: 1}`
`{13: 3, 12: 1, 11: 1, 14: 1}`
`{13: 3, 12: 1, 11: 1, 14: 1, 7: 1}`
`{13: 3, 12: 1, 11: 1, 14: 1, 7: 2}`
`{13: 4, 12: 1, 11: 1, 14: 1, 7: 2}`
`{13: 4, 12: 1, 11: 1, 14: 2, 7: 2}`
`{13: 4, 12: 2, 11: 1, 14: 2, 7: 2}`

As we see in the solution of Histogram problem, we have created a dictionary ‘d’ which stores the count of a particular integer in the dictionary.
It creates a key for each integer found and increases the value of key whenever an integer is repeated.

```d={}
for i in l:
if i in d.keys():
d[i] +=1
else:
d[i] = 1```

This following three lines of code converts the dictionary form into tuple form, which is the required type of output :

```for a in d.keys():
number_of_times = d[a]
ans.append((a, number_of_times))```

Finally, we sort the tuple in ascending order by the following code

```ans = sorted(ans, key=lambda x : x[0])
ans = sorted(ans, key=lambda x : x[1])```
Categories: NPTEL solution

### 23 Comments

#### Skylark · March 4, 2018 at 3:02 PM

Correct return’s indentation.

#### Ritesh · March 4, 2018 at 3:33 PM

Bro the code fails in Test case 4 & test case 5…Please Correct it.Great love to your service.

#### Jarwish Roshan.J · March 4, 2018 at 4:19 PM

can you please send the correct solution for transcript problem in week 4 the provided solution is wrong

#### Vivek · March 4, 2018 at 6:05 PM

3/5 Passes,
test case 4 and test case 5 says wrong answer.
can you please correct it
thank you

#### Gaurav · March 4, 2018 at 6:29 PM

Last 2 cases are incorrect…

#### Sreeram · March 5, 2018 at 11:47 AM

Thanks a lot, will you update exam answers during the exam

#### Darshan · March 5, 2018 at 11:50 AM

Last 2 test case is not passed by this code sir can u plz tell me whats the problem in this

#### shubham garg · March 5, 2018 at 1:03 PM

Answer no.2 is generating error so plz correct it

#### Sreeram · March 5, 2018 at 1:47 PM

Good work sir but the return statement in q2 will be outside of all the loops.

#### Swarnava Saha · March 5, 2018 at 2:14 PM

Last 2 test case is not passed by this code sir can u plz tell me whats the problem in this

#### Yash Sodha · March 5, 2018 at 2:44 PM

Hey,

There was intendation error while uploading.
We have fixed the same. It was returning list too soon. Fixed.

Please have a look at the updated post : https://www.hackademic.in/2018/03/04/week-4-program-python-apr-2018/

Thanks,
Hackademic.

#### Mahesh · March 5, 2018 at 3:37 PM

🙂
Tq bro Week 4 assignment is totally completed

Tq so much 🙂

#### Darshan · March 7, 2018 at 9:17 AM

Anyone tell me what we have to do last two test cases not completed

#### shubham garg · March 12, 2018 at 2:40 PM

For Nptle python week 5 programming assignment solution

https://youtu.be/JWxQB4_f1WQ