this post was submitted on 14 Apr 2024
328 points (94.1% liked)

linuxmemes

20785 readers
627 users here now

I use Arch btw


Sister communities:

Community rules

  1. Follow the site-wide rules and code of conduct
  2. Be civil
  3. Post Linux-related content
  4. No recent reposts

Please report posts and comments that break these rules!

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[โ€“] ICastFist@programming.dev 4 points 5 months ago (1 children)

Suddenly I feel like a fucking accomplished programmer, despite only doing some questionable stuff on Godot lately, but never messing up my loops... Not too badly anymore, anyway.

A fizzbuzz type of question I know I would mess up on the modulo operator. I know the logic is if the division of the current_number by 3 has a remainder of zero, write fizz, but I always look up the operator

[โ€“] letsgo@lemm.ee 1 points 5 months ago

Yeah it always feels like "negative logic" to me. If it's not this and not that then don't do the other... Does my head in. Next time I'm going to use a lookup table "x..f.bf..fb.f.." then mod15 the index. f=Fizz, b=Buzz, x=both. Nice thing about this is that it's easier to change with the requirements. Want to shift the second fizz right one? No problem "x..f.b.f.fb.f..". Good luck doing that with the standard approach. Add Gronk which collides with Fizz, Buzz or both at various times? Also no problem - just extend and modify the LUT accordingly and change the mod.

I can already hear people asking why x is at the start. Arrays are indexed from 0. FizzBuzz starts at 1. 15 mod 15 is zero. Loop N from 1-100, switch on lookup[N%15], case 'f' print Fizz, case 'g' print Gronk, case 'p' print FizzGronk and so on. The only "nice" original feature you lose is when both %3 and %5 fire at the same time and it prints FizzBuzz without any extra code.