In over a decade of software development including web systems, games development, command line tools and distributed systems, I've never had to implement a linked list. If you haven't used the modulo operator, FizzBuzz isn't necessarily obvious.
In over a decade of software development including web systems, games development, command line tools and distributed systems, I've never had to implement a linked list. If you haven't used the modulo operator, FizzBuzz isn't necessarily obvious.
Outside of challenges where it's almost required (FizzBuzz), the only thing that I can think of that I've ever used modulo for is to set an upper limit on random numbers. I can probably count the number of times I've needed to do that on one hand and for every one of those I've used several hundred other math functions like abs, floor & trig functions.
I don't really do a lot of math in my code anyway.
It's one of those things I see how to do in the language when I'm first learning the syntax and then move on.
I suppose you also don't often use division in your non-mathy web work. Despite that, would you think it unfair for an employer to think it reflected poorly on you if you couldn't write code to divide two numbers?
But hey, even then, those gotchas overlap with why you shouldn't store prices in floats and devs you don't want to hire do that all the time too.
Some examples...
Outputting a text report with columns. If there were 5 columns, use the "col=i%5" to put it in the correct column as "i" is incremented.
date/time calcs such as...
x%7 is often used for day-of-the-week algorithms
x%3600 and x%60 is often used for converting raw seconds (e.g. convert 86400 seconds per day into hh:mm:ss)
(If programmer doesn't know modulus "x%7", he might hardcode a big switch statement with 7 cases -- which will work, but it's not as elegant.)
round-robin in discrete integer values --> use % modulus
round-robin in continuous values --> use trig sin(), cos()
Say Hello to Leap Seconds for me.
I've used % for various unrelated tasks for more than 20 years. Another example... in languages that didn't have bit shifting like C/C++, I used % to convert bytes to a hex string (e.g. x%16).
x = Time ( now )
...
total_elapsed_time = Time ( now ) - x
print strftime ( total_elapsed_time )
I've actually used your hex example once before!For example with modulo, I feel like I use it quite often, relatively speaking. Somewhat contrived example of usage on the web side of things:
Let's say you're building a grid system that will have an unknown number of items. The grid is 12 columns wide. In the last row, any items that otherwise would be empty should instead have a placeholder item displayed.
Given an arbitrary array of items, how many additional placeholder items should be included in what gets rendered?
const colWidth = 12
const remainder = items.length % colWidth
const numPlaceholders = (remainder === 0) ? 0 : colWidth - remainderMake every nth thing shaded a different color?
Make sure a list of things has a useful amount of elements for a given task?
When I was teaching myself how to program I'm pretty sure I made some terrible pseudo implementation of modulo before I found out it was a thing because that's a naturally occurring need.