Last active
November 25, 2018 20:10
-
-
Save Lazzlo2096/f32d5311f31e2318ae5f1685bd12d1dd to your computer and use it in GitHub Desktop.
Rust Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn dna_to_rna(dna: &str) -> String { | |
// dna.chars().map(|c| match c { 'T' => 'U', _ => c } ).collect() | |
// dna.chars().map(|c| { match c { 'T' => 'U', c => c, } } ).collect() | |
dna.replace("T", "U") | |
} | |
// https://www.codewars.com/kata/reviews/5824a92c5ce9e05d7700000e/groups/5be0706b2934eb31570049b1 | |
// https://www.codewars.com/kata/reviews/5824a92c5ce9e05d7700000e/groups/5b7aba0b0da8ff76920006e4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn count_red_beads(n: u32) -> u32 { | |
if n==0 { 0 } else { (n-1)*2 } | |
} | |
//---- | |
// https://www.codewars.com/kata/reviews/5b76e825a20d72759f001f5b/groups/5b7733d957c6b9f88e0012ad | |
fn count_red_beads(n: u32) -> u32 { | |
if n <= 2 { | |
return 0 | |
} | |
(n - 1) * 2 | |
} | |
//================= | |
fn count_red_beads(n: u32) -> u32 { | |
match n { | |
0 | 1 => 0, | |
_ => (n-1)*2 | |
} | |
} | |
fn count_red_beads(n: u32) -> u32 { | |
match n { | |
n if n < 2 => 0, | |
_ => (n-1)*2, | |
} | |
} | |
fn count_red_beads(n: u32) -> u32 { | |
match n { | |
n if n > 1 => (n - 1) * 2, | |
_ => 0 | |
} | |
} | |
//========== | |
fn count_red_beads(n: u32) -> u32 { | |
2 * n.checked_sub(1).unwrap_or(0) | |
} | |
fn count_red_beads(n: u32) -> u32 { | |
n.checked_sub(1).map(|n| n * 2).unwrap_or(0) | |
} | |
fn count_red_beads(n: u32) -> u32 { | |
2 * n.saturating_sub(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment