owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
25 May 2023
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 | ***
Rhea Maesele |
Emma Cartuyvels|
Luc De Bruyn |
Nele Mullens | *
Els Lommelen | *
Heleen Deroo | *
Anja Leyman |
Pieter Huybrechts| **
Ward Langeraert |**
Cécile Herr |*
Amber Mertens | *
## Challenge 1
### solution Ward: debug()
```r
evenOdd <- function(n) {
char_n <- as.character(n)
counter_even <- 0
counter_odd <- 0
for (i in seq_len(nchar(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))
}
```
### solution Pieter: breakpoints
```r
evenOdd <- function(n) {
char_n <- as.character(n)
counter_even <- 0
counter_odd <- 0
for (i in stringr::str_split(char_n, "", simplify = TRUE)) {
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))
}
```
### Solution Emma: debug()
```r
evenOdd <- function(n) {
char_n <- as.character(n)
counter_even <- 0
counter_odd <- 0
for (i in 1:nchar(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))
}
```
### Solution Nele: breakpoints
```r
evenOdd <- function(n) {
char_n <- as.numeric(strsplit(as.character(n),"")[[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))
}
```
### solution Els: debug()
```r
evenOdd <- function(n) {
char_n <- 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))
}
```
## Challenge 2
### Solution Ward: debug()
```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
out <- as.numeric(paste(n_odd_even_total, collapse = "")) # add this line
return(out) # modify this line
}
steps <- function(n) {
n_steps <- 0 # add this line
while (n != 123) {
n <- step(n)
n_steps <- n_steps + 1
}
return(n_steps)
}
```
### Solution Emma: debug()
```r
#' Function step() takes a positive integer:
#' 886328712442992
#'
#' Write down a positive integer:
#' 398473234
#' Count up the number of even and odd digits, and the total number of digits:
#' 4 5 9
#' String the digits of those three numbers together to make a new number:
#' 459
#' Return it as a number
#' @examples
#' step(398473234)
#' 459
#' step(459)
#' 123
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
number <- toolbox::pasteCols(n_odd_even_total, use_paste0 = TRUE)
number <- as.integer(number)
return(number)
}
#'Function `steps` takes an integer and return how
#' many steps you need before the number 123 is reached.
#' @examples
#' steps(398473234)
#' 2
#' steps(1)
#' 5
#' steps(2)
#' 2
steps <- function(n) {
n_steps <- 0
while (n != 123) {
n <- step(n)
n_steps <- n_steps + 1
}
return(n_steps)
}
```
### Solution Pieter: breakpoints
```r
steps <- function(n) {
n_steps <- 0
while (n != 123) {
n <- as.integer(paste0(step(n), collapse = ""))
n_steps <- n_steps + 1
}
return(n_steps)
}
```
### Solution Rhea: debug
```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
new <- paste0(n_odd_even_total$n_even,n_odd_even_total$n_odd,n_odd_even_total$n_total)
new_number <- as.integer(new)
return(new_number)
}
steps <- function(n) {
n_steps <- 0
while (n != 123) {
n <- step(n)
n_steps <- n_steps + 1
}
return(n_steps)
}
```
### Solution Margot: breakpoint
```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
output <- as.integer(str_c(n_odd_even_total, collapse = ""))
return(output)
}
```
steps <- function(n) {
n_steps <- 0
while (n != 123) {
n <- step(n)
n_steps <- n_steps + 1
}
return(n_steps)
}
## Challenge 3
### Pieter's Solution
```r
assertthat::assert_that(is.numeric(dist_threshold))
```
### Solution Emma
```
get_migrations <- function(df,
dist_threshold,
speed_threshold) {
assertthat::assert_that(
is.numeric(dist_threshold),
msg = "Invalid type of input for dist_threshold: only numeric vectors allowed."
)
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)
}
```
### Solution Rhea
```r
assertthat::assert_that(
is.data.frame(df),
msg = "Invalid type of input: df must be a dateframe."
)
assertthat::assert_that(
is.numeric(dist_threshold),
msg = "Invalid type of input: only numeric vectors allowed."
)
```
```
assertthat::assert_that(all(c("arrival", "totaldistance_m") %in% names(df)))
```