22import re
33
44def 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
79parser = argparse .ArgumentParser (
810 prog = "custom wc in python" ,
@@ -22,7 +24,7 @@ def format_number(number_array):
2224total_words = 0
2325total_characters = 0
2426
25- numbers_row_array = []
27+ total_numbers_row_array = []
2628
2729for 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
4956if (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