Exercism track submissions
0
fork

Configure Feed

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

sublist

+334
+36
rust/sublist/.exercism/config.json
··· 1 + { 2 + "blurb": "Write a function to determine if a list is a sublist of another list.", 3 + "authors": [ 4 + "EduardoBautista" 5 + ], 6 + "contributors": [ 7 + "ashleygwilliams", 8 + "coriolinus", 9 + "cwhakes", 10 + "eddyp", 11 + "EduardoBautista", 12 + "efx", 13 + "ErikSchierboom", 14 + "IanWhitney", 15 + "kytrinyx", 16 + "lutostag", 17 + "mkantor", 18 + "nfiles", 19 + "petertseng", 20 + "rofrol", 21 + "stringparser", 22 + "xakon", 23 + "ZapAnton" 24 + ], 25 + "files": { 26 + "solution": [ 27 + "src/lib.rs" 28 + ], 29 + "test": [ 30 + "tests/sublist.rs" 31 + ], 32 + "example": [ 33 + ".meta/example.rs" 34 + ] 35 + } 36 + }
+8
rust/sublist/.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/sublist/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "sublist" 4 + version = "0.0.0"
+85
rust/sublist/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
+49
rust/sublist/README.md
··· 1 + # Sublist 2 + 3 + Welcome to Sublist 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 + Given two lists determine if the first list is contained within the second 9 + list, if the second list is contained within the first list, if both lists are 10 + contained within each other or if none of these are true. 11 + 12 + Specifically, a list A is a sublist of list B if by dropping 0 or more elements 13 + from the front of B and 0 or more elements from the back of B you get a list 14 + that's completely equal to A. 15 + 16 + Examples: 17 + 18 + * A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B 19 + * A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B 20 + * A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B 21 + * A = [1, 2, 3], B = [1, 2, 3], A is equal to B 22 + * A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B 23 + * A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B 24 + 25 + ## Source 26 + 27 + ### Created by 28 + 29 + - @EduardoBautista 30 + 31 + ### Contributed to by 32 + 33 + - @ashleygwilliams 34 + - @coriolinus 35 + - @cwhakes 36 + - @eddyp 37 + - @EduardoBautista 38 + - @efx 39 + - @ErikSchierboom 40 + - @IanWhitney 41 + - @kytrinyx 42 + - @lutostag 43 + - @mkantor 44 + - @nfiles 45 + - @petertseng 46 + - @rofrol 47 + - @stringparser 48 + - @xakon 49 + - @ZapAnton
+47
rust/sublist/src/lib.rs
··· 1 + use std::cmp::Ordering; 2 + 3 + #[derive(Debug, PartialEq)] 4 + pub enum Comparison { 5 + Equal, 6 + Sublist, 7 + Superlist, 8 + Unequal, 9 + } 10 + 11 + fn is_contained<T: PartialEq>(fl: &[T], sl: &[T], sz: usize) -> bool { 12 + let l = fl.len(); 13 + if l == 0 || sz == 0 { 14 + true 15 + } else { 16 + sl.windows(sz).any(|xs| xs == fl) 17 + } 18 + } 19 + 20 + pub fn sublist<T: PartialEq>(first_list: &[T], second_list: &[T]) -> Comparison { 21 + let l = first_list.len(); 22 + let m = second_list.len(); 23 + 24 + match l.cmp(&m) { 25 + Ordering::Equal => { 26 + if is_contained(first_list, second_list, l) { 27 + Comparison::Equal 28 + } else { 29 + Comparison::Unequal 30 + } 31 + } 32 + Ordering::Less => { 33 + if is_contained(first_list, second_list, l) { 34 + Comparison::Sublist 35 + } else { 36 + Comparison::Unequal 37 + } 38 + } 39 + Ordering::Greater => { 40 + if is_contained(second_list, first_list, m) { 41 + Comparison::Superlist 42 + } else { 43 + Comparison::Unequal 44 + } 45 + } 46 + } 47 + }
+105
rust/sublist/tests/sublist.rs
··· 1 + use sublist::{sublist, Comparison}; 2 + 3 + #[test] 4 + fn empty_equals_empty() { 5 + let v: &[u32] = &[]; 6 + 7 + assert_eq!(Comparison::Equal, sublist(v, v)); 8 + } 9 + 10 + #[test] 11 + fn test_empty_is_a_sublist_of_anything() { 12 + assert_eq!(Comparison::Sublist, sublist(&[], &['a', 's', 'd', 'f'])); 13 + } 14 + 15 + #[test] 16 + fn test_anything_is_a_superlist_of_empty() { 17 + assert_eq!(Comparison::Superlist, sublist(&['a', 's', 'd', 'f'], &[])); 18 + } 19 + 20 + #[test] 21 + fn test_1_is_not_2() { 22 + assert_eq!(Comparison::Unequal, sublist(&[1], &[2])); 23 + } 24 + 25 + #[test] 26 + fn test_compare_larger_equal_lists() { 27 + use std::iter::repeat; 28 + 29 + let v: Vec<char> = repeat('x').take(1000).collect(); 30 + 31 + assert_eq!(Comparison::Equal, sublist(&v, &v)); 32 + } 33 + 34 + #[test] 35 + fn test_sublist_at_start() { 36 + assert_eq!(Comparison::Sublist, sublist(&[1, 2, 3], &[1, 2, 3, 4, 5])); 37 + } 38 + 39 + #[test] 40 + fn sublist_in_middle() { 41 + assert_eq!(Comparison::Sublist, sublist(&[4, 3, 2], &[5, 4, 3, 2, 1])); 42 + } 43 + 44 + #[test] 45 + fn sublist_at_end() { 46 + assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &[1, 2, 3, 4, 5])); 47 + } 48 + 49 + #[test] 50 + fn partially_matching_sublist_at_start() { 51 + assert_eq!(Comparison::Sublist, sublist(&[1, 1, 2], &[1, 1, 1, 2])); 52 + } 53 + 54 + #[test] 55 + fn sublist_early_in_huge_list() { 56 + let huge: Vec<u32> = (1..1_000_000).collect(); 57 + 58 + assert_eq!(Comparison::Sublist, sublist(&[3, 4, 5], &huge)); 59 + } 60 + 61 + #[test] 62 + fn huge_sublist_not_in_huge_list() { 63 + let v1: Vec<u64> = (10..1_000_001).collect(); 64 + let v2: Vec<u64> = (1..1_000_000).collect(); 65 + 66 + assert_eq!(Comparison::Unequal, sublist(&v1, &v2)); 67 + } 68 + 69 + #[test] 70 + fn superlist_at_start() { 71 + assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[1, 2, 3])); 72 + } 73 + 74 + #[test] 75 + fn superlist_in_middle() { 76 + assert_eq!(Comparison::Superlist, sublist(&[5, 4, 3, 2, 1], &[4, 3, 2])); 77 + } 78 + 79 + #[test] 80 + fn superlist_at_end() { 81 + assert_eq!(Comparison::Superlist, sublist(&[1, 2, 3, 4, 5], &[3, 4, 5])); 82 + } 83 + 84 + #[test] 85 + fn superlist_early_in_huge_list() { 86 + let huge: Vec<u32> = (1..1_000_000).collect(); 87 + 88 + assert_eq!(Comparison::Superlist, sublist(&huge, &[3, 4, 5])); 89 + } 90 + 91 + #[test] 92 + fn recurring_values_sublist() { 93 + assert_eq!( 94 + Comparison::Sublist, 95 + sublist(&[1, 2, 1, 2, 3], &[1, 2, 3, 1, 2, 1, 2, 3, 2, 1]) 96 + ); 97 + } 98 + 99 + #[test] 100 + fn recurring_values_unequal() { 101 + assert_eq!( 102 + Comparison::Unequal, 103 + sublist(&[1, 2, 1, 2, 3], &[1, 2, 3, 1, 2, 3, 2, 3, 2, 1]) 104 + ); 105 + }