19: Improve performance of solution for Rust track exercise "Beer Song" at the cost of duplication.
[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Aug 24, 2021, 12:24 PM
Z4YP3EEKPFAFPS7F3UUOHECS65V3QFBQPAGXMHY3HEWQA2R6PA4QCDependencies
- [2]
7NUHCTMK16: Apply `cargo fmt` and `cargo clippy` to the solution for Rust track exercise "Beer Song". Also add previous versions. - [3]
QK6XE5XF13: Add Rust track exercises "Beer Song", "Proverb", "Difference Of Squares", "Sum Of Multiples", "Grains", "Prime Factors", "Armstrong Numbers" and "Reverse String". - [4]
6DOVDD6615: Add solution for Rust track exercise "Beer Song".
Change contents
- replacement in rust/beer-song/src/lib.rs at line 3[3.1565]→[3.1565:1592](∅→∅),[3.1592]→[2.10762:10852](∅→∅),[2.10852]→[3.1657:1675](∅→∅),[3.1657]→[3.1657:1675](∅→∅),[3.1675]→[2.10853:11191](∅→∅),[2.11191]→[3.1931:1934](∅→∅),[3.1931]→[3.1931:1934](∅→∅)
const DICT: [&str; 24] = ["N", // 0"o m", // 1"ore", // 2" bottle", // 3"s", // 4" of beer", // 5" on", // 6" th", // 7"e ", // 8"wall", // 9", ", // 10"n", // 11".\n", // 12"Go to", // 13"st", // 14" and ", // 15"buy som", // 16"m", // 17"99", // 18"Take", // 19" it ", // 20"down", // 21"pass", // 22"around", // 23];pub fn verse(n: u32) -> String {match n {0 => "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n".to_string(),1 => "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n".to_string(),2 => "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n".to_string(),_ => format!("{0} bottles of beer on the wall, {0} bottles of beer.\nTake one down and pass it around, {1} bottles of beer on the wall.\n", n, n - 1),}} - edit in rust/beer-song/src/lib.rs at line 18
struct IndicesWithPrefixes<'a>(&'a [(&'a str, &'a [usize])]); - replacement in rust/beer-song/src/lib.rs at line 19[3.2257]→[3.2257:2467](∅→∅),[3.2467]→[2.11192:11223](∅→∅),[2.11223]→[3.2494:2648](∅→∅),[3.2494]→[3.2494:2648](∅→∅),[3.2648]→[3.37669:37702](∅→∅),[3.37668]→[3.37669:37702](∅→∅),[3.37702]→[3.2649:2721](∅→∅),[3.2721]→[2.11224:11428](∅→∅),[2.11428]→[3.2903:2956](∅→∅),[3.2903]→[3.2903:2956](∅→∅),[3.2956]→[2.11429:11653](∅→∅),[2.11653]→[3.3131:3212](∅→∅),[3.3131]→[3.3131:3212](∅→∅),[3.3212]→[2.11654:11862](∅→∅),[2.11862]→[3.3398:3479](∅→∅),[3.3398]→[3.3398:3479](∅→∅),[3.3479]→[2.11863:12074](∅→∅),[2.12074]→[3.3668:3678](∅→∅),[3.3668]→[3.3668:3678](∅→∅)
impl IndicesWithPrefixes<'_> {fn generate_verse(&self, n: u32) -> String {let n = n as usize;let mut out = String::with_capacity(if n > 2 {LENGTHS[3] + 2 * integer_length(n) + integer_length(n - 1)} else {LENGTHS[n]});self.0.iter().for_each(|(prefix, indices)| {out.push_str(prefix);indices.iter().for_each(|index| out.push_str(DICT[*index]));});out}}pub fn verse(n: u32) -> String {match n {0 => {let mut out = String::with_capacity(LENGTHS[0]);[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 12, 13, 7, 8, 14, 2, 15, 16,8, 17, 2, 10, 18, 3, 4, 5, 6, 7, 8, 9, 12,].iter().for_each(|index| out.push_str(DICT[*index]));out}1 => {let n_string = n.to_string();IndicesWithPrefixes(&[(&n_string, &[3, 5, 6, 7, 8, 9, 10]),(&n_string,&[3, 5, 12, 19, 20, 21, 15, 22, 20, 23, 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12,],),]).generate_verse(n)}2 => {let (n_string, m_string) = (n.to_string(), (n - 1).to_string());IndicesWithPrefixes(&[(&n_string, &[3, 4, 5, 6, 7, 8, 9, 10]),(&n_string, &[3, 4, 5, 12, 19, 6, 8, 21, 15, 22, 20, 23, 10]),(&m_string, &[3, 5, 6, 7, 8, 9, 12]),]).generate_verse(n)}_ => {let (n_string, m_string) = (n.to_string(), (n - 1).to_string());IndicesWithPrefixes(&[(&n_string, &[3, 4, 5, 6, 7, 8, 9, 10]),(&n_string, &[3, 4, 5, 12, 19, 6, 8, 21, 15, 22, 20, 23, 10]),(&m_string, &[3, 4, 5, 6, 7, 8, 9, 12]),]).generate_verse(n)}}}// Might not work properly if results of `powi`s don't fit into the `usize` casts. - replacement in rust/beer-song/src/lib.rs at line 60
if start < end {out.push_str(&verse(e));(e + 1..=s).for_each(|a| {if start > end {out.push_str(&verse(s));(e..=s - 1).rev().for_each(|a| { - replacement in rust/beer-song/src/lib.rs at line 67
out.push_str(&verse(s));(e..=s - 1).rev().for_each(|a| {out.push_str(&verse(e));(e + 1..=s).for_each(|a| {