Lab 2 Arithmetic, Booleans, and Conditionals
Purpose: The purpose of this lab is to practice designing functions, data definitions, and larger programs.
References: Prologue: How To Program, Chapter 1: Arithmetic, Chapter 2: Functions and Programs
In your programming today, make sure to practice (1) writing signatures and purpose statements for every function you design, and (2) writing tests for all your functions, using check-expect.
Arithmetic of Many Kinds
Exercise 1 Hacker Emo Schneider wants to give you rewards points for each day you use his smartphone app. On the first day he gives you 100 points and then he gives you 50 additional points each day up to a maximum of 300 points. Define a function rewards which takes in the number of days you have used the app and returns the number of rewards points that should be given.
Exercise 2 An architect is wondering how a house will look at various sizes. Design a function draw-house which takes in a number, representing the size of a house and draws the house as follows:
the main part of the house is a square of the given size which is colored red.
the roof of the house is a triangle of the given size which is colored black. It should appear directly above the square.
the door of the house is a rectangle which has a width of 1/5 the given size and a height of 2/5 the given size which is colored yellow. It should be placed at the bottom of the square in the center.
Here is an example of a house drawing so you can see what we are looking for:
Don’t forget to require the image teachpack before you begin defining this function!
Exercise 3 Let’s make a (very simplified) Pig Latin translator! Define the function pigify which takes in a word and produces its pig latin equivalent. To do this, remove the first letter of the word, add it to the end, and then add "ay" to the end of that. So, for example, given "fundies" your function should produce "undiesfay". (Yes, we are aware that this is not the full extent of how Pig Latin works.)
Booleans and Conditionals
Exercise 4 Define the function begins-with-vowel? which takes a String and produces #t if the given String begins with a vowel ("a", "e", "i", "o", or "u"). Otherwise it returns #f. Your function should be able to accept any string, regardless of its length. Simplify as much as possible.
Exercise 5 Define the function consonant? which takes a String and returns #t if the given String is NOT a vowel ("a", "e", "i", "o", or "u"). Otherwise it returns #f. You may assume the given string consists only of lowercase letters (so anything other than a vowel is a consonant). Simplify as much as possible.
Exercise 6 Let’s try to make our Pig Latin translator a little better. Define the function pigify.v2 which takes in a word and produces its pig latin equivalent. Here’s the new steps we will use to translate:
If the word begins with a vowel ("a", "e", "i", "o", or "u") we will just add "ay" to the end of the word. For example, given the word "out" your function should produce "outay".
If the word begins with two consonants (e.g. "shush" or "thought") we will remove the first two letters from the word, add them to the end, and then add "ay" at the end of that. For example, given the word "shush" your function should return "ushshay".
Otherwise we will follow the steps in the exercise 3 to translate as normal.
You may assume that the given word has at least two characters.
Please submit the above exercise to the handin server by 10pm EDT on the day of lab.
Before You Go...
If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We love to teach and you will learn. It’s symbiotic!