Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 2cca8b4

Browse files
committed
tests should all now be running, and breaking records is now O(n) rather than O(n^2)
1 parent b0827e8 commit 2cca8b4

3 files changed

Lines changed: 25 additions & 43 deletions

File tree

test/testutils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class TestCase(unittest.TestCase):
1818
@classmethod
1919
def setUpClass(cls):
2020
"""Get all the matching test case files"""
21-
cls.tcs = sorted(Path("w1/tc").glob(cls.glob_pattern))
21+
# Path.glob is lazy, so wrap in list to get iteration ready paths...
22+
cls.tcs = list(Path().glob(cls.glob_pattern))
2223

2324
def read_lines(self, path: Path) -> Iterator[str]:
2425
"""

w1/breaking_records.py

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,21 @@ def breaking_records(scores: List[int]) -> List[int]:
2121
>>> breaking_records([3, 4, 21, 36, 10, 28, 35, 5, 24, 42])
2222
[4, 0]
2323
"""
24-
# keep track of the low and high scores.
25-
hc = [] # note: final high count is this list length
26-
lc = [] # note: final low count is the count of this list
27-
28-
# so that we can keep track of the things processes
29-
processed = []
30-
31-
# iterate over the scores
32-
for s in scores:
33-
# max min are functions of what is processed
34-
try:
35-
_mx = max(processed)
36-
except ValueError:
37-
# zero is not class as a high score so if the score
38-
# was zero, count it as processed and continue on.
39-
processed.append(s)
40-
continue
41-
42-
try:
43-
_mn = min(processed)
44-
except ValueError:
45-
_mn = 0
46-
47-
# count the highs and lows
48-
if s > _mx:
49-
hc.append(s)
50-
51-
if s < _mn:
52-
lc.append(s)
53-
54-
# value was processesed, so append it
55-
processed.append(s)
56-
57-
58-
# done, return the count
59-
return [
60-
len(hc),
61-
len(lc)
62-
]
24+
# keep track of the counts of breaks for min score and max score
25+
min_b = max_b =0
26+
27+
# start off with 0 for max and min counts
28+
mx = mn = scores[0]
29+
30+
for score in scores[1:]:
31+
32+
if score > mx:
33+
mx = score
34+
max_b += 1
35+
36+
elif score < mn :
37+
mn = score
38+
min_b += 1
39+
40+
# all done
41+
return [max_b, min_b]

w1/breaking_records_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Tests for Breaking Records
33
"""
44
from pathlib import Path
5+
from typing import ClassVar
56
from test import testutils
67
from w1.breaking_records import breaking_records
78

@@ -11,12 +12,12 @@ class BreakRecordsTest(testutils.TestCase):
1112
"""
1213
Breaking records test cases
1314
"""
14-
glob_pattern = "w1/tc/4_*.txt"
15+
glob_pattern: ClassVar[str] = "w1/tc/4_*.txt"
1516

1617
def test_breaking_records(self):
1718
"""(W1/4): Breaking Records"""
18-
for tc in self.tcs:
1919

20+
for tc in self.tcs:
2021
# use a subtest scope
2122
with self.subTest(tc=tc):
2223
# read in the lines
@@ -29,4 +30,5 @@ def test_breaking_records(self):
2930
expected = [int(value) for value in lines[3].split()]
3031

3132
# we get what we expected
32-
self.assertEqual(breaking_records(args), expected)
33+
results = breaking_records(args)
34+
self.assertEqual(results, expected)

0 commit comments

Comments
 (0)