Skip to content

Commit 23143f7

Browse files
committed
refactoring wc.py file
1 parent e1b4425 commit 23143f7

1 file changed

Lines changed: 22 additions & 26 deletions

File tree

  • implement-shell-tools/wc

‎implement-shell-tools/wc/wc.py‎

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88

99
args = parser.parse_args()
1010

11+
12+
def format_counts(lines, words, chars):
13+
if not args.l and not args.w and not args.c:
14+
return f"{lines:8}{words:8}{chars:8}"
15+
16+
output = ""
17+
18+
if args.l:
19+
output += f"{lines:8}"
20+
if args.w:
21+
output += f"{words:8}"
22+
if args.c:
23+
output += f"{chars:8}"
24+
25+
return output
26+
27+
1128
total_lines = 0
1229
total_words = 0
1330
total_chars = 0
@@ -27,32 +44,11 @@
2744
total_words += words
2845
total_chars += chars
2946

30-
output = ""
31-
32-
if not args.l and not args.w and not args.c:
33-
output += f"{lines:8}{words:8}{chars:8}"
34-
else:
35-
if args.l:
36-
output += f"{lines:8}"
37-
if args.w:
38-
output += f"{words:8}"
39-
if args.c:
40-
output += f"{chars:8}"
41-
42-
print(output, file)
47+
print(format_counts(lines, words, chars), file)
4348

4449

4550
if len(args.files) > 1:
46-
output = ""
47-
48-
if not args.l and not args.w and not args.c:
49-
output += f"{total_lines:8}{total_words:8}{total_chars:8}"
50-
else:
51-
if args.l:
52-
output += f"{total_lines:8}"
53-
if args.w:
54-
output += f"{total_words:8}"
55-
if args.c:
56-
output += f"{total_chars:8}"
57-
58-
print(output, "total")
51+
print(
52+
format_counts(total_lines, total_words, total_chars),
53+
"total"
54+
)

0 commit comments

Comments
 (0)