-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_in_sum.py
57 lines (43 loc) · 1.33 KB
/
std_in_sum.py
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
"""
This script is used to read input from the standard input.
This input are integer separated by space.
There might multiple lines of input.
The resulting output is the sum of all the integers.
"""
from sys import stdin, stdout, stderr
def read_lines() -> list:
"""
Read lines from the standard input
:return: list of lines read from the standard input.
"""
return stdin.read().splitlines()
def split_lines(lines: list) -> list:
"""
Split the lines into literals
:param lines: A list of lines
:return: A list of literals
"""
return [line.split() for line in lines]
def cast_to_integers(literals: list) -> list:
"""
Cast the literals to integers
:param literals: A list of of list of literals
:return: A list of sums, one by line
"""
return [sum([int(l) for l in literal]) for literal in literals]
def main() -> None:
"""
Main function to read the input from the standard input and print the sum of the integers
"""
lines = read_lines()
literals = split_lines(lines)
integers = cast_to_integers(literals)
# print(sum(integers), file=stdout)
stdout.write(str(sum(integers)) + "\n")
if __name__ == "__main__":
try:
main()
except Exception as e:
# print(e, file=stderr)
stderr.write(e)
exit(1)