Skip to content(if available)orjump to list(if available)

Fizz Buzz without conditionals or booleans

swisniewski

Sigh…

Saying the code doesn’t have conditions or booleans is only true if you completely ignore how the functions being called are being implemented.

Cycle involves conditionals, zip involves conditionals, range involves conditionals, array access involves conditionals, the string concatenation involves conditionals, the iterator expansion in the for loop involves conditionals.

This has orders of magnitude more conditionals than normal fizz buzz would.

Even the function calls involve conditionals (python uses dynamic dispatch). Even if call site caching is used to avoid repeated name lookups, that involves conditionals.

There is not a line of code in that file (even the import statement) that does not use at least one conditional.

So… interesting implementation, but it’s not “fizzbuzz without booleans or conditionals”.

HWR_14

What's a "disguised Boolean" in this context?

null

[deleted]

brudgers

No matter how you calculate FizzBuzz, it is bad engineering.

  const Fizzbuzz = "1 2. Fizz, 4 ... "
  Print Fizzbuzz

hyperhello

Yeah, you can even generate a template for any length of number prefix too. What’s the point of the game without booleans?

unsnap_biceps

Much like stop50's solution, I also used the modulo, but I make use of the terminal to overwrite the number. It's only three lines of code, but I split up the list to be more readable on here.

This works from 1 to 100000000000000000000 before it overflows, and 100000000000000000000 is above the max size of a unsigned 64 bit int, so I feel that it's good enough

    fizzbuzz = [
        "fizzbuzz            ",
        "", "",
        "fizz                ",
        "",
        "buzz                ",
        "fizz                ",
        "", "",
        "fizz                ",
        "buzz                ",
        "",
        "fizz                ",
        "", "" ]
    for n in range(99999999999999999999-30, 100000000000000000000):
        print(f"{n}\r{fizzbuzz[n%15]}")

bluGill

I always wanted to write this with duff's device. switch with fall through is almost never a good thing but it allows for some 'interesting' tricks. Wouldn't be hard, but I have kids so finding half an hour to concentrate is hard.

LanceH

package main

  import (
   "fmt"
   "math/rand"
  )

  var fb [4]string = [4]string{"", "fizz", "buzz", "fizzbuzz"}
  var lucky int64 = 176064004

  func main() {
   for i := 1; i <= 100; i++ {
    if i%15 == 1 {
     rand.Seed(lucky)
    }
    fmt.Printf("%d: %s\n", i, fb[rand.Int63()%4])
   }
  }

frosting1337

There's a conditional, though?

dullcrisp

vim +'exec "norm 99o"|%s/$/\=line(".")/|vert new|exec "norm i\r\rFizz\r\rBuzz\rFizz\r\r\rFizz\rBuzz\r\rFizz\r\r\rFizzBuzz"|exec "norm gg\<c-v>G$y"|bd!|let @q="10a \<esc>\"0gpj"|exec "norm gg10@q"|silent /100/+,$d|silent %s/\d\+\s\+\(\w\+\)/\1'

Now I see it's the same solution as in the post.

stop50

Answer: Modulo or adding "%3" and "%5" before masking it