Personal Insights
Puzzles in ABAP – Starting with FizzBuzz
Hi!
ABAP is an awesome programming language, and there are often many ways to get to the same end result. But as humans, we tend to stick to habits and do the same things in the same ways as always unless we consciously tries new ways. Hopefully these ways are good, but in order to improve and evaluate we need to be challenged or challenge our ideas.
One way to try new things, to experiment, and to expand and learn, is to write one or a couple of solutions to a non-work-related programming challenge. No matter if you’re a novice to ABAP and programming or if you’ve got 30 years under your belt, you could either try to solve it at all, or in some new and obscure way.
Doing it non-work-related might release us from the constraints of requirements, guidelines, habits and routines. But hopefully it will give a positive ROI on our knowledge and will hopefully bring back value to ourselves and our employers and customers.
Sharing is caring
Sharing the solutions could also bring value and knowledge to fellow developers. I started a repository where I intend to collect puzzles and create demo example solutions using the various aspects of the ABAP language, and I welcome you all to join! Either by sharing puzzles or challenges that you deem fit here, or by proposing alternate solutions or comments.
The link to the repository is at the end of this post but if you would like to try it out yourselves first, without being influenced by other solutions, wait a bit until clicking that link.
FizzBuzz
This one is a very simple one and can be done in various ways and searching on the forum here reveals a couple of blog posts talking about it already. (For instance The SAP Technical ABAP Interview Question You Might Have Never Heard Of from as late as august of last year). I think this is bigger outside of the ABAP world that inside.
Fizz Buzz is allegedly originally a childrens game to practice division, with some simple rules.
The first player starts by saying the number 1 and players then take turn counting upwards one number at a time, but:
- If a number is evenly divisable by 3, the player instead says ‘Fuzz’
- If a number is evenly divisable by 5, the player instead says ‘Buzz’
- If a number is evenly divisable by both 3 and 5, the player says ‘FizzBuzz’
So, it would start like this:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz
The challenge here is to implement this in ABAP to output the first 100 rounds of the FizzBuzz game.
Have fun!
And then there was the repository
I will get back to you with more puzzles in the future if you think you’d enjoy that. The central place for them will be this repository on GitHub. Currently there is only FizzBuzz with a couple of example solutions with various degrees of ‘goodness’. 😁
And please share your interesting solutions here in the comments or in the repository. I will gladly have additional alternate solutions added to the fizzbuzz class there (with explanations if needed). Either by a pull request, or by asking me to do it.
nice info
Hi Jörgen Lindqvist,
I accepted your "FizzBuzz" challenge today, and I used the latest ABAP trial for this:
Get latest version of ABAP trial using BTP
Here, I share my approach:
The output looks just like this:
Thank you! I'm glad you shared your solution.
Clever with the string concatenation there in the divider_exam method...
Hope you had fun!
You're welcome. It was fun!
Hi Jörgen,
I gave this a try, also trying to keep the code minimal. So this is what I came up with:
Output:
Regards, Martin.
I'm happy you tried it, and thanks for sharing!
Hi Jorgen
I modified Martin's solution a little:
Thank you for another share! Please also have a look in the repository I linked for a couple of other solutions... 🙂
In fact I shaved off another few lines by doing it like this:
Gave the same output as below.
Nice
I really like this modification. You're just missing the comma in the output to separate the items. 🙂
Which gives this output:
Fizzbuzz
I also remove the '/' so that it writes the items consecutively instead of each one on a new line.
hi Jörgen,
once again a cool blog!!
We did run an internal ABAP CodingDojo with some ABAP newbies last year and I published my solutions of various katas in a public Github repo. I solved most on the ABAP cloud environment, hence my namespace Z174_ in front of most objects 😉
I used TDD with frequent commits, so that one could follow me along reading the commit history. For FizzBuzz the history on Github is a pretty good story. Even though it would have been better to store each kata in it's own package / directory for better readability.
Loved to see your different solutions on Github and your thoughts about them! Plus it's greate to read the discussion the post sparked!
Cheers
Hendrik
Thanks Hendrik, I love to share and I hope I inspire or teach at least something to someone...
And also a good repository you have with other puzzles and test classes as well. I will probably peek back there later on for more inspiration myself... 🙂