···11+{
22+ "blurb": "Pick the best hand(s) from a list of poker hands.",
33+ "authors": [
44+ "coriolinus"
55+ ],
66+ "contributors": [
77+ "Baelyk",
88+ "CGMossa",
99+ "cwhakes",
1010+ "efx",
1111+ "elektronaut0815",
1212+ "ErikSchierboom",
1313+ "lutostag",
1414+ "nfiles",
1515+ "PaulDance",
1616+ "petertseng",
1717+ "rofrol",
1818+ "stringparser",
1919+ "xakon",
2020+ "ZapAnton"
2121+ ],
2222+ "files": {
2323+ "solution": [
2424+ "src/lib.rs"
2525+ ],
2626+ "test": [
2727+ "tests/poker.rs"
2828+ ],
2929+ "example": [
3030+ ".meta/example.rs"
3131+ ]
3232+ },
3333+ "source": "Inspired by the training course from Udacity.",
3434+ "source_url": "https://www.udacity.com/course/viewer#!/c-cs212/"
3535+}
+8
rust/poker/.gitignore
···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+45
rust/poker/README.md
···11+# Poker
22+33+Welcome to Poker on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+66+## Instructions
77+88+Pick the best hand(s) from a list of poker hands.
99+1010+See [wikipedia](https://en.wikipedia.org/wiki/List_of_poker_hands) for an
1111+overview of poker hands.
1212+1313+- Ranking a list of poker hands can be considered a sorting problem.
1414+- Rust provides the [sort](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort) method for `Vec<T> where T: Ord`.
1515+- [`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.
1616+- 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"`.
1717+- 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.
1818+- You might consider implementing a type representing a poker hand which implements `PartialOrd`.
1919+2020+## Source
2121+2222+### Created by
2323+2424+- @coriolinus
2525+2626+### Contributed to by
2727+2828+- @Baelyk
2929+- @CGMossa
3030+- @cwhakes
3131+- @efx
3232+- @elektronaut0815
3333+- @ErikSchierboom
3434+- @lutostag
3535+- @nfiles
3636+- @PaulDance
3737+- @petertseng
3838+- @rofrol
3939+- @stringparser
4040+- @xakon
4141+- @ZapAnton
4242+4343+### Based on
4444+4545+Inspired by the training course from Udacity. - https://www.udacity.com/course/viewer#!/c-cs212/
+7
rust/poker/src/lib.rs
···11+/// Given a list of poker hands, return a list of those hands which win.
22+///
33+/// Note the type signature: this function should return _the same_ reference to
44+/// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal.
55+pub fn winning_hands<'a>(hands: &[&'a str]) -> Vec<&'a str> {
66+ unimplemented!("Out of {:?}, which hand wins?", hands)
77+}
+224
rust/poker/tests/poker.rs
···11+use poker::winning_hands;
22+use std::collections::HashSet;
33+44+fn hs_from<'a>(input: &[&'a str]) -> HashSet<&'a str> {
55+ let mut hs = HashSet::new();
66+ for item in input.iter() {
77+ hs.insert(*item);
88+ }
99+ hs
1010+}
1111+1212+/// Test that the expected output is produced from the given input
1313+/// using the `winning_hands` function.
1414+///
1515+/// Note that the output can be in any order. Here, we use a HashSet to
1616+/// abstract away the order of outputs.
1717+fn test<'a, 'b>(input: &[&'a str], expected: &[&'b str]) {
1818+ assert_eq!(hs_from(&winning_hands(input)), hs_from(expected))
1919+}
2020+2121+#[test]
2222+fn test_single_hand_always_wins() {
2323+ test(&["4S 5S 7H 8D JC"], &["4S 5S 7H 8D JC"])
2424+}
2525+2626+#[test]
2727+#[ignore]
2828+fn test_duplicate_hands_always_tie() {
2929+ let input = &["3S 4S 5D 6H JH", "3S 4S 5D 6H JH", "3S 4S 5D 6H JH"];
3030+ assert_eq!(&winning_hands(input), input)
3131+}
3232+3333+#[test]
3434+#[ignore]
3535+fn test_highest_card_of_all_hands_wins() {
3636+ test(
3737+ &["4D 5S 6S 8D 3C", "2S 4C 7S 9H 10H", "3S 4S 5D 6H JH"],
3838+ &["3S 4S 5D 6H JH"],
3939+ )
4040+}
4141+4242+#[test]
4343+#[ignore]
4444+fn test_a_tie_has_multiple_winners() {
4545+ test(
4646+ &[
4747+ "4D 5S 6S 8D 3C",
4848+ "2S 4C 7S 9H 10H",
4949+ "3S 4S 5D 6H JH",
5050+ "3H 4H 5C 6C JD",
5151+ ],
5252+ &["3S 4S 5D 6H JH", "3H 4H 5C 6C JD"],
5353+ )
5454+}
5555+5656+#[test]
5757+#[ignore]
5858+fn test_high_card_can_be_low_card_in_an_otherwise_tie() {
5959+ // multiple hands with the same high cards, tie compares next highest ranked,
6060+ // down to last card
6161+ test(&["3S 5H 6S 8D 7H", "2S 5D 6D 8C 7S"], &["3S 5H 6S 8D 7H"])
6262+}
6363+6464+#[test]
6565+#[ignore]
6666+fn test_one_pair_beats_high_card() {
6767+ test(&["4S 5H 6C 8D KH", "2S 4H 6S 4D JH"], &["2S 4H 6S 4D JH"])
6868+}
6969+7070+#[test]
7171+#[ignore]
7272+fn test_highest_pair_wins() {
7373+ test(&["4S 2H 6S 2D JH", "2S 4H 6C 4D JD"], &["2S 4H 6C 4D JD"])
7474+}
7575+7676+#[test]
7777+#[ignore]
7878+fn test_two_pairs_beats_one_pair() {
7979+ test(&["2S 8H 6S 8D JH", "4S 5H 4C 8C 5C"], &["4S 5H 4C 8C 5C"])
8080+}
8181+8282+#[test]
8383+#[ignore]
8484+fn test_two_pair_ranks() {
8585+ // both hands have two pairs, highest ranked pair wins
8686+ test(&["2S 8H 2D 8D 3H", "4S 5H 4C 8S 5D"], &["2S 8H 2D 8D 3H"])
8787+}
8888+8989+#[test]
9090+#[ignore]
9191+fn test_two_pairs_second_pair_cascade() {
9292+ // both hands have two pairs, with the same highest ranked pair,
9393+ // tie goes to low pair
9494+ test(&["2S QS 2C QD JH", "JD QH JS 8D QC"], &["JD QH JS 8D QC"])
9595+}
9696+9797+#[test]
9898+#[ignore]
9999+fn test_two_pairs_last_card_cascade() {
100100+ // both hands have two identically ranked pairs,
101101+ // tie goes to remaining card (kicker)
102102+ test(&["JD QH JS 8D QC", "JS QS JC 2D QD"], &["JD QH JS 8D QC"])
103103+}
104104+105105+#[test]
106106+#[ignore]
107107+fn test_three_of_a_kind_beats_two_pair() {
108108+ test(&["2S 8H 2H 8D JH", "4S 5H 4C 8S 4H"], &["4S 5H 4C 8S 4H"])
109109+}
110110+111111+#[test]
112112+#[ignore]
113113+fn test_three_of_a_kind_ranks() {
114114+ //both hands have three of a kind, tie goes to highest ranked triplet
115115+ test(&["2S 2H 2C 8D JH", "4S AH AS 8C AD"], &["4S AH AS 8C AD"])
116116+}
117117+118118+#[test]
119119+#[ignore]
120120+fn test_three_of_a_kind_cascade_ranks() {
121121+ // with multiple decks, two players can have same three of a kind,
122122+ // ties go to highest remaining cards
123123+ test(&["4S AH AS 7C AD", "4S AH AS 8C AD"], &["4S AH AS 8C AD"])
124124+}
125125+126126+#[test]
127127+#[ignore]
128128+fn test_straight_beats_three_of_a_kind() {
129129+ test(&["4S 5H 4C 8D 4H", "3S 4D 2S 6D 5C"], &["3S 4D 2S 6D 5C"])
130130+}
131131+132132+#[test]
133133+#[ignore]
134134+fn test_aces_can_end_a_straight_high() {
135135+ // aces can end a straight (10 J Q K A)
136136+ test(&["4S 5H 4C 8D 4H", "10D JH QS KD AC"], &["10D JH QS KD AC"])
137137+}
138138+139139+#[test]
140140+#[ignore]
141141+fn test_aces_can_end_a_straight_low() {
142142+ // aces can start a straight (A 2 3 4 5)
143143+ test(&["4S 5H 4C 8D 4H", "4D AH 3S 2D 5C"], &["4D AH 3S 2D 5C"])
144144+}
145145+146146+#[test]
147147+#[ignore]
148148+fn test_straight_cascade() {
149149+ // both hands with a straight, tie goes to highest ranked card
150150+ test(&["4S 6C 7S 8D 5H", "5S 7H 8S 9D 6H"], &["5S 7H 8S 9D 6H"])
151151+}
152152+153153+#[test]
154154+#[ignore]
155155+fn test_straight_scoring() {
156156+ // even though an ace is usually high, a 5-high straight is the lowest-scoring straight
157157+ test(&["2H 3C 4D 5D 6H", "4S AH 3S 2D 5H"], &["2H 3C 4D 5D 6H"])
158158+}
159159+160160+#[test]
161161+#[ignore]
162162+fn test_flush_beats_a_straight() {
163163+ test(&["4C 6H 7D 8D 5H", "2S 4S 5S 6S 7S"], &["2S 4S 5S 6S 7S"])
164164+}
165165+166166+#[test]
167167+#[ignore]
168168+fn test_flush_cascade() {
169169+ // both hands have a flush, tie goes to high card, down to the last one if necessary
170170+ test(&["4H 7H 8H 9H 6H", "2S 4S 5S 6S 7S"], &["4H 7H 8H 9H 6H"])
171171+}
172172+173173+#[test]
174174+#[ignore]
175175+fn test_full_house_beats_a_flush() {
176176+ test(&["3H 6H 7H 8H 5H", "4S 5C 4C 5D 4H"], &["4S 5C 4C 5D 4H"])
177177+}
178178+179179+#[test]
180180+#[ignore]
181181+fn test_full_house_ranks() {
182182+ // both hands have a full house, tie goes to highest-ranked triplet
183183+ test(&["4H 4S 4D 9S 9D", "5H 5S 5D 8S 8D"], &["5H 5S 5D 8S 8D"])
184184+}
185185+186186+#[test]
187187+#[ignore]
188188+fn test_full_house_cascade() {
189189+ // with multiple decks, both hands have a full house with the same triplet, tie goes to the pair
190190+ test(&["5H 5S 5D 9S 9D", "5H 5S 5D 8S 8D"], &["5H 5S 5D 9S 9D"])
191191+}
192192+193193+#[test]
194194+#[ignore]
195195+fn test_four_of_a_kind_beats_full_house() {
196196+ test(&["4S 5H 4D 5D 4H", "3S 3H 2S 3D 3C"], &["3S 3H 2S 3D 3C"])
197197+}
198198+199199+#[test]
200200+#[ignore]
201201+fn test_four_of_a_kind_ranks() {
202202+ // both hands have four of a kind, tie goes to high quad
203203+ test(&["2S 2H 2C 8D 2D", "4S 5H 5S 5D 5C"], &["4S 5H 5S 5D 5C"])
204204+}
205205+206206+#[test]
207207+#[ignore]
208208+fn test_four_of_a_kind_cascade() {
209209+ // with multiple decks, both hands with identical four of a kind, tie determined by kicker
210210+ test(&["3S 3H 2S 3D 3C", "3S 3H 4S 3D 3C"], &["3S 3H 4S 3D 3C"])
211211+}
212212+213213+#[test]
214214+#[ignore]
215215+fn test_straight_flush_beats_four_of_a_kind() {
216216+ test(&["4S 5H 5S 5D 5C", "7S 8S 9S 6S 10S"], &["7S 8S 9S 6S 10S"])
217217+}
218218+219219+#[test]
220220+#[ignore]
221221+fn test_straight_flush_ranks() {
222222+ // both hands have straight flush, tie goes to highest-ranked card
223223+ test(&["4H 6H 7H 8H 5H", "5S 7S 8S 9S 6S"], &["5S 7S 8S 9S 6S"])
224224+}