<jupyter_start><jupyter_text>CHAPTER 10 exercises **Exercise 1: Revise a previous program as follows: Read and parse the “From”lines and pull out the addresses from the line. Count the number of messages fromeach person using a dictionary.After all the data has been read, print the person with the most commits bycreating a list of (count, email) tuples from the dictionary. Then sort the list inreverse order and print out the person who has the most commits.**Sample Line:From <EMAIL> Sat Jan 5 09:14:16 2008Enter a file name: mbox-<EMAIL> 5Enter a file name: <EMAIL> 195<jupyter_code>fname = input('Enter a file name:')
fhand = open(fname) 
counts = dict()
tmp = list()

for line in fhand:
    words = line.split()
    if len(words) < 3 or  words[0] != 'From' : continue
    emails = words[1]
    emails = emails.split()
    for email in emails :
        if email not in counts: 
            counts[email] = 1
        else:
            counts[email] +=1
            
for k, v in counts.items() :
    newtup = (v, k)
    tmp.append(newtup)

tmp = sorted(tmp, reverse=True)
for v, k in tmp[:1]:
    print(k, v)
fname = input('Enter a file name:')
fhand = open(fname) 
counts = dict()
tmp = list()

for line in fhand:
    words = line.split()
    if len(words) < 3 or  words[0] != 'From' : continue
    emails = words[1]
    emails = emails.split()
    for email in emails :
        if email not in counts: 
            counts[email] = 1
        else:
            counts[email] +=1
            
for k, v in counts.items() :
    newtup = (v, k)
    tmp.append(newtup)

tmp = sorted(tmp, reverse=True)
for v, k in tmp[:1]:
    print(k, v)<jupyter_output>Enter a file name:mbox.txt
<EMAIL> 195<jupyter_text>**Exercise 2: This program counts the distribution of the hour of the day for eachof the messages. You can pull the hour from the “From” line by finding the timestring and then splitting that string into parts using the colon character. Onceyou have accumulated the counts for each hour, print out the counts, one per line,sorted by hour as shown below.**Sample Execution:python timeofday.pyEnter a file name: mbox-short.txt04 306 107 109 210 311 614 115 216 417 218 119 1<jupyter_code>fname = input('Enter a file name:')
fhand = open(fname)
counts = dict()
tmp = list()

for line in fhand:
    words = line.split()
    if len(words) < 3 or  words[0] != 'From' : continue
    time = words[5]
    chunks = time.split(':')
    hours = chunks[0]
    hours = hours.split()
    for hour in hours :
        if hour not in counts: 
            counts[hour] = 1
        else:
            counts[hour] +=1
s = sorted(counts.items())

for k, v in s:
    print(k, v)
fname = input('Enter a file name:')
fhand = open(fname)
counts = dict()
tmp = list()

for line in fhand:
    words = line.split()
    if len(words) < 3 or  words[0] != 'From' : continue
    time = words[5]
    chunks = time.split(':')
    hours = chunks[0]
    hours = hours.split()
    for hour in hours :
        if hour not in counts: 
            counts[hour] = 1
        else:
            counts[hour] +=1
s = sorted(counts.items())

for k, v in s:
    print(k, v)<jupyter_output>Enter a file name:mbox.txt
00 23
01 10
02 11
03 19
04 23
05 10
06 45
07 42
08 72
09 166
10 198
11 154
12 108
13 114
14 150
15 152
16 181
17 97
18 58
19 46
20 36
21 36
22 28
23 18<jupyter_text>**Exercise 3: Write a program that reads a file and prints the letters in decreasingorder of frequency. Your program should convert all the input to lower case andonly count the letters a-z. Your program should not count spaces, digits, punctuation,or anything other than the letters a-z. Find text samples from several differentlanguages and see how letter frequency varies between languages. Compare yourresults with the tables at wikipedia.org/wiki/Letter_frequencies.**<jupyter_code>import string
fname = input('Enter the file name: ')
try:
    fhand = open(fname, encoding='utf8')
except:
    print('File cannot be opened:', fname)
    exit()
counts = dict()

for line in fhand:
    line = line.translate(str.maketrans('', '', string.punctuation))
    line = line.lower()
    words = line.split()
    for x in words:
        word = x.split()
        word = word [0]
        for y in range (0 , len(word)):
            letter = word[y]
            if letter.isalpha() == True:
                counts [letter] = counts.get(letter,0) + 1

lst = list()
for ltr, count in list(counts.items()):
    lst.append((count, ltr))
    
lst.sort(reverse=True)

for count, ltr in lst:
    print(count, ltr)

#sanity check : how many letters
print (len(lst))<jupyter_output>Enter the file name: mbox-short.txt
5436 e
5223 a
4494 i
4174 o
4064 r
4050 t
3738 s
3123 u
3088 c
2575 n
2497 p
2436 m
2004 d
1832 l
1392 h
1257 f
1167 k
1134 b
1027 g
997 v
959 j
643 y
586 w
482 x
78 z
57 q
26
{'fraction_non_alphanumeric': 0.10058187863674148, 'fraction_numerical': 0.060058187863674145, 'mean_word_length': 2.9289795918367347, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '479915', 'license': 'MIT', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1595, 'n_words': 656, 'path': 'CHAPTER 10 exercises.ipynb', 'repo_name': 'AbhishekMali21/PYTHON-FOR-EVERYBODY', 'size': '4871'}