Exercism track submissions
0
fork

Configure Feed

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

luhn

+471
+40
rust/luhn/.exercism/config.json
··· 1 + { 2 + "blurb": "Given a number determine whether or not it is valid per the Luhn formula.", 3 + "authors": [ 4 + "IanWhitney" 5 + ], 6 + "contributors": [ 7 + "AvasDream", 8 + "bitfield", 9 + "coriolinus", 10 + "cwhakes", 11 + "efx", 12 + "ErikSchierboom", 13 + "gibfahn", 14 + "idealhack", 15 + "lutostag", 16 + "mkantor", 17 + "navossoc", 18 + "nfiles", 19 + "petertseng", 20 + "rofrol", 21 + "stkent", 22 + "stringparser", 23 + "workingjubilee", 24 + "xakon", 25 + "ZapAnton" 26 + ], 27 + "files": { 28 + "solution": [ 29 + "src/lib.rs" 30 + ], 31 + "test": [ 32 + "tests/luhn.rs" 33 + ], 34 + "example": [ 35 + ".meta/example.rs" 36 + ] 37 + }, 38 + "source": "The Luhn Algorithm on Wikipedia", 39 + "source_url": "http://en.wikipedia.org/wiki/Luhn_algorithm" 40 + }
+8
rust/luhn/.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/luhn/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "luhn" 4 + version = "1.6.1"
+85
rust/luhn/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
+102
rust/luhn/README.md
··· 1 + # Luhn 2 + 3 + Welcome to Luhn 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 a number determine whether or not it is valid per the Luhn formula. 9 + 10 + The [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) is 11 + a simple checksum formula used to validate a variety of identification 12 + numbers, such as credit card numbers and Canadian Social Insurance 13 + Numbers. 14 + 15 + The task is to check if a given string is valid. 16 + 17 + Validating a Number 18 + ------ 19 + 20 + Strings of length 1 or less are not valid. Spaces are allowed in the input, 21 + but they should be stripped before checking. All other non-digit characters 22 + are disallowed. 23 + 24 + ## Example 1: valid credit card number 25 + 26 + ```text 27 + 4539 3195 0343 6467 28 + ``` 29 + 30 + The first step of the Luhn algorithm is to double every second digit, 31 + starting from the right. We will be doubling 32 + 33 + ```text 34 + 4_3_ 3_9_ 0_4_ 6_6_ 35 + ``` 36 + 37 + If doubling the number results in a number greater than 9 then subtract 9 38 + from the product. The results of our doubling: 39 + 40 + ```text 41 + 8569 6195 0383 3437 42 + ``` 43 + 44 + Then sum all of the digits: 45 + 46 + ```text 47 + 8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80 48 + ``` 49 + 50 + If the sum is evenly divisible by 10, then the number is valid. This number is valid! 51 + 52 + ## Example 2: invalid credit card number 53 + 54 + ```text 55 + 8273 1232 7352 0569 56 + ``` 57 + 58 + Double the second digits, starting from the right 59 + 60 + ```text 61 + 7253 2262 5312 0539 62 + ``` 63 + 64 + Sum the digits 65 + 66 + ```text 67 + 7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57 68 + ``` 69 + 70 + 57 is not evenly divisible by 10, so this number is not valid. 71 + 72 + ## Source 73 + 74 + ### Created by 75 + 76 + - @IanWhitney 77 + 78 + ### Contributed to by 79 + 80 + - @AvasDream 81 + - @bitfield 82 + - @coriolinus 83 + - @cwhakes 84 + - @efx 85 + - @ErikSchierboom 86 + - @gibfahn 87 + - @idealhack 88 + - @lutostag 89 + - @mkantor 90 + - @navossoc 91 + - @nfiles 92 + - @petertseng 93 + - @rofrol 94 + - @stkent 95 + - @stringparser 96 + - @workingjubilee 97 + - @xakon 98 + - @ZapAnton 99 + 100 + ### Based on 101 + 102 + The Luhn Algorithm on Wikipedia - http://en.wikipedia.org/wiki/Luhn_algorithm
+100
rust/luhn/src/lib.rs
··· 1 + fn dbl_and_check(x: u32) -> u32 { 2 + let i = x * 2; 3 + if i > 9 { 4 + i - 9 5 + } else { 6 + i 7 + } 8 + } 9 + 10 + #[derive(Debug, Clone)] 11 + struct Input(Vec<char>, usize); 12 + 13 + impl From<&str> for Input { 14 + fn from(x: &str) -> Self { 15 + Input(x.chars().rev().collect(), 0) 16 + } 17 + } 18 + 19 + impl Iterator for Input { 20 + type Item = Parsed; 21 + 22 + fn next(&mut self) -> Option<Self::Item> { 23 + let len = self.0.len(); 24 + match self.1 { 25 + x if x < len => { 26 + let this = self.0.get(x); 27 + match this { 28 + Some(sp) if (*sp).is_whitespace() => { 29 + self.1 += 1; 30 + Some(Parsed::Space) 31 + } 32 + Some(t) => (|x| { 33 + self.1 += 1; 34 + match x { 35 + Some(v) => Some(Parsed::Digit(v)), 36 + None => Some(Parsed::SomethingElse), 37 + } 38 + })(t.to_digit(10)), 39 + None => None, 40 + } 41 + } 42 + _ => None, 43 + } 44 + } 45 + } 46 + 47 + #[derive(Debug)] 48 + enum Parsed { 49 + Digit(u32), 50 + Space, 51 + SomethingElse, 52 + } 53 + 54 + impl Parsed { 55 + fn is_something_else(&self) -> bool { 56 + match self { 57 + Parsed::SomethingElse => true, 58 + _ => false, 59 + } 60 + } 61 + 62 + fn is_digit(&self) -> bool { 63 + match self { 64 + Parsed::Digit(_) => true, 65 + _ => false, 66 + } 67 + } 68 + 69 + fn get_digit(&self) -> Option<u32> { 70 + match self { 71 + Parsed::Digit(x) => Some(*x), 72 + Parsed::Space => None, 73 + _ => None, 74 + } 75 + } 76 + } 77 + 78 + /// Check a Luhn checksum. 79 + pub fn is_valid(code: &str) -> bool { 80 + let inp: Input = Input::from(code); 81 + let has_non_digit_non_space = inp.clone().any(|x| x.is_something_else()); 82 + let only_digits = inp 83 + .clone() 84 + .filter(|x| x.is_digit()) 85 + .map_while(|x| x.get_digit()); 86 + 87 + if has_non_digit_non_space || only_digits.clone().count() <= 1 { 88 + false 89 + } else { 90 + let y: u32 = only_digits 91 + .clone() 92 + .enumerate() 93 + .map(|(id, el)| match id { 94 + _ if id % 2 != 0 => dbl_and_check(el), 95 + _ => el, 96 + }) 97 + .sum(); 98 + y % 10 == 0 99 + } 100 + }
+132
rust/luhn/tests/luhn.rs
··· 1 + use luhn::*; 2 + 3 + fn process_valid_case(number: &str, is_luhn_expected: bool) { 4 + assert_eq!(is_valid(number), is_luhn_expected); 5 + } 6 + 7 + #[test] 8 + fn test_single_digit_strings_can_not_be_valid() { 9 + process_valid_case("1", false); 10 + } 11 + 12 + #[test] 13 + #[ignore] 14 + fn test_a_single_zero_is_invalid() { 15 + process_valid_case("0", false); 16 + } 17 + 18 + #[test] 19 + #[ignore] 20 + fn test_a_simple_valid_sin_that_remains_valid_if_reversed() { 21 + process_valid_case("059", true); 22 + } 23 + 24 + #[test] 25 + fn test_a_simple_valid_sin_that_becomes_invalid_if_reversed() { 26 + process_valid_case("59", true); 27 + } 28 + 29 + #[test] 30 + #[ignore] 31 + fn test_a_valid_canadian_sin() { 32 + process_valid_case("055 444 285", true); 33 + } 34 + 35 + #[test] 36 + #[ignore] 37 + fn test_invalid_canadian_sin() { 38 + process_valid_case("055 444 286", false); 39 + } 40 + 41 + #[test] 42 + #[ignore] 43 + fn test_invalid_credit_card() { 44 + process_valid_case("8273 1232 7352 0569", false); 45 + } 46 + 47 + #[test] 48 + #[ignore] 49 + fn test_valid_number_with_an_even_number_of_digits() { 50 + process_valid_case("095 245 88", true); 51 + } 52 + 53 + #[test] 54 + fn strings_that_contain_non_digits_are_invalid() { 55 + process_valid_case("055a 444 285", false); 56 + } 57 + 58 + #[test] 59 + #[ignore] 60 + fn test_valid_strings_with_punctuation_included_become_invalid() { 61 + process_valid_case("055-444-285", false); 62 + } 63 + 64 + #[test] 65 + #[ignore] 66 + fn symbols_are_invalid() { 67 + process_valid_case("055£ 444$ 285", false); 68 + } 69 + 70 + #[test] 71 + #[ignore] 72 + fn test_single_zero_with_space_is_invalid() { 73 + process_valid_case(" 0", false); 74 + } 75 + 76 + #[test] 77 + #[ignore] 78 + fn test_more_than_a_single_zero_is_valid() { 79 + process_valid_case("0000 0", true); 80 + } 81 + 82 + #[test] 83 + #[ignore] 84 + fn test_input_digit_9_is_correctly_converted_to_output_digit_9() { 85 + process_valid_case("091", true); 86 + } 87 + 88 + #[test] 89 + #[ignore] 90 + /// using ascii value for doubled non-digit isn't allowed 91 + /// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid. 92 + /// This test is designed to avoid that solution. 93 + fn test_using_ascii_value_for_doubled_nondigit_isnt_allowed() { 94 + process_valid_case(":9", false); 95 + } 96 + 97 + #[test] 98 + #[ignore] 99 + /// valid strings with a non-digit added at the end become invalid 100 + fn test_valid_strings_with_a_nondigit_added_at_the_end_become_invalid() { 101 + process_valid_case("059a", false); 102 + } 103 + 104 + #[test] 105 + #[ignore] 106 + /// valid strings with symbols included become invalid 107 + fn test_valid_strings_with_symbols_included_become_invalid() { 108 + process_valid_case("055# 444$ 285", false); 109 + } 110 + 111 + #[test] 112 + #[ignore] 113 + /// using ascii value for non-doubled non-digit isn't allowed 114 + /// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid. 115 + /// This test is designed to avoid that solution. 116 + fn test_using_ascii_value_for_nondoubled_nondigit_isnt_allowed() { 117 + process_valid_case("055b 444 285", false); 118 + } 119 + 120 + #[test] 121 + #[ignore] 122 + /// valid number with an odd number of spaces 123 + fn test_valid_number_with_an_odd_number_of_spaces() { 124 + process_valid_case("234 567 891 234", true); 125 + } 126 + 127 + #[test] 128 + #[ignore] 129 + /// non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed 130 + fn test_invalid_char_in_middle_with_sum_divisible_by_10_isnt_allowed() { 131 + process_valid_case("59%59", false); 132 + }