Created
April 9, 2015 15:46
-
-
Save MatthewIreland/cd25d8665cb4696852c4 to your computer and use it in GitHub Desktop.
Python script for checking the proposed September challenge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on 9 Apr 2015 | |
@author: mti | |
''' | |
import datetime | |
import random | |
import string | |
""" | |
Constants | |
""" | |
TEST_CALLSIGN_LENGTH = 6 | |
TARGET_WORDS = ["CAMBRIDGE","LONDON","PRESTATYN","OXFORD","YORK"] # Ordered! | |
""" | |
Global variables | |
""" | |
contactList = [] | |
""" | |
Main classes/functions | |
""" | |
class WorkedStation: | |
"Date/callsign pair" | |
def __init__(self, date, call): | |
self.date = date | |
self.call = call | |
def __repr__(self): | |
return "<WorkedStation date:%s call:%s>" % (self.date, self.call) | |
def __str__(self): | |
return "%s %s" % (self.date, self.call) | |
def checkContacts(contactList): | |
# sort contact list by date worked | |
sortedContactList = sorted(contactList, key=lambda contact : contact.date) | |
sortedCalls = map(lambda contact : contact.call, sortedContactList) | |
workedCharacters = map(lambda call : list(call*2), sortedCalls) | |
workedCharacters = [val for sublist in workedCharacters for val in sublist] | |
maxPosition = 0 | |
for targetWord in TARGET_WORDS: | |
targetChars = list(targetWord) | |
for targetChar in targetChars: | |
try: | |
i = workedCharacters.index(targetChar) | |
maxPosition = i if i>maxPosition else maxPosition | |
workedCharacters.remove(targetChar) | |
except ValueError: | |
print "Challenge failed!" | |
exit(1) | |
del workedCharacters[0:maxPosition-1] | |
maxPosition=0 | |
print "Challenge success!" | |
exit(0) | |
""" | |
Test data/usage | |
""" | |
def generateRandomTestContacts(numTestContacts): | |
for i in range(numTestContacts): | |
date = datetime.date(2015, 9, random.randint(1,30)) | |
callsign = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(TEST_CALLSIGN_LENGTH)) | |
contactList.append(WorkedStation(date,callsign)) | |
if __name__ == '__main__': | |
generateRandomTestContacts(90) | |
checkContacts(contactList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment