-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount
68 lines (50 loc) · 1.1 KB
/
count
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/python3
import sys
import argparse
# input to file
input_data = []
input_data = sys.stdin.read()
#
#################
### FUNCTIONS ###
def P_(text):
sys.stdout.write(text)
def E_(text):
sys.stderr.write(text)
sys.stderr.flush()
def letters_():
arr_letters = []
data = input_data
for letter in data:
if letter != "\n":
arr_letters.append(letter)
return arr_letters
def words_():
data = input_data
data = data.split()
return data
def count_(func):
data = func
iter = 0
for i in data:
if i != " ":
iter += 1
return iter
## condences function calls
numletters = count_(letters_())
numwords = count_(words_())
# parsing arguments
parser = argparse.ArgumentParser(description='COUNT: counts input based on arguemnts')
parser.add_argument('-c', type=str, help='counts piped input based on "words" or "letters"')
# parse
args = parser.parse_args()
def arguments(count_):
if count_ == "letters":
print(numletters)
elif count_ == "words":
print(numwords)
else:
E_("Error: in arguements(), issue with parsing args")
################## end FUNCTIONS
### MAIN ###
arguments(args.c)