Neko moves to the rightmost cell - part 2

Neko moves to the rightmost cell - Part 2

Let’s write another letter to Python:


Dear Python,

Make Neko move right by calling the function `right()’.
Store the value returned from `right’ in a variable `a’.
As long as `a’ is not zero, do the same thing again and
again and again ….

Now, look at the following function:

def neko_lastcolumn():
   a = right()
   while (a != 0):
       a = right()

The program and the `letter’ mean the same! When Neko reaches the
last cell, the function `right’ `returns’ 0. At that point, the
condition a != 0 (remember, a!=0 means `a NOT EQUAL to zero’) becomes
false and the loop ends.

Test it out at the Python prompt!

It’s easy to write a similar function which will take Neko down to the
last row - try it! You may call this function `neko_lastrow’

Write two other functions neko_firstcolumn and neko_firstrow.

Can you now write a function which will make Neko run round and round
the chessboard? That is, assume initially Neko is at the top left
corner - he runs to the last column, then runs down to the last row,
then runs left to the first column and then runs up to the
first row - this is done again and again and again … You may
call this function neko_runaround.

Hints: (a) You will have to write an `infinite’ loop - an
infinte loop is one which never ends - it’s condition never
becomes false. We had seen an example in a previous chapter.
(b) You can think of using the functions neko_lastcolumn,
neko_lastrow etc to make things simple.