r/programminghelp • u/[deleted] • Jul 25 '20
Python Solution breakdown help - High Five problem (Python)
This is the High Five problem (Leetcode). I found the working solution pasted below (Python) and I don't understand it. I have a working knowledge of Python, but I'm thrown off by the different data structures. I made substitutions for the loops but the results did not convey anything of substance to me. I'm especially confused by items: List[List[int]]) -> List[List[int]
Can anyone break down the solution for me?
Problem:
Given a list of scores of different students, return the average score of each student's top five scores in the order of each student's id.
Each entry items[i]has items[i][0]the student's id, and items[i][1]the student's score. The average score is calculated using integer division.
Example 1:
Input: [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
Output: [[1,87],[2,88]]
Explanation: The average of the student with id = 1 is 87. The average of the student with id = 2 is 88.6. But with integer division their average converts to 88.
Note:
1 <= items.length <= 1000
items[i].length == 2
The IDs of the students is between 1 to 1000
The score of the students is between 1 to 100
For each student, there are at least 5 scores
Solution:
class Solution:
def highFive(self, items: List[List[int]]) -> List[List[int]]:
items.sort(reverse=True)
res = []
curr = []
idx = items[0][0]
for i, val in items:
if i == idx:
if len(curr) < 5:
curr.append(val)
else:
res.append([idx, sum(curr) // len(curr)])
curr = [val]
idx = i
res.append([idx, sum(curr) // len(curr)])
res = res[::-1]
return res
1
u/amoliski Jul 25 '20
As for the function itself:
For more on destructuring, look here:
https://blog.tecladocode.com/destructuring-in-python/