The game is implemented in various languages, including PHP Ruby and Python and most seem to be structured as:
- create a randomized list of numbers
- while the list is not sorted:
- ask for and input the number to be reversed
- reverse that portion of the list.
Here is proof for Python:
If you were testing an implementation or otherwise running it, every once in a while it would just exit. Run it again and it would most likely work for a long time. It is not something I would like to use the normal xUnit type tests/continuous testing regime to uncover as although a transient fail would be noted, those test regimes rely heavily on easy reproducibility of the failure.
>>> n = 10
>>> sortlist = range(n)
>>> randlist = range(n)
>>> random.shuffle(randlist)
>>> x = 0
>>> while randlist != sortlist:
... x += 1
... random.shuffle(randlist)
...
>>> print x
714418
>>>
I guess what would be needed is knowledge of corner cases. For shuffle, corner cases to think of would include returning the input order, reverse input order, sorted, and reverse sorted order.
- Paddy.
P.S. Work pays me to be this finickity when testing ASICs
Excellent catch!
ReplyDeleteThis reminds me of winmine's behavior. It makes sure that your first click is never on a mine.
(However, in some cases, your first click might solve the game.)
@lorg: sure? I can remember blowing up several times on my first click in winmine.
ReplyDeletePaddy: in the interest of finickity, it would be "Work pays me to be this finicky...".
Nice post. Keep it up.
moranar:
ReplyDeleteyes, at least starting from some version.
I don't get it... what's the error exactly? If the list is sorted, it should require 0 tries, and it does. 0 is a number.
ReplyDeleteThe idea is to play the game. If a randomised sort produces the items in order then the while loop is not taken , depriving you of playing the game. If the test was a do-while loop with the test at the end then the player would always be presented with something but the while loop can give you no game to play.
Delete