[quiz] [QUIZ 5b & 5e] Date differences, and betting

Larry Clapp larry at theclapp.org
Thu Jul 19 23:36:14 UTC 2007


Beginner:

Adapted from Perl Quiz #2:
[ http://perl.plover.com/~alias/list.cgi?mss:11:200210:ccbcblpijjjcnhnkabep ]

Write a function, days-diff, to compute the time difference, in days,
between two dates.  The dates will be strings in the format

        Wed Oct 16 2002

For example:

        (days-diff "Wed Oct 16 2002" "Wed Oct 23 2002")
	=> 7

        (days-diff "Wed Oct 16 2002" "Tue Oct 16 2001")
	=> -365

Error checking: at minimum, signal an error if either value is
unparsable or internally inconsistent (e.g. if 10/16/2002 is not, in
fact, a Wednesday).  More advanced users can establish useful
restarts.

Dependencies on your Lisp's time functions are okay.

Expert:

Adapted from Expert Perl Quiz #2:
[ http://perl.plover.com/~alias/list.cgi?mss:12:200210:mflioldnngfgmfnlfljf ]

The local high school baseball team, the Kent Pitman High School
Phoenixes, will be playing a series of games against their rivals, the
Kenny Tilton Memorial High School Growlin' Fungus.  The series lasts
at most five games, and ends when one team has won three games.  You
want to bet $80 on the Phoenixes to win the series, but your bookie
will only take bets on individual games.  (The bookie pays even money
on all bets.)

A mathematically-inclined friend solves the problem for you, giving
you the following instructions:

  Bet $30 on each of the first two games.

  Bet $20 on the third game if either team has won both of the first
  two games, $40 otherwise.

  Bet $40 on the fourth game, if there is one.
  Bet $80 on the fifth game, if there is one.

  At the end of the series, you will be ahead by exactly $80 if the
  Phoenixes have won the series, and behind by exactly $80 if the
  Growlin' Fungus have won.

We could summarize the instructions in a table like this:

        If the game score is:
           0 to 0, bet $30
           1 to 0, bet  30
           1 to 1, bet  40
           2 to 0, bet  20
           2 to 1, bet  40
           2 to 2, bet  80

Write a function which calculates the appropriate bet for any such
contest, given the total amount you want to risk on the series, the
length of the series, and the current game score.  For example,

        (bet 80 5 2 1)
	=> 40

because if you want to risk $80 on a 5-game series, and one team is
presently ahead 2 games to 1, you should bet $40.  

Similarly 

        (bet 1000 7 2 1)
	=> 375

(That is, if you're trying to bet $1000 on the current [circa 10/2002]
World Series baseball match, you need to bet $375 on the outcome of
tonight's game.  If you started with $1000, and followed this
function's advice, you'd have $625 left if you had been betting on the
Giants and $1375 if you had been betting on the Angels.  For people
living in places where the World Series is irrelevant: the match is a
best-four-of-seven series of games; at present, the Anaheim Angels are
beating the San Francisco Giants two games to one, with the fourth
game scheduled for tonight.)

Error checking: at minimum, signal an error if any of the parameters
are invalid or internally inconsistent (e.g. negatives, length of
series is less than number of games played so far, etc).  More
advanced users can establish useful restarts.




More information about the Quiz mailing list