csev/py4e

Typo on Chapter 11

ck39 opened this issue · 2 comments

ck39 commented

Exercise 2 at the bottom of chapter 11 Regex (https://www.py4e.com/html3/11-regex)
The solution to the average for 'mbox-txt':

Enter file:mbox.txt
38444.0323119

I think the answer should be 38549.7949721 ?

csev commented

Thanks - I reworded to ask for an integer and fixed the numbers. It takes 30 minutes for the online versions to update.

I suppose the answer will be 38444 instead of 38549.

import re

fh = open("mbox.txt")
count = 0
total = 0
for line in fh:
    line = line.rstrip()
    lst = re.findall('[A-Za-z]+ [A-Za-z]+: ([0-9]+)', line)
    if len(lst) > 0:
        for x in lst:
            total += int(x)
            count += 1

print(total // count)

I used this code to fetch the result.
For mbox-short.txt the result is correct as mentioned i.e. 39756
while for mbox.txt its 38444 instead of 38549.

I would be happy if you could correct me if I'm wrong. :)
@csev @ck39