Py4Inf Solutions
Py4Inf Solutions
--- 3.3 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.
--- 5.2 Write a program that repeatedly reads numbers until the user enters “done”.
Once “done” is entered, print out the maximum and minimum of the numbers. If
the user enters anything other than a number, detect their mistake using try and
except and print an error message and skip to the next number.
--- 6.5 Take the following Python code that stores a string:
st = "X-DSPAM-Confidence: 0.8475"
# colon = st.find(":")
# length = len(st)
# substring = st[colon + 1 : length]
substring = st.split(":")[1]
number = float(substring)
print "Number: ", number
--- 7.1 Write a program to read through a file and print the contents of the file (line
by line) all in upper case.
--- 7.2 Write a program to prompt for a file name, and then read through the file.
When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the
line to extract the floating point number on the line. Count these lines and the
compute the total of the spam confidence values from these lines. When you reach
the end of the file, print out the average spam confidence.
fhand = open("romeo.txt")
words =[]
for line in fhand:
llist = line.lower().split(" ")
for word in llist:
print word
word= word.strip()
if word not in words:
words.append(word)
print sorted(words)
--- 8.5 Write a program to read through the mail box data and when you find line
that starts with “From”, you will split the line into words using the split function.
We are interested in who sent the message which is the second word on the From
line.
You will parse the From line and print out the second word for each From line and
then you will also count the number of From (not From:) lines and print out a count
at the end.
fhand= open("mbox-short.txt")
lines = 0
for line in fhand:
if line.startswith("From "):
lines+=1
print "There are",lines,"lines in the file with From as the first word"
--- 9.3 Write a program to read through a mail log, and build a histogram using a
dictionary to count how many messages have come from each email ad- dress and
print the dictionary.
--- Output
print c
--- 9.4 Add code to the above program to figure out who has the most messages in
the file. After all the data has been read and the dictionary has been created, look
through the dictionary using a maximum loop to find who has the most messages
and print how many messages the person has.
--- Output
--- 10.2 This program counts the distribution of the hour of the day for each of the
messages. You can pull the hour from the “From” line by finding the time string
and then splitting that string into parts using the colon character. Once you have
accumulated the counts for each hour, print out the counts, one per line, sorted by
hour as shown below.
-- Output
lst = list()
for key in c:
value = c[key]
lst.append( (value, key) )
lst.sort()