Exercise W1-1 - Temperature Conversion

convert_temperature <- function(x, F_to_C=FALSE) {
  if(F_to_C == FALSE){
    y <- (x * 9 / 5) + 32
  }else if (F_to_C == TRUE){
    y <- (x - 32) * 5 / 9
  }else {y <- 'parameter error'}
  return(y)
}

convert_temperature (24)
## [1] 75.2
convert_temperature (109, F_to_C=TRUE)
## [1] 42.77778
convert_temperature (109, F_to_C='a')
## [1] "parameter error"

Exercise W1-2 - Future Value

future_value <- function(x, i, t) {
  if(is.numeric(x) == 1 & is.numeric(i) == 1 & is.numeric(t) == 1){
    x * (1 + i)^t
  }
  else {'Please make sure the values you entered is a number'}
}

future_value(100,0.07,5)
## [1] 140.2552
future_value(100,'a',5)
## [1] "Please make sure the values you entered is a number"

Exercise W1-3 - Color Hex Code

First we need to learn the composition of hex codes:

Wikipedia definition of the structure for RGB to hexadecimal under the header Converting RGB to hexadecimal

generate_hex_code <- function(x) {
  
  for (b in c(1:x)) {
    y <- runif(x)
    y <- y * 256
    y <- trunc(y)
    i <- trunc(y/16)
    r <- y %% 16
    i <- data.frame(i)
    r <- data.frame(r)
    i[i == "10"] <- 'A'
    r[r == "10"] <- 'A'
    i[i == "11"] <- 'B'
    r[r == "11"] <- 'B'
    i[i == "12"] <- 'C'
    r[r == "12"] <- 'C'
    i[i == "13"] <- 'D'
    r[r == "13"] <- 'D'
    i[i == "14"] <- 'E'
    r[r == "14"] <- 'E'
    i[i == "15"] <- 'F'
    r[r == "15"] <- 'F'
    z <- data.frame(as.list(i),as.list(r)) %>% unite(RGB, i,r,sep='')
    
    c <- paste(z[1,1], z[2,1], z[3,1])
    print(c)
  }
}


generate_hex_code(5)
## [1] "0B B7 E5"
## [1] "E3 9B 2F"
## [1] "C8 87 D7"
## [1] "D3 3E B2"
## [1] "20 4D 67"