Hint: Read the Getting Started Tips, Programming Basics Tips, and Functions 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.

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.

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.

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.

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. So:

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. Hint 2: This is directly related to the numberOfPoolBalls(rows) homework problem!

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:

londonHour dcHour(londonHour)
0 (midnight) 7 (7pm)
10 (10am) 5 (5am)
12 (noon) 7 (7am)
17 (5pm) 12 (noon)
18 (6pm) 1 (1pm)

Note: the hardest part is when it is 12 o’clock in DC.

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:


EMSE 6574, Sec. 11: Programming for Analytics (Fall 2019)
George Washington University | School of Engineering & Applied Science
Dr. John Paul Helveston | jph@gwu.edu | Mondays | 6:10–8:40 PM | Phillips Hall 108 | |
This work is licensed under a Creative Commons Attribution 4.0 International License.
See the licensing page for more details about copyright information.
Content 2019 John Paul Helveston