--- title: Drill 1 Solutions, Fall 2020 tags: Drills-F20, 2020 --- # Drill 1 Solutions, Fall 2020 ## Question 1 How would you create a solid, red, circle with a radius of 20 pixels using Pyret? ( ) `circle(10, "solid", "red")` ( ) `circle(40, "solid", "red")` ( ) `circle(20, "solid", "red")` ( ) `circle("twenty", "solid", "red")` :::spoiler Click the arrow for the answer! `circle(20, "solid", "red")` ::: ## Question 2 Look at the following piece of code. ``` include image circ = circle(20, "solid", "red") rect = rectangle(120, 60, "solid", "white") overlay([shape1], [shape2]) ``` Fill in the blanks shape1 and shape2 such that the code makes the flag of Japan. <!-- Note: GS doesn't support multiple answers for short response -- ask kathi/doug how to proceed --> shape1: __________ :::spoiler Click the arrow for the answer circ ::: shape2: __________ :::spoiler Click the arrow for the answer rect ::: ## Question 3 Look at the following piece of code. ``` include image sq = square(30, "solid", "green") tri = triangle(40, "solid", "red") ``` How would you place tri over sq such that it forms a house-like structure? ![](https://i.imgur.com/IetxX1f.png) ( ) `overlay-xy(sq, 0, 0, tri)` ( ) `above(sq, tri)` ( ) `overlay-xy(tri, 0, 0, sq)` ( ) `above(tri, sq)` :::spoiler Click the arrow for the answer `above(tri, sq)` ::: ## Question 4 Looking at this code, how would you use overlay-xy in order to place a small square offset just inside a bigger square? ``` include image big-sq = square(40, "solid", "green") small-sq = square(20, "solid", "blue") ``` ![](https://i.imgur.com/60ktyko.png) ( ) `overlay-xy(small-sq, big-sq) ` ( ) `overlay-xy(small-sq, 10, 10, big-sq)` ( ) `overlay-xy(big-sq, 10, 10, small-sq)` ( ) `overlay-xy(small-sq, -10, -10, big-sq)` ( ) `overlay-xy(big-sq, -10, -10, small-sq)` :::spoiler Answer and explanation ````overlay-xy(small-sq, -10, -10, big-sq)```` The big square needs to move up and to the left! ::: ## Question 5 Look at the following code that tries to draw a bar graph of three values: ``` beside-align("bottom", rectangle(20, 10 * 4, "solid", "blue"), beside-align("bottom", rectangle(20, 25 * 4, "solid", "red"), rectangle(20, 18 * 4, "solid", "purple"))) ``` Assume you wanted to create a function to capture the similarities in the code that creates the bars. How many inputs would this function have? ( ) 1 ( ) 2 ( ) 3 ( ) 4 :::spoiler Answer 2 All three calls to rectangle have 20 as their first input, so this is not something that we would want to include as an input to our new function because it never changes. The second input to rectangle does change, though, so a good input for our new function could be the size of the bar. The third input to rectangle is always solid, so we do not need to make it an input to our new function. Finally, the fourth input to rectangle is the color, and it is different for each bar, so we would want it to be an input to our function. ::: ## Question 6 Fill in the blanks in the following code to produce a function that computes the area of a rectangle: ``` fun area(width :: [intype1], height :: [intype2]) -> [outtype]: [body] end ``` intype1: ________ :::spoiler Answer for intype1 Number `width` is a Number because it can be multiplied with `height` to get an area. If it were another type, the multiplication would fail. ::: intype2: ________ :::spoiler Answer for intype2 Number `height` is a Number because it can be multiplied with width to get an area. If it were another type, the multiplication would `fail` ::: outtype: _______ :::spoiler Answer for outtype (Number) The area of a rectangle is a Number value. When we multiply two Numbers, we get a different Number. ::: body: ________ :::spoiler Answer for body (width * height) The area of a rectangle is defined by the width multiplied by the height, and we know that this will result in a Number which is returned from the function. ::: ## Question 7 The documentation for rotate in the image library shows the following input/output type summary: ``` rotate :: ( angle :: Number, img :: Image) -> Image ``` Which of the following could be provided as the angle input, according to the types? Check all that apply. [ ] rectangle(20, 60, "outline", "yellow") [ ] 42 [ ] (25 / 2) [ ] "90 degrees" [ ] 23.8 [ ] "90" [ ] -50 [ ] num-min(95, 6 * 22) [ ] 1064 :::spoiler Answer [ ] rectangle(20, 60, "outline", "yellow") [X] 42 [X] (25 / 2) [ ] "90 degrees" [X] 23.8 [ ] "90" [X] -50 [X] num-min(95, 6 * 22) [X] 1064 Any number is valid for an input marked as type Number. Even though some numbers might not make sense (angles over 360, for example), the Number type simply requires a Number value or an expression that evaluates (computes to) a Number. Explanations for incorrect answers: * rectangle(20, 60, "outline", "yellow"): This produces an image, but the output must be a Number. * "90 degrees" : This is a string that describes an angle, but is not a Number. * "90" : This is a string containing a number, but is not a Number.