6.6

Assignment 2

home work!

Programming Language BSL

Due Date Monday 9/17 at 9pm

Purpose To write simple functions & tests relating to numbers, images, and strings

Expectations
  • You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions.

  • You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.

  • Function signatures/purpose statements are not required, but attempting them would be good practice and you may receive useful feedback for future assignments.

Graded Exercises

Exercise 1 Write a function, convert-to-inches, that takes as arguments a number of yards, feet, and inches (in that order) and converts to a total number of inches. For example, (convert-to-inches 4 1 3) would produce 159 inches. Write two check-expects: one expressing the example above, and a different one of your choosing.

Exercise 2 Jill is now preparing for her second year of study at Hogwarts School of Witchcraft and Wizardry and is back in Diagon Alley to convert more currency. This year she stumbles upon a new vendor, promising a volume discount: for all conversions below 100 Pounds, the same rules as last year apply (3 Pound flat fee, then 5 Pounds per Galleon); however, conversions after that point do not incur the 3-Pound fee. For example, 13 Pounds will buy 2 Galleons, but 100 Pounds will buy 20 Galleons.

Write a function, diagon-convert-discount to convert from Pounds to Galleons under this new pricing scheme.

Write 4 check-expects: the two provided in the description, one new one of your choosing that incurs the fee, and one new one of your choosing that does not incur the fee.

Exercise 3 In a "grayscale" image, each pixel color is represented as a number from 0-255, where 0 is pure black and 255 is pure white. Below is a visual depiction of the spectrum, segmented into 16 blocks, representing 16 colors via the first value:

Notice that all colors up to, but not including, 32 appear essentially "black"; all colors 240 and higher appear essentially "white"; and all other values appear as varying shades of "gray".

Write a function, grayscale, that converts from a pixel color, supplied as an integer argument, to the visually apparent color as a string ("black", "gray", or "white").

Write sufficient check-expects to fully test this function. Specifically, you must include the absolute boundaries of valid pixel values (0 and 255), the neighborhoods of each visually apparent boundary (for example, the neighborhood of "black"->"gray" requires testing 31, 32, and 33); as well as another pixel value for each of the apparent colors.

Exercise 4 Write the function, wordplay, that takes as arguments a wordplay operation, as well as three words. The valid operations are "first", which returns the first of the three words; "last", which returns the third of the three words; and "hyphenate", which returns the three words, in order, with dashes between them.

For example, (wordplay "first" "a" "b" "c") should return "a", whereas (wordplay "last" "a" "b" "c") should return "c", and (wordplay "hyphenate" "a" "b" "c") should return "a-b-c".

Write check-expects that test the three examples above, as well as at least one more for each of the three operations. Feel free to be creative in choosing your own words for the three additional examples, but be respectful!