# 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.)


x=1
while (x<16) {
  if (x%%15 ==0) {
    print(paste0(x,'- Industrial Engineering'))
  }else if (x%%5==0) {
    print(paste0(x,'- Engineering'))
  }else if (x%%3==0) {
    print(paste0(x,'- Industrial'))
  } else {
    print(x)
  }
  x=x+1
}
## [1] 1
## [1] 2
## [1] "3- Industrial"
## [1] 4
## [1] "5- Engineering"
## [1] "6- Industrial"
## [1] 7
## [1] 8
## [1] "9- Industrial"
## [1] "10- Engineering"
## [1] 11
## [1] "12- Industrial"
## [1] 13
## [1] 14
## [1] "15- Industrial Engineering"
# Exercise 2 (Travelling Salesperson)

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
calculate_total_distance = function(distance_input, route_info) {
  total_distance=0
  for(x in 1:(length(route_info)-1)) {
    total_distance = total_distance + distance_input[route_info[x],route_info[x+1]]
  }
  return(paste0("Total Distance is ", total_distance))
}

calculate_total_distance(distance_input = distance_matrix, route_info = c(1,5,2,6,7,2,4,1))
## [1] "Total Distance is 143"
# Exercise 3 (Number Guessing Game)

my_num = floor(runif(1, min=1, max=100))
print("I have a number on my mind. It is between 1 and 100. Can you guess it in 10 trials?")
## [1] "I have a number on my mind. It is between 1 and 100. Can you guess it in 10 trials?"
for(i in 1:10){
  quess = readline("Enter a number between 1 and 100: ")
  if(quess>my_num){
    print("My number is lower than your guess. Try again...")
  } else if(quess<my_num){
    print("My number is higher than your guess. Try again...")
  } else if(quess==my_num){
    return("Congratulations! Correct answer.")
    break
  }
  print(paste0("Remaining guesses: ", 10-i))
}
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 9"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 8"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 7"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 6"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 5"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 4"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 3"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 2"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 1"
## Enter a number between 1 and 100: 
## [1] "My number is higher than your guess. Try again..."
## [1] "Remaining guesses: 0"
return("Game over :/")
## [1] "Game over :/"
# Exercise 4

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)

print(USDTRY[1:60])
##  [1] 7.0106 6.9695 6.9604 7.0850 6.9685 6.9900 7.0969 7.0576 7.0953 7.1821
## [11] 7.2739 7.2785 7.3347 7.2367 7.2643 7.1314 7.0973 7.0766 7.0123 7.0446
## [21] 7.1216 7.0721 6.8936 6.9392 6.8684 6.8825 6.9047 6.9036 6.9229 6.8890
## [31] 6.9900 7.0580 6.9712 7.0195 6.9244 6.9453 6.9466 6.9272 6.9856 7.0791
## [41] 7.1593 7.2734 7.2278 7.3125 7.3146 7.2119 7.2581 7.2988 7.2873 7.2906
## [51] 7.2874 7.2431 7.3553 7.3118 7.4006 7.2636 7.2429 7.1462 7.0799 7.0924
### 1
max_value = max(USDTRY)
min_value = min(USDTRY)
print(max_value)
## [1] 7.4006
print(min_value)
## [1] 6.8684
### 2
lowest_day = which.min(USDTRY)
highest_day = which.max(USDTRY)
print(highest_day)
## [1] 55
print(lowest_day)
## [1] 25
### 3
mean_value = mean(USDTRY)
std_value = sd(USDTRY, na.rm = FALSE)
print(mean_value)
## [1] 7.107803
print(std_value)
## [1] 0.1502218
### 4
which( USDTRY >= 7.3)
## [1] 13 44 45 53 54 55
### 5 - ANLAMADIMM
falls_tf <- c(3.7,USDTRY[-length(USDTRY)]) > USDTRY
print(falls_tf)
##  [1] FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
## [13] FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE
## [25]  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
## [37] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
## [49]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
print(USDTRY)
##  [1] 7.0106 6.9695 6.9604 7.0850 6.9685 6.9900 7.0969 7.0576 7.0953 7.1821
## [11] 7.2739 7.2785 7.3347 7.2367 7.2643 7.1314 7.0973 7.0766 7.0123 7.0446
## [21] 7.1216 7.0721 6.8936 6.9392 6.8684 6.8825 6.9047 6.9036 6.9229 6.8890
## [31] 6.9900 7.0580 6.9712 7.0195 6.9244 6.9453 6.9466 6.9272 6.9856 7.0791
## [41] 7.1593 7.2734 7.2278 7.3125 7.3146 7.2119 7.2581 7.2988 7.2873 7.2906
## [51] 7.2874 7.2431 7.3553 7.3118 7.4006 7.2636 7.2429 7.1462 7.0799 7.0924
falls_days<-sum(falls_tf)

### 6
sorted_USDTRY=sort(USDTRY, decreasing = TRUE)
sorted_USDTRY[10]
## [1] 7.2873
### 8 - ANLAMADIM
change_days<-which(abs(c(3.7,USDTRY[-length(USDTRY)]) - USDTRY)>=0.12)
print(change_days)
## [1]  1  4 16 23 56
### 9 - ANLAMADIM
perc_change <- USDTRY/c(3.7,USDTRY[-length(USDTRY)])
opportunity_day <- which.max(perc_change) - 1
perc_profit <- round(max(perc_change)-1,3)*100

### 10 - ANLAMADIM
my_capital <- 100
for(i in 1:length(USDTRY)){
  if(perc_change[i] > 1){
    my_capital <- my_capital*perc_change[i]
  }
}
net_profit <- my_capital - 100
print(net_profit)
## [1] 142.1088