Skip to content

Commit 3d0b1bf

Browse files
committed
completed logic for wc in python
1 parent 11e8232 commit 3d0b1bf

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

implement-shell-tools/wc/custom_wc.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import re
33

44
def format_number(number_array):
5-
return [str(number).rjust(4) for number in number_array]
5+
space_array = [3, 4, 4]
6+
result = map(lambda number, space: str(number).rjust(space), number_array, space_array)
7+
return "".join(list(result))
68

79
parser = argparse.ArgumentParser(
810
prog="custom wc in python",
@@ -22,7 +24,7 @@ def format_number(number_array):
2224
total_words = 0
2325
total_characters = 0
2426

25-
numbers_row_array = []
27+
total_numbers_row_array = []
2628

2729
for file_path in files_array:
2830
with open(file_path) as file:
@@ -31,20 +33,34 @@ def format_number(number_array):
3133
words = len(re.findall(r"\S+", context))
3234
characters = len(context)
3335

36+
numbers_row_array = []
3437
if (args.lines):
3538
numbers_row_array.append(lines)
3639
if (args.words):
3740
numbers_row_array.append(words)
3841
if (args.characters):
3942
numbers_row_array.append(characters)
43+
44+
if (len(numbers_row_array) == 1 and len(files_array) == 1):
45+
print(f'{numbers_row_array[0]} {file_path}')
4046

41-
if (len(numbers_row_array) > 0):
42-
print(numbers_row_array)
47+
elif (len(numbers_row_array) > 0):
48+
print(f'{format_number(numbers_row_array)} {file_path}')
4349
else:
44-
print(lines, words, characters, file_path)
50+
print(format_number([lines, words, characters]), file_path)
51+
4552
total_lines += lines
4653
total_words += words
4754
total_characters += characters
4855

4956
if (len(files_array) > 1):
50-
print(total_lines, total_words, total_characters, 'total')
57+
if (args.lines):
58+
total_numbers_row_array.append(total_lines)
59+
if (args.words):
60+
total_numbers_row_array.append(total_words)
61+
if (args.characters):
62+
total_numbers_row_array.append(total_characters)
63+
if (len(total_numbers_row_array) > 0):
64+
print(f'{format_number(total_numbers_row_array)} total')
65+
else:
66+
print(format_number([total_lines, total_words, total_characters]), 'total')

0 commit comments

Comments
 (0)