def scylla_or_charybdis(moves, n): This problem was inspired by the article "A Magical Answer to the 80-Year-Old Puzzle" in Quanta Magazine. Thanks to your recent cunning stunts, your nemesis in life is trapped inside a devious game where, for a refreshing change from the usual way of things, you get to be the Final Boss. (Everyone is the hero in their own story until they become a villain in somebody else's, after all.) This final level is a one-dimensional video game platform that reaches n-1 steps from its center to both directions. At each end of this platform exactly n steps away from the center, your two lethal friends Scylla and Charybdis are licking their lips in anticipation of a tasty morsel. Your nemesis starts at the center of this platform. To start this deadly combat of wits, she must immediately commit to her entire sequence of future moves, given to this function as a string of characters '+' (“� just a step to the ri-i-i-i-ight � ”) and '-' (move one step to the left). Your task is to find and return a positive integer k so that executing every k:th step of moves (so this subsequence starts from position k-1 and includes every k:th element from then on) makes your nemesis fall into one of the two possible dooms n steps away from the center to each direction. If several values of k would work, return the smallest value of the possible values of k that minimizes the number of steps before the downfall. Otherwise, those freaking Tolkien eagles will once again swoop down to rescue your nemesis. She will then return in a third year analysis of algorithms course as an adversary who puts your functions through a similar wringer of wits!
Scylla or Charybdis?
def scylla_or_charybdis(moves, n):
This problem was inspired by the article "A Magical Answer to the 80-Year-Old Puzzle" in Quanta
Magazine. Thanks to your recent cunning stunts, your nemesis in life is trapped inside a devious game where, for a refreshing change from the usual way of things, you get to be the Final Boss. (Everyone is the hero in their own story until they become a villain in somebody else's, after all.)
This final level is a one-dimensional video game platform that reaches n-1 steps from its center to both directions. At each end of this platform exactly n steps away from the center, your two lethal friends Scylla and Charybdis are licking their lips in anticipation of a tasty morsel.
Your nemesis starts at the center of this platform. To start this deadly combat of wits, she must immediately commit to her entire sequence of future moves, given to this function as a string of characters '+' (“� just a step to the ri-i-i-i-ight � ”) and '-' (move one step to the left). Your
task is to find and return a positive integer k so that executing every k:th step of moves (so this subsequence starts from position k-1 and includes every k:th element from then on) makes your nemesis fall into one of the two possible dooms n steps away from the center to each direction.
If several values of k would work, return the smallest value of the possible values of k that minimizes the number of steps before the downfall. Otherwise, those freaking Tolkien eagles will once again swoop down to rescue your nemesis. She will then return in a third year analysis of
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images