Exercism track submissions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

parallel letter frequency

+524
+41
rust/parallel-letter-frequency/.exercism/config.json
··· 1 + { 2 + "blurb": "Count the frequency of letters in texts using parallel computation.", 3 + "authors": [ 4 + "EduardoBautista" 5 + ], 6 + "contributors": [ 7 + "andrewclarkson", 8 + "ashleygwilliams", 9 + "ccouzens", 10 + "ClashTheBunny", 11 + "coriolinus", 12 + "cwhakes", 13 + "EduardoBautista", 14 + "efx", 15 + "ErikSchierboom", 16 + "etrepum", 17 + "glennpratt", 18 + "IanWhitney", 19 + "kytrinyx", 20 + "lutostag", 21 + "mkantor", 22 + "nfiles", 23 + "petertseng", 24 + "rofrol", 25 + "sjwarner-bp", 26 + "stringparser", 27 + "xakon", 28 + "ZapAnton" 29 + ], 30 + "files": { 31 + "solution": [ 32 + "src/lib.rs" 33 + ], 34 + "test": [ 35 + "tests/parallel-letter-frequency.rs" 36 + ], 37 + "example": [ 38 + ".meta/example.rs" 39 + ] 40 + } 41 + }
+8
rust/parallel-letter-frequency/.gitignore
··· 1 + # Generated by Cargo 2 + # will have compiled files and executables 3 + /target/ 4 + **/*.rs.bk 5 + 6 + # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 + # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 8 + Cargo.lock
+4
rust/parallel-letter-frequency/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "parallel-letter-frequency" 4 + version = "0.0.0"
+85
rust/parallel-letter-frequency/HELP.md
··· 1 + # Help 2 + 3 + ## Running the tests 4 + 5 + Execute the tests with: 6 + 7 + ```bash 8 + $ cargo test 9 + ``` 10 + 11 + All but the first test have been ignored. After you get the first test to 12 + pass, open the tests source file which is located in the `tests` directory 13 + and remove the `#[ignore]` flag from the next test and get the tests to pass 14 + again. Each separate test is a function with `#[test]` flag above it. 15 + Continue, until you pass every test. 16 + 17 + If you wish to run _only ignored_ tests without editing the tests source file, use: 18 + 19 + ```bash 20 + $ cargo test -- --ignored 21 + ``` 22 + 23 + If you are using Rust 1.51 or later, you can run _all_ tests with 24 + 25 + ```bash 26 + $ cargo test -- --include-ignored 27 + ``` 28 + 29 + To run a specific test, for example `some_test`, you can use: 30 + 31 + ```bash 32 + $ cargo test some_test 33 + ``` 34 + 35 + If the specific test is ignored, use: 36 + 37 + ```bash 38 + $ cargo test some_test -- --ignored 39 + ``` 40 + 41 + To learn more about Rust tests refer to the online [test documentation][rust-tests]. 42 + 43 + [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html 44 + 45 + ## Submitting your solution 46 + 47 + You can submit your solution using the `exercism submit src/lib.rs` command. 48 + This command will upload your solution to the Exercism website and print the solution page's URL. 49 + 50 + It's possible to submit an incomplete solution which allows you to: 51 + 52 + - See how others have completed the exercise 53 + - Request help from a mentor 54 + 55 + ## Need to get help? 56 + 57 + If you'd like help solving the exercise, check the following pages: 58 + 59 + - The [Rust track's documentation](https://exercism.org/docs/tracks/rust) 60 + - [Exercism's support channel on gitter](https://gitter.im/exercism/support) 61 + - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) 62 + 63 + Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. 64 + 65 + ## Rust Installation 66 + 67 + Refer to the [exercism help page][help-page] for Rust installation and learning 68 + resources. 69 + 70 + ## Submitting the solution 71 + 72 + Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. 73 + 74 + ## Feedback, Issues, Pull Requests 75 + 76 + The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! 77 + 78 + If you want to know more about Exercism, take a look at the [contribution guide]. 79 + 80 + ## Submitting Incomplete Solutions 81 + It's possible to submit an incomplete solution so you can see how others have completed the exercise. 82 + 83 + [help-page]: https://exercism.io/tracks/rust/learning 84 + [github]: https://github.com/exercism/rust 85 + [contribution guide]: https://exercism.io/docs/community/contributors
+75
rust/parallel-letter-frequency/README.md
··· 1 + # Parallel Letter Frequency 2 + 3 + Welcome to Parallel Letter Frequency on Exercism's Rust Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + Count the frequency of letters in texts using parallel computation. 9 + 10 + Parallelism is about doing things in parallel that can also be done 11 + sequentially. A common example is counting the frequency of letters. 12 + Create a function that returns the total frequency of each letter in a 13 + list of texts and that employs parallelism. 14 + 15 + Learn more about concurrency in Rust here: 16 + 17 + - [Concurrency](https://doc.rust-lang.org/book/ch16-00-concurrency.html) 18 + 19 + ## Bonus 20 + 21 + This exercise also includes a benchmark, with a sequential implementation as a 22 + baseline. You can compare your solution to the benchmark. Observe the 23 + effect different size inputs have on the performance of each. Can you 24 + surpass the benchmark using concurrent programming techniques? 25 + 26 + As of this writing, test::Bencher is unstable and only available on 27 + *nightly* Rust. Run the benchmarks with Cargo: 28 + 29 + ``` 30 + cargo bench 31 + ``` 32 + 33 + If you are using rustup.rs: 34 + 35 + ``` 36 + rustup run nightly cargo bench 37 + ``` 38 + 39 + - [Benchmark tests](https://doc.rust-lang.org/stable/unstable-book/library-features/test.html) 40 + 41 + Learn more about nightly Rust: 42 + 43 + - [Nightly Rust](https://doc.rust-lang.org/stable/book/2018-edition/appendix-06-nightly-rust.html) 44 + - [Rustup: Working with nightly](https://github.com/rust-lang-nursery/rustup.rs#working-with-nightly-rust) 45 + 46 + ## Source 47 + 48 + ### Created by 49 + 50 + - @EduardoBautista 51 + 52 + ### Contributed to by 53 + 54 + - @andrewclarkson 55 + - @ashleygwilliams 56 + - @ccouzens 57 + - @ClashTheBunny 58 + - @coriolinus 59 + - @cwhakes 60 + - @EduardoBautista 61 + - @efx 62 + - @ErikSchierboom 63 + - @etrepum 64 + - @glennpratt 65 + - @IanWhitney 66 + - @kytrinyx 67 + - @lutostag 68 + - @mkantor 69 + - @nfiles 70 + - @petertseng 71 + - @rofrol 72 + - @sjwarner-bp 73 + - @stringparser 74 + - @xakon 75 + - @ZapAnton
+102
rust/parallel-letter-frequency/benches/benchmark.rs
··· 1 + #![feature(test)] 2 + extern crate parallel_letter_frequency; 3 + extern crate test; 4 + 5 + use std::collections::HashMap; 6 + use test::Bencher; 7 + 8 + #[bench] 9 + fn bench_tiny_parallel(b: &mut Bencher) { 10 + let tiny = &["a"]; 11 + b.iter(|| parallel_letter_frequency::frequency(tiny, 3)); 12 + } 13 + 14 + #[bench] 15 + fn bench_tiny_sequential(b: &mut Bencher) { 16 + let tiny = &["a"]; 17 + b.iter(|| frequency(tiny)); 18 + } 19 + 20 + #[bench] 21 + fn bench_small_parallel(b: &mut Bencher) { 22 + let texts = all_texts(1); 23 + b.iter(|| parallel_letter_frequency::frequency(&texts, 3)); 24 + } 25 + 26 + #[bench] 27 + fn bench_small_sequential(b: &mut Bencher) { 28 + let texts = all_texts(1); 29 + b.iter(|| frequency(&texts)); 30 + } 31 + 32 + #[bench] 33 + fn bench_large_parallel(b: &mut Bencher) { 34 + let texts = all_texts(30); 35 + b.iter(|| parallel_letter_frequency::frequency(&texts, 3)); 36 + } 37 + 38 + #[bench] 39 + fn bench_large_sequential(b: &mut Bencher) { 40 + let texts = all_texts(30); 41 + b.iter(|| frequency(&texts)); 42 + } 43 + 44 + /// Simple sequential char frequency. Can it be beat? 45 + pub fn frequency(texts: &[&str]) -> HashMap<char, usize> { 46 + let mut map = HashMap::new(); 47 + 48 + for line in texts { 49 + for chr in line.chars().filter(|c| c.is_alphabetic()) { 50 + if let Some(c) = chr.to_lowercase().next() { 51 + (*map.entry(c).or_insert(0)) += 1; 52 + } 53 + } 54 + } 55 + 56 + map 57 + } 58 + 59 + fn all_texts(repeat: usize) -> Vec<&'static str> { 60 + [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER] 61 + .iter() 62 + .cycle() 63 + .take(3 * repeat) 64 + .flat_map(|anthem| anthem.iter().cloned()) 65 + .collect() 66 + } 67 + 68 + // Poem by Friedrich Schiller. The corresponding music is the European Anthem. 69 + pub const ODE_AN_DIE_FREUDE: [&str; 8] = [ 70 + "Freude schöner Götterfunken", 71 + "Tochter aus Elysium,", 72 + "Wir betreten feuertrunken,", 73 + "Himmlische, dein Heiligtum!", 74 + "Deine Zauber binden wieder", 75 + "Was die Mode streng geteilt;", 76 + "Alle Menschen werden Brüder,", 77 + "Wo dein sanfter Flügel weilt.", 78 + ]; 79 + 80 + // Dutch national anthem 81 + pub const WILHELMUS: [&str; 8] = [ 82 + "Wilhelmus van Nassouwe", 83 + "ben ik, van Duitsen bloed,", 84 + "den vaderland getrouwe", 85 + "blijf ik tot in den dood.", 86 + "Een Prinse van Oranje", 87 + "ben ik, vrij, onverveerd,", 88 + "den Koning van Hispanje", 89 + "heb ik altijd geëerd.", 90 + ]; 91 + 92 + // American national anthem 93 + pub const STAR_SPANGLED_BANNER: [&str; 8] = [ 94 + "O say can you see by the dawn's early light,", 95 + "What so proudly we hailed at the twilight's last gleaming,", 96 + "Whose broad stripes and bright stars through the perilous fight,", 97 + "O'er the ramparts we watched, were so gallantly streaming?", 98 + "And the rockets' red glare, the bombs bursting in air,", 99 + "Gave proof through the night that our flag was still there;", 100 + "O say does that star-spangled banner yet wave,", 101 + "O'er the land of the free and the home of the brave?", 102 + ];
+79
rust/parallel-letter-frequency/src/lib.rs
··· 1 + use std::{collections::HashMap, sync::mpsc::channel, thread}; 2 + 3 + fn split_evenly<T>(n: usize, xs: &[T]) -> impl Iterator<Item = &[T]> { 4 + struct _Iter<'a, T>(usize, &'a [T]); 5 + 6 + impl<'a, T> Iterator for _Iter<'a, T> { 7 + type Item = &'a [T]; 8 + 9 + fn next(&mut self) -> Option<Self::Item> { 10 + if self.1.len() == 0 { 11 + None 12 + } else if self.0 == 0 { 13 + Some(self.1) 14 + } else { 15 + let extra = if self.1.len() % self.0 == 0 { 0 } else { 1 }; 16 + let mid = self.1.len() / self.0 + extra; 17 + let (first, next) = self.1.split_at(mid); 18 + self.1 = next; 19 + self.0 -= 1; 20 + Some(first) 21 + } 22 + } 23 + } 24 + 25 + _Iter(n, xs) 26 + } 27 + 28 + fn process_str(inp: Vec<String>) -> Vec<(char, usize)> { 29 + inp.iter() 30 + .flat_map(|xs| { 31 + xs.to_lowercase() 32 + .chars() 33 + .filter_map(|c| { 34 + if c.is_alphabetic() { 35 + Some((c, 1)) 36 + } else { 37 + None 38 + } 39 + }) 40 + .collect::<Vec<_>>() 41 + }) 42 + .collect() 43 + } 44 + 45 + pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> { 46 + if input.is_empty() { 47 + HashMap::new() 48 + } else { 49 + let vss: Vec<Vec<String>> = split_evenly(worker_count, input) 50 + .collect::<Vec<&[&str]>>() 51 + .iter() 52 + .map(|&a| a.iter().map(|&a| String::from(a)).collect()) 53 + .collect(); 54 + 55 + let vss_c = vss.clone(); 56 + let (tx, rx) = channel(); 57 + 58 + vss.into_iter().for_each(|s| { 59 + let tx = tx.clone(); 60 + thread::spawn(move || { 61 + let cs: Vec<(char, usize)> = process_str(s); 62 + tx.send(cs).unwrap(); 63 + }); 64 + }); 65 + 66 + 67 + let mut r: HashMap<char, usize> = HashMap::new(); 68 + for _ in vss_c { 69 + r = match rx.recv() { 70 + Ok(v) => v.iter().fold(r, |mut acc, (c, count)| { 71 + acc.entry(*c).and_modify(|x| *x += 1).or_insert(*count); 72 + acc 73 + }), 74 + _ => r, 75 + } 76 + } 77 + r 78 + } 79 + }
+130
rust/parallel-letter-frequency/tests/parallel-letter-frequency.rs
··· 1 + use std::collections::HashMap; 2 + 3 + use parallel_letter_frequency as frequency; 4 + 5 + // Poem by Friedrich Schiller. The corresponding music is the European Anthem. 6 + const ODE_AN_DIE_FREUDE: [&str; 8] = [ 7 + "Freude schöner Götterfunken", 8 + "Tochter aus Elysium,", 9 + "Wir betreten feuertrunken,", 10 + "Himmlische, dein Heiligtum!", 11 + "Deine Zauber binden wieder", 12 + "Was die Mode streng geteilt;", 13 + "Alle Menschen werden Brüder,", 14 + "Wo dein sanfter Flügel weilt.", 15 + ]; 16 + 17 + // Dutch national anthem 18 + const WILHELMUS: [&str; 8] = [ 19 + "Wilhelmus van Nassouwe", 20 + "ben ik, van Duitsen bloed,", 21 + "den vaderland getrouwe", 22 + "blijf ik tot in den dood.", 23 + "Een Prinse van Oranje", 24 + "ben ik, vrij, onverveerd,", 25 + "den Koning van Hispanje", 26 + "heb ik altijd geëerd.", 27 + ]; 28 + 29 + // American national anthem 30 + const STAR_SPANGLED_BANNER: [&str; 8] = [ 31 + "O say can you see by the dawn's early light,", 32 + "What so proudly we hailed at the twilight's last gleaming,", 33 + "Whose broad stripes and bright stars through the perilous fight,", 34 + "O'er the ramparts we watched, were so gallantly streaming?", 35 + "And the rockets' red glare, the bombs bursting in air,", 36 + "Gave proof through the night that our flag was still there;", 37 + "O say does that star-spangled banner yet wave,", 38 + "O'er the land of the free and the home of the brave?", 39 + ]; 40 + 41 + #[test] 42 + fn test_no_texts() { 43 + assert_eq!(frequency::frequency(&[], 4), HashMap::new()); 44 + } 45 + 46 + #[test] 47 + #[ignore] 48 + fn test_one_letter() { 49 + let mut hm = HashMap::new(); 50 + hm.insert('a', 1); 51 + assert_eq!(frequency::frequency(&["a"], 4), hm); 52 + } 53 + 54 + #[test] 55 + #[ignore] 56 + fn test_case_insensitivity() { 57 + let mut hm = HashMap::new(); 58 + hm.insert('a', 2); 59 + assert_eq!(frequency::frequency(&["aA"], 4), hm); 60 + } 61 + 62 + #[test] 63 + #[ignore] 64 + fn test_many_empty_lines() { 65 + let v = vec![""; 1000]; 66 + assert_eq!(frequency::frequency(&v[..], 4), HashMap::new()); 67 + } 68 + 69 + #[test] 70 + #[ignore] 71 + fn test_many_times_same_text() { 72 + let v = vec!["abc"; 1000]; 73 + let mut hm = HashMap::new(); 74 + hm.insert('a', 1000); 75 + hm.insert('b', 1000); 76 + hm.insert('c', 1000); 77 + assert_eq!(frequency::frequency(&v[..], 4), hm); 78 + } 79 + 80 + #[test] 81 + fn test_punctuation_doesnt_count() { 82 + assert!(!frequency::frequency(&WILHELMUS, 4).contains_key(&',')); 83 + } 84 + 85 + #[test] 86 + #[ignore] 87 + fn test_numbers_dont_count() { 88 + assert!(!frequency::frequency(&["Testing, 1, 2, 3"], 4).contains_key(&'1')); 89 + } 90 + 91 + #[test] 92 + #[ignore] 93 + fn test_all_three_anthems_1_worker() { 94 + let mut v = Vec::new(); 95 + for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() { 96 + for line in anthem.iter() { 97 + v.push(*line); 98 + } 99 + } 100 + let freqs = frequency::frequency(&v[..], 1); 101 + assert_eq!(freqs.get(&'a'), Some(&49)); 102 + assert_eq!(freqs.get(&'t'), Some(&56)); 103 + assert_eq!(freqs.get(&'ü'), Some(&2)); 104 + } 105 + 106 + #[test] 107 + #[ignore] 108 + fn test_all_three_anthems_3_workers() { 109 + let mut v = Vec::new(); 110 + for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() { 111 + for line in anthem.iter() { 112 + v.push(*line); 113 + } 114 + } 115 + let freqs = frequency::frequency(&v[..], 3); 116 + assert_eq!(freqs.get(&'a'), Some(&49)); 117 + assert_eq!(freqs.get(&'t'), Some(&56)); 118 + assert_eq!(freqs.get(&'ü'), Some(&2)); 119 + } 120 + 121 + #[test] 122 + #[ignore] 123 + fn test_non_integer_multiple_of_threads() { 124 + let v = vec!["abc"; 999]; 125 + let mut hm = HashMap::new(); 126 + hm.insert('a', 999); 127 + hm.insert('b', 999); 128 + hm.insert('c', 999); 129 + assert_eq!(frequency::frequency(&v[..], 4), hm); 130 + }