Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active February 28, 2024 03:12
Show Gist options
  • Save chase-lambert/abc35d4a48579193ceac1dfcd037065e to your computer and use it in GitHub Desktop.
Save chase-lambert/abc35d4a48579193ceac1dfcd037065e to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 24.02.26
pub fn remove_digit(n: i32, digit: i32) -> i32 {
let digit_char = char::from_digit(digit as u32, 10).unwrap();
let n_str = n.to_string();
let mut max_num = 0;
for (i, c) in n_str.char_indices() {
if c == digit_char {
let new_num_str = [&n_str[..i], &n_str[i + 1..]].concat();
let new_num = new_num_str.parse::<i32>().unwrap();
if new_num > max_num {
max_num = new_num;
}
}
}
if max_num == 0 {
return n;
}
max_num
}
#[test]
fn remove_digit_test() {
assert_eq!(3415926, remove_digit(31415926, 1));
assert_eq!(231, remove_digit(1231, 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment