-
Notifications
You must be signed in to change notification settings - Fork 2
/
hw4-conditionals-testing.Rmd
70 lines (53 loc) · 4.05 KB
/
hw4-conditionals-testing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "Homework 4 - Conditionals & Testing"
output:
html_document:
number_sections: false
toc: no
---
> **Due**: 28 September by 11:00 pm
>
> **Weight**: This assignment is worth **4%** of your final grade.
>
> **Purpose, Skills, & Knowledge**: The purposes of this assignment are:
>
> - To practice using conditional statements while writing functions in R.
> - To practice the skill of writing and using test functions to understand the problem.
> - To practice computational problem solving.
>
> **Assessment**: Each question indicates the % of the assignment grade, summing to 100%. The credit for each question will be assigned as follows:
>
> - 0% for not attempting a response.
> - 50% for attempting the question but with _major_ errors.
> - 75% for attempting the question but with _minor_ errors.
> - 100% for correctly answering the question.
>
> **Rules**:
>
> - Problems marked **SOLO** may not be worked on with other classmates, though you may consult instructors for help.
> - For problems marked **COLLABORATIVE**, you may work in groups of up to 3 students who are in this course this semester. You may not split up the work -- everyone must work on every problem. And you may not simply copy any code but rather truly work together.
> - Even though you work collaboratively, you still must submit your own solutions.
### 1) Staying organized [SOLO, 5%]
Download and use [this template](templates/hw4.zip) for your assignment. Inside the "hw4" folder, open and edit the R script called "hw4.R" and fill out your name, GW Net ID, and the names of anyone you worked with on this assignment.
> ### **Writing test functions**
> For each of the following functions, write a test function first, and then write the function. **Your test functions will count for half of the available credit for each problem**. Think carefully about the test cases to include in your test functions.
### 2) `guessAnimal(hasFourLegs, climbsTrees)` [SOLO, 15%]
Write the function `guessAnimal(hasFourLegs, climbsTrees)` that returns a statement guessing an animal based on whether it has four legs and whether it climbs trees. Here is the expected behavior:
<div style="width:500px">
`hasFourLegs` | `climbsTrees` | statement
--------------|---------------|-------------
`TRUE` | `TRUE` | "It's probably a cat"
`TRUE` | `FALSE` | "It's probably a dog"
`FALSE` | `TRUE` | "It's probably a snake"
`FALSE` | `FALSE` | "Hmm, I'm not sure"
</div>
### 3) `isPositiveMultipleOf4Or7(n)` [SOLO, 15%]
Write the function `isPositiveMultipleOf4Or7(n)` that returns `TRUE` if `n` is a positive multiple of 4 or 7 and `FALSE` otherwise. Note than `n` could be any data type.
### 4) `isEvenPositiveInt(x)` [COLLABORATIVE, 20%]
Given an arbitrary value `x`, return `TRUE` if it is an integer, and it is positive, and it is even (all 3 must be true), or `FALSE` otherwise. If the value `x` is not an integer, the function should return `FALSE` rather than error. So, `isEvenPositiveInt("yikes!")` returns `FALSE`, and `isEvenPositiveInt(123456)` returns `TRUE`.
### 5) `isLeapYear(year)` [SOLO, 20%]
Write the function `isLeapYear(year)` that returns `TRUE` if `year` is a [leap year](https://www.mathsisfun.com/leap-years.html) and `FALSE` otherwise. Assume that `year` is a positive integer.
### 6) `getTheCents(n)` [COLLABORATIVE, 20%]
Write the function `getTheCents(n)` which takes a value `n` that represents a payment in US dollars and returns the number of cents in the payment. For example, if `n` is `2.45`, the function should return `45`. If `n` is an integer, the function should return `0`, as it has `0` cents; if it isn't a number, it should return `NULL`, because a non-number payment make no cents (ha!). If the payment has partial cents (for example, `3.953`), it should be rounded to the nearest cent (in this case, `95` cents).
### 7) Submit your files [SOLO, 5%]
Create a zip file of all the files in your R project folder for this assignment and submit the zip file on Blackboard (note: to receive full credit, your submission must follow the above format of using a correctly-named R Project and `.R` script).