Live data from Hacker News

FizzBuzz in ten languages

iolivia.me

71–80 of 103 posts

Re: FizzBuzz in ten languages

#71
post #41

A bit shorter python version: for x in range(1, 101): out = "" if x % 3 == 0: out += "Fizz" if x % 5 == 0: out += "Buzz" print(out or x)

  print('\n'.join([
      'fizzbuzz' if x%15 == 0 else
      'fizz' if x%3 == 0 else
      'buzz' if x%5 == 0 else
      str(x)
          for x in range(1, 101)]))
Or

  for x in ['fizzbuzz' if x%15 == 0 else
      'fizz' if x%3 == 0 else
      'buzz' if x%5 == 0 else
      x for x in range(1, 101)]:
    print x

Re: FizzBuzz in ten languages

#72
post #41

A bit shorter python version: for x in range(1, 101): out = "" if x % 3 == 0: out += "Fizz" if x % 5 == 0: out += "Buzz" print(out or x)

Python one liner:

print('\n'.join([{(True, False): 'Fizz', (False, True): 'Buzz', (True, True): 'FizzBuzz'}.get((i % 3 == 0, i % 5 == 0), str(i)) for i in range(1, 101)]))

Re: FizzBuzz in ten languages

#73

Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?

A few others that are similar: - fibonacci sequence to n (ooh, look, isn't recursion pretty) - parallel map (simple demonstration of parallelism in languages that allow it) [slightly more involved, webapp hello worlds] - basic blog (various MVC web frameworks, normally along lines of if I run commands x, y and z I get models, views and controllers and it all works) - basic todo application (various frontend framework…

One common one when learning recursion or lispy things is factorial with recursion? But it's also in proof as well. It may just be really simple or something so I'm not sure my example counts

Re: FizzBuzz in ten languages

#74
post #33

Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?

It was popularized by Jeff Atwood in his blog entry "Why Can't Programmers.. Program?" https://blog.codinghorror.com/why-cant-programmers-program/ Atwood referenced Imran Ghory who seems to have originated the test.

This is neat, I didn't know it started there.

Re: FizzBuzz in ten languages

#75
post #63

I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…

Maybe I'm missing something, but how is that any more readable than: for val in xrange(1, 100): if val % 15 == 0: print "FizzBuzz" elif val % 5 == 0: print "Buzz" elif val % 3 == 0: print "Fizz" else: print val

I agree with you. Pattern matching is detrimental to readability in this case. Everybody can understand your example above. The pattern matching one leaves me a little O_o (pun intended) despite I'm using pattern matching everyday in Elixir.

Sometimes boring code beats clever code.

Re: FizzBuzz in ten languages

#77
post #4

Crossposting my comment from the blog: The Rust example looks a bit strange, since you're matching on a variable, but don't actually use it. Instead, you could write the match like this: let div_by_three = x % 3 == 0; let div_by_five = x % 5 == 0; match (div_by_three, div_by_five) { (true, true) => println!("FizzBuzz"), (true, false) => println!("Fizz"), (false, true) => println!("Buzz"), _ => println!("{}", x), } ..…

That looks so much like Elixir. for x IO.puts("FizzBuzz") [0, _] -> IO.puts("Fizz") [_, 0] -> IO.puts("Buzz") _ -> x end end

Any language that has pattern matching looks similar. Much nicer than if/else/switch.

Re: FizzBuzz in ten languages

#78
Fast. Faster. Fastest. The FizzBuzz Gold Standard. Lookup pre-calculated ("hard coded") constants. Don't over-engineer :-):

    def fizzbuzz
       [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14,
  "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26,
  "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38,
  "Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fizz", 49, "Buzz",
  "Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62,
  "Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74,
  "FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86,
  "Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98,
  "Fizz", "Buzz"]
    end
Source: https://yukimotopress.github.io/fizzbuzz#gold-standard

Re: FizzBuzz in ten languages

#80

Fast. Faster. Fastest. The FizzBuzz Gold Standard. Lookup pre-calculated ("hard coded") constants. Don't over-engineer :-): def fizzbuzz [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38, "Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fi…

Plus a program to produce the pre-calculated data, to avoid typos. :-)
Post reply on HN