Skip to content

Instantly share code, notes, and snippets.

@zcakzwa
zcakzwa / gist:2bf34f782db0c43847250b5fa86bc2bf
Created July 5, 2016 12:43
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 c…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d=dict()
for line in handle:
if not line.startswith("From "):
continue
else:
line=line.split()
line=line[5]
@zcakzwa
zcakzwa / gist:aebffa8eecd13022ba125b03a721b247
Created July 5, 2016 05:01
5.2 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 the numbers from the book …
largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
try:
num = int(num)
except:
print "Invalid input"
continue
@zcakzwa
zcakzwa / gist:c52cf06d35c0748f61f48ec985ddb07a
Created July 5, 2016 05:01
4.6 Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a v…
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter Rates:")
r = float(rate)
def computepay(h,r):
if h > 40:
payoff = 40*r +(h-40)*1.5*r
else:
payoff = h*r
@zcakzwa
zcakzwa / gist:54b4cd2fdf81a263e2b96269cac1c283
Created July 5, 2016 04:43
9.4 Write a program to read through the mbox-short.txt and figure out who has the 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 th…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d = dict()
for line in handle:
if not line.startswith("From "):
continue
else:
list=line.split()
list=list[1]
@zcakzwa
zcakzwa / gist:19a448416b9e7da3dc2096da72690914
Created July 5, 2016 03:59
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 …
# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
t=line.find("0")
number= float(line[t:])
count = count + 1
@zcakzwa
zcakzwa / gist:5882d461ed97829e068531282b32d7ec
Created July 5, 2016 03:53
6.5 Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.
text = "X-DSPAM-Confidence: 0.8475";
t = text.find("0")
h = text[t:t+6]
h = float(h)
print h
@zcakzwa
zcakzwa / gist:1d4904c6f9683967fb61e3650a40e916
Created July 5, 2016 03:44
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. You can download the sample data at http://www.pythonlearn.com/code/words.txt
# Use words.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
for line in fh:
line_upper=line.upper()
line_strip=line_upper.rstrip()
print line_strip
@zcakzwa
zcakzwa / gist:0cc15a79ac4b36b8151236bce2e3fa8e
Last active July 5, 2016 04:20
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…
fname = raw_input("Enter file name: ")
fh = open(fname)
count=0
for line in fh:
if not line.startswith("From "):
continue
else:
list=line.split()
print list[1]
count=count+1
@zcakzwa
zcakzwa / gist:c04fb15f6fffc1d3fdb2f761608b6582
Created July 5, 2016 03:20
8.4 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…
fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line=line.rstrip()
line= line.split()
for c in line:
if c in lst:
continue
else: