Exercises in this document can be completed after a basic R training. Your code’s output should be very similar to the output given below questions. Some commands have set.seed() function. Actions requiring randomness can be synced with randomness seeds.

Exercise 1 (FizzBuzz)

In a loop from 1 to 15, if a value can be exactly divided by 3, print “Industrial”; if by 5, print “Engineering”; if by 15, print “Industrial Engineering”. (p.s. You shouldn’t print Industrial or Engineering when the value can be exactly divided by 15). (Tip: You can use modulo operator (%%). For example 4%%3 is 1 and 4%%2 is 0.)

## [1] 1
## [1] 2
## [1] 3
## [1] "Industrial"
## [1] 4
## [1] 5
## [1] "Engineering"
## [1] 6
## [1] "Industrial"
## [1] 7
## [1] 8
## [1] 9
## [1] "Industrial"
## [1] 10
## [1] "Engineering"
## [1] 11
## [1] 12
## [1] "Industrial"
## [1] 13
## [1] 14
## [1] 15
## [1] "Industrial Engineering"
##                        1                        2                        3 
##                       ""                       ""             "Industrial" 
##                        4                        5                        6 
##                       ""            "Engineering"             "Industrial" 
##                        7                        8                        9 
##                       ""                       ""             "Industrial" 
##                       10                       11                       12 
##            "Engineering"                       ""             "Industrial" 
##                       13                       14                       15 
##                       ""                       "" "Industrial Engineering"

Exercise 2 (Travelling Salesperson)

You can see distances between 10 cities on the data set below.

#Copy paste the following lines to get the distance matrix
set.seed(2017)
distance_matrix<-matrix(sample(10:50,100,replace=TRUE),ncol=10)
distance_matrix[lower.tri(distance_matrix)]<-0
distance_matrix <- distance_matrix + t(distance_matrix)
diag(distance_matrix)<-0

distance_matrix
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    0   42   49   28   27   22   36   50   36    35
##  [2,]   42    0   31   17   16   18   19   26   19    38
##  [3,]   49   31    0   14   21   12   29   26   50    48
##  [4,]   28   17   14    0   28   33   32   23   30    25
##  [5,]   27   16   21   28    0   28   49   28   50    36
##  [6,]   22   18   12   33   28    0   18   15   10    15
##  [7,]   36   19   29   32   49   18    0   41   48    27
##  [8,]   50   26   26   23   28   15   41    0   22    25
##  [9,]   36   19   50   30   50   10   48   22    0    37
## [10,]   35   38   48   25   36   15   27   25   37     0

Consider a salesperson operating in these cities. For a given route create a function which will return the total distance that the salesperson need to cover. Use distance matrix as input.

Calculate the total distance of the route 1-5-2-6-7-2-4-1.

calculate_total_distance(distance_input=distance_matrix,route_info=c(1,5,2,6,7,2,4,1))
## [1] "Total distance: 143"

Exercise 3 (Number Guessing Game)

Have the function to choose a number between 1 and 100 and create a game where the user is asked to guess that number in at most 10 trials. At each trial report whether the guessed number is higher or lower than the actual number. It is either a win or a game over after 10 trials.

## [1] "I have a number on my mind. It is between 1 and 100. Can you guess it in 10 trials?"
## [1] "Enter a number between 1 and 100: 5"
## [1] "My number is higher than your guess."
## [1] "Low 6"
## [1] "Remaining guesses: 9"
## [1] "Enter a number between 1 and 100: 35"
## [1] "My number is lower than your guess."
## [1] "High 34"
## [1] "Remaining guesses: 8"
## [1] "Enter a number between 1 and 100: 9"
## [1] "My number is higher than your guess."
## [1] "Low 10"
## [1] "Remaining guesses: 7"
## [1] "Enter a number between 1 and 100: 14"
## [1] "My number is higher than your guess."
## [1] "Low 15"
## [1] "Remaining guesses: 6"
## [1] "Enter a number between 1 and 100: 32"
## [1] "My number is lower than your guess."
## [1] "High 31"
## [1] "Remaining guesses: 5"
## [1] "Enter a number between 1 and 100: 15"
## [1] "My number is higher than your guess."
## [1] "Low 16"
## [1] "Remaining guesses: 4"
## [1] "Enter a number between 1 and 100: 20"
## [1] "My number is higher than your guess."
## [1] "Low 21"
## [1] "Remaining guesses: 3"
## [1] "Enter a number between 1 and 100: 29"
## [1] "My number is lower than your guess."
## [1] "High 28"
## [1] "Remaining guesses: 2"
## [1] "Enter a number between 1 and 100: 21"
## [1] "My number is higher than your guess."
## [1] "Low 22"
## [1] "Remaining guesses: 1"
## [1] "Enter a number between 1 and 100: 22"
## [1] "My number is higher than your guess."
## [1] "Low 23"
## [1] "Remaining guesses: 0"
## [1] "Game Over :/"

Exercise 4

Assume today’s USD/TRY is 7.05. We calculate the next 60 days’ parity forecast as follows.

#Copy paste the two lines below
set.seed(135)
USDTRY<-round(7.05*exp(cumsum((0.02-0.5*(0.2^2))/252 + 0.2*rnorm(60)*sqrt(1/252))),4)

#Check the first 10 days with your data
print(USDTRY[1:10])
##  [1] 7.0106 6.9695 6.9604 7.0850 6.9685 6.9900 7.0969 7.0576 7.0953 7.1821

Calculate the following information below with the USDTRY data.

  1. Calculate highest and lowest values. (Lowest: 6.8684. Highest: 7.4006)
  2. Find the lowest and highest days. (Lowest: 25. gün. Highest: 55. gün.)
  3. Calculate the mean and the standard deviation of the forecast. (Mean: 7.1078. Std. dev.: 0.15.)
  4. Find the days where USDTRY is higher than or equal to 7.3. (13, 44, 45, 53, 54, 55)
  5. In how many of these days value of USDTRY fell compared to previous day? (Don’t forget to add today’s value) (27)
  6. What is the 10th highest value of USDTRY? (7.2873)
  7. Find the streak days. Find how many days USDTRY fell in a row, and state the dates. (4 days. From day 16. to 19.)
  8. Find the days where the value of USDTRY changed by 0.12 or more. (1, 4, 16, 23, 56)
  9. If I were to invest in USDTRY on a single day, which day would it be and what would be my percentage profit? (Day 0. 89.5 percent profit.)
  10. Let’s say I have 100 TRY today. What is the maximum amount of profit that I can make at the end of 60 days by buying and selling USDTRY daily? (142.11)