Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions exercises/poker/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use counter::Counter;
///
/// Note the type signature: this function should return _the same_ reference to
/// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal.
pub fn winning_hands<'a>(hands: &[&'a str]) -> Option<Vec<&'a str>> {
pub fn winning_hands<'a>(hands: &[&'a str]) -> Result<Vec<&'a str>, &'static str> {
let mut hands = hands
.into_iter()
.map(|source| Hand::try_from(*source))
.collect::<Result<Vec<_>, _>>().ok()?;
.collect::<Result<Vec<_>, _>>()?;
hands.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Less));
hands.last().map(|last| {
hands
Expand All @@ -21,6 +21,7 @@ pub fn winning_hands<'a>(hands: &[&'a str]) -> Option<Vec<&'a str>> {
.map(|hand| hand.source)
.collect()
})
.ok_or("No winning hand")
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion exercises/poker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
///
/// Note the type signature: this function should return _the same_ reference to
/// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal.
pub fn winning_hands<'a>(hands: &[&'a str]) -> Option<Vec<&'a str>> {
pub fn winning_hands<'a>(hands: &[&'a str]) -> Result<Vec<&'a str>, &'static str> {
unimplemented!("Out of {:?}, which hand wins?", hands)
}
12 changes: 12 additions & 0 deletions exercises/poker/tests/poker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ fn test_single_hand_always_wins() {
test(&["4S 5S 7H 8D JC"], &["4S 5S 7H 8D JC"])
}

#[test]
#[ignore]
fn test_empty_input() {
assert!(&winning_hands(&[""]).is_err())
}

#[test]
#[ignore]
fn test_not_enough_cards() {
assert!(&winning_hands(&["4S 5S 7H 8D"]).is_err())
}

#[test]
#[ignore]
fn test_highest_card_of_all_hands_wins() {
Expand Down