*From Chris Pine, Learn to Program Hey All -- In the below source code they array is called in the method as x, y, but the "if" statements are looking to [y][x]. Can anyone explain to me why they are switched? If you make them [x][y] it still outputs the correct number (23). Thanks in advance, Jordan ## Start of Example # These are just to make the map # easier for me to read. "M" is # visually more dense than "o". M = '' land '' o = '' water '' world = [[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,M], [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], [M,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 x > 10 && y > 10 size = 0 else if world[x][y] != '' land '' return 0 end end # Either it '' s water or we already # counted it, but either way, we don '' t # want to count it now. # So first we count this tile... size = 1 world[x][y] = '' counted land '' # ...then we count all of the # neighboring eight tiles (and, # of course, their neighbors by # way of the recursion). size = size + continent_size(world, x-1, y-1) size = size + continent_size(world, x , y-1) size = size + continent_size(world, x+1, y-1) size = size + continent_size(world, x-1, y ) size = size + continent_size(world, x+1, y ) size = size + continent_size(world, x-1, y+1) size = size + continent_size(world, x , y+1) size = size + continent_size(world, x+1, y+1) size end puts continent_size(world, 5, 5) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
WORLD_VALUES = {:o => :water, :X => :land} WORLD = [[:o,:o,:o,:o,:o,:o,:o,:o,:o,:o], [:o,:o,:o,:o,:X,:X,:o,:o,:o,:o], [:o,:o,:o,:o,:o,:o,:o,:o,:X,:X], [:o,:o,:o,:X,:o,:o,:o,:o,:o,:X], [:o,:o,:o,:X,:o,:X,:X,:o,:o,:o], [:o,:o,:o,:o,:X,:X,:X,:X,:o,:o], [:o,:o,:o,:X,:X,:X,:X,:X,:X,:X], [:o,:o,:o,:X,:X,:o,:X,:X,:X,:o], [:o,:o,:o,:o,:o,:o,:X,:X,:o,:o], [:X,:X,:o,:o,:o,:X,:o,:o,:o,:o], [:o,:o,:o,:o,:o,:o,:o,:o,:o,:o]] def continent_size(x, y) size = 0 begin WORLD.first(x).each do |row| row.first(y).each do |col| size += 1 if WORLD_VALUES[col] == :land end end rescue return "invalid continent size" end return size end continent_size 10, 10 #should be 30 HTH, Aldo Sarmiento jordantheous wrote:> *From Chris Pine, Learn to Program > > Hey All -- > In the below source code they array is called in the method as x, y, > but the "if" statements are looking to [y][x]. Can anyone explain to > me why they are switched? If you make them [x][y] it still outputs > the correct number (23). > Thanks in advance, > Jordan > > ## Start of Example > > # These are just to make the map > # easier for me to read. "M" is > # visually more dense than "o". > > M = '' land '' > o = '' water '' > world = [[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,M], > [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], > [M,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 x > 10 && y > 10 > size = 0 > else > if world[x][y] != '' land '' > return 0 > end > end > # Either it '' s water or we already > # counted it, but either way, we don '' t > # want to count it now. > > > # So first we count this tile... > size = 1 > world[x][y] = '' counted land '' > # ...then we count all of the > # neighboring eight tiles (and, > # of course, their neighbors by > # way of the recursion). > size = size + continent_size(world, x-1, y-1) > size = size + continent_size(world, x , y-1) > size = size + continent_size(world, x+1, y-1) > size = size + continent_size(world, x-1, y ) > size = size + continent_size(world, x+1, y ) > size = size + continent_size(world, x-1, y+1) > size = size + continent_size(world, x , y+1) > size = size + continent_size(world, x+1, y+1) > size > end > > puts continent_size(world, 5, 5)-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 23, 8:34 pm, jordantheous <jmaca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> *From Chris Pine, Learn to Program > > Hey All -- > In the below source code they array is called in the method as x, y, > but the "if" statements are looking to [y][x]. Can anyone explain to > me why they are switched? If you make them [x][y] it still outputs > the correct number (23). > Thanks in advance, > Jordan >What you''ve got there is an array of rows (rather than an actual 2D construct), so it''s normal that [y][x] first selects a row, then an element in that row. Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Fred, Thanks for responding. I was assuming that because of the line: continent_size world, x, y x defined the row and y defined the array value within that row. Is that not true? world[x][y] and world[y][x] only outputs the right value because 5, 5 is right smack in the middle of the array of arrays. If you change it to 4, 3 and run it with world[x][y] and then try it with world[y][x] you get two different values. I''m still not sure why the code starts with x, y and then get''s switched to [y][x] (I''ve pasted the original below showing the [y][x] values. Am I just being dense?: # These are just to make the map # easier for me to read. "M" is # visually more dense than "o". M = '' land '' o = '' water '' world = [[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,M], [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], [M,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 x > 10 && y > 10 size = 0 else if world[y][x] != '' land '' return 0 end end # Either it '' s water or we already # counted it, but either way, we don '' t # want to count it now. # So first we count this tile... size = 1 world[y][x] = '' counted land '' # ...then we count all of the # neighboring eight tiles (and, # of course, their neighbors by # way of the recursion). size = size + continent_size(world, x-1, y-1) size = size + continent_size(world, x , y-1) size = size + continent_size(world, x+1, y-1) size = size + continent_size(world, x-1, y ) size = size + continent_size(world, x+1, y ) size = size + continent_size(world, x-1, y+1) size = size + continent_size(world, x , y+1) size = size + continent_size(world, x+1, y+1) size end puts continent_size(world, 5, 5) On Jan 23, 1:38 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 23, 8:34 pm, jordantheous <jmaca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > *From Chris Pine, Learn to Program > > > Hey All -- > > In the below source code they array is called in the method as x, y, > > but the "if" statements are looking to [y][x]. Can anyone explain to > > me why they are switched? If you make them [x][y] it still outputs > > the correct number (23). > > Thanks in advance, > > Jordan > > What you''ve got there is an array of rows (rather than an actual 2D > construct), so it''s normal that [y][x] first selects a row, then an > element in that row. > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Jan 2009, at 17:45, jordantheous wrote:> > Hey Fred, > > Thanks for responding. > > I was assuming that because of the line: > > continent_size world, x, y > > x defined the row and y defined the array value within that row. Is > that not true? > > > world[x][y] and world[y][x] only outputs the right value because 5, 5 > is right smack in the middle of the array of arrays. If you change it > to 4, 3 and run it with world[x][y] and then try it with world[y][x] > you get two different values. I''m still not sure why the code starts > with x, y and then get''s switched to [y][x] (I''ve pasted the original > below showing the [y][x] values. Am I just being dense?: >The code is written so that 9,0 calculates the value for the point at the far right of the top row. You need to permute the values, since the first row is given by world[0], and the far right element is world[0][9] You could just not permute the x&y and say that the world must be specified as an array of columns rather than an array of rows but that would result in the map being less human readable. Fred> # These are just to make the map > # easier for me to read. "M" is > # visually more dense than "o". > > M = '' land '' > o = '' water '' > world = [[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,M], > [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], > [M,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 x > 10 && y > 10 > size = 0 > else > if world[y][x] != '' land '' > return 0 > end > end > # Either it '' s water or we already > # counted it, but either way, we don '' t > # want to count it now. > > > # So first we count this tile... > size = 1 > world[y][x] = '' counted land '' > # ...then we count all of the > # neighboring eight tiles (and, > # of course, their neighbors by > # way of the recursion). > size = size + continent_size(world, x-1, y-1) > size = size + continent_size(world, x , y-1) > size = size + continent_size(world, x+1, y-1) > size = size + continent_size(world, x-1, y ) > size = size + continent_size(world, x+1, y ) > size = size + continent_size(world, x-1, y+1) > size = size + continent_size(world, x , y+1) > size = size + continent_size(world, x+1, y+1) > size > end > > puts continent_size(world, 5, 5) > > > > > On Jan 23, 1:38 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Jan 23, 8:34 pm, jordantheous <jmaca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> *From Chris Pine, Learn to Program >> >>> Hey All -- >>> In the below source code they array is called in the method as x, y, >>> but the "if" statements are looking to [y][x]. Can anyone explain >>> to >>> me why they are switched? If you make them [x][y] it still outputs >>> the correct number (23). >>> Thanks in advance, >>> Jordan >> >> What you''ve got there is an array of rows (rather than an actual 2D >> construct), so it''s normal that [y][x] first selects a row, then an >> element in that row. >> >> Fred > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---