• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help creating algorithm for a problem

 
Jorge Vazquez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been doing some exercises from codingbat, and I haven't figure out a way to solve this particular problem; may be someone could point me how to approach this. Thanks

http://codingbat.com/prob/p155405


 
Frank Hanner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Vazquez wrote:I've been doing some exercises from codingbat, and I haven't figure out a way to solve this particular problem; may be someone could point me how to approach this. Thanks

http://codingbat.com/prob/p155405




Do you see a pattern at all?

It's divided into n sections. Inside each section is n numbers (n*n). Each section is essentially counting down. With each section progression the number of zeros decreases by 1 and the appropriate number for the count down is inserted. If that get you thinking about it the right way, I'll show you some example code that might help.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the problem that you don't see the pattern, or is it that you see the pattern but are having trouble translating it to code?
 
Jorge Vazquez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Is the problem that you don't see the pattern, or is it that you see the pattern but are having trouble translating it to code?

yeah, I see the pattern, I'm just having difficulty putting into code...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is an outline of my first thoughts of how to solve it.

Firstly, break the array up into n groups. So that means we've got an outer loop from 1 to n.

Then I'd fill each group from the right-hand side. That means you're counting up from 1 to the group index to work out the value you're inserting, so you've got an inner loop. Then it's just a matter of working out the array position based on the two loop indices.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic