## Sandy Graham

• Module 7
• Module 4
• Module 3

### Lecture Self-Check

Here are some quick self-check problems. After each lecture, I will try to post one or two short questions that you should be able to answer based on the previous lecture material. These are problems that you should be able to answer before the beginning of the next lecture.

Please do not ask me to post the solutions to these problems. Do not ask about these problems on the Learn discussion forum. The purpose here is for you as an individual to determine if you have understood the previous material, and that you can create your own solution. In most cases, you should know if your answer is correct by typing in a solution in DrRacket.

If you are unable to come up with your own solutions for these problems then I encourage you to visit office hours, as soon as possible, to make sure you understand the material from the course. You are also welcome to bring your answers to the problems to office hours to confirm that they are good solutions.

• Lecture 18 - Tuesday, November 12
• Rewrite Q3 of Assignment 4 using abstract list functions.
• Without using DrRacket, determine the result of evaluating the following expressions:
• (map (lambda (x) (* (+ x 3) 5)) (list -1 0 1))
• (build-list 5 (lambda (x) x))
• (foldr * 1 (filter odd? (list 1 2 3 4 5)))
• Lecture 17 - Thursday, November 7
• Using abstract list functions, write a function called `count-letters` that consumes a string (`phrase`) and produces the number of letters in `phrase`. For example:
`(count-letters "Hello world!")`
produces 10.
• Lecture 16 - Tuesday, November 5
• Write a Racket expression using `map` that consumes a list of random non-negative integers and produces a list of Boolean values, where the list produced contains the value `true` matching the numbers that are perfect squares `false` matching the numbers that are not perfect squares from the original list.
• Lecture 15 - Thursday, October 31
• Write the `(union setA setB)` that consumes two sorted lists of distinct strings and produces a list of the elements that appear in either setA or setB. The list produced should not have any duplicate values, and should appear in sorted order.
For example: ```(union (list "a" "b" "c" "f") (list "b" "d" "e" "f" "g"))``` should produce
`(list "a" "b" "c" "d" "e" "f" "g")`.
• Lecture 14 - Tuesday, October 29
• Write the `(interlace s1 s2)` that consumes two strings of equal length and forms the string composed of alternating characters from s1 and s2, starting with the first character from s1. For example: `(interlace "abc" "def")` produces `"adbecf"`.
• Lecture 13 - Thursday, October 24
• Write the equivalent of (list 1 "two" empty) without list abbreviations. You can check that it is correct by comparing the two versions using `equal?`
• Write a function called `swap`, that consumes a list that contains lists that have two elements, and produces a copy of that list except the elements of the internal lists are swapped. For example, `(swap (list (list 1 2) (list 3 4) (list "a" "b")))` produces `(list (list 2 1) (list 4 3) (list "b" "a"))`
• Lecture 12 - Tuesday, October 22
• Rewrite the sorting function described slides 40 - 47 of Module 6 so that rather than sorting a list of numbers, it sorts a list of characters in non-descending order.
• Lecture 11 - Thursday, October 10
• Write a function called `add-in-range` that consumes two natural numbers n and m, and produces the sum of the numbers in the range n to m inclusive. For example: `(add-in-range 5 10)` produces 45.
• Lecture 10 - Tuesday, October 8
• Write a function called `even-nums` that consumes a list of integers (`loi`) and produces a list of Boolean values. The values in the list produced will be `true` in the spots matching the even numbers in `loi` and false in the spots matching the odd numbers in `loi`.
For example: `(even-nums (cons -1 (cons 0 (cons 3 empty))))`
produces `(cons false (cons true (cons false empty)))`.
• Write a function called `count-letters` that consumes a string (`phrase`) and produces the number of letters in `phrase`. For example:
`(count-letters "Hello world!")` produces 10.
• Lecture 9 - Thursday, October 3
• Write a function called `start-end-count` that consumes a list of strings (`los`) the number of strings in the list that start and end with the same character. As with the problem from September 24, assume that an empty string and a string of length 1 are both considered to start and end with the same character. For examples `(start-end-count (cons "" (cons "abc" (cons "abababa" empty))))` produces `2`.
• Lecture 8 - Tuesday, October 1
• Define a constant called `mylist` that has 4 elements in the following order: "a", 4, "third", and "end".
• Assume the following constant has been defined:
`(define testlist (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty))))))`
What would be produced by the following expressions? Note that some might cause an error.
``` (first (rest testlist)) (rest (first testlist)) (rest (rest (rest (rest testlist)))) (first (rest (rest (rest (rest testlist)))))) ```
• Write an expression that produces the number 3 from `testlist`
• Lecture 7 - Thursday, September 28
• Write a function called no-penny that consumes a natural number cents between 0 and 99 inclusive and produces the amount to be paid since there are no pennies. If cents is a multiple of 5, then the function will produce the value consumed. If cents is a number that ends with 1, 2, 6, or 7, then value produced will be the amount rounded down to the nearest multiple of 5. If cents is a number that ends with 3, 4, 8, or 9, then value produced will be the amount rounded up to the nearest multiple of 5.
• Lecture 6 - Tuesday, September 24
• Write a function called same-start-end? that consumes a string: astring and produces true if the string consumed starts and ends with the same character, and produces false otherwise. An empty string, and a string of length 1, should both produce true. The function should not use `cond`.
• Lecture 5 - Thursday, September 19
• Write a function called glue-string that consumes two parameters: string-A, and string-B, and produces a string formed by the last n characters from string-B followed by string-A, where n is the number of characters in string-A. You may assume that the length of string-B is greater than or equal to the length of string-A.
For example `(glue-string "xyz" "abcdefg")` produces `"efgxyz"`.
• Lecture 4 - Tuesday, September 17
• In many tournaments, competitors are awarded points as follows:
2 points for a win, 1 point for a tie, and 0 points for a loss.
Write a function called round-robin-pts that consumes three parameters: num-wins, num-losses, and games-played, and produces the number of points earned according to the rule described.
Include all design recipe components in your solution.
• Write a function called point-differential that consumes the number of wins, losses, and games played for two teams (a total of 6 parameters), and produces the difference between the points the two teams earned as described above. The result should be a non-negative integer. The built-in function abs will be useful. Use the function round-robin-pts as a helper function in your solution.
• Lecture 3 - Thursday, September 12
• E = mc2 is probably the most famous equation in the world describing Albert Einstein's theory of special relativity.
The E represents kinetic energy, the m represents mass, and c represents the speed of light.
• Define a constant called speed-of-light that is set to 299792458, representing the speed of light in metres per second.
• Define a function called kinetic-energy that consumes a single parameter representing the mass of an object, and produces the value equivalent to the E in Einstein's equation.
• Assuming that you have defined the constant and function described above, trace this Racket expression using the substitution rules described in Module 1.
`(kinetic-energy 1000)`
• Lecture 2 - Tuesday, September 10
• Define a function called `perimeter`, that consumes two parameters, `r-length` and `r-width`, representing the dimensions of a rectangle and produces the perimeter of the rectangle. Remember that the perimeter of a rectangle is the sum of the lengths of the sides of the rectangle.
• Lecture 1 - Thursday, September 5
• Convert these expressions to valid Racket expressions:
• 2 * -4 * 5
• 6 - 12/3 + 4 * (9 - 1)
• (4 + 3)/(12 -7)
• Evaluate this Racket expression (without using the computer):
• (- (* 3 12) (+ (* 4 5) 1))