Skip to content

Commit 1fa2965

Browse files
bite 54
1 parent 9c39f3b commit 1fa2965

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
/149/README.md
2828
/136/README.md
2929
/15/README.md
30+
/54/README.md

Diff for: 54/poem.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import textwrap
2+
3+
INDENTS = 4
4+
poem = """
5+
Remember me when I am gone away,
6+
Gone far away into the silent land;
7+
When you can no more hold me by the hand,
8+
9+
Nor I half turn to go yet turning stay.
10+
11+
Remember me when no more day by day
12+
You tell me of our future that you planned:
13+
Only remember me; you understand
14+
"""
15+
16+
17+
def print_hanging_indents(poem):
18+
lines = textwrap.dedent(poem).splitlines()
19+
count = 0
20+
for line in lines:
21+
if len(line) == 0:
22+
count = 1
23+
if count == 0:
24+
line = textwrap.indent(line, prefix=' '*INDENTS)
25+
if len(line) != 0:
26+
count = 0
27+
# print(count)
28+
if len(line) != 0:
29+
print(line)
30+
31+
32+
print_hanging_indents(poem)

Diff for: 54/test_poem.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from poem import print_hanging_indents
2+
3+
# part of William Shakespeare's play Hamlet
4+
shakespeare_unformatted = """
5+
To be, or not to be, that is the question:
6+
Whether 'tis nobler in the mind to suffer
7+
8+
The slings and arrows of outrageous fortune,
9+
Or to take Arms against a Sea of troubles,
10+
"""
11+
12+
shakespeare_formatted = """
13+
To be, or not to be, that is the question:
14+
Whether 'tis nobler in the mind to suffer
15+
The slings and arrows of outrageous fortune,
16+
Or to take Arms against a Sea of troubles,
17+
"""
18+
19+
# part of Remember, by Christina Rosetti
20+
rosetti_unformatted = """
21+
Remember me when I am gone away,
22+
Gone far away into the silent land;
23+
When you can no more hold me by the hand,
24+
25+
Nor I half turn to go yet turning stay.
26+
27+
Remember me when no more day by day
28+
You tell me of our future that you planned:
29+
Only remember me; you understand
30+
"""
31+
32+
33+
rosetti_formatted = """
34+
Remember me when I am gone away,
35+
Gone far away into the silent land;
36+
When you can no more hold me by the hand,
37+
Nor I half turn to go yet turning stay.
38+
Remember me when no more day by day
39+
You tell me of our future that you planned:
40+
Only remember me; you understand
41+
"""
42+
43+
44+
def test_shakespeare_text(capfd):
45+
print_hanging_indents(shakespeare_unformatted)
46+
output = capfd.readouterr()[0]
47+
assert output.strip() == shakespeare_formatted.strip()
48+
49+
50+
def test_rosetti_poem(capfd):
51+
print_hanging_indents(rosetti_unformatted)
52+
output = capfd.readouterr()[0]
53+
assert output.strip() == rosetti_formatted.strip()

0 commit comments

Comments
 (0)