8  Exercises I

8.1 Exercises 1: Writing functions

E1.1 IsMonth

Write a function IsMonth which takes a single numerical value as input and returns TRUE iff that value may represent a month of the year.

IsMonth <- function (x) {
    # your code goes here
}
# Check if your implementation is correct with the followings
IsMonth(12.9) # FALSE
IsMonth(12)   # TRUE
IsMonth(1)    # TRUE
IsMonth(-10)  # FALSE

E1.2 IsLeapYear

Write a function IsLeapYear which takes a single numerical value as input and returns TRUE iff that value may represent a leap year. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

IsLeapYear <- function (x) {
    # your code goes here
}
# Check if your implementation is correct with the followings
IsLeapYear(2000) # TRUE
IsLeapYear(1900) # FALSE

E1.3 Bmi

Write a function Bmi which calculates the Body Mass Index (BMI) given weight (in kilograms) and height (in meters) as inputs.

\[bmi =\frac{weight}{height^2}\]

Bmi <- function(weight, height) {
  # your code goes here
}
# Check if your implementation is correct with the followings
Bmi(weight = 90, height = 1.75) # 29.38776

E1.4 FeetToMeters

Write a function FeetToMeters which converts a numeric value in feet to meters. Given \[1 ~ feet = 0.3048 ~ meters\]

FeetToMeters <- function(ft) {
  # your code goes here
}
# if your code is correct, this expression should evaluate to TRUE
all.equal(FeetToMeters(10), 3.048) 

E1.5 SqFeetToSqMeters

Use your implementation of FeetToMeters to convert a numeric value from square feet to square meters.

SqFeetToSqMeters <- function(sq_ft) {
  # your code goes here
}
# if your code is correct, this expression should evaluate to TRUE
all.equal(SqFeetToSqMeters(100), 9.290304)

8.2 Exercises 2: Control flow

E2.1 SumMultiples

Problem 1, Project Euler: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a function SumMultiples to find the sum of all the multiples of 3 or 5 below 1000.

SumMultiples <- function(factor_1 = 3, factor_2 = 5, threshold = 1000) {
  # your code goes here
}
# test your code with the following calls
SumMultiples(threshold = 10) # 23
SumMultiples() # 233168

E2.2 SumEvenFib

Problem 2, Project Euler: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

\[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\]

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

SumEvenFib <- function(max_fib) {
  # your code goes here
}
# test your code with the following calls
SumEvenFib(4e6) # 4613732

E2.3 Hadley, 6.6.1.1

Hadley, 6.6.1.1: Explain the following results:

sum(1, 2, 3)  # 6
mean(1, 2, 3) # 1

sum(1, 2, 3, na.omit = TRUE)  # 7
mean(1, 2, 3, na.omit = TRUE) # 1

E2.4 Hadley, 6.5.4.3

Hadley, 6.5.4.3: Without running the following code, answer the questions in the comment:

y <- 10
f1 <- function(x = {y <- 1; 2}, y = 0) {
  return(x + y)
}
x <- f1() # 1. what is the value of x here?
y         # 2. what is the value of y here?

E2.5 IsPalindrome

A palindrome is a sequence of characters which reads the same backward as forward. A number is a palindrome if its value is the same if it were written backwards. For example, 121, 53935, 11 are all palindrome numbers.

Write a function IsPalindrome which takes an integer x as input and returns TRUE iff it is a palindrome.

IsPalindrome <- function(x) {
  # your code
}
stopifnot(
  IsPalindrome(0)    &&
  IsPalindrome(1)    &&
  IsPalindrome(11)   &&
  IsPalindrome(121)  &&
  IsPalindrome(5412145)  &&
  !IsPalindrome(-121) &&
  !IsPalindrome(1231) &&
  !IsPalindrome(123)
)
print("Passed all tests")

E2.6 MaxPalindrome

Problem 4, Project Euler The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.

MaxPalindrome <- function() {
  # your code
}
print(MaxPalindrome()) # 906609