Beginner Help with code example from book

J

Joseph L. Casale

I am trying to understand how the following code knows not to count a tile =
that has been previously counted, its an example from Chris Pine's Learn to=
Program from Pragmatic Bookshelf. I understand how the first size =3D size=
+ ... recursively starts counting, but for example when you go to the next=
size =3D ... how is the possible overlap accounted for?

Thanks!
jlc

# These are just to make the map
# easier for me to read. "M" is
# visually more dense than "o".
M =3D ' land'
o =3D ' water'
world =3D [[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]]
def continent_size world, x, y
if world[y][x] !=3D ' land'
# Either it' s water or we already
# counted it, but either way, we don' t
# want to count it now.
return 0
end
# So first we count this tile...
size =3D 1
world[y][x] =3D ' counted land'
# ...then we count all of the
# neighboring eight tiles (and,
# of course, their neighbors by
# way of the recursion).
size =3D size + continent_size(world, x-1, y-1)
size =3D size + continent_size(world, x , y-1)
size =3D size + continent_size(world, x+1, y-1)
size =3D size + continent_size(world, x-1, y )
size =3D size + continent_size(world, x+1, y )
size =3D size + continent_size(world, x-1, y+1)
size =3D size + continent_size(world, x , y+1)
size =3D size + continent_size(world, x+1, y+1)
size
end
 
M

Mark Bush

Joseph said:
I am trying to understand how the following code knows not to count a
tile that has been previously counted, its an example from Chris Pine's
def continent_size world, x, y
if world[y][x] != ' land'
# Either it' s water or we already
# counted it, but either way, we don' t
# want to count it now.
return 0
end
:
world[y][x] = ' counted land'

The world is changed. When ' land' is counted, it turns into ' counted
land' so on iteration, the "if" sees that it is not land and returns 0.
 
J

Joseph L. Casale

PiA+IGRlZiBjb250aW5lbnRfc2l6ZSB3b3JsZCwgeCwgeQ0KPiA+IGlmIHdvcmxkW3ldW3hdICE9
ICcgbGFuZCcNCj4gPiAjIEVpdGhlciBpdCcgcyB3YXRlciBvciB3ZSBhbHJlYWR5DQo+ID4gIyBj
b3VudGVkIGl0LCBidXQgZWl0aGVyIHdheSwgd2UgZG9uJyB0DQo+ID4gIyB3YW50IHRvIGNvdW50
IGl0IG5vdy4NCj4gPiByZXR1cm4gMA0KPiA+IGVuZA0KPiA+IDoNCj4gPiB3b3JsZFt5XVt4XSA9
ICcgY291bnRlZCBsYW5kJw0KPg0KPiBUaGUgd29ybGQgaXMgY2hhbmdlZC4gIFdoZW4gJyBsYW5k
JyBpcyBjb3VudGVkLCBpdCB0dXJucyBpbnRvICcgY291bnRlZA0KPiBsYW5kJyBzbyBvbiBpdGVy
YXRpb24sIHRoZSAiaWYiIHNlZXMgdGhhdCBpdCBpcyBub3QgbGFuZCBhbmQgcmV0dXJucyAwLg0K
Pg0KPiAtLQ0KPiBQb3N0ZWQgdmlhIGh0dHA6Ly93d3cucnVieS1mb3J1bS5jb20vLg0KDQpBcHBy
ZWNpYXRlIHRoZSBwb2ludGVyLCB0aGFua3MgZm9yIGJlYXJpbmcgd2l0aCBtZSA6KQ0KV2h5IGhh
cyB0aGUgYXV0aG9yIHJldmVyc2VkIHRoZSAiWCIgYW5kICJZIj8gb24gYSBzcXVhcmUgd29ybGQg
aXQgd291bGQgbm90IG1hdHRlciwgYnV0Li4uIFdhcyB0aGVyZSBhbnkgc2lnbmlmaWNhbmNlIHRv
IHRoaXM/DQoNClRoYW5rcyBhZ2FpbiENCmpsYw0KDQo=
 
M

Mark Bush

Joseph said:
Why has the author reversed the "X" and "Y"? on a square world it would
not matter, but... Was there any significance to this?

Because of the way the world has been defined. The definition is visual
and so it is natural to look at it and think of "x" as across and "y" as
up and down. That means that "y" refers to the row and "x" as the item
in the row. The world is an array of rows, so the row index comes
first. Hence [y][x].
 
T

Todd Benson

def continent_size world, x, y
if world[y][x] != ' land'
# Either it' s water or we already
# counted it, but either way, we don' t
# want to count it now.
return 0
end
:
world[y][x] = ' counted land'

The world is changed. When ' land' is counted, it turns into ' counted
land' so on iteration, the "if" sees that it is not land and returns 0.

Appreciate the pointer, thanks for bearing with me :)
Why has the author reversed the "X" and "Y"? on a square world it would not matter, but... Was there any significance to this?

I'm pretty sure it doesn't matter on any surface as long all tiles are
described in 2 dimensions (which they would have to, if you use a grid
of x's and y's.) Think, for example, the surface of a 3-D globe cut
up into 4-sided tiles of different size. The only drawback is that
you have to pick poles to topologically shrink a sphere into a surface
(this is often done by turning it into a torus/donut, but doesn't have
to be)

I haven't read Pine's book, but the x's and y's switcheroo can be
explained, I suppose, like this...

On a mathematical graph, a larger x means further right, and a larger
y means further up. In a computer image, you have to remember, of
course, larger y usually means further down.

So, think of [0,3]. On an xy grid, that point would lie 3 units up on
the vertical axis.

For a multi-dimensional array [0,3] refers to the zeroth array, third
item (or, first array and fourth item, depending on how your mind
works).

In your visual multi-dimensional array world map, that would be the
fourth tile to the right, which would -- at least for most people --
be the x dimension, and not the y.

Hope that makes some sense,
Todd
 
J

Joseph L. Casale

I haven't read Pine's book, but the x's and y's switcheroo can be
explained, I suppose, like this...

On a mathematical graph, a larger x means further right, and a larger
y means further up. In a computer image, you have to remember, of
course, larger y usually means further down.

So, think of [0,3]. On an xy grid, that point would lie 3 units up on
the vertical axis.

For a multi-dimensional array [0,3] refers to the zeroth array, third
item (or, first array and fourth item, depending on how your mind
works).

In your visual multi-dimensional array world map, that would be the
fourth tile to the right, which would -- at least for most people --
be the x dimension, and not the y.

Hope that makes some sense,
Todd

Mark, Todd,
Thanks for your time,
I really appreciate it!
jlc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top