-
Notifications
You must be signed in to change notification settings - Fork 1
/
P3-functions.Rmd
159 lines (127 loc) · 5.41 KB
/
P3-functions.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "Practice 3 - Functions"
output:
html_document:
number_sections: false
toc: no
---
```{r setup, echo=FALSE, message=FALSE, warning=FALSE}
rm(list=objects()) # start with a clean workspace
source("knitr_setup.R")
```
**Hint**: Read the [Getting Started Tips](L1.2-getting-started.html#tips), [Programming Basics Tips](L1.3-programming-basics.html#tips), and [Functions Tips](L3.1-functions.html#tips) - these could come in handy!
# Mystery Functions
In just a few words of plain English, what does each of the following functions do? Provide some explanation for your answer. Try to answer this first without copying the functions into R, then go ahead and copy them in and explore what they do.
```{r, eval=FALSE}
f <- function(x, y) {
epsilon = 0.0000001
d1 = abs(x)^2
d2 = abs(y)^0.5
return(abs(d1 - d2) < epsilon)
}
g <- function(x) {
return((x == as.integer(x)) & (x %/% 10 == x %% 10))
}
h <- function(x) {
return((x == as.integer(x)) & (x >= 0) &
((as.integer(round(x^0.5)))^2 == x))
}
```
# Write functions
Here are a bunch of 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.
### `celsiusToFahrenheit(celsius)`
Given a temperature in Celsius, return the same temperature in Fahrenheit. So `celsiusToFahrenheit(0)` returns `32`, and `celsiusToFahrenheit(100)` returns `212`.
```{r, eval=FALSE}
testCelsiusToFahrenheit <- function() {
cat("Testing celsiusToFahrenheit()...")
stopifnot(celsiusToFahrenheit(0) == 32)
stopifnot(celsiusToFahrenheit(100) == 212)
cat("Passed!\n")
}
```
### `isPerfectCube(x)`
Given an integer value `x`, returns `TRUE` if it is a perfect cube and `FALSE` otherwise. That is, return `TRUE` if there is another integer `y` such that `x = y^3`. Thus, `isPerfectCube(27)` returns `TRUE`, but `isPerfectCube(16)` returns `FALSE`.
```{r, eval=FALSE}
testIsPerfectCube <- function() {
cat("Testing isPerfectCube()...")
stopifnot(isPerfectCube(0) == TRUE)
stopifnot(isPerfectCube(125) == TRUE)
stopifnot(isPerfectCube(-125) == TRUE)
stopifnot(isPerfectCube(8) == TRUE)
stopifnot(isPerfectCube(-8) == TRUE)
stopifnot(isPerfectCube(27) == TRUE)
stopifnot(isPerfectCube(-27) == TRUE)
stopifnot(isPerfectCube(64) == TRUE)
stopifnot(isPerfectCube(63) == FALSE)
cat("Passed!\n")
}
```
### `kthDigit(x, k)`
Given two integers, `x` and `k`, return the kth digit of `x`, counting from the right. Negative numbers should work too.
```{r, eval=FALSE}
testKthDigit <- function() {
cat("Testing kthDigit()...")
stopifnot(kthDigit(789, 1) == 9)
stopifnot(kthDigit(789, 2) == 8)
stopifnot(kthDigit(789, 3) == 7)
stopifnot(kthDigit(789, 4) == 0)
stopifnot(kthDigit(-789, 1) == 9)
cat("Passed!\n")
}
```
### `nthFibonacciNumber(n)`
Write the function `nthFibonacciNumber(n)` that takes a positive integer `n` and (without using loops) returns the nth [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number). So:
```{r, eval=FALSE}
testNthFibonacciNumber <- function() {
cat("Testing nthFibonacciNumber()...")
stopifnot(nthFibonacciNumber(1) == 1)
stopifnot(nthFibonacciNumber(2) == 1)
stopifnot(nthFibonacciNumber(3) == 2)
stopifnot(nthFibonacciNumber(4) == 3)
stopifnot(nthFibonacciNumber(5) == 5)
stopifnot(nthFibonacciNumber(6) == 8)
cat("Passed!\n")
}
```
### `isTriangularNumber(n)`
A number is triangular if it equals `1+2+3+...+N` for some integer `N`. Given an integer value `n`, return `TRUE` if it is triangular, and `FALSE` otherwise. Note that this relates to the pool ball problems. Hint: For this problem you should research [Triangular Numbers](https://en.wikipedia.org/wiki/Triangular_number). Hint 2: This is directly related to the `numberOfPoolBalls(rows)` homework problem!
```{r, eval=FALSE}
testIsTriangularNumber <- function() {
cat("Testing isTriangularNumber()...")
stopifnot(isTriangularNumber(0) == TRUE)
stopifnot(isTriangularNumber(1) == TRUE)
stopifnot(isTriangularNumber(4) == FALSE)
stopifnot(isTriangularNumber(6) == TRUE)
stopifnot(isTriangularNumber(54) == FALSE)
stopifnot(isTriangularNumber(55) == TRUE)
stopifnot(isTriangularNumber(56) == FALSE)
cat("Passed!\n")
}
```
### `dcHour(londonHour)`
This function takes an integer, the current hour in London, and returns the current hour in Washington DC (which is 5 hours behind London). However, London time is given in 24-hour time (so `londonHour` is between 0 and 23, inclusive), but Washington DC time must be returned in 12-hour time (so the result must be between 1 and 12, inclusive, where "am" and "pm" are ignored). Here are some examples:
<div class="tableSmall">
`londonHour` | `dcHour(londonHour)`
-------------| -------------------
0 (midnight) | 7 (7pm)
10 (10am) | 5 (5am)
12 (noon) | 7 (7am)
17 (5pm) | 12 (noon)
18 (6pm) | 1 (1pm)
</div>
Note: the hardest part is when it is 12 o'clock in DC.
```{r, eval=FALSE}
testDcHour <- function() {
cat("Testing testDcHour()...")
stopifnot(dcHour(0) == 7)
stopifnot(dcHour(10) == 5)
stopifnot(dcHour(12) == 7)
stopifnot(dcHour(17) == 12)
stopifnot(dcHour(18) == 1)
cat("Passed!\n")
}
```
---
**Page sources**:
Some content on this page has been modified from other courses, including:
- CMU [15-112: Fundamentals of Programming](http://www.kosbie.net/cmu/spring-13/15-112/)