<jupyter_start><jupyter_text>**Exercise 2.2 (optional):** Write code that classifies a given amount of money (which you store in a variable `amount`), specified in cents, as greater monetary units. Your code lists the monetary equivalent in dollars (100 ct), quarters (25 ct), dimes (10 ct), nickels (5 ct), and pennies (1 ct). Your program should report the maximum number of dollars that fit in the amount, then the maximum number of quarters that fit in the remainder after you subtract the dollars, then the maximum number of dimes that fit in the remainder after you subtract the dollars and quarters, and so on for nickels and pennies. The result is that you express the amount as the minimum number of coins needed.  **Can you do it again? Would you change anything from the last time you had to do this?<jupyter_code># Cashier code
amount = 1156

def cashier(amount):
    '''
    Test1:
        input: 1156
        output: {'dollars': 11, 'quarters': 2, 'dimes': 0, 'nickels': 0, 'pennies': 2}
    '''
    cash = dict()
    cash['dollars'] = amount // 100
    cents_reminder = amount % 100
    cash['quarters'] = cents_reminder // 25
    cents_reminder = cents_reminder % 25
    cash['dimes'] = cents_reminder // 10
    cents_reminder = cents_reminder % 10
    cash['nickels'] = cents_reminder // 5
    cents_reminder = cents_reminder % 5
    cash['pennies'] = cents_reminder
    return cash

cashier(1156)<jupyter_output><empty_output><jupyter_text>**Exercise (optional)**: The factorial of a positive integer is that integer, multiplied by all positive integers that are lower (excluding zero). You write the factorial as the number with an exclamation mark after it. E.g., the factorial of 5 is `5! = 5 * 4 * 3 * 2 * 1 = 120`. Write a function that calculates the factorial of its (integer) parameter. Test your function for different parameter values, but do not use very large numbers as factorials grow exponentially. Hint: to do this with a `while` loop, you need at least one more variable.<jupyter_code># Factorial
def factorial(number):                                
    factorial = 1
    while number > 1:
        factorial *= number
        number -= 1
    return factorial

print(factorial(5))<jupyter_output>120<jupyter_text>**Exercise 5.1 (optional):** Write a function that prints a multiplication table for digits 1 to 10. A multiplication table for the numbers 1 to `num = 3` looks as follows:`. |  1  2  3``------------``1 |  1  2  3``2 |  2  4  6``3 |  3  6  9`So the labels on the rows are multiplied by the labels on the columns, and the result is shown in the cell that is on that row/column combination.<jupyter_code># Print multiplication table
def print_multiplication_table(n):
    
    line1 = str(". | ")
    for x in range(1, n+1):
        line1 += str(x) + " "
    
    print (line1)
    print ("---"*n)
    
    for i in range (1, n+1):
        line = str(i) + " | "
        for j in range (1, n+1):
            line += str(i*j) + " "
        
        print(line)
        
print_multiplication_table(3)<jupyter_output>. | 1 2 3 
---------
1 | 1 2 3 
2 | 2 4 6 
3 | 3 6 9<jupyter_text>**Exercise 5.2 (optional):** If you did the previous exercise with a `while` loop, then do it again with a `for` loop. If you did it with a `for` loop, then do it again with a `while` loop. If you did not use a loop at all, you should be ashamed of yourself.<jupyter_code># Print multiplication table
def print_multiplication_table(n):
    
    line1 = str(".    | ")
    x = 1
    while x <= n:
        line1 += str(x) + "\t"
        x += 1
    
    print (line1)
    print ("---------"*n)
    
    i = 1
    while i <= n:
        line = str(i) + "    | " 
        j = 1
        while j <= n:
            line += str(i * j) + "\t"
            j += 1
        i += 1 
        print (line)

print_multiplication_table(5)<jupyter_output>.    | 1	2	3	4	5	
---------------------------------------------
1    | 1	2	3	4	5	
2    | 2	4	6	8	10	
3    | 3	6	9	12	15	
4    | 4	8	12	16	20	
5    | 5	10	15	20	25<jupyter_text>**Exercise 6.1 (optional):** Count how many of each vowel (`a`, `e`, `i`, `o`, `u`) there are in the text string in the next cell, and print the count for each vowel with a single formatted string. Remember that vowels can be both lower and uppercase.<jupyter_code># Counting vowels.
text = """And Saint Attila raised the hand grenade up on high,
saying, "O Lord, bless this thy hand grenade, that with it
thou mayst blow thine enemies to tiny bits, in thy mercy." 
And the Lord did grin. And the people did feast upon the lambs, 
and sloths, and carp, and anchovies, and orangutans, and 
breakfast cereals, and fruit bats, and large chu..."""
def count_vowels(text):
    counta= 0
    counte= 0
    counti= 0
    counto= 0
    countu= 0
    for i in text:
        if i == "a" or i == "A":
            counta += 1
        if i == "e" or i == "E":
            counte += 1
        if i == "i" or i == "I":
            counti += 1
        if i == "o" or i == "O":
            counto += 1
        if i == "u" or i == "U":
            countu += 1
    return "a appears {} times, e appears {} times, i appears {} times, o appears {} times, u appears {} times".format(counta, counte, counti, counto, countu)
    
count_vowels(text)<jupyter_output><empty_output><jupyter_text>**Exercise 6.5 (optional):** Typical autocorrect functions are the following: 1. if a word starts with two capitals, followed by a lower-case letter, the second capital is made lower case; 2. if a sentence contains a word that is immediately followed by the same word, the second occurrence is removed; 3. if a sentence starts with a lower-case letter, that letter is turned into a capital; 4. if a word consists entirely of capitals, except for the first letter which is lower case, then the case of the letters in the word is reversed; and 5. if the sentence contains the name of a day (in English) which does not start with a capital, the first letter is turned into a capital. Write a program that takes a sentence and makes these auto-corrections.<jupyter_code># Autocorrect.
sentence = "as it turned out our chance meeting with REverend aRTHUR BElling was \
was to change our whole way of life, and every sunday we'd hurry along to St lOONY up the Cream BUn and Jam."

def main(text):
    
    # doublewords
    text = doublewords(text)
    # capitals reversed
    text = capital_s(text)
    # capitalize days
    text = days(text)
    
    return text

def upperlower(w):
    if w[0].isupper() and w[1].isupper():
        return w.lower().capitalize()
    return w

def doublewords(text):
    prev_w = ""
    n_l = []
    l = text.lower().split()
    for w in l:
        #calling upperlower here since we already loop through the words of a sentence
        w = upperlower(w)
        if w != prev_w:
            n_l.append(w)
        prev_w = w
    return " ".join(n_l)

def capital_s(s):
    return s.capitalize()

def days(s):
    days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    nl = []
    for word in s.split():
        if word in days:
            nl.append(word.capitalize())
        else:
            nl.append(word)
    return " ".join(nl)
    
if __name__ == "__main__":    
    main(sentence)

main(sentence)<jupyter_output><empty_output><jupyter_text>Exercise 11.3 (Optional)In this directory you find a file `blakepoems.txt`. Write a program that processes the contents of this file, line by line. It creates an output file in the current working directory called `blkpms.txt`, which has the same contents as `blakepoems.txt`, except that all the vowels are removed (case-insensitively). At the end, display how many characters you read, and how many characters you wrote. If you want to check the contents of `blkpms.txt`, you can either open it in a text editor, or display the first 10 lines or so at the end of your program.<jupyter_code>## Joby's solution is more neat than mine, piggybacking on it with a few modifications here!

## Your Code Here
import re 
fp = open("./text-files/blakepoems.txt","r")

def readThelines(f):
    while True:
        buffer = f.readline()             
        if buffer == "":
            break
        processLine(buffer)

def processLine(l):
    line = re.sub('a|e|i|o|u|A|E|I|O|U', '', l)
    createFile(line)
    
def createFile(li):
    new = open("blkpms.txt", "a+")
    new.writelines(li)
    
def characterCount(x):
    y = open("blkpms.txt", "r")
    xd = x.read()
    yd = y.read()
    charCountOrginal = len(xd)
    charCountNew = len(yd)
    print("Read Characters {}, written characters {}".format(charCountOrginal, charCountNew))

readThelines(fp)
characterCount(fp)
def anti_vowel(text):
    for i in "aeiouAEIOU":
        text = text.replace(i,"")
    return text

def main():
    with open("./text-files/blakepoems.txt") as text_file:
        file = open('./text-files/blkpms.txt', 'w+')
        print("Begin.....")
        counter = 0
        for line in text_file:
            cln = anti_vowel(line)
            file.write(cln)
            if counter % 100 == 0:
                print(f"Writting line {counter}.......",)
            counter +=1
                
        print("Finished!")    
        
        file.close()

if __name__ == "__main__":
    main()
    
# Print the contents of dictionary 
#for key in list(d.keys()): 
#    print(key, ":", d[key])<jupyter_output>Begin.....
Writting line 0.......
Writting line 100.......
Writting line 200.......
Writting line 300.......
Writting line 400.......
Writting line 500.......
Writting line 600.......
Writting line 700.......
Writting line 800.......
Writting line 900.......
Writting line 1000.......
Writting line 1100.......
Writting line 1200.......
Writting line 1300.......
Writting line 1400.......
Finished!
{'fraction_non_alphanumeric': 0.11962845769112994, 'fraction_numerical': 0.02521179953046851, 'mean_word_length': 2.581140350877193, 'pattern_counts': {'":': 10, '<': 28, '<?xml version=': 0, '>': 26, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '236270', 'license': 'Apache-2.0', 'n_tokens_mistral': 3265, 'n_tokens_neox': 3059, 'n_words': 1248, 'path': 'M1_Python/d4_Strings_and_files/solutions/Intro to Python - Additional Exercises- Solved.ipynb', 'repo_name': 'succeedme/Strive_Main', 'size': '9797'}