Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created January 23, 2021 02:03
Show Gist options
  • Save ZechCodes/ab8e095fb931dc2e0aefab5690101141 to your computer and use it in GitHub Desktop.
Save ZechCodes/ab8e095fb931dc2e0aefab5690101141 to your computer and use it in GitHub Desktop.
Challenge 169 - Card Flipping Game

Challenge 169 - Card Flipping Game

This challenge is about a simple card flipping solitaire game. You're presented with a sequence of cards, some face up, some face down. You can remove any face up card, but you must then flip the adjacent cards (if any). The goal is to successfully remove every card. Making the wrong move can get you stuck.

In this challenge, a 1 signifies a face up card and a 0 signifies a face down card. We will also use zero-based indexing, starting from the left, to indicate specific cards. So, to illustrate a game, consider this starting card set.

0100110

I can choose to remove card 1, 4, or 5 since these are face up. If I remove card 1, the game looks like this (using . to signify an empty spot):

1.10110

I had to flip cards 0 and 2 since they were adjacent. Next I could choose to remove cards 0, 2, 4, or 5. I choose card 0:

..10110

Since it has no adjacent cards, there were no cards to flip. I can win this game by continuing with: 2, 3, 5, 4, 6.

Supposed instead I started with card 4:

0101.00

This is unsolvable since there's an "island" of zeros, and cards in such islands can never be flipped face up.

Input Description

As input you will be given a sequence of 0 and 1, no spaces.

Output Description

Your program should return True if there is a solution and False if no solution exists.

Examples

flip_cards("0100110") -> True

flip_cards("01001100111") -> False

flip_cards("100001100101000") -> True
import unittest
def flip_cards(cards: str) -> bool:
return False # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertTrue(flip_cards("0100110"))
def test_2(self):
self.assertFalse(flip_cards("01001100111"))
def test_3(self):
self.assertTrue(flip_cards("100001100101000"))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment