View Source:
FizzBuzz
Note:
This page has been locked and cannot be edited.
I read this [blog posting on a trivial programming test|http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/] at two in the morning and I got worried: was I an actual programmer? Thus I decided to implement ~FizzBuzz in bash 3: <verbatim> for i in {1..100} do ([ $(($i % 15)) -eq 0 ] && echo FizzBuzz ) || \ ([ $(($i % 3)) -eq 0 ] && echo Fizz ) || \ ([ $(($i % 5)) -eq 0 ] && echo Buzz ) || \ echo $i done </verbatim> A few thoughts: # Note the use of the new bash 3 builtin iterator <code>{1..100}</code> to generate the list instead of using a counter variable, seq, or jot. # Note also the use of bash builtin arithmetic evaluation instead of bc or expr. # I wrote this in the one line style with conditional operators instead of branching. For some reason I am more comfortable in that style. # Is there a way to make the return value of a math operation be the mathematical result? I couldn't think of one offhand. That leads to the awkward <code>~[ x -eq 0 ~]</code> construct. I suspect that could be replaced with a combination of expr and eval. I'd like to hear if anyone can write this in a more compact and/or elegant fashion in bash 3. However, I'm not really interested in going the obfuscated code route - what's the most compact way you can write this script that is still readable and elegant? ----- CategoryGeekStuff CategoryBlog
Please enable JavaScript to view the
comments powered by Disqus.
HollenbackDotNet
Home Page
Popular Pages
All Categories
Main Categories
General Interest
Geek Stuff
DevOps
Linux Stuff
Pictures
Search
Toolbox
RecentChanges
RecentNewPages
What links here
Printable version
AllPages
RecentChanges
Recent Changes Cached
No changes found
Favorite Categories
ActionPage
(150)
WikiPlugin
(149)
GeekStuff
(137)
PhpWikiAdministration
(102)
Help/PageList
(75)
Help/MagicPhpWikiURLs
(75)
Blog
(69)
Pictures
(60)
GeneralInterest
(44)
LinuxStuff
(38)
Views
View Page
View Source
History
Diff
Sign In