Topics: algorithms, parts of algorithms
Human beings write algorithms for a wide variety of activities; not just computation by electronic digital computers. One of the most common kinds of algorithms we write are recipes, which are most typically implemented by other human beings.
a. Find an interesting recipe. Copy and cite that recipe.
b. For that recipe, list the parameters and variables.
c. For each parameter, list any implicit or explicit limitations on the input. For example, if you say that “flour” is an input for your recipe, must the flour be wheat flour, or will rice flour do?
d. For that recipe, indicate any situations in which repetition and conditionals are used. (If neither repetition nor conditionals are used, indicate how the recipe might be extended to include repetition or conditionals.)
e. For that recipe, indicate what basic operations or subroutines the recipe author assumes that the reader already knows how to do.
f. Most recipes are written for “average” cooks. Either rewrite the algorithm for a beginning cook or write a detailed set of instructions for one of the “assumed knowledge” subroutines of the algorithm.
g. Most recipes are written for “average” cooks. Rewrite the algorithm for an expert cook.
Topics: Scheme basics, Numeric computation
Many computers, including those on MathLAN, represent time as the number of seconds elapsed since midnight on January 1, 1970, a time known as the UNIX epoch. We’ll use these timestamps to compute the day of week from a timestamp.
We will represent days of the week with integers; Sunday is represented by 0, Monday by 1, Tuesday by 2, and so on until Saturday, which is represented by 6. January 1, 1970 was a Thursday (or a 4 in our representation). Write a series of definitions that assign the variable day-of-week to be the day of the week for the day represented by the time in the variable timestamp.
Submit at least four example dates to show that your code works correctly. Here is one such example (which you may include in your four examples):
; This timestamp is for 12:00am GMT on May 25, 1977, the original release of Star Wars Episeode IV
; This day was a Wednesday
(define timestamp-1 233366400)
; You may want to include intermediate definitions here to make your final expression simpler
(define day-of-week-1 ...)
; day-of-week-1 should now be define to 3, which represents Wednesday
You may find the website http://www.timestampgenerator.com useful for creating test cases.
Note: You may ignore complicated time and date issues like time zones and leap seconds in your day calculations. To make sure your answers are correct, I recommend choosing times in Greenwich Mean Time.
Topics: Scheme basics, Numeric computation
The swim team needs a program to tally the times for members’ events.
Suppose the values time-1-min, time-1-sec, time-2-min, and
time-2-sec represent the times of two swimmers - swimmer 1 took
time-1-min minutes and time-1-sec seconds and swimmer 2 took
time-2-min minutes and time-2-sec. You can assume that all of
these times are sensible values (i.e., positive integers, with
seconds in the range 0 to 59, inclusive).
Write a series of definitions that assign the names time-total-min and
time-total-sec to the computed sum of the two times. For example,
if swimmer one took 2 minutes and 15 seconds and swimmer two took
2 minutes and 11 seconds, then the total time is 4 minutes and 26
seconds. Similarly, if swimmer one took 2 minutes and 35 seconds and
swimmer two took 2 minutes and 36 seconds, then the total time is 5
minutes and 11 seconds. As the second example suggests, you will need
to keep the sum of seconds sum in a sensible range.
Submit at least four examples that show that your code works correctly, including in potentially complex situations. Here is one such example.
> (define time-1-min 2)
> (define time-1-sec 40)
> (define time-2-min 1)
> (define time-2-sec 21)
> (define time-total-min ...)
> (define time-total-sec ...)
> time-total-min
4
> time-total-sec
1
Topics: lists, take, drop
In many situations we
find that we want to extract a sublist of another list: All the
entries starting at position s and ending right before position t.
Write a procedure, sublist that takes three inputs —
a list, a starting position, and an ending position — and returns
the sublist that starts at the starting position and ends right before
the ending position.
> (define words (list "alpha" "beta" "gamma" "delta" "epsilon" "phi"))
> (sublist words 1 4)
'("beta" "gamma" "delta")
As the example suggests, you should keep the elements in the same order.
Note: DrRacket may have a built-in sublist command. You may not use
it or any similar procedure. You should rely on the procedures you’ve
already learned.
We will primarily evaluate your work on correctness (does your code compute what it’s supposed to and are your procedure descriptions accurate); clarity (is it easy to tell what your code does and how it acheives its results; is your writing clear and free of jargon); and concision (have you kept your work short and clean, rather than long and rambly).