-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11-turnin.py
43 lines (35 loc) · 1.24 KB
/
ex11-turnin.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
# This version is stripped of colors, emojis, and any extra stuff that makes the code more challenging to grade for instructors
# importing re
import re
# opening file
filename = 'regex_sum_1136998.txt'
filetxt = open(filename)
# declarations
regex = '[0-9]+'
sumInts = 0
countInts = 0
integers = []
newIntList = []
newIntItemStr = ''
# stripping whitespace and finding integers in each line of the file
for line in filetxt :
line = line.rstrip()
integers.append(re.findall(regex, line))
# removing empty list items in the file line lists (lines that had no integers)
onlyInts = [listItems for listItems in integers if listItems]
# stringifying everything (there are currently list items within the larger list)
for eachItem in onlyInts :
for insideEach in eachItem :
newIntItemStr = newIntItemStr + ' ' + insideEach
newIntList = newIntItemStr
# splitting separate items into a single list based on spaces
numList = newIntList.split()
# getting final numbers from final integer list
for numItems in numList :
# list item count
countInts += 1
# list items sum
sumInts += int(numItems)
# final print outputs
print('The total integer count is ' + str(countInts))
print('The sum of all integers is ' + str(sumInts))