#1.1
greeting <- function(name, city) {
  return(paste("Hi, I am", name, "and I am from", city))
}

#1.2
name <- "Isabel"
city <- "New York"
result <- greeting(name, city)
cat(result) 
## Hi, I am Isabel and I am from New York
#1.3

greeting_with_default <- function(name, city = "Seattle") {
  return(paste("Hi, I am", name, "and I am from", city))
}

name1 <- "Isabel"
result1 <- greeting_with_default(name1)
cat(result1)
## Hi, I am Isabel and I am from Seattle
#usage with a different city:
name2 <- "Isabel"
city2 <- "San Francisco"
result2 <- greeting_with_default(name2, city2)
cat(result2)
## Hi, I am Isabel and I am from San Francisco
#1.2.1

removeDigits <- function(strings) {
  cleaned_strings <- gsub("[0-9]", "", strings)
  return(cleaned_strings)
}
input_strings <- c("INFO 201", "CSE 142", "mps-803c", "K2-team '21")
result <- removeDigits(input_strings)
cat(result)
## INFO  CSE  mps-c K-team '
#2.1
movies <- c("Coraline", "Nimona", "Puss in Boots", "Megamind", "Superbad", "The Book of Life")
print(movies)
## [1] "Coraline"         "Nimona"           "Puss in Boots"    "Megamind"        
## [5] "Superbad"         "The Book of Life"
#2.2
top3 <- movies[1:3]
print(top3)
## [1] "Coraline"      "Nimona"        "Puss in Boots"
#2.3
excited <- paste(movies, "is a great movie!")
print(excited)
## [1] "Coraline is a great movie!"         "Nimona is a great movie!"          
## [3] "Puss in Boots is a great movie!"    "Megamind is a great movie!"        
## [5] "Superbad is a great movie!"         "The Book of Life is a great movie!"
#2.4
movies_except_4th <- movies[-4]
print(movies_except_4th)
## [1] "Coraline"         "Nimona"           "Puss in Boots"    "Superbad"        
## [5] "The Book of Life"
#2.5
numbers <- 70:79
#2.6
num_count <- length(numbers)
print(num_count)
## [1] 10
#2.7
mean_value <- mean(numbers)
print(mean_value)
## [1] 74.5
#2.8
sixty <- 60:69
#2.9
combined_numbers <- c(numbers, sixty)
print(combined_numbers)
##  [1] 70 71 72 73 74 75 76 77 78 79 60 61 62 63 64 65 66 67 68 69
#2.2.1
combined_numbers <- c(70:79, 60:69)
iLogical <- combined_numbers > 70
print(iLogical)
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#2.2.2
result_numbers <- combined_numbers[iLogical]
print(result_numbers)
## [1] 71 72 73 74 75 76 77 78 79
#2.2.3
# Compute the sum of iLogical
sum_iLogical <- sum(iLogical)
# Print the result and explain what it means
print(sum_iLogical)
## [1] 9
#2.2.4
count_numbers_above_70 <- length(combined_numbers[combined_numbers > 70])
print(count_numbers_above_70)
## [1] 9
#2.2.5
result_numbers_not_exceeding_70 <- combined_numbers[!iLogical]
print(result_numbers_not_exceeding_70)
##  [1] 70 60 61 62 63 64 65 66 67 68 69
#2.2.6
is_even <- combined_numbers %% 2 == 0
print(is_even)
##  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
## [13]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
#2.2.7
even_numbers <- combined_numbers[is_even]
print(even_numbers)
##  [1] 70 72 74 76 78 60 62 64 66 68
#2.3.1
numbers <- -5:5
print(numbers)
##  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5
#2.3.2
numbers[numbers < 0] <- 0
print(numbers)
##  [1] 0 0 0 0 0 0 1 2 3 4 5
#2.3.3
relu <- function(vector) {
  vector[vector < 0] <- 0
  return(vector)
}
#2.3.4
input_vector1 <- c(-3, 2, -1, 4, 0, -5)
result_vector1 <- relu(input_vector1)
print(result_vector1)
## [1] 0 2 0 4 0 0
#2.2.5
abs_value <- function(vector) {
  vector[vector < 0] <- -vector[vector < 0]
  return(vector)
}

input_vector1 <- c(-3, 2, -1, 4, 0, -5)
result_vector1 <- abs_value(input_vector1)
print(result_vector1)
## [1] 3 2 1 4 0 5
#2.4.1
students <- c("Bao-yu", "Dai-yu", "Tan-chun", "Xi-chun")
incomes <- c(30000, 60000, 90000, 120000)
#2.4.2
support_eligibility <- ifelse(incomes < 75000, 75000 - incomes, 0)
eligible_students <- students[support_eligibility > 0]
cat("Students eligible for support:", eligible_students, "\n")
## Students eligible for support: Bao-yu Dai-yu
#2.4.3
cat("Student Name\tEligibility for Support\n")
## Student Name Eligibility for Support
for (i in 1:length(students)) {
  cat(students[i], "\t\t", support_eligibility[i], "\n")
}
## Bao-yu        45000 
## Dai-yu        15000 
## Tan-chun          0 
## Xi-chun       0
#3.1
country <- list(
  name = "United States",
  capital = "Washington DC",
  population = 331000000,
  continent = "North America"
)
#3.2
country_names <- names(country)
print(country_names)
## [1] "name"       "capital"    "population" "continent"
#3.3
capital <- country$capital
#3.4
population <- country[["population"]]
print(population)
## [1] 3.31e+08
#3.5
component_name <- "area" 
value <- 1000000 
country[[component_name]] <- value