create account

Different levels of FizzBuzz (using Ruby) by tipy

View this thread on: hive.blogpeakd.comecency.com
· @tipy ·
$0.30
Different levels of FizzBuzz (using Ruby)
If you are old enough like me, you probably already did (or make pass) a [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) exercise during an interview.
I don't think it is still used anymore but it used to be a classic.

So let's grab a beer and start coding.

## First implementations

The first implementation here is the basic one, it works fine and is pretty much the same on every language.

```ruby
1.upto(100) do |i|
  if i % 3 == 0 && i % 5 == 0
    puts 'FizzBuzz'
  elsif i % 3 == 0
    puts 'Fizz'
  elsif i % 5 == 0
    puts 'Buzz'
  else
    puts i
  end
end
```

But we can get rid of one useless `if` statement.
Some people would say that the ternary operator is not that much readable and
prefer using a boolean but both solutions are totally fine for me.

```ruby
1.upto(100) do |i|
  result = ''
  if i % 3 == 0
    result += 'Fizz'
  end
  if i % 5 == 0
    result += 'Buzz'
  end

  puts result == '' ? i : result
end
```

Finally if the developer is familiar with the language, he should write more "idiomatic" code, Ruby in our case :

```ruby
1.upto(100) do |i|
  result = ''
  result += 'Fizz' if (i % 3).zero?
  result += 'Buzz' if (i % 5).zero?

  puts result.empty? ? i : result
end
```

## Use a datastructure

What if we need to extends this code ?
What if I want to add another prime number, for example 7 is 'Bazz'?
The next level for this exercise should be to use a datastructure, like a dictionary in python or a hash in ruby.

```ruby
FIZZ_BUZZ = {
  fizz: 3,
  buzz: 5
}

1.upto(100) do |i|
  result = ''
  FIZZ_BUZZ.map do |text, divisor|
    result += text.to_s.capitalize if (i % divisor).zero?
  end

  puts result.empty? ? i : result
end
```

## Use lambdas

For our last version, let's use a lambda and move our modulo predicate to the hash

```ruby
FIZZ_BUZZ = {
  fizz: ->(i) { (i % 3).zero? },
  buzz: ->(i) { (i % 5).zero? }
}

1.upto(100) do |i|
  result = ''
  FIZZ_BUZZ.map do |text, predicate|
    result += text.to_s.capitalize if predicate.call(i)
  end

  puts result.empty? ? i : result
end
```

## Bonus point: machine learning :)

A crazy guy implemented a solution using machine learning
[Check it out](https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/), it's really fun.

πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 35 others
properties (23)
authortipy
permlinkdifferent-levels-of-fizzbuzz-using
categoryhive-169321
json_metadata{"links":["https://en.wikipedia.org/wiki/Fizz_buzz","https://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/"],"tags":["hive-169321","programming","ruby","beer"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-07-27 15:11:48
last_update2021-07-27 15:11:48
depth0
children6
last_payout2021-08-03 15:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.152 HBD
curator_payout_value0.151 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,240
author_reputation2,619,324,280,425
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,152,859
net_rshares472,481,472,730
author_curate_reward""
vote details (99)
@biggestloser ·
hey @tipy everytime I see your programming post you seem to use different language, earlier it was Go now it is Ruby.
You really are a pro at programming😎 😎 😎
πŸ‘  
properties (23)
authorbiggestloser
permlinkqx55pw
categoryhive-169321
json_metadata{"users":["tipy"],"app":"hiveblog/0.1"}
created2021-08-01 03:30:00
last_update2021-08-01 03:30:00
depth1
children2
last_payout2021-08-08 03:30:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length158
author_reputation705,599,888,230
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries
0.
accountdemotruk
weight300
1.
accounthiveonboard
weight100
2.
accounttipu
weight100
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,246,979
net_rshares1,144,966,095
author_curate_reward""
vote details (1)
@tipy ·
Thanks for you kind comment.
In fact I'm a pro :) in the sense that programming is what I do for a living.
I switched language a few times during my career but Ruby is still my favorite language.
πŸ‘  
properties (23)
authortipy
permlinkre-biggestloser-202181t74224955z
categoryhive-169321
json_metadata{"tags":["ecency"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-08-01 05:42:24
last_update2021-08-01 05:42:24
depth2
children1
last_payout2021-08-08 05:42:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length195
author_reputation2,619,324,280,425
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,248,919
net_rshares976,223,223
author_curate_reward""
vote details (1)
@biggestloser ·
Good to know, I have just started learning programming.
I am more into python.....
Perhaps you can give me some tips, as you are more experienced than me...
properties (22)
authorbiggestloser
permlinkqx5cew
categoryhive-169321
json_metadata{"app":"hiveblog/0.1"}
created2021-08-01 05:54:48
last_update2021-08-01 05:54:48
depth3
children0
last_payout2021-08-08 05:54:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length156
author_reputation705,599,888,230
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries
0.
accountdemotruk
weight300
1.
accounthiveonboard
weight100
2.
accounttipu
weight100
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,249,070
net_rshares0
@cadawg ·
$0.03
Hi there, thanks for sharing this in programming. I like the twist you have put upon fizz buzz to make this more interesting. I have solved this problem before and it’s interesting to see how others do so
πŸ‘  ,
properties (23)
authorcadawg
permlinkre-tipy-qx3066
categoryhive-169321
json_metadata{"tags":["hive-169321"],"app":"peakd/2021.07.5"}
created2021-07-30 23:34:54
last_update2021-07-30 23:34:54
depth1
children1
last_payout2021-08-06 23:34:54
cashout_time1969-12-31 23:59:59
total_payout_value0.015 HBD
curator_payout_value0.015 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length204
author_reputation100,771,927,095,688
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,224,284
net_rshares49,893,981,226
author_curate_reward""
vote details (2)
@tipy ·
Thanks a lot for your comment, and your upvote, it helps a lot
properties (22)
authortipy
permlinkre-cadawg-2021731t10624713z
categoryhive-169321
json_metadata{"tags":["hive-169321"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-07-31 08:06:24
last_update2021-07-31 08:06:24
depth2
children0
last_payout2021-08-07 08:06:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length62
author_reputation2,619,324,280,425
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,229,734
net_rshares0
@hivebuzz ·
Congratulations @tipy! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@tipy/upvoted.png?202107271640"></td><td>You received more than 10 upvotes.<br>Your next target is to reach 50 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@tipy) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/pud-202108"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-202108">Hive Power Up Day - August 1st 2021 - Hive Power Delegation</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-tipy-20210727t165238000z
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-07-27 16:52:39
last_update2021-07-27 16:52:39
depth1
children0
last_payout2021-08-03 16:52:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length925
author_reputation369,400,771,956,148
root_title"Different levels of FizzBuzz (using Ruby)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,154,540
net_rshares0