The Problem
After his brush with the Justice Department over fundraising with Buddhist monks, the Vice President devised a plan to ensure that such activities are carried out in a more discrete manner and are kept less noticeable. Realizing the Democratic National Committee’s need for more and more money in the campaign war chest, Mr. Gore devised a “rhythm method” for accepting campaign donations from Buddhist monks and yet avoiding the conception of an independent counsel. Gore’s theory is that if the donations are spaced no less than 28 days apart, an independent counsel will not be necessary.
To help Mr. Gore keep track of when it’s okay to accept money from Buddhist monks, you must write a program to automatically scan the White House email logs for messages from “[email protected]” addressed to“[email protected]” (the code name for the Al Gore Rhythm Method). Each such email is a secret entry in Mr.Gore’s Buddhist monk fundraising dairy. Your program must send Mr. Gore (“[email protected]”) a reply to each such email advising him when the next donation may be accepted. You should assume that the email was sent the day that the donation was accepted. To maintain more secrecy, Mr. Gore refers to a Buddhist monk as a “BM.”
Sample Input
Your program must process the White House electronic mail log, stored in the file whmail.log as input. The mail log is an ASCII text file which contains a sequence of one or more valid email messages. Each email message consists of a header followed by the body. A header consists of exactly four lines, each of which begins with a unique keyword. The first line is the sender line and begins with the keyword From which is followed by a character string representing the email address of the sender. The second line is the recipient line and begins with the keyword To which is followed by a character string representing the email address of the recipient of the message. The third line is the date line and begins with the keyword
Date
which is followed by a character string representing the date on which the email message was received. The fourth line is the subject line and begins with the keyword Subject which is followed by an arbitrary character string. A single blank space separates the keyword on each header line from the character string that follows. The body of an email message is simply a sequence of one or more lines of text any of which may be blank. The body begins on the fifth line of the email message. There will be normal White House email interspersed with the Al Gore Rhythm email, but your program should ignore all email other than those from “veep” to “buddha.” Sample contents ofwhmail.log
could appear as:From [email protected] To [email protected] Date Saturday, October 4, 1997 Subject Get together Hey, honeypie. Ole Big Daddy sure is missin you. I’ll be a lookin for you to come around again this weekend. Love, Bill From [email protected] To [email protected] Date Monday, October 6, 1997 Subject BM Dear Buddha, I just had a BM. Please advise. From [email protected] To [email protected] Date Wednesday, October 8, 1997 Subject Roby Ridge Mr. President: The situation with the lady in Roby is quickly deteriorating. I advise we use an Apache loaded with napalm to flush the crazy woman out. If it kills her, it serves her right for that Vaseline trick. Dead or alive, at least it will be over. If I don’t hear from you within the next hour, I’ll send for the chopper. Janet
Sample Output
The output of your program must be directed to the screen and must consist of a reply email for each Al Gore Rhythm email found in the log. Each reply must specify the date on which the next donation may be accepted. Your output must be formatted exactly as that below, which is the output corresponding to the sample input above.
From [email protected] To [email protected] Date Saturday, November 8, 1997 Subject Re: BM Thank you for advising me of your BM. You may not have another BM until Monday, November 3, 1997.
algore.py
import datetime
def search_match(t, f):
return t.startswith('From [email protected]') and f.startswith('To [email protected]')
def get_date(date):
prefix = date.strftime('%A, %B ')
day = date.strftime('%d').lstrip('0')
postfix = date.strftime(', %Y')
return '{0}{1}{2}'.format(prefix, day, postfix)
with open('whmail.log') as f:
for line in f:
if search_match(line, f.readline()):
date, subject = datetime.datetime.strptime(f.readline().strip()[5:], "%A, %B %d, %Y"), f.readline()
subject = 'Re:' + subject[7:]
limit = date + datetime.timedelta(days=28)
sent = limit + datetime.timedelta(days=5)
print('From [email protected]\nTo [email protected]\nDate {0}\nSubject {1}\nThank you for advising me of your BM. You may not have\nanother BM until {2}'.format(get_date(sent), subject, get_date(limit)))
Any advice on performance enhancement and solution simplification is appreciated, as are topical comments!
f.readline()
within the conditionals I realized that makes things dependent on order... \$\endgroup\$