DEV Community

Erik Anderson
Erik Anderson

Posted on

A Beginner Program I'm Rather Proud Of

What does it do? It prints a four by four grid pattern.

Thrilling, I know

But I got really excited once I debugged it and it worked.

Code is here, and a slightly longer description is here.

Top comments (11)

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

Good work! Seems like a project that can grow with you, too, since you could change how you go about printing the grid.

Collapse
 
datadeverik profile image
Erik Anderson

Good point! It'd be interesting to make it print a grid with n rows and m columns (taking n, and m, integers). Or perhaps working on being line-efficient and expressive.
And thanks!

Collapse
 
rachelsoderberg profile image
Rachel Soderberg • Edited

I don't know python well, so I couldn't tell at a glance if it already did this, but my first thought was also that it could be a good exercise to change it to working recursively for n number of rows/cols

Thread Thread
 
datadeverik profile image
Erik Anderson

Interesting. There is currently no recursion. I had been thinking about adding the capability of printing n rows and m columns, but I was thinking of doing that through loops, not recursion. Do you have a quick description or example of what recursion might look like in this situation?

Thread Thread
 
rachelsoderberg profile image
Rachel Soderberg

Good question - I'll think on that a bit and let you know! I had an idea that it would probably work but didn't really ponder it very hard.

Thread Thread
 
rachelsoderberg profile image
Rachel Soderberg

Well, I couldn't articulate what I was thinking well without code, so I decided to take the opportunity for a little practice and wrote up a quick application that does basically what yours does (though I stuck to a single symbol for each "square" instead of building them nicely as you did. It's in C#, but hopefully you can still get the idea:
Grid Printer

Thread Thread
 
datadeverik profile image
Erik Anderson

Cool! Thanks for thinking about it and I'm glad I instigated some practice.
The language barrier is pretty strong, but I do see a while loop, which makes sense. However, I don't see any recursive function calls. Though I'm not sure of the syntax for function definition in C#, so I may be missing something.
Or I may be mis-understanding recursion. According to what I've recently learned a program is working recursively when a function calls itself. Do I have that right?
A week or two ago I wrote a post on Dev about iterative vs recursive functions, which I had just learned and was excited about.
Based on my understanding as I laid it out in that post, and my (potentially incorrect) reading of your code, yours looks more like an iterative solution.
I'm digging into this because I'm particulary interested in your earlier comment that "it could be a good exercise to change it to working recursively for n number of rows/cols" because I don't understand what that would look like.
All of that being, it seems to me that an iterative solution makes sense here.

Thread Thread
 
rachelsoderberg profile image
Rachel Soderberg

I think you're correct and my solution was iterative as well, to write the correct number of rows. My understanding is that a recursive method calls itself, as you said. Defining methods in C# is where I wrote "private static void PrintGridSquares(int squareCounter){} and calling a method looks like PrintGridSquares(value). I called the method originally in Main(), then it gets called by itself for the number of squares in the grid (squareCounter). I created a new line at each row end, to make it a square grid.

I hooe that helps clear it up a bit!

Collapse
 
datadeverik profile image
Erik Anderson

Thanks! That's really engouraging. :)

Collapse
 
thecodepixi profile image
Emmy | Pixi

Great work! I don't know Python but after taking a look at your code I'm interested to learn. It was so easy to understand what you wrote!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.