-
Notifications
You must be signed in to change notification settings - Fork 1
/
P4-conditionals-testing.Rmd
76 lines (58 loc) · 1.51 KB
/
P4-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
71
72
73
74
75
76
---
title: "Practice 4 - Conditionals & Testing"
output: html_document
---
```{r setup, echo=FALSE, message=FALSE, warning=FALSE}
rm(list=objects()) # start with a clean workspace
source("knitr_setup.R")
```
# Code tracing
### 1)
After running this code, what is the value of `a` and `b`?
```{r, eval = FALSE}
f <- function(x) {
x = x + 1
if ((x %% 2) == 0) {
x = x - 1
}
x = 2*x
}
a <- f(7)
b <- f(12)
```
### 2)
Write the output of this code by hand:
```{r, eval = FALSE}
f <- function(x) {
if ((x %% 3) == 0) {
cat('woo!')
return(x %/% 3)
}
return(x %% 2)
}
cat(f(9))
cat(f(11))
```
### 3)
Write the output of this code by hand:
```{r, eval = FALSE}
f <- function(x) {
if (x > 0) {
cat('abc')
x = 2*x
} else if (x <= 0) {
x = abs(x)
cat('cba')
}
cat(x)
}
cat(f(-9))
cat(f(15))
```
# Write functions
Here are some functions you should be able to write. Any of these may appear (directly or modified) on a quiz or exam! Your function should be able to pass the test functions provided.
## `isPositiveMultipleOf4Or7(n)`
### a)
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.
### b)
Write the test function `testIsPositiveMultipleOf4Or7()` that tests the function `isPositiveMultipleOf4Or7(n)` for a variety of values of `n`. Consider cases that you might not expect, such as cases where `n` is not a number.