Skip to content

Commit

Permalink
Added 2012 Solutions; Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 1bf4783 commit 3871acd
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 1 deletion.
17 changes: 17 additions & 0 deletions 2012/Junior/J1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# J1 2012: Speed fines are not fine!
#
# simple decision and watch the formatting!
#

speedLimit = input("Enter the speed limit: ")
recordedSpeed = input("Enter the recorded speed or the car: ")

if recordedSpeed <= speedLimit:
print "Congratulations, you are within the speed limit!"
elif recordedSpeed <= speedLimit + 20:
print "You are speeding and your fine is $100."
elif recordedSpeed <= speedLimit + 30:
print "You are speeding and your fine is $270."
else:
print "You are speeding and your fine is $500."

19 changes: 19 additions & 0 deletions 2012/Junior/J2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# J2 2012: Sounds fishy!
#
# a simple decision.
#

a = input()
b = input()
c = input()
d = input()

if a == b == c == d:
print "Fish At Constant Depth"
elif a < b < c < d:
print "Fish Rising"
elif a > b > c > d:
print "Fish Diving"
else:
print "No Fish"

23 changes: 23 additions & 0 deletions 2012/Junior/J3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# J3 2012: Icon Scaling!
#
# simple loops.
#

k = input()
star = ""
x = ""
space = ""
for i in range(k):
star = star + "*"
x = x + "X"
space = space + " "

for i in range (k):
print star+x+star
for i in range (k):
print space+x+x
for i in range (k):
print star+space+star



22 changes: 22 additions & 0 deletions 2012/Junior/J4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# J4 2012: Big Bang Secrets
#
# straight forward character manipulation
# in a classic substitution cipher.
#
# the shift = 3 * position + k
#

k = input()
cipher = raw_input()

plain = ""
for i in range(len(cipher)):
letter = cipher[i:i+1]
shift = 3 * (i+1) + k
x = ord(letter) - shift
if x < ord("A"):
x = ord("Z") + x - ord("A") + 1
plain = plain + chr(x)

print plain

90 changes: 90 additions & 0 deletions 2012/Junior/J5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# J5 2012: A Coin Game
#
# this is a brute force, Breadth First Search
# of the game tree.
#
# Each "move" is an array strings
#
# Each move is stored in a tree (a big array)
# along with the level of the move in the tree.
#
# This WILL work VERY quickly for up to 4 coins.
# (and completely DIES at 5 coins :-) so see S4.)

n = 0

class Node:
def __init__(self, m, l):
self.m = m
self.level = l

def done (move):
global n
i = 0
while i < n and move[i] == str(i+1):
i = i + 1
return i == n

# given a move, see if it is already in the tree
def inTree(t, m):
for x in t:
if x.m == m:
return True
return False

#creates a new move, given an oldmove and two positions,
# p1 is the position to take from, p2 - add to
def createNewMove (oldmove, p1, p2):
global n
newmove = []
for i in range (n):
newmove.append(oldmove[i])

newmove[p2] = newmove[p1][0:1] + newmove[p2]
newmove[p1] = newmove[p1][1:]

# don't allow big guy to move left
if p2 < p1 and newmove[p2][0:1] == str(n):
return oldmove
else:
return newmove

# this is the basic Breadth Order Search algorithm
# the "queue" is merely an index in the tree.
def search(move):
global n
if done(move):
return 0
else:
tree = []
queue = 0
tree.append(Node(move,0))
while queue < len(tree):
x = tree[queue]
queue = queue + 1
for i in range(n):
# move right
if i < n-1:
if len(x.m[i+1]) == 0 or x.m[i][0:1] < x.m[i+1][0:1]:
newmove = createNewMove(x.m, i, i+1)
if not inTree(tree, newmove):
tree.append(Node(newmove,x.level + 1))
if done(newmove):
return x.level + 1
# move left
if i > 0:
if len(x.m[i-1]) == 0 or x.m[i][0:1] < x.m[i-1][0:1]:
newmove = createNewMove(x.m, i, i-1)
if not inTree(tree, newmove):
tree.append(Node(newmove,x.level + 1))
if done(newmove):
return x.level + 1

return "IMPOSSIBLE"

n = input()
while n > 0:
m = raw_input().strip().split()
print search(m)
n = input()

18 changes: 18 additions & 0 deletions 2012/Senior/S1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# S1 2012: Don't Pass Me the Ball
#
# this is due to Daniel Cressman
#
# This is a combination problem, as any Grade 12
# who has taken Data Management will tell you.
#
# the formula is C(n,r) = n! / ((n-r)! * r!)
# for n object, taken r at a time.
#
# n = the given number - 1
# r = 3
#
# therefore the answer is (n)(n-1)(n-2) / 6

file = open("s1.5.in", 'r')
n = eval(file.readline()) - 1
print (n*(n-1)*(n-2))/6
42 changes: 42 additions & 0 deletions 2012/Senior/S2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# S2 2012: Aromatic Numbers
#
# straight forward Roman Numeral type
# character manipulation problem.


# converts the letter "X", "L", etc into the
# equivalent value
def R (x):
if x == 'I':
return 1
elif x == 'V':
return 5
elif x == 'X':
return 10
elif x == 'L':
return 50
elif x == 'C':
return 100
elif x == 'D':
return 500
else:
return 1000

file = open("s2.2.in", 'r')
aromatic = file.readline().strip()
oldr = 999999
olds = 0
value = 0
for i in range(0, len(aromatic), 2):
a = eval(aromatic[i:i+1])
r = R(aromatic[i+1:i+2])
s = a * r
value = value + s
if r > oldr:
value = value - 2 * olds
olds = s
oldr = r

print value


67 changes: 67 additions & 0 deletions 2012/Senior/S3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# S3 2012: Absolutely Acidic
#
# For the largest files this takes about 45sec.
# All that is spent reading. The sort takes
# less than 1 second.
#
# The algorithm is very straight forward;
# read the file and count the readings and store in a list of 1..1000
# sort the list descending (sub sorting helps case 1)
# max and second max are at the first so determine the case
# and calculate the answer.

class Info:
def __init__(self, r, c):
self.reading = r
self.count = c


file = open("s3.5.in", 'r')

# initialize the list
freq = []
for i in range(1001):
freq.append(Info(i,0))

# read the file and add the frequencies
# need to skip the first line.
firstline = True
for line in file:
if firstline:
firstline = False
else:
r = eval(line)
freq[r].count = freq[r].count + 1

# sort freq, by count, subsorted by the reading
# use insertion sort method
for i in range (1,1001):
hold = freq[i]
j = i - 1
while j >= 0 and (freq[j].count < hold.count or
(freq[j].count == hold.count and
freq[j].reading < hold.reading)):
freq[j+1] = freq[j]
j = j - 1
freq[j+1] = hold

# determine the type
multihigh = freq[0].count == freq[2].count
multisecondhigh = freq[0].count != freq[1].count and freq[1].count == freq[2].count

# get the answer
if multihigh:
i = 1
while i < 1001 and freq[0].count == freq[i].count:
i = i + 1
print abs(freq[0].reading - freq[i-1].reading)
elif multisecondhigh:
i = 3
m = abs(freq[0].reading - freq[1].reading)
while i < 1001 and freq[1].count == freq[i].count:
m = max (m, abs(freq[0].reading - freq[i].reading))
i = i + 1
print m
else:
print abs(freq[0].reading - freq[1].reading)

Loading

0 comments on commit 3871acd

Please sign in to comment.