Archived
1
0
Fork 0

inital commit

This commit is contained in:
Michael Loveys 2021-09-08 10:33:48 -05:00
commit bb326fbd0a
31 changed files with 4172 additions and 0 deletions

17
Assignment 10.2/main.py Normal file
View file

@ -0,0 +1,17 @@
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
emailhour = dict()
for line in handle:
line = line.strip().split()
if len(line) > 0:
if line[0] == "From":
hour = line[5].split(":")[0]
emailhour[hour] = emailhour.get(hour, 0) + 1
for hour,numberofemails in sorted(emailhour.items()):
print(hour,numberofemails)

22
Assignment 10.2/readme.md Normal file
View file

@ -0,0 +1,22 @@
10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
```From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008```
Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.
Desired Output
```
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
```

2
Assignment 2.2/main.py Normal file
View file

@ -0,0 +1,2 @@
name = input("Enter your name")
print("Hello", name)

3
Assignment 2.2/readme.md Normal file
View file

@ -0,0 +1,3 @@
Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. Enter **Sarah** in the pop-up box when you are prompted so your output will match the desired output.
Desired Output: ``Hello Sarah``

6
Assignment 2.3/main.py Normal file
View file

@ -0,0 +1,6 @@
hours = input("Hours Worked: ")
payrate = input("Hourly Pay Rate: ")
pay = float(hours) * float(payrate)
print("Pay:", pay)

1
Assignment 2.3/readme.md Normal file
View file

@ -0,0 +1 @@
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.

15
Assignment 3.1/main.py Normal file
View file

@ -0,0 +1,15 @@
hours_worked = input("Enter Hours Worked: ")
hours_worked = float(hours_worked)
hourly_rate = input("Enter Hourly Rate: ")
hourly_rate = float(hourly_rate)
total_pay = 0;
if hours_worked > 40:
overtime_hours = hours_worked - 40
total_pay = (hourly_rate * 40.0) + (overtime_hours * (hourly_rate * 1.5))
elif hours_worked < 40:
total_pay = (hourly_rate * hours_worked)
print(total_pay)

1
Assignment 3.1/readme.md Normal file
View file

@ -0,0 +1 @@
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use **45 hours** and a **rate of 10.50 per hour** to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.

17
Assignment 3.3/main.py Normal file
View file

@ -0,0 +1,17 @@
score = input("Enter Score: ")
try:
score = float(score)
except:
score = 0
if score >= 0.9:
print("A")
elif score >= 0.8:
print("B")
elif score >= 0.7:
print("C")
elif score >= 0.6:
print("D")
else:
print("F")

12
Assignment 3.3/readme.md Normal file
View file

@ -0,0 +1,12 @@
Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
```
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.
```

25
Assignment 4.6/main.py Normal file
View file

@ -0,0 +1,25 @@
def computepay (hours, rate):
try:
hours = float(hours)
rate = float(rate)
except:
print("Invalid hours or rate received")
return 0;
totalpay = 0
if hours > 40:
# Standard Pay for first 40 hours
totalpay = (40 * rate)
# Total Pay is added to Overtime Pay for everything over the first 40 at 1.5x the rate
totalpay = totalpay + ((hours - 40) * (rate * 1.50))
else:
totalpay = hours * rate;
return totalpay
workedhours = input("Enter Hours Worked: ")
hourlyrate = input("Enter Hourly Rate: ")
print("Pay", computepay(workedhours,hourlyrate))

8
Assignment 4.6/readme.md Normal file
View file

@ -0,0 +1,8 @@
Write a program to prompt the user for hours and rate per hour using input to compute gross pay.
Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours.
Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value.
Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75).
You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.

32
Assignment 5.2/main.py Normal file
View file

@ -0,0 +1,32 @@
smallest = None
largest = None
# Start Loop until done is received and both smallest and largest have values
while True:
number = input("Please enter a number: ")
try:
number = int(number)
except:
if number == "done":
if smallest is None or largest is None:
print("Not Enough Information to determine smallest and largest")
else:
print("Maximum is", largest)
print("Minimum is", smallest)
break;
else:
print("Invalid input")
continue
if smallest is None:
smallest = number
elif number < smallest:
smallest = number
if largest is None:
largest = number
elif number > largest:
largest = number

8
Assignment 5.2/readme.md Normal file
View file

@ -0,0 +1,8 @@
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
Desired Output:
```
Invalid input
Maximum is 10
Minimum is 2
```

7
Assignment 6.5/main.py Normal file
View file

@ -0,0 +1,7 @@
text = "X-DSPAM-Confidence: 0.8475"
confidence = text.find("0")
confidence = float(text[confidence:])
print(confidence)

0
Assignment 6.5/readme.md Normal file
View file

8
Assignment 7.1/main.py Normal file
View file

@ -0,0 +1,8 @@
# If accepting input to the file name:
#filename = input("Please enter the file name to read: ")
filename = "words.txt"
filehandle = open (filename, 'r')
for line in filehandle:
print(line.rstrip().upper())

1
Assignment 7.1/readme.md Normal file
View file

@ -0,0 +1 @@
7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below.

24
Assignment 7.1/words.txt Normal file
View file

@ -0,0 +1,24 @@
Writing programs or programming is a very creative
and rewarding activity You can write programs for
many reasons ranging from making your living to solving
a difficult data analysis problem to having fun to helping
someone else solve a problem This book assumes that
{\em everyone} needs to know how to program and that once
you know how to program, you will figure out what you want
to do with your newfound skills
We are surrounded in our daily lives with computers ranging
from laptops to cell phones We can think of these computers
as our personal assistants who can take care of many things
on our behalf The hardware in our current-day computers
is essentially built to continuously ask us the question
What would you like me to do next
Our computers are fast and have vasts amounts of memory and
could be very helpful to us if we only knew the language to
speak to explain to the computer what we would like it to
do next If we knew this language we could tell the
computer to do tasks on our behalf that were reptitive
Interestingly, the kinds of things computers can do best
are often the kinds of things that we humans find boring
and mind-numbing

18
Assignment 7.2/main.py Normal file
View file

@ -0,0 +1,18 @@
# Use the file name mbox-short.txt as the file name
#fn = input("File Name: ")
fn = "mbox-short.txt"
fh = open(fn)
count = 0
confidence = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
score = line.find("0")
score = float(line[score:])
count = count + 1
confidence = confidence + score
print ("Average spam confidence:", confidence / count)

12
Assignment 7.2/readme.md Normal file
View file

@ -0,0 +1,12 @@
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
```
X-DSPAM-Confidence: 0.8475
```
Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.
Desired Output:
```
Average spam confidence: 0.7507185185185187
```

12
Assignment 8.4/main.py Normal file
View file

@ -0,0 +1,12 @@
fh = open("romeo.txt")
words = []
for line in fh:
for word in line.rstrip().split():
if not word in words:
words.append(word)
words.sort()
print(words)

6
Assignment 8.4/readme.md Normal file
View file

@ -0,0 +1,6 @@
Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.
Desired Output:
```
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
```

4
Assignment 8.4/romeo.txt Normal file
View file

@ -0,0 +1,4 @@
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

23
Assignment 8.5/main.py Normal file
View file

@ -0,0 +1,23 @@
fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
line = line.split()
# Check Length, Count from and print email
if len(line) > 0:
if line[0] == "From":
count = count + 1
print(line[1])
# Iterate over words in the line and if from print email
# for word in line:
# if word == "From":
# count = count + 1
# print(line[1])
print("There were", count, "lines in the file with From as the first word")

File diff suppressed because it is too large Load diff

39
Assignment 8.5/readme.md Normal file
View file

@ -0,0 +1,39 @@
8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:
```From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008```
You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.
Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.
Desired Output:
```
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with From as the first word
```

24
Assignment 9.4/main.py Normal file
View file

@ -0,0 +1,24 @@
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
senders = dict()
for line in handle:
line = line.strip().split()
if len(line) > 0:
if line[0] == "From":
email = line[1]
senders[email] = senders.get(email, 0) + 1
largesender = None
largecount = None
for email,count in senders.items():
if largesender == None or count > largecount:
largesender = email
largecount = count
print(largesender,largecount)

File diff suppressed because it is too large Load diff

6
Assignment 9.4/readme.md Normal file
View file

@ -0,0 +1,6 @@
9.4 Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
Desired Output:
```
cwen@iupui.edu 5
```

0
readme.md Normal file
View file