Diff: FizzBuzz
Differences between current version and previous revision of FizzBuzz.
Other diffs: Previous Major Revision, Previous Author
Newer page: | version 2 | Last edited on March 1, 2012 3:27 pm | by PhilHollenback | |
Older page: | version 1 | Last edited on January 18, 2010 3:55 pm | by PhilHollenback | Revert |
@@ -20,30 +20,7 @@
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?
-----
-<?plugin RawHtml
-<script>
-var idcomments_acct = '011e5665a1128cdbe79c8077f0f04353';
-var idcomments_post_id;
-var idcomments_post_url;
-</script>
-<span id="IDCommentsPostTitle" style="display:none"></span>
-<script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>
-?>
+CategoryGeekStuff
------
-
-<?plugin RawHtml
-<center>
-<script type="text/javascript"><!--
-google_ad_client = "pub-5011581245921339";
-google_ad_width = 728;
-google_ad_height = 90;
-google_ad_format = "728x90_as";
-google_ad_channel ="";
-//--></script>
-<script type="text/javascript"
- src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
-</script>
-</center>
-?>
+CategoryBlog
current version
I read this blog posting on a trivial programming test at two in the morning and I got worried: was I an actual programmer? Thus I decided to implement FizzBuzz in bash 3:
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
A few thoughts:
- Note the use of the new bash 3 builtin iterator
{1..100}
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
[ x -eq 0 ]
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?