You should be automatically redirected . If not, visit
and update your bookmarks.


Let's play

I chanced upon this little diversion today, and I thought it was quite cute.

The game of REVERSE requires you to arrange a list of numbers in numerical order from left to right. To move, you tell the computer how many numbers (counting from the left) to reverse. For example, if the current list is 2 3 4 5 1 6 7 8 9 and you reverse 4, the result will be 5 4 3 2 1 6 7 8 9. Now if you reverse 5, you win.

It looks a bit like this when played in the terminal:

(4 2 3 1 5 6 7 8 9 10)
Reverse how many?
(1 3 2 4 5 6 7 8 9 10)
Reverse how many?
(2 3 1 4 5 6 7 8 9 10)
Reverse how many?
(3 2 1 4 5 6 7 8 9 10)
Reverse how many?
(1 2 3 4 5 6 7 8 9 10)
Done! That took you 19 steps!

Well, before you read further, you can wander off and write a newLISP version of your own.

Don't worry, I'll wait here until you get back.

No, really - I'm happy to wait. I'll surf the web a bit, and polish up the keys of my keyboard (using a bottle of Reverse Polish, since newLISP now courses through my veins).

Ah, have you finished? Good.

Now you can compare your exquisitely-fashioned jewel of a program with my version:

(define (rev-slice n lst)
    (append (reverse (0 n lst)) (n lst)))
(set 'steps 0 'goal (sequence 1 10) 'state (randomize goal))
(until (= goal state)
    (println state "\nReverse how many?")
    (inc 'steps)
    (set 'state (rev-slice (int (read-line)) state)))
(string "\\nDone! That took you " steps " steps!")

Well, it's nice and short, but it will insist on printing out the list as a list, and the ending could do with a bit of work. So more of a roughly-cut diamond, perhaps.

That's about it, really, but if you wander over to Paul Bissex 's site, you'll be able to see what it looks like in different languages. Good fun, and fascinating to see what's hard and what's easy.


At 08:20, Anonymous newdep said...

Have you tried a negative number already in your reversing? should it be that way?

At 08:23, Anonymous newdep said...

I needed some time to figure out what it does but its quit a nice little game ;-)

At 09:16, Blogger newlisper said...

no i didn't try a negative number. After looking at the other versions, I noticed that everyone was worried about number of lines/characters, and didn't put much error checking in. so there's plenty of things that could be changed!


Post a Comment

Links to this post:

Create a Link

<< Home