# INBO CODING CLUB 27 August 2024 Welcome! ## Share your code snippet If you want to share your code snippet, copy paste your snippet within a section of three backticks (```): As an **example**: ``` library(tidyverse) ``` (*you can copy paste this example and add your code further down*) ## Yellow sticky notes No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below). ## Participants Name | Challenges --- | --- Damiano Oldoni | *** Emma Cartuyvels| Wouter Depaepe |** Sanne Govaert | *** Rhea Maesele | Jorre Vannieuwenhuyze | Pieter Huybrechts|*** Soria Delva | Frank Huysentruyt | Marijke Thoonen | Adriaan Seynaeve| Cecilia Sensalari | Janne Adolf |* Jonas Bertels | Falk Mielke | * Lotte Pohl | ** Nick Dillen | * ## Challenge 1 ### Damiano's solution (example) Copy paste this section to show your solutions. ```r # dummy code print("This is how to insert code.") ``` ### Emma's solution I used debugOnce and chatGPT (Gemini didn't work). ```r evenOdd <- function(n) { char_n <- strsplit(as.character(n), split = "")[[1]] counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Pieters solution ```r evenOdd <- function(n) { char_n <- as.character(n) counter_even <- 0 counter_odd <- 0 for (i in stringr::str_split_1(char_n, pattern = "")) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Jorre I just looked at the code :-) ```r evenOdd <- function(n) { char_n <- as.character(n) |> str_split_1('') counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(c(n_even = counter_even, n_odd = counter_odd)) } ``` ### Sanne's solution ```r evenOdd <- function(n) { char_n <- stringr::str_split_1(as.character(n), "") counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Nick's solution Loop over chars in n was not working -> strsplit ```r evenOdd <- function(n) { char_n <- unlist(strsplit(as.character(n), "")) counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Franks solution I used browser() to locate the error and then ChatGPT for the solution ```r evenOdd <- function(n) { char_n <- as.character(n) counter_even <- 0 counter_odd <- 0 for (i in 1:nchar(as.character(char_n))) { digit <- as.integer(substr(char_n,i,i)) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Wouter (and GEMINI)'s solution ```r evenOdd <- function(n) { # Convert the number to a character string digits <- strsplit(as.character(n), "")[[1]] # Initialize counters for even and odd digits evenCount <- 0 oddCount <- 0 # Iterate over each digit for (digit in digits) { # Check if the digit is even or odd if (as.numeric(digit) %% 2 == 0) { evenCount <- evenCount + 1 } else { oddCount <- oddCount + 1 } } # Return a list containing the counts return(list(even = evenCount, odd = oddCount)) } ``` ### falk (opgelost met `print` ) ```r for (i in 1:nchar(char_n)) { this_char <- substr(char_n, i, i) digit <- as.integer(this_char) ``` ### Janne's solution Solved with breakpoints and "Next" and googling ("r split string into individual characters") ```r evenOdd <- function(n) { char_n <- as.character(n) %>% strsplit(., "") %>% unlist() counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Soria's solution (example) ```r evenOdd <- function(n) { char_list <- strsplit(as.character(n), "")[[1]] counter_even <- 0 counter_odd <- 0 for (i in seq_along(char_list)) { digit<-char_list[i]%>% as.integer() if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ### Lotte's solution ```r evenOdd <- function(n) { char_n <- unlist(strsplit(as.character(n), split = "")) # used debug (breakpoints) counter_even <- 0 counter_odd <- 0 for (i in char_n) { digit <- as.integer(i) if (digit %% 2 == 0) { counter_even <- counter_even + 1 } else { counter_odd <- counter_odd + 1 } } return(list(n_even = counter_even, n_odd = counter_odd)) } ``` ## Challenge 2 ### Sanne's solution ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits n_number <- paste0(n_odd_even_total$n_even, n_odd_even_total$n_odd, n_odd_even_total$n_total) %>% as.numeric() return(n_number) steps <- function(n) { n_steps <- 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } } ``` ### Wouter's solution ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits newNumber <- paste0(n_odd_even_total$even, n_odd_even_total$odd, n_odd_even_total$n_total) return(as.integer(newNumber)) } steps <- function(n) { n_steps <- 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Pieter's solution ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits # coerce our list into a number new_number <- as.numeric(paste(n_odd_even_total, collapse = "")) return(new_number) } steps <- function(n) { n_steps <- 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Jorre Used `browser()` ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even |> str_c(collapse='') |> str_c(total_digits) |> as.integer() } steps <- function(n) { n_steps = 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Emma's solution ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- paste0(n_odd_even[[1]], n_odd_even[[2]], total_digits) return(as.integer(n_odd_even_total)) } steps <- function(n) { n_steps = 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Janne's solution solved by looking at the function output / error message and code ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits n_odd_even_total_final <- n_odd_even_total %>% unlist() %>% paste(., collapse = "") %>% as.numeric(.) return(n_odd_even_total_final) } steps <- function(n) { n_steps <- 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Falk used `print()` ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits as_a_number <- paste(as.character(n_odd_even_total), collapse = '') return(as_a_number) # return(n_odd_even_total) } steps <- function(n) { n_steps <- 0 while (n != 123) { n <- step(n) n_steps <- n_steps + 1 } return(n_steps) } ``` ### Soria's solution ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even$n_total <- total_digits n_odd_even<-as.integer(paste(n_odd_even,collapse = "")) return(n_odd_even) } steps <- function(n) { while (n != 123) { n <- step(n) if (!exists("n_steps")){ n_steps<-1 }else{ n_steps <- n_steps + 1 } } return(n_steps) } ``` ### Lotte's solution (solved by debugging + chatGPT) ```r step <- function(n) { n_odd_even <- evenOdd(n) total_digits <- nchar(n) n_odd_even_total <- n_odd_even n_odd_even_total$n_total <- total_digits return(n_odd_even_total) } steps <- function(n) { n_steps <- 0 while (n != 123) { n <- as.numeric(paste(unlist(step(n)), collapse = "")) n_steps <- n_steps + 1 n_steps } return(n_steps) } ``` ### Marijke's & Chat GPT's solution ```r step <- function(n) { # Use evenOdd function to get the counts of even and odd digits n_odd_even <- evenOdd(n) # Get the total number of digits total_digits <- nchar(n) # Concatenate the counts into a single string result_str <- paste0(n_odd_even$n_even, n_odd_even$n_odd, total_digits) # Convert the string to a number and return it return(as.integer(result_str)) } steps <- function(n) { n_steps <- 0 # Initialize step counter while (n != 123) { n <- step(n) # Apply the step function n_steps <- n_steps + 1 # Increment step counter } return(n_steps) # Return the number of steps } ``` ### Nick's solution Debugging and dont like how the list behaves -> change output to character/integers ```r return(as.integer(paste0(x,y))) ``` Init n_steps =0 before `while` loop in `steps()` ## Challenge 3 ### Pieter's solution ```r get_migrations <- function(df, dist_threshold, speed_threshold) { assertthat::assert_that( is.numeric(dist_threshold) && dist_threshold > 0, msg = "distance threshold should be a positive number" ) assertthat::assert_that( is.numeric(speed_threshold) && speed_threshold > 0, msg = "speed threshold should be a positive number" ) assertthat::assert_that( "totaldistance_m" %in% colnames(df), msg = "totaldistance_m column not found in input data frame" ) df %>% mutate(dist_threshold = totaldistance_m + dist_threshold) %>% rowwise() %>% mutate(first_dist_to_use = custom_min( df$totaldistance_m[df$totaldistance_m >= dist_threshold])) %>% mutate(row_first_dist_to_use = if_else( !is.na(first_dist_to_use), which(df$totaldistance_m == first_dist_to_use)[1], NA)) %>% ungroup() %>% mutate(time_first_dist_to_use = if_else(!is.na(row_first_dist_to_use), df$arrival[row_first_dist_to_use], NA)) %>% mutate(delta_totdist = first_dist_to_use - totaldistance_m) %>% mutate(delta_t = as.numeric(as.duration(time_first_dist_to_use - departure))) %>% mutate(migration_speed = (delta_totdist / delta_t)) %>% mutate(downstream_migration = migration_speed >= speed_threshold) } ``` ### Sanne's solution ```r get_migrations <- function(df, dist_threshold, speed_threshold) { if (!is.numeric(dist_threshold)) { cli::cli_abort(c( "{.arg dist_threshold} must be numeric.", "i" = "{.arg dist_threshold} is an object of class {class(dist_threshold)}." )) } if (!"totaldistance_m" %in% names(df)){ cli::cli_abort(c( "Column `totaldistance_m`is missing from {.arg df}.", "i" = "Column names of {.arg df} are {names(df)}." )) } df %>% mutate(dist_threshold = totaldistance_m + dist_threshold) %>% rowwise() %>% mutate(first_dist_to_use = custom_min( df$totaldistance_m[df$totaldistance_m >= dist_threshold])) %>% mutate(row_first_dist_to_use = if_else( !is.na(first_dist_to_use), which(df$totaldistance_m == first_dist_to_use)[1], NA)) %>% ungroup() %>% mutate(time_first_dist_to_use = if_else(!is.na(row_first_dist_to_use), df$arrival[row_first_dist_to_use], NA)) %>% mutate(delta_totdist = first_dist_to_use - totaldistance_m) %>% mutate(delta_t = as.numeric(as.duration(time_first_dist_to_use - departure))) %>% mutate(migration_speed = (delta_totdist / delta_t)) %>% mutate(downstream_migration = migration_speed >= speed_threshold) } ``` ### Emma's solution ```r get_migrations <- function(df, dist_threshold, speed_threshold) { assertthat::assert_that( is.numeric(dist_threshold), is.numeric(speed_threshold), msg = "Invalid type of input: dist_threshold and speed_threshold should be numeric." ) assertthat::assert_that( "totaldistance_m" %in% colnames(df), msg = "A column with the name totaldistance_m should be present in your dataframe." ) df %>% mutate(dist_threshold = totaldistance_m + dist_threshold) %>% rowwise() %>% mutate(first_dist_to_use = custom_min( df$totaldistance_m[df$totaldistance_m >= dist_threshold])) %>% mutate(row_first_dist_to_use = if_else( !is.na(first_dist_to_use), which(df$totaldistance_m == first_dist_to_use)[1], NA)) %>% ungroup() %>% mutate(time_first_dist_to_use = if_else(!is.na(row_first_dist_to_use), df$arrival[row_first_dist_to_use], NA)) %>% mutate(delta_totdist = first_dist_to_use - totaldistance_m) %>% mutate(delta_t = as.numeric(as.duration(time_first_dist_to_use - departure))) %>% mutate(migration_speed = (delta_totdist / delta_t)) %>% mutate(downstream_migration = migration_speed >= speed_threshold) } ``` ## Bonus challenge ### Pieter's solution ```r # Check that n is a numeric vector of length == 1, a number. assertthat::assert_that( assertthat::is.number(n) ) assertthat::assert_that( is.integer(n), msg = "n should be an integer" ) ## Alternativly assertthat::assert_that(n > 0) ## Disallow large numbers assertthat::assert_that(n < 9999) ### Using order of magnitude instead assertthat::assert_that(floor(log10(n)) < 4, msg = "Number is too large") ```