Skip to content

Commit ba90a96

Browse files
Add files via upload
1 parent 53b8274 commit ba90a96

File tree

4 files changed

+1209
-0
lines changed

4 files changed

+1209
-0
lines changed
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#The basic outline of this problem is to read the file, look for integers using
2+
#the re.findall(), looking for a regular expression of '[0-9]+' and then
3+
#converting the extracted strings to integers and summing up the integers.
4+
import re
5+
intl=list()
6+
7+
fname=input("Please enter the file name: ")
8+
if len(fname)<1:#for ease of testing
9+
fname="regex_sum_42.txt"
10+
11+
#using a regex [0-9]+ isolates numbers in a line
12+
fhandle=open(fname)
13+
for lines in fhandle:
14+
if re.search('[0-9]+',lines):
15+
nums=re.findall('[0-9]+',lines)
16+
for num in nums:
17+
i=int(num)
18+
intl.append(num)
19+
20+
print(intl)
21+
print(sum(intl))

0 commit comments

Comments
 (0)