3 ms·
FizzBuzz is one of those puzzles where majority of experienced programmers won't find the solution easily as it involves the modulo operator in weird ways. The
by 72f988bf 5y ago
FizzBuzz is one of those puzzles where majority of experienced programmers won't find the solution easily as it involves the modulo operator in weird ways.
The modulo operator is rarely used in practice, unless you're writing your own hash table or some other internal where bucketizing by some ID is relevant.
Using the modulo within a for-loop though, virtually never happens.
So unless you're niche where using modulo every other day, or you're just out of school where you just learned about "+,-,*,/,%" , you have a good chance at failing fizzbuzz if you don't remember it's "that modulo 3 and modulo 5 silly puzzle"
There are better ways of testing for iterating, iterating through a real collection of elements is already much better than FizzBuzz.
- jmaygarden 5y agoI'd never considered the modulo operator to be that uncommon, but I've mostly worked in systems software and firmware. Is that really the case?
- pc86 5y agoThe only time I've ever used it in programming is FizzBuzz.
- jmaygarden 5y agoTo prevent rehashing this, I have discovered the exact same discussion from 5 years ago. https://news.ycombinator.com/item?id=11884645 https://news.ycombinator.com/item?id=11884645
- ffdzghg 5y agoCurious: How do you do “every other row” in a loop if you don’t do modulo?
- pc86 5y agoWhy would you want to do every other row in a database? That just sounds like "a random half" with extra steps if you're doing math on your DBMS's equivalent of RowID.
- arp242 5y agoThings like applying a different colour or styling for visual effect is a common-ish reason, or parsing some input which looks like: Stuff I want Extra stuff belonging to the above line I want to ignore More stuff I want More ignore stuff [..etc..] Which can be from a CLI tool, a web scraper tool, something copy pasted from a webpage, or something like that. This data can often be "messy" but in a regular way. I've parsed WikiText tables from Wikipedia like this (very crude, but it only needs to run once). Another example of "% 2" is for centre aligning strings in a grid (e.g. a terminal; if it's odd you need to add one extra space to one side). I've also used it for time calculations (e.g. printing seconds as "hour:minutes:seconds"). That's from the top of my head; I've probably used it in other things as well. Note this is all high-level application work: no muckery with low-level controllers, firmware, or whatnot. Is the % operator very common? No; but I don't think it's as "virtually never happens"-uncommon as was mentioned a few posts up the thread.
- wst_ 5y agoNot advocating for being oblivious about modulo op, but you could use stepping in some languages. Example, in Kotlin: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/step.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/s...