mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 17:01:28 -04:00
add some exe
This commit is contained in:
parent
47e5ee3918
commit
bb6afce467
3 changed files with 107 additions and 0 deletions
37
interview_cake/data_structures/angry_bird.py
Normal file
37
interview_cake/data_structures/angry_bird.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
#!/bin/python
|
||||
|
||||
"""
|
||||
Each round, players receive a score between 0 and 100, which you use to rank them from highest to lowest. So far you're using an algorithm that sorts in O(n\lg{n})O(nlgn) time, but players are complaining that their rankings aren't updated fast enough. You need a faster sorting algorithm.
|
||||
|
||||
Write a function that takes:
|
||||
|
||||
a list of unsorted_scores
|
||||
the highest_possible_score in the game
|
||||
and returns a sorted list of scores in less than O(n\lg{n})O(nlgn) time.
|
||||
"""
|
||||
|
||||
def sort_scores(unsorted_scores, highest_score):
|
||||
|
||||
score_counts = [0] * (highest_score+1)
|
||||
|
||||
for score in unsorted_scores:
|
||||
score_counts[score] += 1
|
||||
|
||||
sorted_scores = []
|
||||
|
||||
for score in range(len(score_counts)-1, -1, -1):
|
||||
count = score_counts[score]
|
||||
|
||||
for i in range(count):
|
||||
sorted_scores.append(score)
|
||||
|
||||
return sorted_scores
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
unsorted_scores = [37, 89, 41, 65, 91, 53]
|
||||
HIGHEST_POSSIBLE_SCORE = 100
|
||||
|
||||
print sort_scores(unsorted_scores, HIGHEST_POSSIBLE_SCORE)
|
Loading…
Add table
Add a link
Reference in a new issue