[Newbies] Reverse Game in seven lines
gaelli at emergent.de
Tue Jul 11 16:54:18 UTC 2006
>> p.p.s. inspired by Paul Bissex - a guy who once wrote a small article
>> about squeak for Wired - challenge on:
>> I wrote an Etoys version of this "reverse"-game.
>> It can be found on
>> (I hope you all have the squeakland plugin installed... ;-) )
>> It has only a few lines more than the smalltalk (I included a
>> smalltalk version), python, ruby,... version but comes with a much
>> more sophisticated user interface.
>> So I do think that Etoys are the way to go... no matter what the
>> language is underneath - be it smalltalk/python/ruby/etc...
On Jul 7, 2006, at 8:09 PM, Chris Muller wrote:
> Hi Markus, I didn't see any Smalltalk on that page. Just for fun I
> coded up that reverse game and posted it, we'll see if it shows
> up. It
> was 8 lines "beating" Ruby by 2 lines and Python by 1.
> Here's the code I ended up with:
> numbers := (1 to: 9) asArray shuffled.
> steps := 0.
> [ Transcript cr; show: numbers.
> numbers isSorted ] whileFalse:
> [ flipCount := (FillInTheBlank request: 'reverse how many?')
> 1 to: flipCount//2 do: [ :n | numbers swap: n with: flipCount-n+1 ].
> steps := steps + 1 ].
> Transcript cr; show: 'done, that took you ', steps printString, '
> Strange, I'm not sure why there are ~20 languages (including "REBOL")
> represented but no mention of Smalltalk anywhere, especially if you
> submitted it..
thanks! Using your swap and isSorted methods and our extreme naked
objects approach ;-)
I came up with a version of 7 lines:
steps := 0.
numbers := (1 to: 9) asArray shuffled.
[numbers isSorted ] whileFalse:
[flipCount:= numbers indexOf: ((SelectionMenu selections: numbers)
startUpWithCaption: 'Revert up to which number?') .
1 to: flipCount//2 do: [ :n | numbers swap: n with: flipCount-n+1 ].
steps := steps + 1].
PopUpMenu inform: 'You needed ', steps asString,' steps.'
Don't know, why it did not show up, Paul sent me a mail that he liked
squeak and wanted to add it...so I added him here on the bcc.
Anyone tried the Etoys-Version? Someone told me that it was not
working for him, but I did not investigate...
p.s. The following Python script does not work for me under OS-X.
Maybe sorted() and reversed() need some library here?
numbers = random.sample(range(1,10), 9)
steps = 0
while numbers != sorted(numbers):
print " ".join(map(str, numbers))
flipcount = int(raw_input("Reverse how many? "))
numbers[:flipcount] = reversed(numbers[:flipcount])
steps += 1
print "Done! That took you %d steps." % steps
More information about the Beginners