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.
As input you will be given a sequence of 0
and 1
, no spaces.
Your program should return True
if there is a solution and False
if no solution exists.
flip_cards("0100110") -> True
flip_cards("01001100111") -> False
flip_cards("100001100101000") -> True