From e6309177f361a4cd0396d742d8e39c328a051fbe Mon Sep 17 00:00:00 2001 From: William Perron Date: Tue, 7 Mar 2023 09:42:06 -0500 Subject: [PATCH] add word scrambling (issue #290) --- Cargo.lock | 72 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + scramble/Cargo.toml | 9 ++++++ scramble/src/main.rs | 38 +++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 scramble/Cargo.toml create mode 100644 scramble/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 1acaa6d..46139b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,78 @@ version = 3 name = "balance" version = "0.1.0" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "repeated-groups" version = "0.1.0" + +[[package]] +name = "scramble" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index d919de0..9ea4962 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,5 @@ members = [ "balance", "repeated-groups", + "scramble", ] diff --git a/scramble/Cargo.toml b/scramble/Cargo.toml new file mode 100644 index 0000000..1b3db98 --- /dev/null +++ b/scramble/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "scramble" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/scramble/src/main.rs b/scramble/src/main.rs new file mode 100644 index 0000000..1731970 --- /dev/null +++ b/scramble/src/main.rs @@ -0,0 +1,38 @@ +use rand::seq::SliceRandom; + +/// If you mix up the order of letters in a word, many people can slitl raed and urenadnstd tehm. Write a function that +/// takes an input sentence, and mixes up the insides of words (anything longer than 3 letters). +/// +/// Example: +/// +/// ``` +/// > scramble(["A quick brown fox jumped over the lazy dog."]) +/// > "A qciuk bwron fox jmepud oevr the lzay dog." +/// ``` + +fn main() { + let mut sentence = "A quick brown fox jumped over the lazy dog.".to_string(); + scramble(&mut sentence); + println!("{}", sentence); +} + +fn scramble(sentence: &mut String) { + let mut i = 0; + while i < sentence.len() { + if let Some(word_len) = sentence.get(i..).unwrap().find(' ') { + if word_len >= 4 { + let inner = sentence.get_mut(i + 1..i + word_len - 1).unwrap(); + + // shuffle it + let mut rng = rand::thread_rng(); + unsafe { + inner.as_bytes_mut().shuffle(&mut rng); + } + } + + i = i + word_len + 1; + } else { + break; + } + } +}