Damiano Oldoni
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 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") ```

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully