Skip to content

Commit

Permalink
Added 2007 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 6f41291 commit e7a73b6
Show file tree
Hide file tree
Showing 13 changed files with 1,001 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 2007/Junior/J1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
% CCC 2007 J1: Who is in the Middle
%
% Simple if else condition

var a, b, c : int
get a, b, c
if (b <= a and a <= c) or (b >= a and a >= c) then
put a
elsif (a <= b and b <= c) or (a >= b and b >= c) then
put b
else
put c
end if

38 changes: 38 additions & 0 deletions 2007/Junior/J2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
% CCC 2007 J2: Speak TXTMSG
%
% Simple if else condition and a loop

var s : string
loop
put "Enter phrase> " ..
get s
if s = "CU" then
put "see you"
elsif s = ":-)" then
put "I'm happy"
elsif s = ":-(" then
put "I'm unhappy"
elsif s = ";-)" then
put "wink"
elsif s = ":-P" then
put "stick out my tongue"
elsif s = "(~.~)" then
put "sleepy"
elsif s = "TA" then
put "totally awesome"
elsif s = "CCC" then
put "Canadian Computing Competition"
elsif s = "CUZ" then
put "because"
elsif s = "TY" then
put "thank-you"
elsif s = "YW" then
put "you're welcome"
elsif s = "TTYL" then
put "talk to you later"
else
put s
end if
exit when s = "TTYL"
end loop

36 changes: 36 additions & 0 deletions 2007/Junior/J3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
% CCC 2007 J3: Deal or No Deal Calculator
%
% simple 1D array processing
%

var a : array 1 .. 10 of int
var n, x, sum, offer : int
var avg : real

a (1) := 100
a (2) := 500
a (3) := 1000
a (4) := 5000
a (5) := 10000
a (6) := 25000
a (7) := 50000
a (8) := 100000
a (9) := 500000
a (10) := 1000000

get n
for i : 1 .. n
get x
a (x) := 0
end for
sum := 0
for i : 1 .. 10
sum := sum + a (i)
end for
avg := sum / (10 - n)
get offer
if offer > avg then
put "deal"
else
put "no deal"
end if
49 changes: 49 additions & 0 deletions 2007/Junior/J4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
% CCC 2007
% J4: Anagram Checker
%
% Determine is two phrases are anagrams of each other.
%
% let s and t be the two strings
% For each letter in s, find it in t
% if not found, not an anagram
% if found, replace it with a " "
% (so it can't be found a second time)
% if you've done the entire loop,
% its an anagram. (provided t is all spaces)
%

var s, t : string
var x, i : int
var bad : boolean

put "Enter first phrase> " ..
get s : *
put "Enter second phrase> " ..
get t : *

bad := false
i := 1
loop
exit when i > length (s) or bad
if s (i) not= " " then
x := index (t, s (i))
if x = 0 then
bad := true
put "Is not an anagram."
else
t := t (1 .. x - 1) + " " + t (x + 1 .. *)
end if
end if
i := i + 1
end loop
if not bad then
for j : 1 .. length (t)
bad := bad or t (j) not= " "
end for
if not bad then
put "Is an anagram."
else
put "Is not an anagram."
end if
end if

102 changes: 102 additions & 0 deletions 2007/Junior/J5_1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
% CCC 2007
% J5: Keep on Truckin'
%
% Arrays and dynamic programming :-)
%
% Step 1: sort the motels
% Step 2: find the first motels you can get to on day one.
% step 3: (the hard step)
% The idea is to find all the ways to
% EVERY motel, not just the last.
% For each motel:
% find out which motels to can get to from there
% (subtract the distances)
% The key thing is:
% suppose you can get to motel D in 2 ways.
% suppose you can get to motel K in 3 ways,
% (without going thru D first, maybe thru A, B and C).
% then if you can get from D to K,
% the numbers of ways to K is 2 + 3 = 5 ways!
%

var motel : array 1 .. 50 of int
var ways : array 1 .. 50 of int
var minn, maax, size, n : int

% insertion sort to get the motels in order after input
procedure sortMotels
var j, hold : int
for i : 2 .. size
hold := motel (i)
j := i - 1
loop
exit when j < 1 or motel (j) < hold
motel (j + 1) := motel (j)
j := j - 1
end loop
motel (j + 1) := hold
end for
end sortMotels

function between (a, b, c : int) : boolean
result a <= b and b <= c
end between


size := 13
motel (1) := 990
motel (2) := 1010
motel (3) := 1970
motel (4) := 2030
motel (5) := 2940
motel (6) := 3060
motel (7) := 3930
motel (8) := 4060
motel (9) := 4970
motel (10) := 5030
motel (11) := 5990
motel (12) := 6010
motel (13) := 7000

% get input
get minn
get maax
get n
for i : 1 .. n
get motel (i + size)
end for
size := size + n
sortMotels

% load the ways for the first motels
for i : 1 .. size
if between (minn, motel (i), maax) then
ways (i) := 1
else
ways (i) := 0
end if
end for

% work thruough all the motels and all the ways
% answering "can you get to the next motel?"
for i : 1 .. size - 1

% can you get to i?
if ways (i) > 0 then

for j : i + 1 .. size

% can you get from i to j?
if between (minn, motel (j) - motel (i), maax) then
ways (j) := ways (j) + ways (i)
end if
end for
end if
end for

put ways (size)





75 changes: 75 additions & 0 deletions 2007/Junior/J5_2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
% CCC 2007
% J5: Keep on Truckin'
%
% Arrays and dynamic programming
% This approach is due to Konstantin Lopyrev
%
% This is a more straight forward dp approach
% than my version. Simply stated the ways to a motel i
% = the sum of all ways to previous motels in the range
% i-max to i-min.
%
% The use of a boolean array eliminates the need for
% sorting.

var motel : array 0 .. 7000 of boolean
var ways : array 0 .. 7000 of int
var minn, maax, n, d, a : int

for i : 0 .. 7000
motel (i) := false
end for
motel (0) := true
motel (990) := true
motel (1010) := true
motel (1970) := true
motel (2030) := true
motel (2940) := true
motel (3060) := true
motel (3930) := true
motel (4060) := true
motel (4970) := true
motel (5030) := true
motel (5990) := true
motel (6010) := true
motel (7000) := true

% get input
get minn
get maax
get n
for i : 1 .. n
get d
motel (d) := true
end for

% you can get to zero 1 way
ways (0) := 1

for i : 1 .. 7000

% for each motel
if motel (i) then

% the ways to that motel = the sum of the ways
% from the previous motels in the range
% i-max to i-min (check that i-max is not less than 0)
ways (i) := 0
a := i - maax
if a < 0 then
a := 0
end if
for j : a .. i - minn
if motel (j) then
ways (i) := ways (i) + ways (j)
end if
end for
end if
end for

put ways (7000)





75 changes: 75 additions & 0 deletions 2007/Junior/J5_3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
% CCC 2007
% J5: Keep on Truckin'
%
% This is a recursive approach created by
% Wen-Hao Lue of Bayview Glen Private School
%
% Recursively find a route to 7000. When you find one
% add one to a counter, otherwise keep going.
%
% In more detail:
% if your at 7000
% you made it, add 1 to a counter
% else you're still on the road
% recursively call this for EVERY motel
% that you can get to from here
%

var motel : array 1 .. 50 of int
var ways : int := 0
var minn, maax, size, n : int

function between (a, b, c : int) : boolean
result a <= b and b <= c
end between

% this recursively tries every route to the end (7000)
procedure findPath (distance : int)
if distance = 7000 then
% made it to the end!
% One more way to get there
ways := ways + 1

else
% recursively try all motels that you can get to
for i : 1 .. size
if between (minn, motel (i) - distance, maax) then
findPath (motel (i))
end if
end for
end if
end findPath

size := 13
motel (1) := 990
motel (2) := 1010
motel (3) := 1970
motel (4) := 2030
motel (5) := 2940
motel (6) := 3060
motel (7) := 3930
motel (8) := 4060
motel (9) := 4970
motel (10) := 5030
motel (11) := 5990
motel (12) := 6010
motel (13) := 7000

% get input
get minn
get maax
get n
for i : 1 .. n
get motel (i + size)
end for
size := size + n

% find all the ways from 0
findPath (0)

put ways





Loading

0 comments on commit e7a73b6

Please sign in to comment.