Exercism track submissions
0
fork

Configure Feed

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

poker wip

+410
+35
rust/poker/.exercism/config.json
··· 1 + { 2 + "blurb": "Pick the best hand(s) from a list of poker hands.", 3 + "authors": [ 4 + "coriolinus" 5 + ], 6 + "contributors": [ 7 + "Baelyk", 8 + "CGMossa", 9 + "cwhakes", 10 + "efx", 11 + "elektronaut0815", 12 + "ErikSchierboom", 13 + "lutostag", 14 + "nfiles", 15 + "PaulDance", 16 + "petertseng", 17 + "rofrol", 18 + "stringparser", 19 + "xakon", 20 + "ZapAnton" 21 + ], 22 + "files": { 23 + "solution": [ 24 + "src/lib.rs" 25 + ], 26 + "test": [ 27 + "tests/poker.rs" 28 + ], 29 + "example": [ 30 + ".meta/example.rs" 31 + ] 32 + }, 33 + "source": "Inspired by the training course from Udacity.", 34 + "source_url": "https://www.udacity.com/course/viewer#!/c-cs212/" 35 + }
+8
rust/poker/.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
+6
rust/poker/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "poker" 4 + version = "1.1.0" 5 + 6 + [dependencies]
+85
rust/poker/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
+45
rust/poker/README.md
··· 1 + # Poker 2 + 3 + Welcome to Poker 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 + Pick the best hand(s) from a list of poker hands. 9 + 10 + See [wikipedia](https://en.wikipedia.org/wiki/List_of_poker_hands) for an 11 + overview of poker hands. 12 + 13 + - Ranking a list of poker hands can be considered a sorting problem. 14 + - Rust provides the [sort](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort) method for `Vec<T> where T: Ord`. 15 + - [`Ord` types](https://doc.rust-lang.org/std/cmp/trait.Ord.html) form a [total order](https://en.wikipedia.org/wiki/Total_order): exactly one of `a < b`, `a == b`, or `a > b` must be true. 16 + - Poker hands do not conform to a total order: it is possible for two hands to be non-equal but have equal sort order. Example: `"3S 4S 5D 6H JH"`, `"3H 4H 5C 6C JD"`. 17 + - Rust provides the [`PartialOrd` trait](https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html) to handle the case of sortable things which do not have a total order. However, it doesn't provide a standard `sort` method for `Vec<T> where T: PartialOrd`. The standard idiom to sort a vector in this case is `your_vec.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::{Less|Equal|Greater}));`, depending on your needs. 18 + - You might consider implementing a type representing a poker hand which implements `PartialOrd`. 19 + 20 + ## Source 21 + 22 + ### Created by 23 + 24 + - @coriolinus 25 + 26 + ### Contributed to by 27 + 28 + - @Baelyk 29 + - @CGMossa 30 + - @cwhakes 31 + - @efx 32 + - @elektronaut0815 33 + - @ErikSchierboom 34 + - @lutostag 35 + - @nfiles 36 + - @PaulDance 37 + - @petertseng 38 + - @rofrol 39 + - @stringparser 40 + - @xakon 41 + - @ZapAnton 42 + 43 + ### Based on 44 + 45 + Inspired by the training course from Udacity. - https://www.udacity.com/course/viewer#!/c-cs212/
+7
rust/poker/src/lib.rs
··· 1 + /// Given a list of poker hands, return a list of those hands which win. 2 + /// 3 + /// Note the type signature: this function should return _the same_ reference to 4 + /// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal. 5 + pub fn winning_hands<'a>(hands: &[&'a str]) -> Vec<&'a str> { 6 + unimplemented!("Out of {:?}, which hand wins?", hands) 7 + }
+224
rust/poker/tests/poker.rs
··· 1 + use poker::winning_hands; 2 + use std::collections::HashSet; 3 + 4 + fn hs_from<'a>(input: &[&'a str]) -> HashSet<&'a str> { 5 + let mut hs = HashSet::new(); 6 + for item in input.iter() { 7 + hs.insert(*item); 8 + } 9 + hs 10 + } 11 + 12 + /// Test that the expected output is produced from the given input 13 + /// using the `winning_hands` function. 14 + /// 15 + /// Note that the output can be in any order. Here, we use a HashSet to 16 + /// abstract away the order of outputs. 17 + fn test<'a, 'b>(input: &[&'a str], expected: &[&'b str]) { 18 + assert_eq!(hs_from(&winning_hands(input)), hs_from(expected)) 19 + } 20 + 21 + #[test] 22 + fn test_single_hand_always_wins() { 23 + test(&["4S 5S 7H 8D JC"], &["4S 5S 7H 8D JC"]) 24 + } 25 + 26 + #[test] 27 + #[ignore] 28 + fn test_duplicate_hands_always_tie() { 29 + let input = &["3S 4S 5D 6H JH", "3S 4S 5D 6H JH", "3S 4S 5D 6H JH"]; 30 + assert_eq!(&winning_hands(input), input) 31 + } 32 + 33 + #[test] 34 + #[ignore] 35 + fn test_highest_card_of_all_hands_wins() { 36 + test( 37 + &["4D 5S 6S 8D 3C", "2S 4C 7S 9H 10H", "3S 4S 5D 6H JH"], 38 + &["3S 4S 5D 6H JH"], 39 + ) 40 + } 41 + 42 + #[test] 43 + #[ignore] 44 + fn test_a_tie_has_multiple_winners() { 45 + test( 46 + &[ 47 + "4D 5S 6S 8D 3C", 48 + "2S 4C 7S 9H 10H", 49 + "3S 4S 5D 6H JH", 50 + "3H 4H 5C 6C JD", 51 + ], 52 + &["3S 4S 5D 6H JH", "3H 4H 5C 6C JD"], 53 + ) 54 + } 55 + 56 + #[test] 57 + #[ignore] 58 + fn test_high_card_can_be_low_card_in_an_otherwise_tie() { 59 + // multiple hands with the same high cards, tie compares next highest ranked, 60 + // down to last card 61 + test(&["3S 5H 6S 8D 7H", "2S 5D 6D 8C 7S"], &["3S 5H 6S 8D 7H"]) 62 + } 63 + 64 + #[test] 65 + #[ignore] 66 + fn test_one_pair_beats_high_card() { 67 + test(&["4S 5H 6C 8D KH", "2S 4H 6S 4D JH"], &["2S 4H 6S 4D JH"]) 68 + } 69 + 70 + #[test] 71 + #[ignore] 72 + fn test_highest_pair_wins() { 73 + test(&["4S 2H 6S 2D JH", "2S 4H 6C 4D JD"], &["2S 4H 6C 4D JD"]) 74 + } 75 + 76 + #[test] 77 + #[ignore] 78 + fn test_two_pairs_beats_one_pair() { 79 + test(&["2S 8H 6S 8D JH", "4S 5H 4C 8C 5C"], &["4S 5H 4C 8C 5C"]) 80 + } 81 + 82 + #[test] 83 + #[ignore] 84 + fn test_two_pair_ranks() { 85 + // both hands have two pairs, highest ranked pair wins 86 + test(&["2S 8H 2D 8D 3H", "4S 5H 4C 8S 5D"], &["2S 8H 2D 8D 3H"]) 87 + } 88 + 89 + #[test] 90 + #[ignore] 91 + fn test_two_pairs_second_pair_cascade() { 92 + // both hands have two pairs, with the same highest ranked pair, 93 + // tie goes to low pair 94 + test(&["2S QS 2C QD JH", "JD QH JS 8D QC"], &["JD QH JS 8D QC"]) 95 + } 96 + 97 + #[test] 98 + #[ignore] 99 + fn test_two_pairs_last_card_cascade() { 100 + // both hands have two identically ranked pairs, 101 + // tie goes to remaining card (kicker) 102 + test(&["JD QH JS 8D QC", "JS QS JC 2D QD"], &["JD QH JS 8D QC"]) 103 + } 104 + 105 + #[test] 106 + #[ignore] 107 + fn test_three_of_a_kind_beats_two_pair() { 108 + test(&["2S 8H 2H 8D JH", "4S 5H 4C 8S 4H"], &["4S 5H 4C 8S 4H"]) 109 + } 110 + 111 + #[test] 112 + #[ignore] 113 + fn test_three_of_a_kind_ranks() { 114 + //both hands have three of a kind, tie goes to highest ranked triplet 115 + test(&["2S 2H 2C 8D JH", "4S AH AS 8C AD"], &["4S AH AS 8C AD"]) 116 + } 117 + 118 + #[test] 119 + #[ignore] 120 + fn test_three_of_a_kind_cascade_ranks() { 121 + // with multiple decks, two players can have same three of a kind, 122 + // ties go to highest remaining cards 123 + test(&["4S AH AS 7C AD", "4S AH AS 8C AD"], &["4S AH AS 8C AD"]) 124 + } 125 + 126 + #[test] 127 + #[ignore] 128 + fn test_straight_beats_three_of_a_kind() { 129 + test(&["4S 5H 4C 8D 4H", "3S 4D 2S 6D 5C"], &["3S 4D 2S 6D 5C"]) 130 + } 131 + 132 + #[test] 133 + #[ignore] 134 + fn test_aces_can_end_a_straight_high() { 135 + // aces can end a straight (10 J Q K A) 136 + test(&["4S 5H 4C 8D 4H", "10D JH QS KD AC"], &["10D JH QS KD AC"]) 137 + } 138 + 139 + #[test] 140 + #[ignore] 141 + fn test_aces_can_end_a_straight_low() { 142 + // aces can start a straight (A 2 3 4 5) 143 + test(&["4S 5H 4C 8D 4H", "4D AH 3S 2D 5C"], &["4D AH 3S 2D 5C"]) 144 + } 145 + 146 + #[test] 147 + #[ignore] 148 + fn test_straight_cascade() { 149 + // both hands with a straight, tie goes to highest ranked card 150 + test(&["4S 6C 7S 8D 5H", "5S 7H 8S 9D 6H"], &["5S 7H 8S 9D 6H"]) 151 + } 152 + 153 + #[test] 154 + #[ignore] 155 + fn test_straight_scoring() { 156 + // even though an ace is usually high, a 5-high straight is the lowest-scoring straight 157 + test(&["2H 3C 4D 5D 6H", "4S AH 3S 2D 5H"], &["2H 3C 4D 5D 6H"]) 158 + } 159 + 160 + #[test] 161 + #[ignore] 162 + fn test_flush_beats_a_straight() { 163 + test(&["4C 6H 7D 8D 5H", "2S 4S 5S 6S 7S"], &["2S 4S 5S 6S 7S"]) 164 + } 165 + 166 + #[test] 167 + #[ignore] 168 + fn test_flush_cascade() { 169 + // both hands have a flush, tie goes to high card, down to the last one if necessary 170 + test(&["4H 7H 8H 9H 6H", "2S 4S 5S 6S 7S"], &["4H 7H 8H 9H 6H"]) 171 + } 172 + 173 + #[test] 174 + #[ignore] 175 + fn test_full_house_beats_a_flush() { 176 + test(&["3H 6H 7H 8H 5H", "4S 5C 4C 5D 4H"], &["4S 5C 4C 5D 4H"]) 177 + } 178 + 179 + #[test] 180 + #[ignore] 181 + fn test_full_house_ranks() { 182 + // both hands have a full house, tie goes to highest-ranked triplet 183 + test(&["4H 4S 4D 9S 9D", "5H 5S 5D 8S 8D"], &["5H 5S 5D 8S 8D"]) 184 + } 185 + 186 + #[test] 187 + #[ignore] 188 + fn test_full_house_cascade() { 189 + // with multiple decks, both hands have a full house with the same triplet, tie goes to the pair 190 + test(&["5H 5S 5D 9S 9D", "5H 5S 5D 8S 8D"], &["5H 5S 5D 9S 9D"]) 191 + } 192 + 193 + #[test] 194 + #[ignore] 195 + fn test_four_of_a_kind_beats_full_house() { 196 + test(&["4S 5H 4D 5D 4H", "3S 3H 2S 3D 3C"], &["3S 3H 2S 3D 3C"]) 197 + } 198 + 199 + #[test] 200 + #[ignore] 201 + fn test_four_of_a_kind_ranks() { 202 + // both hands have four of a kind, tie goes to high quad 203 + test(&["2S 2H 2C 8D 2D", "4S 5H 5S 5D 5C"], &["4S 5H 5S 5D 5C"]) 204 + } 205 + 206 + #[test] 207 + #[ignore] 208 + fn test_four_of_a_kind_cascade() { 209 + // with multiple decks, both hands with identical four of a kind, tie determined by kicker 210 + test(&["3S 3H 2S 3D 3C", "3S 3H 4S 3D 3C"], &["3S 3H 4S 3D 3C"]) 211 + } 212 + 213 + #[test] 214 + #[ignore] 215 + fn test_straight_flush_beats_four_of_a_kind() { 216 + test(&["4S 5H 5S 5D 5C", "7S 8S 9S 6S 10S"], &["7S 8S 9S 6S 10S"]) 217 + } 218 + 219 + #[test] 220 + #[ignore] 221 + fn test_straight_flush_ranks() { 222 + // both hands have straight flush, tie goes to highest-ranked card 223 + test(&["4H 6H 7H 8H 5H", "5S 7S 8S 9S 6S"], &["5S 7S 8S 9S 6S"]) 224 + }