17
17
18
18
19
19
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"""
21
21
csv_url = get_last_src_url (url ).replace ("jpg" , "csv" )
22
22
rows = (line .rstrip ("," ).split (", " ) for line in read_riddle (csv_url ).splitlines ())
23
23
return list (chain .from_iterable (rows ))
24
24
25
25
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
33
31
34
32
35
33
def extract_formula (cells , width , height ):
36
- """Extract the formula hidden in the CSV"""
34
+ """Extracts the formula hidden in the CSV"""
37
35
it = iter (cells )
38
36
formula = [[" " ] * width for _ in range (height )]
39
37
for x in range (width ):
@@ -44,17 +42,14 @@ def extract_formula(cells, width, height):
44
42
45
43
46
44
def apply_formula (cells ):
47
- """Apply the hidden formula on cells"""
45
+ """Applies the hidden formula on cells"""
48
46
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 ])
54
49
55
50
56
51
url = "http://www.pythonchallenge.com/pc/ring/yankeedoodle.html"
57
52
cells = read_csv_cells (url )
58
- factors = obtain_factors (len (cells ))
53
+ factors = factorize (len (cells ))
59
54
print ("\n " .join ("" .join (row ) for row in extract_formula (cells , * factors [::- 1 ])))
60
55
print (bytes (apply_formula (cells )).decode ())
0 commit comments