about summary refs log tree commit diff
path: root/users/wpcarro/scratch/deepmind/part_two/top-scores.py
blob: 0ac349c1f88076ee1b422bdd81403fd3468d04e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest


def sort_scores(xs, highest_possible_score):
    result = []
    buckets = [0] * highest_possible_score

    for x in xs:
        buckets[x - 1] += 1

    for i in range(highest_possible_score - 1, -1, -1):
        if buckets[i] > 0:
            for _ in range(buckets[i]):
                result.append(i + 1)

    return result


# Tests
class Test(unittest.TestCase):
    def test_no_scores(self):
        actual = sort_scores([], 100)
        expected = []
        self.assertEqual(actual, expected)

    def test_one_score(self):
        actual = sort_scores([55], 100)
        expected = [55]
        self.assertEqual(actual, expected)

    def test_two_scores(self):
        actual = sort_scores([30, 60], 100)
        expected = [60, 30]
        self.assertEqual(actual, expected)

    def test_many_scores(self):
        actual = sort_scores([37, 89, 41, 65, 91, 53], 100)
        expected = [91, 89, 65, 53, 41, 37]
        self.assertEqual(actual, expected)

    def test_repeated_scores(self):
        actual = sort_scores([20, 10, 30, 30, 10, 20], 100)
        expected = [30, 30, 20, 20, 10, 10]
        self.assertEqual(actual, expected)


unittest.main(verbosity=2)