Skip to content

Commit

Permalink
gotta-snatch-em-all: change task to use symmetric set difference (#2795)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
sanderploegsma authored May 8, 2024
1 parent f01c396 commit f6a53f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions exercises/concept/gotta-snatch-em-all/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ collection.contains("Scientuna");

You really want your friends to join your Blorkemon™️ madness, so it's time to start trading!

Not every trade is worth doing, or can be done at all.
You cannot trade a card you don't have, and you shouldn't trade a card for one that you already have.
When trading with friends not every trade is worth doing, or can be done at all.
You should only trade if both you and your friend have a card the other does not have.

Implement the `canTrade` method, that takes your current collection and the collection of one of your friends.
It should return a `boolean` indicating whether a trade is possible, following the rules above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ static boolean addCard(String card, Set<String> collection) {
}

static boolean canTrade(Set<String> myCollection, Set<String> theirCollection) {
Set<String> cardsWorthTradingFor = new HashSet<>(theirCollection);
cardsWorthTradingFor.removeAll(myCollection);
return !myCollection.isEmpty() && !cardsWorthTradingFor.isEmpty();
Set<String> myUniqueCards = new HashSet<>(myCollection);
Set<String> theirUniqueCards = new HashSet<>(theirCollection);
myUniqueCards.removeAll(theirCollection);
theirUniqueCards.removeAll(myCollection);
return !myUniqueCards.isEmpty() && !theirUniqueCards.isEmpty();
}

static Set<String> commonCards(List<Set<String>> collections) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ void testCanTradeBothCollectionsMixedCards() {

@Test
@Tag("task:3")
@DisplayName("canTrade returns true when my collection is a non-empty subset of their collection")
@DisplayName("canTrade returns false when my collection is a non-empty subset of their collection")
void testCanTradeMyCollectionSubsetOfTheirCollection() {
Set<String> myCollection = new HashSet<>(Set.of("Gyros", "Garilord"));
Set<String> theirCollection = new HashSet<>(Set.of("Garilord", "Veevee", "Gyros"));
assertThat(GottaSnatchEmAll.canTrade(myCollection, theirCollection)).isTrue();
assertThat(GottaSnatchEmAll.canTrade(myCollection, theirCollection)).isFalse();
}

@Test
Expand Down

0 comments on commit f6a53f9

Please sign in to comment.