<jupyter_start><jupyter_text>Week 3 Lab ExercisesBlank notebook to be used for class exercises.**Name**: <NAME>  Exercise 1Read the file “mbox.txt” line-by-line and calculate and print the following:- The total number of lines in the file.- The number of lines that contain the substring “From:”<jupyter_code>my_file = open('mbox.txt')

total = 0
total_from = 0 
for line in my_file: 
    total += 1
    if 'From:' in line:
        total_from += 1

my_file.close()

print(total)
print(total_from)
oFile = open('file.txt', 'w')
oFile.write('1')
oFile.write('2')
oFile.write('3')
oFile.close()

tmp = 0
iFile = open('file.txt')
for line in iFile:
    tmp += 1
iFile.close()<jupyter_output><empty_output><jupyter_text>Exercise 2Write code that reads the file line-by-line numbers.txt, then does thefollowing:- Sum all the numbers in numbers.txt, then prints the numbers to thescreen.Next, append the string "SUM: k", wherekis the calculated sum. to the end of numbers.txt as a new line.Finally, print the entire file to make sure you appended to the file correctly.The absolute path for the file is "../data/numbers.txt"**Important Python concepts:** for, open ('a' and 'r'), write(), print()<jupyter_code># If you mess up the file and need to try again, run this cell again to recreate the file
with open('./numbers.txt', 'w') as out_file:
    out_file.write('42\n18\n22\n18')
    
with open('numbers.txt', 'r') as in_file:
    for row in in_file:
        print(row.strip())
file_sum = 0 

my_file = open('./numbers.txt', 'r')
for line in my_file:
    number = float(line.strip())
    print(line.strip())
    file_sum += number
my_file.close()
file_sum
file_sum = '\nSUM:' + str(file_sum)
my_file = open('./numbers.txt', 'a')

my_file.write(file_sum)

my_file.close()<jupyter_output><empty_output><jupyter_text>Run the line below to check your work<jupyter_code>with open('numbers.txt', 'r') as in_file:
    for row in in_file:
        print(row.strip())<jupyter_output>42
18
22
18
SUM:100.0<jupyter_text>Exercise 3Write a function to calculate your pay given two arguments: hoursWorks and dollarsPerHour. The function should return how much you should be paid. When calculating the final amount, give the employee 1.5 times the hourly rate for hours worked above 40 hours.<jupyter_code># Test arguments
hours = 45 # You worked for 45 hours
pay = 10 # You make 10 dollars and hour
def pay(hoursWorks, dollarsPerHour):
    if hoursWorks <= 40: 
        total = hoursWorks * dollarsPerHour
    else: 
        total = 40 * dollarsPerHour + 1.5*(hoursWorks-40)*dollarsPerHour
    return total
myOutput = pay(41,10)
print(myOutput)<jupyter_output>415.0<jupyter_text>Exercise 4Create Asserts to test your code from Exercise 2. Specifically, test the following test cases:- Check whether the functions outputs 20 with inputs hoursWorks = 2 and dollarsPerHour = 10.- Check whether the function does **not** output 410 if hoursWorks = 41 and dollarsPerHour = 10.After all cases are passed successfully, print "Asserts Completed Successfully".<jupyter_code>assert(pay(2, 10) == 20)
assert(pay(41,10) != 410)
assert(pay(41,10) == 415)

print("Asserts Completed Successfully")<jupyter_output>Asserts Completed Successfully<jupyter_text>Exercise 5Write code to count the number of times a "risk" word appears from the "risk_lexicon" in the string variable named "text." Please ignore case (i.e., you should lowercase everything). Lexicon-based analysis is popular because it quantifies various aspects of language.The output of your code should look like the following:```Risk Count: 2```**HINT:** This exercise will require the use of a **for loop**, an **if statement**, and the string operations **.lower() and .split(" ")**.<jupyter_code>risk_lexicon = set(['danger', 'concern', 'risk', 'risky', 'doubt'])
text = "CocaCola is a risky RISKY investment !!!!"

counter = 0 
for word in text.split(" "):
    if word.lower() in risk_lexicon:
        counter += 1

print("Risk Count: {}".format(counter))<jupyter_output>Risk Count: 2
{'fraction_non_alphanumeric': 0.11429282868525896, 'fraction_numerical': 0.0249003984063745, 'mean_word_length': 4.649789029535865, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '310034', 'license': 'MIT', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1252, 'n_words': 469, 'path': 'Exercises/Week-3-Lab.ipynb', 'repo_name': 'ashleyncortez/Data-Foundations', 'size': '4023'}