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.
author | tipy |
---|---|
permlink | different-levels-of-fizzbuzz-using |
category | hive-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"} |
created | 2021-07-27 15:11:48 |
last_update | 2021-07-27 15:11:48 |
depth | 0 |
children | 6 |
last_payout | 2021-08-03 15:11:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.152 HBD |
curator_payout_value | 0.151 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,240 |
author_reputation | 2,619,324,280,425 |
root_title | "Different levels of FizzBuzz (using Ruby)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,152,859 |
net_rshares | 472,481,472,730 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
sqube | 0 | 351,135,841 | 1% | ||
dkmathstats | 0 | 75,837,138,175 | 55% | ||
tipy | 0 | 1,148,597,243 | 100% | ||
albuquerque | 0 | 0 | 5% | ||
balloonfiesta | 0 | 0 | 5% | ||
cheeksndong | 0 | 0 | 5% | ||
badteacher | 0 | 0 | 5% | ||
bobcox | 0 | 0 | 5% | ||
crapshoot | 0 | 0 | 5% | ||
bonesandblue | 0 | 0 | 5% | ||
collateralabuse | 0 | 0 | 5% | ||
bugbalm | 0 | 0 | 5% | ||
chickunlittle | 0 | 0 | 5% | ||
belowthebelt | 0 | 0 | 5% | ||
chaisemytail | 0 | 0 | 5% | ||
bakedbaklava | 0 | 0 | 5% | ||
beartrax | 0 | 0 | 5% | ||
alphabetsoup | 0 | 0 | 5% | ||
angermanagement | 0 | 0 | 5% | ||
breakaleg | 0 | 0 | 5% | ||
chickinwinginit | 0 | 0 | 5% | ||
allmostfree | 0 | 0 | 5% | ||
xmarksthespot | 0 | 0 | 5% | ||
cashcowtipping | 0 | 0 | 5% | ||
cryzzappl | 0 | 0 | 5% | ||
bitcornholeo | 0 | 0 | 5% | ||
chompingatdabit | 0 | 0 | 5% | ||
bitcornhole | 0 | 0 | 5% | ||
bailbond | 0 | 0 | 5% | ||
airdropter | 0 | 0 | 5% | ||
bemyneighbor | 0 | 0 | 5% | ||
bigposter | 0 | 0 | 5% | ||
checkplease | 0 | 0 | 5% | ||
cokeandasmile | 0 | 0 | 5% | ||
bangheadonwall | 0 | 0 | 5% | ||
applehobbler | 0 | 0 | 5% | ||
barbieque | 0 | 0 | 5% | ||
clawtya | 0 | 0 | 5% | ||
boldnugget | 0 | 0 | 5% | ||
boldanugget | 0 | 0 | 5% | ||
miraclewhipz | 0 | 0 | 5% | ||
cheapdate | 0 | 0 | 5% | ||
bumpuglies | 0 | 0 | 5% | ||
ballachange | 0 | 0 | 5% | ||
aleashasubwufr | 0 | 0 | 5% | ||
balzosteeleez | 0 | 0 | 5% | ||
bentoversiemez | 0 | 0 | 5% | ||
bearassmemunch | 0 | 0 | 5% | ||
cumunik8 | 0 | 0 | 5% | ||
chezmamatajawk | 0 | 0 | 5% | ||
cadawg | 0 | 392,690,543,063 | 100% | ||
crypnwaffles | 0 | 0 | 5% | ||
barklikedoge | 0 | 0 | 5% | ||
mutiahanum | 0 | 152,177,229 | 100% | ||
cumplayntdesk | 0 | 0 | 5% | ||
thepassword | 0 | 0 | 5% | ||
thepasswordis | 0 | 0 | 5% | ||
confuciussay | 0 | 0 | 5% | ||
theman4aday | 0 | 0 | 5% | ||
buttcheekz | 0 | 0 | 5% | ||
flowersforyou | 0 | 0 | 5% | ||
chainclicks | 0 | 0 | 5% | ||
baitandswitch | 0 | 0 | 5% | ||
dontlookangry | 0 | 0 | 5% | ||
youroncamera | 0 | 0 | 5% | ||
voices5g | 0 | 0 | 5% | ||
damnautocorrect | 0 | 0 | 5% | ||
almightydevil | 0 | 0 | 5% | ||
uneditedlapel | 0 | 0 | 5% | ||
aclu | 0 | 0 | 5% | ||
churchlady | 0 | 0 | 5% | ||
twodudesandacup | 0 | 0 | 5% | ||
amerekis | 0 | 0 | 5% | ||
beahero | 0 | 0 | 5% | ||
fraggedbya5yold | 0 | 0 | 5% | ||
williamgarybusey | 0 | 0 | 5% | ||
subpoena | 0 | 0 | 5% | ||
mysnapshots | 0 | 0 | 5% | ||
mailfraud | 0 | 0 | 5% | ||
virtualbox | 0 | 0 | 5% | ||
lordkevinsapathy | 0 | 0 | 5% | ||
thesatanicbible | 0 | 0 | 5% | ||
not4sail | 0 | 0 | 5% | ||
fourwinds | 0 | 0 | 5% | ||
nicelegs | 0 | 0 | 5% | ||
omnialeather | 0 | 0 | 5% | ||
adamsspareribs | 0 | 0 | 5% | ||
homealone | 0 | 0 | 5% | ||
fuckingbitches | 0 | 0 | 5% | ||
thousandislands | 0 | 0 | 5% | ||
thousandisland | 0 | 0 | 5% | ||
athousandtokens | 0 | 0 | 5% | ||
signup4airdrops | 0 | 0 | 5% | ||
disneyplus | 0 | 0 | 5% | ||
dipshits | 0 | 0 | 5% | ||
bigbowloffuck | 0 | 0 | 5% | ||
akhyar23 | 0 | 94,059,330 | 100% | ||
biggestloser | 0 | 993,041,536 | 100% | ||
garcondiable | 0 | 1,214,780,313 | 100% |
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π π π
author | biggestloser | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
permlink | qx55pw | ||||||||||||||||||
category | hive-169321 | ||||||||||||||||||
json_metadata | {"users":["tipy"],"app":"hiveblog/0.1"} | ||||||||||||||||||
created | 2021-08-01 03:30:00 | ||||||||||||||||||
last_update | 2021-08-01 03:30:00 | ||||||||||||||||||
depth | 1 | ||||||||||||||||||
children | 2 | ||||||||||||||||||
last_payout | 2021-08-08 03:30:00 | ||||||||||||||||||
cashout_time | 1969-12-31 23:59:59 | ||||||||||||||||||
total_payout_value | 0.000 HBD | ||||||||||||||||||
curator_payout_value | 0.000 HBD | ||||||||||||||||||
pending_payout_value | 0.000 HBD | ||||||||||||||||||
promoted | 0.000 HBD | ||||||||||||||||||
body_length | 158 | ||||||||||||||||||
author_reputation | 705,599,888,230 | ||||||||||||||||||
root_title | "Different levels of FizzBuzz (using Ruby)" | ||||||||||||||||||
beneficiaries |
| ||||||||||||||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||||||||||||||
percent_hbd | 10,000 | ||||||||||||||||||
post_id | 105,246,979 | ||||||||||||||||||
net_rshares | 1,144,966,095 | ||||||||||||||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tipy | 0 | 1,144,966,095 | 100% |
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.
author | tipy |
---|---|
permlink | re-biggestloser-202181t74224955z |
category | hive-169321 |
json_metadata | {"tags":["ecency"],"app":"ecency/3.0.18-vision","format":"markdown+html"} |
created | 2021-08-01 05:42:24 |
last_update | 2021-08-01 05:42:24 |
depth | 2 |
children | 1 |
last_payout | 2021-08-08 05:42:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 195 |
author_reputation | 2,619,324,280,425 |
root_title | "Different levels of FizzBuzz (using Ruby)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,248,919 |
net_rshares | 976,223,223 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
biggestloser | 0 | 976,223,223 | 100% |
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...
author | biggestloser | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
permlink | qx5cew | ||||||||||||||||||
category | hive-169321 | ||||||||||||||||||
json_metadata | {"app":"hiveblog/0.1"} | ||||||||||||||||||
created | 2021-08-01 05:54:48 | ||||||||||||||||||
last_update | 2021-08-01 05:54:48 | ||||||||||||||||||
depth | 3 | ||||||||||||||||||
children | 0 | ||||||||||||||||||
last_payout | 2021-08-08 05:54:48 | ||||||||||||||||||
cashout_time | 1969-12-31 23:59:59 | ||||||||||||||||||
total_payout_value | 0.000 HBD | ||||||||||||||||||
curator_payout_value | 0.000 HBD | ||||||||||||||||||
pending_payout_value | 0.000 HBD | ||||||||||||||||||
promoted | 0.000 HBD | ||||||||||||||||||
body_length | 156 | ||||||||||||||||||
author_reputation | 705,599,888,230 | ||||||||||||||||||
root_title | "Different levels of FizzBuzz (using Ruby)" | ||||||||||||||||||
beneficiaries |
| ||||||||||||||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||||||||||||||
percent_hbd | 10,000 | ||||||||||||||||||
post_id | 105,249,070 | ||||||||||||||||||
net_rshares | 0 |
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
author | cadawg |
---|---|
permlink | re-tipy-qx3066 |
category | hive-169321 |
json_metadata | {"tags":["hive-169321"],"app":"peakd/2021.07.5"} |
created | 2021-07-30 23:34:54 |
last_update | 2021-07-30 23:34:54 |
depth | 1 |
children | 1 |
last_payout | 2021-08-06 23:34:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.015 HBD |
curator_payout_value | 0.015 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 204 |
author_reputation | 100,771,927,095,688 |
root_title | "Different levels of FizzBuzz (using Ruby)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,224,284 |
net_rshares | 49,893,981,226 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tipy | 0 | 1,185,566,454 | 100% | ||
dustsweeper | 0 | 48,708,414,772 | 7.87% |
Thanks a lot for your comment, and your upvote, it helps a lot
author | tipy |
---|---|
permlink | re-cadawg-2021731t10624713z |
category | hive-169321 |
json_metadata | {"tags":["hive-169321"],"app":"ecency/3.0.18-vision","format":"markdown+html"} |
created | 2021-07-31 08:06:24 |
last_update | 2021-07-31 08:06:24 |
depth | 2 |
children | 0 |
last_payout | 2021-08-07 08:06:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 62 |
author_reputation | 2,619,324,280,425 |
root_title | "Different levels of FizzBuzz (using Ruby)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,229,734 |
net_rshares | 0 |
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>
author | hivebuzz |
---|---|
permlink | hivebuzz-notify-tipy-20210727t165238000z |
category | hive-169321 |
json_metadata | {"image":["http://hivebuzz.me/notify.t6.png"]} |
created | 2021-07-27 16:52:39 |
last_update | 2021-07-27 16:52:39 |
depth | 1 |
children | 0 |
last_payout | 2021-08-03 16:52:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 925 |
author_reputation | 369,400,771,956,148 |
root_title | "Different levels of FizzBuzz (using Ruby)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,154,540 |
net_rshares | 0 |