<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7186238830705847593</id><updated>2012-03-03T23:07:06.275-07:00</updated><category term='FizzBuzz'/><category term='Text'/><category term='Python'/><category term='Kotlin'/><category term='Number'/><category term='Ceylon'/><category term='Buzz'/><category term='Java'/><category term='Groovy'/><category term='Fizz'/><title type='text'>1 2 Fizz 4 Buzz</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-422200563971208414</id><published>2012-03-06T15:05:00.000-07:00</published><updated>2012-03-03T22:58:53.229-07:00</updated><title type='text'>#16 - Putting the Fun in Function</title><content type='html'>Kotlin has functions.  Whodathunk?  You've been seeing them since the first code samples but perhaps we should put it to real use.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;fun fizzBuzz(i : Int) : String {&lt;br /&gt;    return when (i%15) {&lt;br /&gt;      0 -&amp;gt; "FizzBuzz";&lt;br /&gt;      5,10 -&amp;gt; "Buzz";&lt;br /&gt;      3,6,9,12 -&amp;gt; "Fizz";&lt;br /&gt;      else -&amp;gt; i.toString();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;fun main(args : Array&amp;lt;String&amp;gt;) {&lt;br /&gt;  for (i in 1..100) {&lt;br /&gt;    println (fizzBuzz(i));&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The function deceleration syntax is very reminiscent of Pascal, the third programming language I ever learned (various BASICs and Action! were the first two). These differ from C[#/++] and Java in several ways.&lt;br /&gt;&lt;br /&gt;One difference is that there are only three letters for &lt;code&gt;fun&lt;/code&gt;, rather than all 8 for &lt;code&gt;function&lt;/code&gt;.  Even with IDE code completion, &lt;code&gt;f&lt;/code&gt; &lt;code&gt;[ctrl+space]&lt;/code&gt; &lt;code&gt;[Enter]&lt;/code&gt; is still three keystrokes.  I guess they could put in an extra space to save one, but then I'de be compelled to count &lt;code&gt;[ctrl+space]&lt;/code&gt; as two keystrokes (even though it is one cord).&lt;br /&gt;&lt;br /&gt;The next difference is that the type follows the symbol name.  This is more of a western approach to surnames than the typical braces and semicolons approach which places the type before the symbol, if you consider the type of the symbol to be the family name of the symbol.  In a sense, placing the identity of the symbol above that of it's role.&lt;br /&gt;&lt;br /&gt;One other quirk is that there are no primitive types in Kotlin, or there are no object types of primitives (via the &lt;code&gt;Int&lt;/code&gt; type deceleration on the first line).  Actually there are object primitive types, but they are the nullable primitive types. Except that you use a capitalized name, which implies they are &lt;code&gt;Object&lt;/code&gt; types, which they are unless they happen to have a nullable modifier on the object type, which makes it a different type rather than a modification of the original type.  So to express this in a particle physics metaphor this is an anti-quirk to a Java quirk, which means they annihilate each other. Except they are quirks not quarks, so a strange quark, or quirk, is left over. Which only matters when comparing it to a particular implementation platform. Charming.&lt;br /&gt;&lt;br /&gt;Finally, unlike Pascal there are no procedures.  Everything is a function, even if it returns &lt;code&gt;Nothing&lt;/code&gt;.  The closest analog to a procedure would be a function with no declared return type, which is then implied to mean &lt;code&gt;Tuple0&lt;/code&gt;, a tuple with zero members, which appears as of the writing of this post to be disctinct from &lt;code&gt;Nothing&lt;/code&gt;.  Remember, {ε} ≠ ∅.  Isn't math fun?!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-422200563971208414?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/422200563971208414/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/03/16-putting-fun-in-function.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/422200563971208414'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/422200563971208414'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/03/16-putting-fun-in-function.html' title='#16 - Putting the Fun in Function'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-4171713046678328998</id><published>2012-02-28T15:05:00.000-07:00</published><updated>2012-02-28T16:40:58.457-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Text'/><category scheme='http://www.blogger.com/atom/ns#' term='FizzBuzz'/><title type='text'>FizzBuzz 15 - Technically Correct (But Oh So Wrong)</title><content type='html'>I am going to keep with my theme idea for "FizzBuzz" counts. &amp;nbsp;These solutions are the ones that make your eyes bleed or make you brace for impact. &amp;nbsp;Technically this solution is correct, but it is oh so wrong. &amp;nbsp;This solution is in Text. &amp;nbsp;Not Tex, Text.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:html"&gt;1&lt;br /&gt;2&lt;br /&gt;Fizz&lt;br /&gt;4&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;7&lt;br /&gt;8&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;11&lt;br /&gt;Fizz&lt;br /&gt;13&lt;br /&gt;14&lt;br /&gt;FizzBuzz&lt;br /&gt;16&lt;br /&gt;17&lt;br /&gt;Fizz&lt;br /&gt;19&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;22&lt;br /&gt;23&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;26&lt;br /&gt;Fizz&lt;br /&gt;28&lt;br /&gt;29&lt;br /&gt;FizzBuzz&lt;br /&gt;31&lt;br /&gt;32&lt;br /&gt;Fizz&lt;br /&gt;34&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;37&lt;br /&gt;38&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;41&lt;br /&gt;Fizz&lt;br /&gt;43&lt;br /&gt;44&lt;br /&gt;FizzBuzz&lt;br /&gt;46&lt;br /&gt;47&lt;br /&gt;Fizz&lt;br /&gt;49&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;52&lt;br /&gt;53&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;56&lt;br /&gt;Fizz&lt;br /&gt;58&lt;br /&gt;59&lt;br /&gt;FizzBuzz&lt;br /&gt;61&lt;br /&gt;62&lt;br /&gt;Fizz&lt;br /&gt;64&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;67&lt;br /&gt;68&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;71&lt;br /&gt;Fizz&lt;br /&gt;73&lt;br /&gt;74&lt;br /&gt;FizzBuzz&lt;br /&gt;76&lt;br /&gt;77&lt;br /&gt;Fizz&lt;br /&gt;79&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;82&lt;br /&gt;83&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;br /&gt;86&lt;br /&gt;Fizz&lt;br /&gt;88&lt;br /&gt;89&lt;br /&gt;FizzBuzz&lt;br /&gt;91&lt;br /&gt;92&lt;br /&gt;Fizz&lt;br /&gt;94&lt;br /&gt;Buzz&lt;br /&gt;Fizz&lt;br /&gt;97&lt;br /&gt;98&lt;br /&gt;Fizz&lt;br /&gt;Buzz&lt;/pre&gt;&lt;br /&gt;If you ever interview with me and attempt this answer, be forewarned: I will make you present a complete solution. &amp;nbsp;Even if it takes the whole time I have allotted. &amp;nbsp;The only recovery would be to change your answer to a &lt;a href="http://groovyconsole.appspot.com/script/643001" target="_blank"&gt;one liner&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;However, if you use a specific "interpreter" this is, indeed, source code that will solve the problem. On Linux and Mac one popular solution is &lt;code&gt;cat&lt;/code&gt;.  More advanced candidates would use &lt;code&gt;mode&lt;/code&gt; or &lt;code&gt;less&lt;/code&gt;.  Those candidates I would send to the IT department to vet (since this is evidence of command line aptitude) but I keep them off of the programming team.  The interpreter on Windows is &lt;code&gt;type&lt;/code&gt;, and I am not sure where I would send candidates who knew that answer.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-4171713046678328998?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/4171713046678328998/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/fizzbuzz-15-technically-correct-but-oh.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/4171713046678328998'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/4171713046678328998'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/fizzbuzz-15-technically-correct-but-oh.html' title='FizzBuzz 15 - Technically Correct (But Oh So Wrong)'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-7170871658956637107</id><published>2012-02-24T15:05:00.000-07:00</published><updated>2012-03-03T11:41:24.088-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Kotlin'/><title type='text'>#14 - Express Yourself</title><content type='html'>When creating a new language the designers really need to look at the cool features from current languages and steal the best parts.  But you need to be cautious about not just picking up fashions.  Remember: fashion is what you look at 20 years later and say "That looks so ugly."&lt;br /&gt;&lt;br /&gt;But the reality is that only time will tell what is simply fashion and what is not.One of the cooler features of Ruby is that everything is an expression.  Control structures included.  That may be why ruby lacks the three expression for loop.  That and a mild aversion to semi-colons.  I'de post this code in Ruby, except that the new kid on the block is Kotlin, and everyone loves to play with the new kid.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;fun main(args : Array&amp;lt;String&amp;gt;) {&lt;br /&gt;  for (i:Int in 1..100) {&lt;br /&gt;    println (&lt;br /&gt;      when (i%15) {&lt;br /&gt;        0 -&amp;gt; "FizzBuzz" &lt;br /&gt;        5, 10 -&amp;gt; "Buzz"&lt;br /&gt;        3, 6, 9, 12 -&amp;gt; "Fizz"&lt;br /&gt;        else -&amp;gt; i&lt;br /&gt;      })&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is only a mild variation from the previous solution. Instead of printing out the results inside the when blocks the whole result of the when block is printed out.  Structures like this figure into some of the design decisions like making the case statements only one expression.  Evert expression on Kotlin returns a value.  Even if it returns &lt;code&gt;Nothing&lt;/code&gt; (and there is a language construct for Nothing).&lt;br /&gt;&lt;br /&gt;Hopefully the "everything is an expression" thing isn't just&amp;nbsp;fashion. &amp;nbsp;Ideally when you come back in 20 years (or 20 weeks) and see yourself using control structures as expressions you won't ask yourself &amp;nbsp;"What was I thinking?"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-7170871658956637107?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/7170871658956637107/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/14-express-yourself.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/7170871658956637107'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/7170871658956637107'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/14-express-yourself.html' title='#14 - Express Yourself'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-7957961373565420375</id><published>2012-02-21T15:05:00.000-07:00</published><updated>2012-02-21T15:17:11.693-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Kotlin'/><title type='text'>#13 - When in St. Petersberg</title><content type='html'>That was a short break.  Basically I wasn't liking the structure I was forcing myself into.  Once I get a large corpus of solutions I can look into formatting, but in the mean time I will focus on stuff that entertains me, free of schedule constraints or themes.&lt;br /&gt;&lt;br /&gt;But last Tuesday JetBrains publicly released the beta of &lt;a href="http://confluence.jetbrains.net/display/Kotlin/Welcome" target="_blank"&gt;Kotlin&lt;/a&gt;, their entry into the alternative not-just-JVM languages.  There are several very interesting features of this language.  And best of all it runs in an IDE I use on a daily basis: &lt;a href="http://www.jetbrains.com/idea/" target="_blank"&gt;IntelliJ Idea&lt;/a&gt;.  Since this will be the first of several posts, I will first present the solution that most of them will build on.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;fun main(args : Array&amp;lt;String&amp;gt;) {&lt;br /&gt;    for (i in 1..100) {&lt;br /&gt;        when (i%15) {&lt;br /&gt;            0 -&amp;gt; println("FizzBuzz");&lt;br /&gt;            5,10 -&amp;gt; println("Buzz");&lt;br /&gt;            3,6,9,12 -&amp;gt; println("Fizz");&lt;br /&gt;            else -&amp;gt; println(i);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Instead of a switch statement Kotlin has a when statement (lines 3-8).  This is for the most part syntactic sugar, but real sugar not the high fructose code syrup sold to public school programming students.  At first blush it looks like changing the word &lt;code&gt;when&lt;/code&gt; for &lt;code&gt;switch&lt;/code&gt; and some other changes in the case blocks.  And in this example, it is all the same.  But it is denser and hence reads better.&lt;br /&gt;&lt;br /&gt;First, you will not that there is no implicit fall-through.  Each case statement matches up to exactly one expression (and that expression can be a multi-expression block).  To fall through the rest of the switch you must explicitly use the &lt;code&gt;continue&lt;/code&gt; statement.  And even then it will simply continue to evaluate itself against the conditions rather than jump to the next block.  So the old-school switch drop throughs which were the source of some mysterious bugs are not longer explicitly allowed.&lt;br /&gt;&lt;br /&gt;Second, because of the single use structure of the blocks, multiple cases are combined into one comma-separated expression.  This is possible because there is no possibility of fall-through between each label.&lt;br /&gt;&lt;br /&gt;Finally, the default case (denoted by the &lt;code&gt;else&lt;/code&gt; case) is required except in instances when "the compiler can prove that all possible cases are covered with branch conditions".&lt;br /&gt;&lt;br /&gt;For this solution, I took advantage of the 15 number pattern in FizzBuzz and simply switched on the mod of 15.  There is one case for a FizzBuzz, two for a Buzz, and four for a Fizz.  Everything else falls into the else block.  This actually reads fairly close to the text description of the problem statement.&lt;br /&gt;&lt;br /&gt;The IntelliJ team writing Kotlin is based mostly in Russia. The name Kotlin is from the name of an island near St. Petersberg, which is not really that close to Rome.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-7957961373565420375?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/7957961373565420375/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/13-when-in-st-petersberg.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/7957961373565420375'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/7957961373565420375'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/13-when-in-st-petersberg.html' title='#13 - When in St. Petersberg'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-385896861350678656</id><published>2012-02-13T22:00:00.000-07:00</published><updated>2012-02-13T22:01:13.438-07:00</updated><title type='text'>This isn't Dr Scholl's, going on hiatus.</title><content type='html'>This blog project isn't quite "gelling" like I expected it to mentally, so I am going to take a hiatus of indeterminate length.  Too many different ideas that don't come down to a focal point just yet.  I think the real problem is that I really need to plan all 100 entries into themed blocks, kind of like what I doing with repetition. &lt;br /&gt;&lt;br /&gt;The whole Fizz for languages, Buzz for longer elaborations, and FizzBuzz for esoteric languages was a novel setup, the content of the number entries was&amp;nbsp;letting&amp;nbsp;me down. &amp;nbsp;So if and when I come back to this, I will at least go at it with an entire roadmap (if not text and worked solutions) rather than a rough idea. &amp;nbsp;However I see this more as a fast paced&amp;nbsp;conference&amp;nbsp;presentation about "Everething you need to know about programming I can show you in FizzBuzz" with 100 variations in 50 minutes, across multiple languages. &amp;nbsp;But that may be because I prefer speaking to writing.&lt;br /&gt;&lt;br /&gt;I do have some code in the hopper that abuses Java language features. &amp;nbsp;I will post some of those in April as it keeps with the American theme of April Fools Day.&lt;br /&gt;&lt;br /&gt;And the title is a reference to a really annoying &lt;a href="http://www.drscholls.com/drscholls/massaginggel.jsp" target="_blank"&gt;shoe inserts ad&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-385896861350678656?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/385896861350678656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/this-isnt-dr-scholls-going-on-hiatus.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/385896861350678656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/385896861350678656'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/this-isnt-dr-scholls-going-on-hiatus.html' title='This isn&apos;t Dr Scholl&apos;s, going on hiatus.'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-1189532664772225732</id><published>2012-02-09T15:05:00.000-07:00</published><updated>2012-02-09T16:58:38.884-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><category scheme='http://www.blogger.com/atom/ns#' term='Fizz'/><title type='text'>Fizz 12 - A Snake, Hiding in the Whitespace (Python)</title><content type='html'>For this Fizz, I have decided to tackle one of the other preferred languages at Google: Python.  Since there is enough to talk about with Python I decided to do a relatively straight port of the first FizzBuzz I posted.&lt;br /&gt;&lt;br /&gt;&lt;pre &gt;for i in range(1, 101) :&lt;br /&gt;    if i%15 == 0 :&lt;br /&gt;    &amp;#09;print "FizzBuzz"&lt;br /&gt;    elif i%5 == 0 :&lt;br /&gt;    &amp;#09;print "Buzz"&lt;br /&gt;    elif i%3 == 0 :&lt;br /&gt;    &amp;#09;print "Fizz"&lt;br /&gt;    else :&lt;br /&gt;    &amp;#09;print i&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Like most "scripting" languages there is not a whole lot of overhead and ceremony in this code sample. &amp;nbsp;Lots of items are implied via their absence. &amp;nbsp;The namespace has a nice default, the main procedure to call is simply the statements at the root of the parse tree. &amp;nbsp;And there is a large set of functions available in the default namespace.&lt;br /&gt;&lt;br /&gt;Another unique feature of Python that is white space is significant. &amp;nbsp;Very significant. &amp;nbsp;There are no opening and closing braces to group the control structures. &amp;nbsp;You must indent deeper than that parent control structure and maintain that indentation for multiple&amp;nbsp;statements&amp;nbsp;as a part of that structure. &amp;nbsp;In essence, the "good practice" of indenting nested control structures is hard wired as a requirement into the grammar of the language.&lt;br /&gt;&lt;br /&gt;But the most relevant part of Python in relation to the current subject of discussion is how it loops. &amp;nbsp;The calculations and decisions in this example are merely syntactical variations of what we have seen before. &amp;nbsp;Python lacks a three expression loop seen in braces-and-semi-colon languages. &amp;nbsp;Instead it relies on an "iterator" based loop where the decisions about what the next item is and deciding when to stop is encapsulated inside an object called an Iterator. &amp;nbsp;In this case, the &lt;code&gt;range&lt;/code&gt; function returns an iterator that returns the numbers from one to one hundred in order. &amp;nbsp;Ignore the fact that the second argument in the function is actually greater than one hundred. &amp;nbsp;Software is rife with off by one errors.&lt;br /&gt;&lt;br /&gt;On last point. &amp;nbsp;The&amp;nbsp;indentations&amp;nbsp;must be&amp;nbsp;consistent within the control structure. &amp;nbsp;In other words if you start indenting four more spaces than you had before then each statement in the control structure must have the same amount of indentation. &amp;nbsp;Whether&amp;nbsp;they be tabs or any number of spaces. &amp;nbsp;And you can even mix tabs and spaces, if you are&amp;nbsp;consistent. &amp;nbsp;This example mixes tabs and spaces for the print statements. &amp;nbsp;So, applying the FizzBuzz hypothesis we can conclude that typical programmers can do indentation&amp;nbsp;consistently. &amp;nbsp;The real question is if they will (and they won't because everyone has seen enough evidence to the contrary).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-1189532664772225732?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/1189532664772225732/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/fizz-12-snake-hiding-in-whitespace.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/1189532664772225732'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/1189532664772225732'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/fizz-12-snake-hiding-in-whitespace.html' title='Fizz 12 - A Snake, Hiding in the Whitespace (Python)'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-3189434033297142556</id><published>2012-02-07T15:05:00.000-07:00</published><updated>2012-02-07T15:05:00.101-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#11 - Define Recursion: See Recursion</title><content type='html'>There is one last principal form of repetition remaining to be discussed. This form of repetition is the bane of many first year Computer Science majors and dreaded only more by the professors and TAs who must answer these questions that are best paraphrased as "I just don't get it." That principle? Recursion. I have found no more precise and snarky answer than found in &lt;a href="http://stackoverflow.com/a/23882/8020"&gt;this stackoverflow answer&lt;/a&gt;:&lt;br /&gt;&lt;blockquote class="tr_bq"&gt;If you understand recursion, you're done. Otherwise go read &lt;a href="http://stackoverflow.com/a/23882/8020"&gt;this StackOverflow question&lt;/a&gt;...&lt;/blockquote&gt;The link, of course, is back to the very same question.&lt;br /&gt;&lt;pre class="brush: java"&gt;public class FizzBuzz11 {&lt;br /&gt;&lt;br /&gt;  public static void main(String[] arg) {&lt;br /&gt;    recurse(100);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static void recurse(int num) {&lt;br /&gt;    if (num &amp;gt; 0) {&lt;br /&gt;      recurse(num - 1);&lt;br /&gt;      if (num % 15 == 0) {&lt;br /&gt;        System.out.println("FizzBuzz");&lt;br /&gt;      } else if (num % 5 == 0) {&lt;br /&gt;        System.out.println("Buzz");&lt;br /&gt;      } else if (num % 3 == 0) {&lt;br /&gt;        System.out.println("Fizz");&lt;br /&gt;      } else {&lt;br /&gt;        System.out.println(num);&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;There are no control structures traditionally associated with repetition in this sample. &amp;nbsp;No &lt;code&gt;for&lt;/code&gt; loops of any stripe, no &lt;code&gt;while&lt;/code&gt; or &lt;code&gt;do&lt;/code&gt;/&lt;code&gt;while&lt;/code&gt;. &amp;nbsp;Not even a &lt;code&gt;GOTO&lt;/code&gt; statement (that would be harmful).  What does do the repetition is the call to the &lt;code&gt;recurse&lt;/code&gt; method at line 9.  That line, of course, is in the very method that it is calling.  A snake eating it's own tail.&lt;br /&gt;&lt;br /&gt;Of course, a snake will never be able to fully consume itself. &amp;nbsp;This is assuming it could get over the pain of biting it's own tail, which is about as bad as the anguish felt by many students&amp;nbsp;experiencing their&amp;nbsp;first semester of programming. &amp;nbsp; At some point things will either hurt too much or the snake's stomach will get too full. &amp;nbsp;This is called the "base case" where a simple answer is known and we can unwind the stack.&lt;br /&gt;&lt;br /&gt;There are a lot of really neat&amp;nbsp;intricacies&amp;nbsp;that can be used with recursion, but this task is much to simple to demonstrate them in this form. &amp;nbsp;The form used for this recursion is to count backwards (line 10) from the last answer needed (line 4), stop when we hit zero (line 9), and when unwinding the stack calculate the answer for the number at hand (lines 11 to 18). &amp;nbsp;There is no work done both before and after the recursion, only after. &amp;nbsp;There is no multiple&amp;nbsp;winding&amp;nbsp;and unwinding of the stack, just one dive in and out. &amp;nbsp;And there is no use of the returned values from the recursion, in fact there is no return value at all.&lt;br /&gt;&lt;br /&gt;All of this aligns with my experience with recursion seen 'in the field.' &amp;nbsp;Simple recursion can be handled by anyone deserving to be paid market rates. &amp;nbsp;But you throw in the more interesting cases (as a computer&amp;nbsp;scientist&amp;nbsp;would consider interesting) you are better off shipping it in a binary compiled format. &amp;nbsp;Because if the typical developer is given a chance to mess with the source code, they will break something. &amp;nbsp;Until the point they suddenly&amp;nbsp;understand&amp;nbsp;it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-3189434033297142556?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/3189434033297142556/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/11-define-recursion-see-recursion.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/3189434033297142556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/3189434033297142556'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/11-define-recursion-see-recursion.html' title='#11 - Define Recursion: See Recursion'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-4426952948148774560</id><published>2012-02-02T15:05:00.000-07:00</published><updated>2012-02-02T15:05:00.881-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Groovy'/><category scheme='http://www.blogger.com/atom/ns#' term='Buzz'/><title type='text'>Buzz 10 - How Many Times?</title><content type='html'>I'm going to be changing up my plan for Buzz entries. &amp;nbsp;I had intended on writing 500-1500 essays tangentially related to the FizzBuzz at hand (much like my &lt;a href="http://www.amazon.com/Fizz-Three-Essays-Programming-ebook/dp/B0067UJNS2" target="_blank"&gt;tiny e-book on Amazon&lt;/a&gt;) but I see several problems with it. First, it can ruin the natural progression of solutions if I hold out one solution for a particular post I have planned. &amp;nbsp;Second, it will ruin the the general feel of this blog, going from technical to random life experiences and then back. &amp;nbsp;Third, writing that much text on a deadline is kinda hard with my current life balance. &amp;nbsp;Really, should 1st graders have homework? &amp;nbsp;Even if it is reading with a parent 20 minutes a night?&lt;br /&gt;&lt;br /&gt;So here is the new Buzz plan... for each Buzz I will revisit a language introduced in a previous Fizz entry, and re-work a number entry not in that language using some of the unique features of the language. &amp;nbsp;For this first entry the selection is limited, but actually relevant to&amp;nbsp;the&amp;nbsp;current numeric theme, especially since Groovy has so many ways to do repitition.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: groovy"&gt;100 times {&lt;br /&gt;  int i = it+1&lt;br /&gt;  if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;    println "FizzBuzz"&lt;br /&gt;  } else if (i % 3 == 0) {&lt;br /&gt;    println "Fizz"&lt;br /&gt;  } else if (i % 5 == 0) {&lt;br /&gt;    println "Buzz"&lt;br /&gt;  } else {&lt;br /&gt;    println i&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Lines one and two are the focus for this post, because the rest of it is a simple transliteration to standard Groovy.&lt;br/&gt;&lt;br/&gt;On first reading, it does what it says.  Repeat the enclosed block 100 times.  But in the syntax sugar and details of the API are some common assumptions.  First, what the current count is matters, but there is no explicit variable for that.  Each closure without an argument block gets a single variable &lt;code&gt;it&lt;/code&gt; that you don't need to declare and just exists.  Kind of like &lt;code&gt;this&lt;/code&gt; in methods. (And when is some programming language going to introduce &lt;code&gt;that&lt;/code&gt;?)&lt;br/&gt;&lt;br/&gt;The next assumption is what number to count from.  You would think that counting from 1 would be sensible.  But Groovy uses zero based arrays, like nearly all popular languages, so instead it starts all couting from zero.  Line 2 is an optimization so we don't have to extend our math into the rest of the method.&lt;br/&gt;&lt;br/&gt;The rest is pure syntactic sugar.  When a symbol that could be a method trails an object with no punctuation it is presumed to be a method call in Groovy.  And when a single closure follows a method call, whether or not it is in the argument list, it is added to the argument list of the method.  The integer 100 is boxed up to a &lt;code&gt;java.lang.Integer&lt;/code&gt; class.  And via the Meta-Object-Protocol we can graft in a random &lt;code&gt;times&lt;/code&gt; method onto the &lt;code&gt;Ingeger&lt;/code&gt; class even though it doesn't exist in the plain JDK.  &lt;br /&gt;&lt;br /&gt;The end result is something that looks a little bit more like a spoken sentence, but not too close.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-4426952948148774560?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/4426952948148774560/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/buzz-10-how-many-times.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/4426952948148774560'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/4426952948148774560'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/02/buzz-10-how-many-times.html' title='Buzz 10 - How Many Times?'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-2210511210570455390</id><published>2012-01-31T15:05:00.001-07:00</published><updated>2012-01-31T16:30:18.082-07:00</updated><title type='text'>Fizz 9 - Fire Ready Aim (Dart)</title><content type='html'>For this Fizz I choose one of the most recent languages: Google's &lt;a href="http://www.dartlang.org/" target="_blank"&gt;Dart&lt;/a&gt;. &amp;nbsp;While trolling Google Plus I found &lt;a href="https://plus.google.com/111984871214655251881/posts/SNKPubCZNK5" target="_blank"&gt;this solution&lt;/a&gt; by &lt;a href="https://plus.google.com/111984871214655251881" target="_blank"&gt;Yosuke Sato&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: js"&gt;// fizz buzz&lt;br /&gt;main() {&lt;br /&gt;  for(int i=0;i++&amp;lt;100;)&lt;br /&gt;    print((i%3==0?"Fizz":"")+(i%5==0?"Buzz":"")||i);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is a fairly dense rendering of FizzBuzz and gets right to the point without much ceremony: one loop, one single print statement, and a minimum of braces and parenthesis based on operator order of precedence. &lt;br /&gt;&lt;br /&gt;Things get interesting when we encounter the short-circuit or operator. &amp;nbsp;On the left we have a string type, and on the right we have an integer type. &amp;nbsp;Java would reject this out of hand because short-circuit logical operations can be applied only to boolean primitive types. &amp;nbsp;Dart, however, has more in common with JavaScript, and uses an&amp;nbsp;entirely&amp;nbsp;different approach. &amp;nbsp;First the left hand side is evaluated, and what matters is if it is false-equivilant value: &amp;nbsp;false, null, zero, or the empty string. &amp;nbsp;The &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator will return the left side if it is negative, and the &lt;code&gt;||&lt;/code&gt; operator will return the right hand side if the left is negative. &amp;nbsp;With boolean values you get simple solutions. &amp;nbsp;With string and integer types you get more interesting uses. &amp;nbsp;Such as using&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;||&lt;/span&gt;&amp;nbsp;to resolve defaults and the &lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&amp;nbsp;operator to null-check complex objects.&lt;br /&gt;&lt;br /&gt;As an aside, the &lt;a href="http://dartlang.org/"&gt;dartlang.org&lt;/a&gt; sample compiler will refuse to run this FizzBuzz in checked mode. &amp;nbsp;In unchecked mode it will complain about the types. &amp;nbsp;Perhaps it does have more in common with Java than I realized at first.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-2210511210570455390?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/2210511210570455390/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-9-fire-ready-aim-dart.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/2210511210570455390'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/2210511210570455390'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-9-fire-ready-aim-dart.html' title='Fizz 9 - Fire Ready Aim (Dart)'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-3386721819513370856</id><published>2012-01-26T15:05:00.000-07:00</published><updated>2012-01-27T14:08:24.621-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#8 - Do while I'm doing</title><content type='html'>Now that we have examined the while loop in C style languages, it's time to consider the while loops delinquent little brother: the do-while loop. &amp;nbsp;This is a fairly unique control structure that requires a keyword both at the beginning and the end of it's structure. &amp;nbsp;Kind of like the try-catch clause, except that you only try once but you do the loop at least once, and then consider&amp;nbsp;whether&amp;nbsp;you continue or not. &amp;nbsp;This does have one slight advantage in readability: &amp;nbsp;if you decide not to continue you are right at the code block you will evaluate next.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class FizzBuzz08 { &lt;br /&gt;  public static void main(String[] args) { &lt;br /&gt;    int i = 1; &lt;br /&gt;    do {&lt;br /&gt;      if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) { &lt;br /&gt;        System.out.println("FizzBuzz"); &lt;br /&gt;      } else if (i % 5 == 0) { &lt;br /&gt;        System.out.println("Buzz"); &lt;br /&gt;      } else if (i % 3 == 0) { &lt;br /&gt;        System.out.println("Fizz"); &lt;br /&gt;      } else { &lt;br /&gt;        System.out.println(i); &lt;br /&gt;      } &lt;br /&gt;    } while (i++ &amp;lt; 100); &lt;br /&gt;  } &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This code isn't dramatically different from the previous entry where the while loop was considered. &amp;nbsp;The body is still on lines 5 through 13, and the first expression is on line 3.&lt;br /&gt;&lt;br /&gt;The differences come in the second and third expression. &amp;nbsp;I combined both of them on line 14 for stylistic reasons. &amp;nbsp;First, the order of the expressions would not have been maintained since the&amp;nbsp;increment&amp;nbsp;would occur before the test. &amp;nbsp;I then moved the post&amp;nbsp;increment&amp;nbsp;into the while test expression so we can use the less than operator instead of the less than and equal. &amp;nbsp;One less keystroke. &amp;nbsp;Although since I am programming in Java I don't know why I worry about minimizing keystrokes.&lt;br /&gt;&lt;br /&gt;This loop is not a direct decomposition of a for loop. &amp;nbsp;The loop body will always be evaluated once regardless of the truth of the expression. &amp;nbsp;So this is not a good case of the always evaluate logic. &amp;nbsp;So if a do-while smells like a simple while, typical developers can code it correctly. &amp;nbsp;However whether they can use the always evaluate aspect correctly is up for debate.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-3386721819513370856?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/3386721819513370856/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/8-do-while-i-doing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/3386721819513370856'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/3386721819513370856'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/8-do-while-i-doing.html' title='#8 - Do while I&amp;#39;m doing'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-221673440591777923</id><published>2012-01-24T15:05:00.000-07:00</published><updated>2012-01-24T15:05:00.484-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#7 - Decomposed Loop</title><content type='html'>It's hard to take a look at a programming solution to FizzBuzz from an absolute ground state. &amp;nbsp;There are at least three concepts that need to be working at a basic level for a solution to be sensible: repetition, decisions, and calculation. &amp;nbsp;To&amp;nbsp;analyze&amp;nbsp;any of those three aspects you need two of them standing fairly unchanged while one of them is decomposed.&lt;br /&gt;&lt;br /&gt;First I shall decompose the nature of the solution by changing the basic ways a loop can be constructed. &amp;nbsp; &amp;nbsp;Given the&amp;nbsp;prevalence&amp;nbsp;of C and Java as arguably the most popular language in programming language for over a quarter century the &lt;a href="http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops" target="_blank"&gt;three expression for loop&lt;/a&gt; is something that any professional or accomplished&amp;nbsp;amateur&amp;nbsp;should understand instinctively. &amp;nbsp;But what does that three expression loop look like when decomposed in a&amp;nbsp;semantically&amp;nbsp;similar fashion??&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class FizzBuzz07 {&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;    int i = 1;&lt;br /&gt;    while (i &amp;lt;= 100) {&lt;br /&gt;      if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;        System.out.println("FizzBuzz");&lt;br /&gt;      } else if (i % 5 == 0) {&lt;br /&gt;        System.out.println("Buzz");&lt;br /&gt;      } else if (i % 3 == 0) {&lt;br /&gt;        System.out.println("Fizz");&lt;br /&gt;      } else {&lt;br /&gt;        System.out.println(i);&lt;br /&gt;      }&lt;br /&gt;      i++;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The three expression for loop actually has four parts. &amp;nbsp;The part that often gets ignored is the loop body,&amp;nbsp;expressed&amp;nbsp;by lines 5 through 13 and is not counted in the three expressions. &amp;nbsp;Since C and Java have zero based arrays, we could call this the zeroth expression.&lt;br /&gt;&lt;br /&gt;The first expression sets initial values and variables and is seen in line 3. &amp;nbsp;Strictly speaking this is not a proper decomposition since the scope of the variable in this decomposition could extend beyond the loop body, if there were expressions other than the loop. &amp;nbsp;The second expression is a test, and is found inside the while expression on line 4. &amp;nbsp;The last expression is found on line 14, executed after the content of the body.&lt;br /&gt;&lt;br /&gt;As an aside, the last entry on Ceylon didn't feature a three expression loop because Ceylon doesn't support the syntax. &amp;nbsp;That was the position Groovy once took but later relented when it added the "classic for loop" in it's 1.5 release. &amp;nbsp;However, Ceylon and Groovy both have a while loop construct. &amp;nbsp;So by applying the FizzBuzz Hypothesis we can see that programmers don't &lt;i&gt;need&lt;/i&gt;&amp;nbsp;a nice neat for loop expression. &amp;nbsp;But it sure looks prettier with one.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-221673440591777923?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/221673440591777923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/7-decomposed-loop.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/221673440591777923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/221673440591777923'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/7-decomposed-loop.html' title='#7 - Decomposed Loop'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-5678474784410773866</id><published>2012-01-19T15:05:00.000-07:00</published><updated>2012-01-31T16:25:34.770-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ceylon'/><category scheme='http://www.blogger.com/atom/ns#' term='Fizz'/><title type='text'>Fizz 6 - This or That (Ceylon)</title><content type='html'>Ceylon is a new JVM language being created by Red Hat and Gavin King. &amp;nbsp;You may recognize that last name.  Gavin also created Hibernate, a data library that underlies many e-commerce sites and is integrated (at the default option) into Grails. &amp;nbsp;Ceylon puts a lot of emphasis into how it handles types, so it seemed like a natural language feature to abuse.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;interface Fizz {}&lt;br /&gt;interface Buzz {}&lt;br /&gt;&lt;br /&gt;class CFizz() satisfies Fizz {&lt;br /&gt;  shared actual String string = "Fizz";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class CBuzz() satisfies Buzz {&lt;br /&gt;  shared actual String string = "Buzz";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class CFizzBuzz() satisfies Fizz &amp;amp; Buzz {&lt;br /&gt;  shared actual String string = "FizzBuzz";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Fizz|Buzz|Natural fizzIt(Natural i) {&lt;br /&gt;  if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;    return CFizzBuzz();&lt;br /&gt;  } else if (i % 5 == 0) {&lt;br /&gt;    return CBuzz();&lt;br /&gt;  } else if (i % 3 == 0) {&lt;br /&gt;    return CFizz();&lt;br /&gt;  } else {&lt;br /&gt;    return i;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;shared void FizzBuzz06() {&lt;br /&gt;  for (i in 1..100) {&lt;br /&gt;    print(fizzIt(i).string);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The particular typing feature I am using is called "Union Types." &amp;nbsp;When you declare a type as &lt;code&gt;TypeA|TypeB&lt;/code&gt; (note the pipe between the two types) you are telling the Ceylon compiler that you will have either &lt;code&gt;TypeA&lt;/code&gt; or &lt;code&gt;TypeB&lt;/code&gt;, it doesn't matter. &amp;nbsp;And you won't have &lt;code&gt;TypeC&lt;/code&gt; (for this example &lt;code&gt;TypeC&lt;/code&gt; is not a &lt;code&gt;TypeA&lt;/code&gt; or &lt;code&gt;TypeB&lt;/code&gt;). &amp;nbsp;Without this construct I would be forced to declare the return type of &lt;code&gt;fizzIt&lt;/code&gt; to be an &lt;code&gt;Object&lt;/code&gt; and hope no stray classes like &lt;code&gt;Klang&lt;/code&gt; shows up.&lt;br /&gt;&lt;br /&gt;There also exists Intersection Types (&lt;code&gt;TypeA&amp;amp;TypeB&lt;/code&gt;) which means that the type is both of &lt;code&gt;TypeA&lt;/code&gt; and &lt;code&gt;TypeB&lt;/code&gt; without having to create some other interface that joins the two.  Instead of createing a new interface &lt;code&gt;RunnableFuture&amp;lt;V&amp;gt;&lt;/code&gt; you would just have &lt;code&gt;Runnable&amp;amp;Future&amp;lt;V&amp;gt;&lt;/code&gt; without the need for a third class.  Throw in stuff like &lt;code&gt;RunnableScheduledFuture&amp;lt;V&amp;gt;&lt;/code&gt; which could have been declared as &lt;code&gt;Runnable&amp;amp;Delayed&amp;amp;Future&amp;lt;V&amp;gt;&lt;/code&gt; and the code savings start piling up.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-5678474784410773866?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/5678474784410773866/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-6-this-or-that-ceylon.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/5678474784410773866'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/5678474784410773866'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-6-this-or-that-ceylon.html' title='Fizz 6 - This or That (Ceylon)'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-1678710352462810514</id><published>2012-01-17T15:05:00.000-07:00</published><updated>2012-01-22T21:35:46.923-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Buzz'/><title type='text'>Buzz 5 - The FizzBuzz Hypothesis</title><content type='html'>For the Buzz entries I intend to write something more involved.  These entries will have a FizzBuzz solution, but the text will only be about FizzBuzz.  And if you &lt;a href="http://en.wiktionary.org/wiki/about" target="_blank"&gt;check&lt;/a&gt; &lt;a href="http://dictionary.reference.com/browse/about" target="_blank"&gt;various&lt;/a&gt; &lt;a href="http://www.merriam-webster.com/dictionary/about" target="_blank"&gt;dictionaries&lt;/a&gt; the act of talking 'about' FizzBuzz gives me a lot of latitude to talk about practically anything tangentially related. &lt;br /&gt;&lt;br /&gt;Today, however, I intend to stay fairly close to FizzBuzz itself.  What I want to talk about is something I will call "The FizzBuzz Hypothesis:"&lt;br /&gt;&lt;blockquote&gt;Given a particular language feature or programming technique, if it cannot be shown as an integral part of a FizzBuzz solution then the typical programmers will not be able to use it correctly.&lt;/blockquote&gt;This statement is worth deconstructing from multiple angles.  First there is the logical structure of the hypothesis:&lt;br /&gt;&lt;blockquote&gt;¬&lt;i&gt;r&lt;/i&gt; → ¬&lt;i&gt;q&lt;/i&gt;&lt;/blockquote&gt;where &lt;i&gt;r&lt;/i&gt; is the ability to express the programming language feature or technique in the form of FizzBuzz and &lt;i&gt;q&lt;/i&gt; is typical programmers using it correctly.  This also implies something by negation.  If typical programmers are using a language feature correctly then there must be some way to make it integral to a FizzBuzz solution, and if it can be proven that there is no such FizzBuzz solution then clearly the typical programmer isn't using it correctly, all other evidence notwithstanding.  Also note the word choice here.  I did not say properly or effectively, I said correctly.  There are proper and effective ways to do things that are not correct.  I am reminded of this every day I watch my children do chores around the house. &lt;br /&gt;&lt;br /&gt;There is also the question of what I mean by the typical programmer.  Since there is a high amount of variability on the high end of the "mean average programmer" the mean pulls up the average such that more than half of the programmers are less than average.  There is also the possibility of a "median" programmer, where half the programmers are better or worse.  This too has its faults, because that means that half of the programmers may not be able to even program a FizzBuzz solution.  Given a large enough sample set that may be true, but I don't think we are talking about anyone who has sat through a LOGO class in elementary school or hand coded some HTML.  When I say "typical programmer" I mean someone within one or two standard deviation of the mean.  I am also assuming that programming ability is a &lt;a href="http://en.wikipedia.org/wiki/Poisson_distribution"&gt;Poisson distribution&lt;/a&gt;.  (I think I've just demonstrated everything I can remember from one semester of college statistics for technical majors.)&lt;br /&gt;&lt;br /&gt;Now why do I express the law as a double negative implication?  I blame the self-esteem driven coddling I received in public schooling.  When you are failing you are "not succeeding" and when you are rebelling you are "not conforming."  Everything is expressed as the desired outcome, and rather than using an antonym the desired word is used, with a negation in front of it.  As a good Computer Science major I spent a few weeks in propositional logic.  This math subset has a lot of theorems.  And one of them is is Transposition, where we can swap the order of the implication as long as we negate both sides.  Taking a desired statement, and adding two negations and still have it mean something is really cool.&lt;br /&gt;&lt;br /&gt;This them begs the question, if a typical programmer is using a language feature or pattern correctly then it can be expressed as a fundamental part of a FizzBuzz solution then why don't I say it like that? The problem is we start with the hypothetical typical programmer.  Next, we would need to see them use the language feature or pattern correctly.  And only then can we conclude an integral FizzBuzz solution exists.  I don't like this formulation because we start with end results looking backwards rather than a prediction looking forward.  We would start with real programmers correctly using real technologies and concluding something arbitrary and contrived.  But if we start with something arbitrary and contrived we can project onto real programmers and real situations without waiting for those real situations to occur.  Much like concluding that a ball will hit the ground &lt;a href="http://www.youtube.com/watch?v=z2n7SXUM9m0" target="_blank"&gt;without dropping it&lt;/a&gt;.  This is especially useful for technologies that have not seen widespread use.  By starting with the technology we can then use this hypothesis as a lens to examine potential technologies &lt;i&gt;a priori&lt;/i&gt; rather than as a post mortem.&lt;br /&gt;&lt;br /&gt;The reason I came to formulating this hypothesis is because of the depths of thinking I went to while contemplating the various parts of this project.  Part of that was reflecting on why it was a good interview question for testing programming skills.  Not design, engineering, or architecture skills, but programming skills in the raw.  It's a bit like the NFL combine.  Before players are drafted by professional teams the teams get all of the top candidates together and test basic skills.  How fast can you run, how much can you bench press.  Let’s see you catch balls, see you throw balls, and see how smart are you.  (No kidding, the NFL sits them down for an IQ test.  And offensive tackles tend to be smarter than the quarterbacks, and way smarter than the wide receivers.)&lt;br /&gt;&lt;br /&gt;But what makes FizzBuzz a good proxy for programming aptitude?  FizzBuzz has three key aspects to most solutions: repetition, calculation, and decision. And these three aspects pretty much sums up most of what programming is about.  There are some exceptions, and most of those interactions are hidden behind APIs calls.  So if a programming language feature or pattern doesn't help you repeat, calculate, or decide, what good is it for programming?&lt;br /&gt;&lt;br /&gt;For this entry I am going to use the FizzBuzz Hypothesis to answer a question that has bothered me since the Java programming language has been released:  why is there both a short circuted and fully evaluated boolean operator in the Java Programming Language?  And can you use both meaningfully?&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java;"&gt;public class FizzBuzz05 {&lt;br /&gt;&lt;br /&gt;  static boolean lessThan100(int val) {&lt;br /&gt;    return val &amp;lt; 100;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  static boolean printNumber(int val) {&lt;br /&gt;    System.out.print(val);&lt;br /&gt;    return true;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  static boolean maybeFizz(int val) {&lt;br /&gt;    if (val % 3 == 0) {&lt;br /&gt;      System.out.print("Fizz");&lt;br /&gt;      return true;&lt;br /&gt;    } else {&lt;br /&gt;      return false;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  static boolean maybeBuzz(int val) {&lt;br /&gt;    if (val % 5 == 0) {&lt;br /&gt;      System.out.print("Buzz");&lt;br /&gt;      return true;&lt;br /&gt;    } else {&lt;br /&gt;      return false;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static void main(String[] a) {&lt;br /&gt;    int num = 0;&lt;br /&gt;    while (lessThan100(num++) &amp;amp;&amp;amp;&lt;br /&gt;      maybeFizz(num) | maybeBuzz(num) || printNumber(num)) {&lt;br /&gt;      System.out.println();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Most of this solution is spent writing a method that makes the key point more readable.  There are four different methods that do four separate things required by the solution.  One method tests if the number is less than one hundred.  One method prints out the number that is passed in.  One method when passed in a number divisible by three prints out "Fizz."  And there is one last helper method that does the same as the last, except testing for 5 and printing "Buzz."  If the Fizz or Buzz methods print out a value, then they return true.  If no value was printed, false is returned.   None of these helper methods print a new line; that will be the task of the main method. You may be wondering where the helper method is that prints out "FizzBuzz".  The reason we don’t need one is the point of this particular exercise.&lt;br /&gt;&lt;br /&gt;The magic happens in line 33, a group of three of the helper methods joined by two different boolean or operators.  The first one is the fully evaluated or "bitwise" version and the other is the short circuited or "logical" version.  According to &lt;a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html" target="_blank"&gt;operator precedence&lt;/a&gt; we work with the bitwise operator and work that one first.  When going through the loop both sides of this operator will be evaluated, regardless of the results.  So in the cases where we have a number evenly divisible by three and five we will get both sides of the expression evaluated, even though we know after fizz that the result will be true.&lt;br /&gt;&lt;br /&gt;The second operator, of lower precedence, is the logical or short circuit operator.  If either of the methods on the left hand side had printed out their FizzBuzz value then we would be getting a true result on the left side.  By short circuit rule we would not evaluate the right hand side and no value would have been printed.  This is a good thing, because the method will always print a number regardless of what it is.  If we print out Fizz or Buzz or FizzBuzz we are not supposed to print out a number at all.  But if neither of the methods return true and no other values have been printed out then we need to print out the number.  Since the left hand side is false in those cases the right hand side is evaluated.  And as a side effect of the evaluation the number is printed.&lt;br /&gt;&lt;br /&gt;So there, in one single expression, we have both a fully evaluated and short circuited logical or operator.  It looks like typical developers &lt;i&gt;can&lt;/i&gt; use this correctly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-1678710352462810514?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/1678710352462810514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/buzz-5-fizzbuzz-hypothesis.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/1678710352462810514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/1678710352462810514'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/buzz-5-fizzbuzz-hypothesis.html' title='Buzz 5 - The FizzBuzz Hypothesis'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-6738578768767132168</id><published>2012-01-12T15:05:00.001-07:00</published><updated>2012-01-12T17:57:55.755-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#4 - Wrap the Math in a Method</title><content type='html'>Sometimes I think I was born to program, other times I think it's a giant cosmic coincidence. &amp;nbsp;When I was in &amp;nbsp;fifth grade my school got lucky and someone donated 15 VIC-20 computers complete with cheapo CRTs. Because of this all of the fifth graders learned how to program basic that year. &amp;nbsp;All of the fifth graders, not just the nerds. &amp;nbsp;In junior high school there was a computer elective as well, which I took at the earliest possible time. &amp;nbsp;Then when I got into high school I was in for a rude shock: &amp;nbsp;you need to be a Junior to take a computer programming class. &amp;nbsp;Two whole years without official credit for goofing off on a computer. &amp;nbsp;What was more awesome was that it was an honest to goodness programming class instead of that survey class I had in junior high. &lt;br /&gt;&lt;br /&gt;So I did what any other socially awkward nerd would do: I asked the teacher if I could get an exception. &amp;nbsp;And I got that exception, and took the class the second semester. &amp;nbsp;The class was well within my capabilities as I had to endure complaints from other students that I was a curve buster with my 105% score in the class (when adding in extra credit). &amp;nbsp;It was the only time I have ever been accused of "busting the curve." &amp;nbsp;But it wasn't all ice cream and lollipops. &amp;nbsp;The teacher didn't always like the way I coded the solutions, particularly the fact I never used procedures. &amp;nbsp;My programs were all one long stream of instructions. &amp;nbsp;Just like a VIC-20 basic program, except there were no line numbers. &amp;nbsp;My thinking was if you are only going to call a procedure at one point in the code then why type all those extra keystrokes? &amp;nbsp;Mr. Shepard was not impressed.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class FizzBuzz04 {&lt;br /&gt;&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;    for (int i = 1; i &amp;lt;= 100; i++) {&lt;br /&gt;      System.out.println(theValue(i));&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static String theValue(int i) {&lt;br /&gt;    if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;      return "FizzBuzz";&lt;br /&gt;    } else if (i % 5 == 0) {&lt;br /&gt;      return "Buzz";&lt;br /&gt;    } else if (i % 3 == 0) {&lt;br /&gt;      return "Fizz";&lt;br /&gt;    } else {&lt;br /&gt;      return Integer.toString(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;I hope Mr. Shepard is impressed now. &amp;nbsp;This actually does, believe it or not, improve the readability and maintainability of the code. &amp;nbsp;It pulls out the biggest third of the problem (doing ugly math) and keeps the other two thirds (counting to one hundred and printing out stuff) in a nice neat package. &lt;br /&gt;&lt;br /&gt;In the main method the function it calls is a&amp;nbsp;wondrous&amp;nbsp;and magical black box. &amp;nbsp;Data goes in, answers come out. This is a fundamental abstraction that actually allows a lot of magical code to work. &amp;nbsp;Consider encryption. &amp;nbsp;Do you know about congruence in number theory? &amp;nbsp;What? &amp;nbsp;Numbers have theories?! &amp;nbsp;And what's that about the Chinese remainders? &amp;nbsp;Don't know, and since I have a procedure I can call I don't care. &lt;br /&gt;&lt;br /&gt;It's magic.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-6738578768767132168?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/6738578768767132168/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/4-wrap-math-in-method.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/6738578768767132168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/6738578768767132168'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/4-wrap-math-in-method.html' title='#4 - Wrap the Math in a Method'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-929664031987904471</id><published>2012-01-10T15:05:00.000-07:00</published><updated>2012-01-11T08:58:24.694-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Groovy'/><category scheme='http://www.blogger.com/atom/ns#' term='Fizz'/><title type='text'>Fizz 3 - Elvis Has Left the Building (Groovy)</title><content type='html'>Whenever I count "Fizz" in this series I will examine a language that has yet to be examined before.  &lt;a href="http://en.wikipedia.org/wiki/List_of_programming_languages" target="_blank"&gt;There is no shortage of candidates&lt;/a&gt;&amp;nbsp;but the trick will be to keep the languages choices relevant and readily verifyable (this could be &lt;a href="http://en.wikipedia.org/wiki/Pseudocode" target="_blank"&gt;tricky&lt;/a&gt;). &amp;nbsp;Ideally I will be able to consider a feature that is relatively unique to that language or family of languages.&lt;br /&gt;&lt;br /&gt;For this post I will examine the &lt;a href="http://groovy.codehaus.org/" target="_blank"&gt;Groovy&lt;/a&gt; programming language. &amp;nbsp;Picking a relatively unique feature for Groovy is a fairly hard prospect since it tries to pull all the best parts from all other languages. &amp;nbsp;It is based on Java, so it's a C like curly brace language. &amp;nbsp;It compiles to JVM byte code. &amp;nbsp;It is object oriented, but strives to delay it's method dispatch as late as possible, enabling multiple dispatch method invocations as seen in Common Lisp. &amp;nbsp;It uses closure like blocks much like you would see in Ruby. &lt;br /&gt;&lt;br /&gt;There are a lot of neat tricks in Groovy so I see myself coming back to this language later in this series. &amp;nbsp;But perhaps the most unique feature of Groovy, or the one feature that it has gone the furthest to popularize, then it would be the "Elvis Operator:"&amp;nbsp;&lt;code&gt;?:&lt;/code&gt; a C style ternary operator with the middle operand removed. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: groovy"&gt;for (int i = 1; i &amp;lt;= 100; i++) {&lt;br /&gt;  String answer = ""&lt;br /&gt;&lt;br /&gt;  if (i%3 == 0) {&lt;br /&gt;    answer += "Fizz"&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  if (i%5 == 0) {&lt;br /&gt;    answer += "Buzz"&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  println answer ?: i&lt;br /&gt;}&lt;/pre&gt; &lt;br/&gt;This isn't the most idiomatic Groovy code for this solution. It is intended to demonstrate just the Elvis operator with all other awesomeness removed. &amp;nbsp;I have a &lt;a href="http://groovyconsole.appspot.com/script/624001" target="_blank"&gt;three statement solution&lt;/a&gt; that shows other Groovy features such as blocks and ranges.  But for the purpose of this post we should focus just on the unique feature.  There, on line 12 of the verbosely expressed code, is the Elvis operator.  Why is it called Elvis?  If you rotate it clockwise the question mark kind of forms Elvis Presley's hair just above his eyes. &lt;br /&gt;&lt;br /&gt;But what does it do? &amp;nbsp;It clearly has no hips to shake suggestively since he is being expressed from the nose up. &amp;nbsp;Elvis picks the first "true" item it sees and returns only that item. &amp;nbsp;Mode technically, the left hand side of the operator is evaluated for "&lt;a href="http://docs.codehaus.org/display/GROOVY/Groovy+Truth" target="_blank"&gt;groovy truth&lt;/a&gt;," and if it returns negative (null, false, zero, or empty string in most cases) then the right hand side of the operator is returned.  If the left had side matches any other value, then the left hand side of the operator is returned. &lt;br /&gt;&lt;br /&gt;This roughly equates to the C style statement &lt;code&gt;x ? x : y&lt;/code&gt;.  If the middle operator leaves the building, along with the&amp;nbsp;white space, you are left with the Elvis operator: &lt;code&gt;x ?: y&lt;/code&gt;.  Even though Elvis has left the building, his legacy remains.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-929664031987904471?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/929664031987904471/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-3-elvis-has-left-building.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/929664031987904471'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/929664031987904471'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2012/01/fizz-3-elvis-has-left-building.html' title='Fizz 3 - Elvis Has Left the Building (Groovy)'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-659164693281614004</id><published>2012-01-05T15:05:00.000-07:00</published><updated>2012-01-12T09:44:19.702-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#2 - Is That Your Final Answer?</title><content type='html'>When it comes to minor tweaks in the presentation of the problem, there is more than one way to make "the smallest change."  Rather than printing out the result immediately we can defer the printing and keep an answer in a variable, changing it as needed.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;public class FizzBuzz02 {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  for (int i = 1; i &amp;lt;= 100; i++) {&lt;br /&gt;   String answer = Integer.toString(i);&lt;br /&gt;   if (i % 3 == 0) {&lt;br /&gt;     answer = "Fizz";&lt;br /&gt;   }&lt;br /&gt;   if (i % 5 == 0) {&lt;br /&gt;     answer = "Buzz";&lt;br /&gt;   }&lt;br /&gt;   if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;     answer = "FizzBuzz";&lt;br /&gt;   }&lt;br /&gt;   System.out.println(answer);&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This example allows two important changes, actually.  The first is the introduction of a variable, and the changing of the variable Once we are done deciding what the result should be we can then print out the result of the variable.&lt;br /&gt;&lt;br /&gt;The second important change is that we can consider each condition on its own. &amp;nbsp;Since the most general cases are considered first and then the most specific are considered later. &amp;nbsp;Since the most specific one is considered last, it can overwrite the previous answer. &amp;nbsp;When you add these two items together, you can see that the variable will be set once, twice, or four times. &amp;nbsp;Never three. &lt;br /&gt;&lt;br /&gt;Another way to think of this is a game show. &amp;nbsp;Rather than playing&amp;nbsp;Jeopardy,&amp;nbsp;where only the first answer given is accepted, we are playing Who Wants to Be a&amp;nbsp;Millionaire, where the last answer given is accepted. &amp;nbsp;Contestants routinely change their answers but the only answer that matters is the last answer.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-659164693281614004?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/659164693281614004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2011/11/2-changing-answer-incomplete.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/659164693281614004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/659164693281614004'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2011/11/2-changing-answer-incomplete.html' title='#2 - Is That Your Final Answer?'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7186238830705847593.post-8372370859310663788</id><published>2012-01-03T15:05:00.000-07:00</published><updated>2012-01-12T09:44:06.029-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Number'/><title type='text'>#1 - Common But Boring</title><content type='html'>Over the next year or so I intend on blogging 100 solutions to the FizzBuzz problem. &amp;nbsp;Each solution will vary in either technique or language, sometimes both. &amp;nbsp;The various "FizzBuzz" posts will also have various themes based on where the number lands. &amp;nbsp;The "number" days will simply be a solution with the prose around it focusing on the solution itself.&lt;br /&gt;&lt;br /&gt;To start things off I thought it was best to focus on one of the more common answers. &amp;nbsp;This solution will be written in Java, which as of this writing was the #1 language on the &lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" target="_blank"&gt;TIOBE index&lt;/a&gt; in December of 2011. &amp;nbsp;This is merely evidence that it one of the most common languages.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class FizzBuzz01 {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  for (int i = 1; i &amp;lt;= 100; i++) {&lt;br /&gt;   if ((i % 3 == 0) &amp;amp;&amp;amp; (i % 5 == 0)) {&lt;br /&gt;    System.out.println("FizzBuzz");&lt;br /&gt;   } else if (i % 5 == 0) {&lt;br /&gt;    System.out.println("Buzz");&lt;br /&gt;   } else if (i % 3 == 0) {&lt;br /&gt;    System.out.println("Fizz");&lt;br /&gt;   } else {&lt;br /&gt;    System.out.println(i);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is not a perfectly straightforward translation of the problem statement into a solution. &amp;nbsp;Without a lot of&amp;nbsp;preparatory&amp;nbsp;code you cannot directly translate the problem statement into code in the order it is presented in the problem statement. &lt;br /&gt;&lt;br /&gt;This sample is about as simple as it can be done in Java.  The only portion worth commenting on is how it transcribes the solution.  Rather than going in the order the clauses are stated in the problem statement we consider the conditions from the last one first. &amp;nbsp;This changes it from a set of &amp;nbsp;"except" statements into a series of simpler&amp;nbsp;if-then-else statements.&lt;br /&gt;&lt;br /&gt;This is of the more common solutions (only considering correct solutions) presented&amp;nbsp;by job candidates when asked this question in a job interview. &amp;nbsp;It is also one of the most boring solutions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7186238830705847593-8372370859310663788?l=12fizz4buzz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://12fizz4buzz.blogspot.com/feeds/8372370859310663788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://12fizz4buzz.blogspot.com/2011/11/1-reverse-order-of-conditions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/8372370859310663788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7186238830705847593/posts/default/8372370859310663788'/><link rel='alternate' type='text/html' href='http://12fizz4buzz.blogspot.com/2011/11/1-reverse-order-of-conditions.html' title='#1 - Common But Boring'/><author><name>Danno Ferrin</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh3.googleusercontent.com/-mpUW2mpZfGo/AAAAAAAAAAI/AAAAAAAABQM/oxddUXnDM7c/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry></feed>
