Skip to content

Instantly share code, notes, and snippets.

@KKarthikeya
KKarthikeya / 10.2.py
Created August 16, 2015 04:32
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…
mails = dict()
for line in open( raw_input("Enter File Name:")):
words = line.strip().split()
if len(words) == 0 and len(words)<2:
continue
if words[0] == 'From':
hours = words[5].split(':')
if hours[0] not in mails:
mails[hours[0]] = 1
else:
@KKarthikeya
KKarthikeya / 9.4.py
Created August 16, 2015 04:31
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 they a…
count = dict()
for line in open(raw_input("Enter File Name:")):
words = line.strip().split()
if len(words) == 0 or len(words) <3:
continue
if words[0] == 'From':
if words[1] not in count:
count[words[1]] = 1
else:
count[words[1]] += 1
@KKarthikeya
KKarthikeya / 8.5.py
Created August 16, 2015 04:29
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 = 0
for mail in open(raw_input("Enter file name:")):
mail = mail.rstrip()
if mail.startswith("From "):
count +=1
words = mail.split()
print words[1]
print "There were",count,"lines in the file with From as the first word"
@KKarthikeya
KKarthikeya / 8.4.py
Created August 16, 2015 04:28
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() function. 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 …
finlist = list()
for line in open(raw_input("Enter the file Name:")):
words = line.rstrip().split()
for arr in words:
if arr in finlist:
continue
else:
finlist.append(arr)
finlist.sort()
print finlist
@KKarthikeya
KKarthikeya / 7.2.py
Created August 16, 2015 04:25
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. You can download the sample data at h…
sum = 0.0
count = 0
fname = open(raw_input("Enter file name: "))
for line in fname:
if not line.startswith("X-DSPAM-Confidence:") : continue
sum = sum + float(line[line.find(" ")+1:])
count = count + 1
print "Average spam confidence:",sum/float(count)
@KKarthikeya
KKarthikeya / 7.1.py
Created August 16, 2015 04:24
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
for data in open(raw_input("Enter file name: ")):
print data.rstrip().upper()
@KKarthikeya
KKarthikeya / 6.5.py
Created August 16, 2015 04:22
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"
epos = text.find(':')
print float(text[epos+1:])
@KKarthikeya
KKarthikeya / 5.2.py
Created August 16, 2015 04:21
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.
largest = None
smallest = None
while True:
num = raw_input("Enter a number:")
try:
if num == 'done':
print "Maximum is",largest
print "Minimum is",smallest
break
num = int(num)
@KKarthikeya
KKarthikeya / 4.6.py
Created August 16, 2015 04:19
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 valu…
def computepay(hours,rate):
if hours>40.0:
p = rate * 40.0
p = p+(1.5*rate*(hours-40))
else:
p = rate*hours
return p
hours = float(raw_input("Enter worked hours: "))
rate = float(raw_input("Enter Pay rate per hour: "))
print computepay(hours,rate)
@KKarthikeya
KKarthikeya / 3.3.py
Last active November 9, 2022 11:06
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 print A, Grade >= 0.8 print B, Grade >= 0.7 print C, Grade>= 0.6 print D, Grade < 0.6 print F If the user enters a value out of range, print a su…
score = float(raw_input("Enter score between 0.0 and 1.0: "))
if score>1.0 or score<0.0 :
print "error"
elif score>=0.9 :
print 'A'
elif score>=0.8 :
print 'B'
elif score>=0.7 :
print 'C'
elif score>=0.6 :