Are these kinds of pattern questions just for fun and to get better at using loops, or are there any interesting applications of these types of pattern questions? Or any interesting math behind them?
All of the above.
Fun is important. If learners aren't having fun, they're likely to quit. As an educator working with young children to adults from all walks of life, I've seen a lot of folks lose interest in programming, often because (I suspect) they didn't see the magic and power in it. Sure, for some learners, jumping into business applications might feel more purposeful than playing with patterns, so there is audience-specificity here.
There's something magical about loops and iterative processes in general. Being able to write a few lines of code and generate a pattern involves making the leap from 1 to 2 to 3 to an arbitrary n
. Generalizing code and figuring out which parts are dynamic (i.e. parameters and variables) is a critical skill in programming. These loops provide visual feedback for this process and allow students to iterate on naive, hardcoded attempts until they've generalized the logic.
If you think of the characters on the screen as pixels, these nested loop exercises translate very well into computer graphics. I have a post on how you can animate these patterns. This is a useful teaching approach for languages other than Scratch and JavaScript that don't have a particularly easy way to make images and animations other than ASCII.
ASCII text applications like this connect students to computer history while increasing their comfort working in the always-relevant terminal ecosystem. ASCII art, Text adventures, creative coding, 10 PRINT, Figlet banners and other console-based applications are just as fun and useful today as they've ever been. npm is chock full of packages for animations, spinners, banners and colors to create user-friendly command line tools for programmers.
From a purely technical perspective, these exercises provide great opportunities for nesting conditions in loops, counting forwards and backwards, incrementing by something other than 1, understanding when for
vs while
is appropriate, getting comfortable with string manipulation and building up to arrays and functions. Given the fun factor and possibilities available, I don't think building an introductory course around ASCII patterns is a particulary absurd idea.
In regards to math, there can be as much or as little of it as you want in making these patterns. Consider patterns in triangular numbers, Pascal's Triangle and cellular automata, for starters. For the right audience, integrating trigonometric functions like sine and cosine functions can provide a great extension of possibilities. Fractals and L-Systems can be fun to play with, given the right resolution and reasonably experienced coders.
Finally, as an educator, puzzles like these offer a great source of assignments that are relatively difficult to find direct solutions for on the internet, potentially helping combat plagiarism. It's easy to communicate the deliverables by showing a few example runs and explaining that it should work on any n
. These problems are language-agnostic.
Code Golf SE has over 1000 posts in the ascii-art tag ripe for exploration. Codewars has a good deal of ASCII shape printing problems with test cases (disclosure: I consult for the company that owns Codewars).