Skip to content

Commit 5cc124b

Browse files
committed
Refactor solution to mission 30
1 parent 51c12f3 commit 5cc124b

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

30-yankeedoodle.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@
1717

1818

1919
def read_csv_cells(url):
20-
"""Read the cells of the CSV mentioned in the riddle"""
20+
"""Reads the cells of the CSV mentioned in the riddle"""
2121
csv_url = get_last_src_url(url).replace("jpg", "csv")
2222
rows = (line.rstrip(",").split(", ") for line in read_riddle(csv_url).splitlines())
2323
return list(chain.from_iterable(rows))
2424

2525

26-
def obtain_factors(n):
27-
"""Obtain the factors of the length of the list of cells"""
28-
size, factors = n, []
29-
for n in range(2, int(sqrt(size))):
30-
if size % n == 0:
31-
factors.extend([n, size // n])
32-
return factors
26+
def factorize(length):
27+
"""Obtains the factors of `length`"""
28+
for n in range(2, int(sqrt(length))):
29+
if length % n == 0:
30+
return n, length // n
3331

3432

3533
def extract_formula(cells, width, height):
36-
"""Extract the formula hidden in the CSV"""
34+
"""Extracts the formula hidden in the CSV"""
3735
it = iter(cells)
3836
formula = [[" "] * width for _ in range(height)]
3937
for x in range(width):
@@ -44,17 +42,14 @@ def extract_formula(cells, width, height):
4442

4543

4644
def apply_formula(cells):
47-
"""Apply the hidden formula on cells"""
45+
"""Applies the hidden formula on cells"""
4846
it = iter(cells)
49-
while True:
50-
try:
51-
yield int(next(it)[5] + next(it)[5] + next(it)[6])
52-
except StopIteration:
53-
break
47+
for _ in range(len(cells) // 3):
48+
yield int(next(it)[5] + next(it)[5] + next(it)[6])
5449

5550

5651
url = "http://www.pythonchallenge.com/pc/ring/yankeedoodle.html"
5752
cells = read_csv_cells(url)
58-
factors = obtain_factors(len(cells))
53+
factors = factorize(len(cells))
5954
print("\n".join("".join(row) for row in extract_formula(cells, *factors[::-1])))
6055
print(bytes(apply_formula(cells)).decode())

0 commit comments

Comments
 (0)