Skip to content

Commit 15ecbcc

Browse files
committed
Problem 13 solution
1 parent 80e54c9 commit 15ecbcc

File tree

3 files changed

+299
-30
lines changed

3 files changed

+299
-30
lines changed

13_twelve_days/song.txt

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
On the first day of Christmas,
2+
My true love gave to me,
3+
A partridge in a pear tree.
4+
5+
On the second day of Christmas,
6+
My true love gave to me,
7+
Two turtle doves,
8+
And a partridge in a pear tree.
9+
10+
On the third day of Christmas,
11+
My true love gave to me,
12+
Three French hens,
13+
Two turtle doves,
14+
And a partridge in a pear tree.
15+
16+
On the fourth day of Christmas,
17+
My true love gave to me,
18+
Four calling birds,
19+
Three French hens,
20+
Two turtle doves,
21+
And a partridge in a pear tree.
22+
23+
On the fifth day of Christmas,
24+
My true love gave to me,
25+
Five gold rings,
26+
Four calling birds,
27+
Three French hens,
28+
Two turtle doves,
29+
And a partridge in a pear tree.
30+
31+
On the sixth day of Christmas,
32+
My true love gave to me,
33+
Six geese a laying,
34+
Five gold rings,
35+
Four calling birds,
36+
Three French hens,
37+
Two turtle doves,
38+
And a partridge in a pear tree.
39+
40+
On the seventh day of Christmas,
41+
My true love gave to me,
42+
Seven swans a swimming,
43+
Six geese a laying,
44+
Five gold rings,
45+
Four calling birds,
46+
Three French hens,
47+
Two turtle doves,
48+
And a partridge in a pear tree.
49+
50+
On the eighth day of Christmas,
51+
My true love gave to me,
52+
Eight maids a milking,
53+
Seven swans a swimming,
54+
Six geese a laying,
55+
Five gold rings,
56+
Four calling birds,
57+
Three French hens,
58+
Two turtle doves,
59+
And a partridge in a pear tree.
60+
61+
On the ninth day of Christmas,
62+
My true love gave to me,
63+
Nine ladies dancing,
64+
Eight maids a milking,
65+
Seven swans a swimming,
66+
Six geese a laying,
67+
Five gold rings,
68+
Four calling birds,
69+
Three French hens,
70+
Two turtle doves,
71+
And a partridge in a pear tree.
72+
73+
On the tenth day of Christmas,
74+
My true love gave to me,
75+
Ten lords a leaping,
76+
Nine ladies dancing,
77+
Eight maids a milking,
78+
Seven swans a swimming,
79+
Six geese a laying,
80+
Five gold rings,
81+
Four calling birds,
82+
Three French hens,
83+
Two turtle doves,
84+
And a partridge in a pear tree.
85+
86+
On the eleventh day of Christmas,
87+
My true love gave to me,
88+
Eleven pipers piping,
89+
Ten lords a leaping,
90+
Nine ladies dancing,
91+
Eight maids a milking,
92+
Seven swans a swimming,
93+
Six geese a laying,
94+
Five gold rings,
95+
Four calling birds,
96+
Three French hens,
97+
Two turtle doves,
98+
And a partridge in a pear tree.
99+
100+
On the twelfth day of Christmas,
101+
My true love gave to me,
102+
Twelve drummers drumming,
103+
Eleven pipers piping,
104+
Ten lords a leaping,
105+
Nine ladies dancing,
106+
Eight maids a milking,
107+
Seven swans a swimming,
108+
Six geese a laying,
109+
Five gold rings,
110+
Four calling birds,
111+
Three French hens,
112+
Two turtle doves,
113+
And a partridge in a pear tree.

13_twelve_days/test.py

+35-30
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@
77
import string
88
from subprocess import getstatusoutput, getoutput
99

10-
prg = './twelve_days.py'
11-
day_one = '\n'.join([
12-
'On the first day of Christmas,', 'My true love gave to me,',
13-
'A partridge in a pear tree.'
14-
])
15-
16-
day_two = '\n'.join([
17-
'On the second day of Christmas,',
18-
'My true love gave to me,',
19-
'Two turtle doves,',
20-
'And a partridge in a pear tree.'
21-
])
10+
PRG = "./twelve_days.py"
11+
DAY_ONE = "\n".join(
12+
[
13+
"On the first day of Christmas,",
14+
"My true love gave to me,",
15+
"A partridge in a pear tree.",
16+
]
17+
)
18+
19+
DAY_TWO = "\n".join(
20+
[
21+
"On the second day of Christmas,",
22+
"My true love gave to me,",
23+
"Two turtle doves,",
24+
"And a partridge in a pear tree.",
25+
]
26+
)
2227

2328

2429
# --------------------------------------------------
2530
def test_exists():
2631
"""exists"""
2732

28-
assert os.path.isfile(prg)
33+
assert os.path.isfile(PRG)
2934

3035

3136
# --------------------------------------------------
3237
def test_usage():
3338
"""usage"""
3439

35-
for flag in ['-h', '--help']:
36-
rv, out = getstatusoutput(f'{prg} {flag}')
40+
for flag in ["-h", "--help"]:
41+
rv, out = getstatusoutput(f"python {PRG} {flag}")
3742
assert rv == 0
3843
assert re.match("usage", out, re.IGNORECASE)
3944

@@ -43,7 +48,7 @@ def test_bad_num():
4348
"""test bad_num"""
4449

4550
for n in [random.choice(r) for r in (range(-10, -1), range(13, 20))]:
46-
rv, out = getstatusoutput(f'{prg} -n {n}')
51+
rv, out = getstatusoutput(f"python {PRG} -n {n}")
4752
assert rv != 0
4853
assert re.search(f'--num "{n}" must be between 1 and 12', out)
4954

@@ -52,43 +57,43 @@ def test_bad_num():
5257
def test_one():
5358
"""test one"""
5459

55-
out = getoutput(f'{prg} -n 1')
56-
assert out.rstrip() == day_one
60+
out = getoutput(f"python {PRG} -n 1")
61+
assert out.rstrip() == DAY_ONE
5762

5863

5964
# --------------------------------------------------
6065
def test_two():
6166
"""test two"""
6267

63-
out = getoutput(f'{prg} --num 2')
64-
assert out == '\n\n'.join([day_one, day_two])
68+
out = getoutput(f"python {PRG} --num 2")
69+
assert out == "\n\n".join([DAY_ONE, DAY_TWO])
6570

6671

6772
# --------------------------------------------------
6873
def test_all_stdout():
6974
"""test"""
7075

71-
out = getoutput(f'{prg}').splitlines()
76+
out = getoutput(f"python {PRG}").splitlines()
7277
assert len(out) == 113
73-
assert out[0] == 'On the first day of Christmas,'
74-
assert out[-1] == 'And a partridge in a pear tree.'
78+
assert out[0] == "On the first day of Christmas,"
79+
assert out[-1] == "And a partridge in a pear tree."
7580

7681

7782
# --------------------------------------------------
7883
def test_all():
7984
"""Test 1-12"""
8085

81-
test_out = './test-out'
86+
test_out = "./test-out"
8287
assert os.path.isdir(test_out)
8388

8489
for n in range(1, 13):
8590
print(n)
8691
# Normal run (STDOUT)
87-
expected_file = os.path.join(test_out, f'{n}.out')
92+
expected_file = os.path.join(test_out, f"{n}.out")
8893
assert os.path.isfile(expected_file)
8994
expected = open(expected_file).read().rstrip()
9095

91-
cmd = f'{prg} -n {n}'
96+
cmd = f"python {PRG} -n {n}"
9297
out = getoutput(cmd).rstrip()
9398
assert out == expected
9499

@@ -98,11 +103,11 @@ def test_all():
98103
os.remove(out_file)
99104

100105
try:
101-
out = getoutput(cmd + f' -o {out_file}').rstrip()
102-
assert out == ''
106+
out = getoutput(cmd + f" -o {out_file}").rstrip()
107+
assert out == ""
103108
assert os.path.isfile(out_file)
104109
output = open(out_file).read().rstrip()
105-
assert len(output.split('\n')) == len(expected.split('\n'))
110+
assert len(output.split("\n")) == len(expected.split("\n"))
106111
assert output.rstrip() == expected.rstrip()
107112
finally:
108113
if os.path.isfile(out_file):
@@ -114,4 +119,4 @@ def random_string():
114119
"""generate a random string"""
115120

116121
k = random.randint(5, 10)
117-
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
122+
return "".join(random.choices(string.ascii_letters + string.digits, k=k))

0 commit comments

Comments
 (0)