<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Programming on Stay Frosty</title><link>https://frosty.blog/tags/programming/</link><description>Recent content in Programming on Stay Frosty</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><copyright>© James Frost</copyright><lastBuildDate>Wed, 10 May 2017 20:49:34 +0000</lastBuildDate><atom:link href="https://frosty.blog/tags/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Inspecting iOS State Restoration Data</title><link>https://frosty.blog/2017/05/10/inspecting-ios-state-restoration-data/</link><pubDate>Wed, 10 May 2017 20:49:34 +0000</pubDate><guid>https://frosty.blog/2017/05/10/inspecting-ios-state-restoration-data/</guid><description>&lt;p&gt;State preservation and restoration allows iOS apps to save state when they&amp;rsquo;re sent into the background, and restore that state if the app is killed and then relaunched. If you&amp;rsquo;ve ever implemented state restoration yourself, you may have run into a case where it&amp;rsquo;d be helpful to see exactly what state iOS was storing.&lt;/p&gt;
&lt;p&gt;It turns out that Apple actually provides a tool to help you do just this, but it&amp;rsquo;s kind of hidden away and documentation is relatively sparse. Fortunately, it&amp;rsquo;s pretty easy to use. When you run it, it&amp;rsquo;ll output a structured plist showing all of your encoded objects, their restoration identifiers, and restoration class information. Here&amp;rsquo;s a step by step guide.&lt;/p&gt;</description><content:encoded><![CDATA[<p>State preservation and restoration allows iOS apps to save state when they&rsquo;re sent into the background, and restore that state if the app is killed and then relaunched. If you&rsquo;ve ever implemented state restoration yourself, you may have run into a case where it&rsquo;d be helpful to see exactly what state iOS was storing.</p>
<p>It turns out that Apple actually provides a tool to help you do just this, but it&rsquo;s kind of hidden away and documentation is relatively sparse. Fortunately, it&rsquo;s pretty easy to use. When you run it, it&rsquo;ll output a structured plist showing all of your encoded objects, their restoration identifiers, and restoration class information. Here&rsquo;s a step by step guide.</p>
<h3 id="1-download-the-tool">1. Download the tool</h3>
<p>Head over to the Apple &ldquo;<a href="https://developer.apple.com/download/more/">Downloads for Apple Developers</a>&rdquo; site and expand the <strong>restorationArchiveTool for iOS</strong> download. You&rsquo;ll want the <code>restorationArchiveTool.dmg</code> file right at the bottom of the list on the right.</p>
<p>Open the dmg, and drag the <code>restorationArchiveTool</code> binary out to somewhere you can easily access it. I placed mine in my <code>~/bin</code> directory.</p>
<h3 id="2-find-your-apps-library-directory">2. Find your app&rsquo;s Library directory</h3>
<p>Next, you need to know the location of your app&rsquo;s <code>Library</code> directory in the simulator. This is where the restoration data gets saved, which you&rsquo;ll inspect with the archive tool. The easiest way to find this is to add a line to your app delegate&rsquo;s <code>application:didFinishLaunchingWithOptions:</code> (or via a breakpoint). Something like this should do the trick:</p>
<p><strong>Objective-C:</strong></p>





<pre tabindex="0"><code>NSLog(@&#34;Library: %@&#34;, [[[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] firstObject] path]);</code></pre><p><strong>Swift:</strong></p>





<pre tabindex="0"><code>print(&#34;Library: \(FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!.path)&#34;)</code></pre><p>The path should get logged out to the console when your app launches. It&rsquo;ll be something like this:</p>





<pre tabindex="0"><code>/Users/bob/Library/Developer/CoreSimulator/Devices/1FF2D964-C615-499E-B1D0-8A7DF7343AE5/data/Containers/Data/Application/D6FE3A2C-9553-4270-8262-A94376EF4E5B/Library</code></pre><h3 id="3-get-some-data">3. Get some data</h3>
<p>This part&rsquo;s simple. Launch the app in the simulator, navigate to the part of the app you&rsquo;re interested in, send the app into the background, and stop the app in Xcode. If you open up the library folder you found in the last step, you&rsquo;ll see a folder named <strong>Saved Application State</strong>. Within that, you&rsquo;ll find a <code>data.data</code> file.</p>
<figure>
  <img src="/images/2017/05/Screen-Shot-2017-05-10-at-21.14.38.png" alt="Screen Shot 2017-05-10 at 21.14.38" loading="lazy">
</figure>
<h3 id="4-run-the-tool">4. Run the tool</h3>
<p>Now you need to run the archive tool and point it at the <code>data.data</code> file you just found. In Terminal, enter the following command, but switch out the location of your <code>restorationArchiveTool</code> binary and your app&rsquo;s <code>data.data</code> as required:</p>





<pre tabindex="0"><code>~/bin/restorationArchiveTool --plist --structured -o output.plist /path/to/your/data/file/data.data</code></pre><p>And you&rsquo;re done! You can then open up the generated plist in Xcode. Amongst other things, you&rsquo;ll see a list of top level objects, keyed by their restoration identifiers. You should notice that the restoration identifiers form a path based upon your view controller hierarchy. You can expand any object to see any extra data it&rsquo;s encoded.</p>
<figure>
  <a href="/images/2017/05/Screen-Shot-2017-05-10-at-20.44.26.png"><img src="/images/2017/05/Screen-Shot-2017-05-10-at-20.44.26-1600.png" alt="Screen Shot 2017-05-10 at 20.44.26" loading="lazy"></a>
</figure>
<p>At the bottom of the file, you&rsquo;ll find a list of restoration classes used by each of the encoded objects.</p>
<figure>
  <a href="/images/2017/05/Screen-Shot-2017-05-10-at-20.44.45.png"><img src="/images/2017/05/Screen-Shot-2017-05-10-at-20.44.45-1600.png" alt="Screen Shot 2017-05-10 at 20.44.45" loading="lazy"></a>
</figure>
<p>If you&rsquo;re having some problems with state restoration, then at the very least being able to see this information can give you some hints about whether your issue is with encoding or decoding. Good luck!</p>
]]></content:encoded></item><item><title>Codify</title><link>https://frosty.blog/2011/10/19/codify/</link><pubDate>Wed, 19 Oct 2011 23:00:00 +0000</pubDate><guid>https://frosty.blog/2011/10/19/codify/</guid><description>&lt;p&gt;I love the iPad. It&amp;rsquo;s casual, immediate, and more intimate than a laptop ever will be. There are wonderful apps available for most activities you could want to do on an iPad: reading, writing, drawing, listening, watching, playing; the list goes on. Much of the time when I want to perform some task (or just relax), I will now reach for my iPad instead of my laptop if possible. However, until now I have been unable to use the iPad for one of my favourite pasttimes: programming&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;</description><content:encoded><![CDATA[<p>I love the iPad. It&rsquo;s casual, immediate, and more intimate than a laptop ever will be. There are wonderful apps available for most activities you could want to do on an iPad: reading, writing, drawing, listening, watching, playing; the list goes on. Much of the time when I want to perform some task (or just relax), I will now reach for my iPad instead of my laptop if possible. However, until now I have been unable to use the iPad for one of my favourite pasttimes: programming<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>.</p>
<p>Enter <strong>Codify</strong>. Codify is a new iPad app that lets you create simple games, prototypes and simulations directly on an iPad. You write code in the <a href="http://www.lua.org/">Lua</a> language using a fantastic code editor (more on that later), with an API that&rsquo;s very similar to <a href="http://www.processing.org">Processing</a>. You can then run that code straight away; play your game, experiment with your prototype, tweak your simulation. It&rsquo;s fast, easy, impressive, and <em>fun</em>.</p>
<figure>
  <a href="http://www.flickr.com/photos/jamesfrost/sets/72157627800191607/"><img src="/images/2015/12/2011-10-20-codify-01.jpg" alt="Codify project selection screen" loading="lazy"></a>
</figure>
<h3 id="running-and-debugging">Running and debugging</h3>
<p>When running your game, you can specify parameters (for example, a character&rsquo;s speed or size) which show up as sliders in a debugging pane on the left hand side of the screen. You can then tweak those parameters <em>as you play</em>, and observe in real time the effect they have on the game. It&rsquo;s very neat. You can also watch variables and print information out to a console. Games themselves run very smoothly (unless you write them badly, of course!) and look great. You can pause, resume, or reset your game state whenever you like. You can also flip back to your code with just one tap.</p>
<figure>
  <a href="http://www.flickr.com/photos/jamesfrost/sets/72157627800191607/"><img src="/images/2015/12/2011-10-20-codify-02.jpg" alt="Running a game in Codify" loading="lazy"></a>
</figure>
<h3 id="help-and-examples">Help and examples</h3>
<p>The app comes with a whole heap of examples, from the basics of using the provided graphics functions to clones of games like Doodle Jump, Snake, and Pong (the latter I wrote myself). The code editor also comes with a nicely presented documentation viewer which provides excellent help on graphics functions, the Lua language itself, parameters, multitouch, sounds, using the iPad&rsquo;s accelerometer, and more.</p>
<p>To see some of the Codify examples in action, check out the following YouTube videos:</p>
<ul>
<li><a href="http://www.youtube.com/watch?v=umpCNWKmlFM">Bit Invader</a></li>
<li><a href="http://www.youtube.com/watch?v=WSWhYxjE-ZY">Lua Jump</a> (Doodle Jump clone)</li>
<li><a href="http://www.youtube.com/watch?v=UaiqkuX5fy8">Dungeon Roller</a></li>
<li><a href="http://www.youtube.com/watch?v=EHqDgEyBF68">Ping</a> (Pong clone)</li>
</ul>
<h3 id="the-code-editor">The code editor</h3>
<p>The code editor has clearly had a <strong>lot</strong> of thought and polish put into it. It has a lot of things you&rsquo;d expect from an editor: syntax highlighting, line numbers, multiple files (with tabs to switch between them), and a function browser. But it also has a slew of features designed to ease the task of programming on a touchscreen device:</p>
<ul>
<li>For methods that take a colour as a parameter (RGBA values, for example <code>(255, 150, 99, 255)</code>), you can simply tap on the parameter and a colour wheel appears in a popover allowing you to choose a colour with touch alone.</li>
<li>You can tap and hold on any number and slide your finger from left to right (or vice versa) to increment / decrement the number without typing.</li>
<li>A row of custom keys above the onscreen keyboard provides shortcuts for common tasks: tabbing, access to documentation, inserting symbols / punctuation, and more.</li>
<li>Codify includes support for displaying sprites (images) and comes bundled with a number of high quality sprite packs from <a href="http://www.lostgarden.com/search/label/free%20game%20graphics">Lost Garden</a>. Similarly to selecting colour parameters, you can tap a sprite parameter and select your sprite from a nice popover without the need for any typing.</li>
</ul>
<figure>
  <a href="http://www.flickr.com/photos/jamesfrost/sets/72157627800191607/"><img src="/images/2015/12/2011-10-20-codify-03.jpg" alt="Selecting a colour in the Codify code editor" loading="lazy"></a>
</figure>
<p>Check out my <a href="http://www.flickr.com/photos/jamesfrost/sets/72157627800191607/">Flickr photo set</a> for a couple more photos showing the Codify interface. I&rsquo;ve also put together a quick <a href="http://www.youtube.com/watch?v=hJ537lFVv1M">YouTube video</a> showing some of its functionality in action (embedded at the bottom of this post).</p>
<p>The <a href="http://www.lua.org/">Lua</a> language was deliberately chosen for Codify because it has a rather &lsquo;wordy&rsquo; syntax, which fits nicely with the iPad&rsquo;s onscreen keyboard. It means less switching to the &lsquo;symbols&rsquo; keyboard, and less frustration for the user. The Lua runtime is also extremely light, which helps keep things snappy.</p>
<h3 id="an-introduction-to-programming---a-brief-aside">An introduction to programming? - a brief aside</h3>
<p>I think that Codify could shine as a tool for teaching programming. I&rsquo;ve long lamented the apparent increase in difficulty for kids (and adults alike, I guess) to get started with programming these days. I began coding on a BBC B in BASIC. Back then, you turned your computer on and you were presented with a flashing prompt, into which you could just start typing your code. Commercial games weren&rsquo;t too advanced, so it was entirely possible you could get at least close to making the sorts of things you played yourself.</p>
<p>These days, where do you start? If you have a Mac or Linux machine, chances are you have something like Ruby or Python built in, so that&rsquo;s fairly easy to get going with. If you&rsquo;re on Windows, you&rsquo;re left to install something yourself. You&rsquo;ll also likely have to install some kind of text editor and work out how to use a command prompt (not a bad thing to do at all, of course, but it&rsquo;s another barrier to entry). And then you&rsquo;ll have to find some kind of games or graphics libraries and learn how those work. And <em>then</em> you might be able to have a go at learning a language and making a simple game. It&rsquo;s a lot to get through and a lot of opportunities for a newcomer to lose their enthusiasm.</p>
<p>We&rsquo;re now finally starting to see some potential solutions emerging on the web. For example, <a href="http://processing.js">Processing.js</a> (a JavaScript port of Processing) and <a href="http://sketchpad.cc">Sketchpad.cc</a> (which lets you just start coding with Processing.js in your browser). Codify is very much like these solutions, but built specifically for iPad and a touch interface. From now on, somebody who wants to get started with simple programming and games may just be able to search the App Store for &lsquo;programming&rsquo;, install Codify, and have a go; perhaps starting off modifying some of the examples and observing the effects their changes have in real time. It may not be as easy to a newcomer as I&rsquo;m imagining, but it sure seems a lot simpler to me than what we&rsquo;ve had in recent years.</p>
<h3 id="wrap-up">Wrap up</h3>
<p>Codify was created by <a href="http://www.twitter.com/twolivesleft">Simeon Nasilowski</a> of <a href="http://twolivesleft.com/">Two Lives Left</a> and will be launching on the App Store on Wednesday 26th October for £5.49 ($7.99), which I think is great value for everything it offers. It&rsquo;s a high quality package that finally lets you code on the iPad, and it does so with style. I highly recommend checking it out if you enjoy programming or think you&rsquo;d like to give it a go. I&rsquo;d be really interested in seeing what you create with it!</p>
<p>I&rsquo;ve also posted a video to YouTube showing some of Codify&rsquo;s features in action:</p>
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
			<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/hJ537lFVv1M?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"></iframe>
		</div>

<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>Okay, so there <em>are</em> one or two existing iPad coding apps. However, these a) don&rsquo;t seem to be as well designed, and b) mainly only seem to let you produce text-based console output. Codify is intended for fast graphical prototyping, and seems to be streets ahead of the competition.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content:encoded></item><item><title>Swapping Variables</title><link>https://frosty.blog/2008/06/01/swapping-variables/</link><pubDate>Sun, 01 Jun 2008 23:00:00 +0000</pubDate><guid>https://frosty.blog/2008/06/01/swapping-variables/</guid><description>&lt;p&gt;I thought I&amp;rsquo;d share a little trick I learnt the other day regarding swapping variables.&lt;/p&gt;
&lt;p&gt;A common coding/computer science task is to swap the values of two variables. In some languages, such as Java, one may start by creating a new temporary variable. For example (assuming we already have two ints, x and y):&lt;/p&gt;





&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This creation of a new variable uses more memory, and whilst in most circumstances would be fine, it could become an issue if we&amp;rsquo;re swapping many times, for example in a sorting algorithm.&lt;/p&gt;</description><content:encoded><![CDATA[<p>I thought I&rsquo;d share a little trick I learnt the other day regarding swapping variables.</p>
<p>A common coding/computer science task is to swap the values of two variables. In some languages, such as Java, one may start by creating a new temporary variable. For example (assuming we already have two ints, x and y):</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kt">int</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y</span><span class="p">;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span></span></span></code></pre></div><p>This creation of a new variable uses more memory, and whilst in most circumstances would be fine, it could become an issue if we&rsquo;re swapping many times, for example in a sorting algorithm.</p>
<p>Some languages allow us to do a nice swap without extra variables, using multiple assignment and tuple packing and unpacking:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span></span></span></code></pre></div><p>This is pretty neat, although is apparently slower than using a temporary variable.</p>
<p>Anyway, yesterday I discovered a third method: XOR swapping. Again, this doesn&rsquo;t require the use of an extra variable, and I thought it was pretty clever (here ^ represents XOR, as in Python):</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span>
</span></span><span class="line"><span class="cl"><span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span>
</span></span><span class="line"><span class="cl"><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span></span></span></code></pre></div><p>Neat, huh? It&rsquo;s fairly simple to see how it works if you simply walk through the steps. Let&rsquo;s swap x = 7, y = 9:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">x</span> <span class="o">=</span> <span class="mi">0111</span>
</span></span><span class="line"><span class="cl"><span class="n">y</span> <span class="o">=</span> <span class="mi">1001</span>
</span></span><span class="line"><span class="cl"><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1110</span>
</span></span><span class="line"><span class="cl"><span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0111</span>
</span></span><span class="line"><span class="cl"><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">^</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1001</span></span></span></code></pre></div><p>Apparently this method is quite commonly used in embedded assembly code, however you won&rsquo;t find it appearing much in code on a desktop computer. It usually is slower to compute than using temporary variables, as most processors attempt to execute commands in parallel - and of course, each of these instructions depends upon the previous result, so they must be executed sequentially. Still, it&rsquo;s a nice thing to know.</p>
<p>The short version: it seems that if you&rsquo;re trying to save memory then XOR swap or use multiple assignment, otherwise if efficiency is an issue, use a temporary variable.</p>
]]></content:encoded></item><item><title>FizzBuzz Follow-up</title><link>https://frosty.blog/2008/05/29/fizzbuzz-follow-up/</link><pubDate>Thu, 29 May 2008 23:00:00 +0000</pubDate><guid>https://frosty.blog/2008/05/29/fizzbuzz-follow-up/</guid><description>&lt;p&gt;I thought I&amp;rsquo;d post a follow-up to yesterday&amp;rsquo;s &lt;a href="http://blog.jameswfrost.co.uk/2008/05/29/fizzbuzz-and-other-problems/"&gt;FizzBuzz&lt;/a&gt; post with some sample solutions, as a bunch of you seemed to find it fun to solve. Feel free to add your own solutions in the comments of this post!&lt;/p&gt;
&lt;p&gt;My own first solution was Python-based, and a fairly simple one:&lt;/p&gt;





&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;FizzBuzz&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Fizz&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Buzz&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Obviously it&amp;rsquo;s not in a function or anything, but I just wanted to do it as fast as possible. Shortly afterwards, I realised that line 2 could be simplified by changing it to:&lt;/p&gt;</description><content:encoded><![CDATA[<p>I thought I&rsquo;d post a follow-up to yesterday&rsquo;s <a href="http://blog.jameswfrost.co.uk/2008/05/29/fizzbuzz-and-other-problems/">FizzBuzz</a> post with some sample solutions, as a bunch of you seemed to find it fun to solve. Feel free to add your own solutions in the comments of this post!</p>
<p>My own first solution was Python-based, and a fairly simple one:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">101</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="nb">print</span> <span class="s2">&#34;FizzBuzz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="nb">print</span> <span class="s2">&#34;Fizz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="nb">print</span> <span class="s2">&#34;Buzz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="nb">print</span> <span class="n">i</span></span></span></code></pre></div><p>Obviously it&rsquo;s not in a function or anything, but I just wanted to do it as fast as possible. Shortly afterwards, I realised that line 2 could be simplified by changing it to:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">15</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span></span></span></code></pre></div><p>Because of course, if a number is a multiple of 3 and 5, then it must be a multiple of 15.</p>
<p>Here are a couple of nice alternatives (ideas courtesy of <a href="http://minus-zero.org/">Dave</a>, although re-written by me because we didn&rsquo;t save them). First, writing FizzBuzz as a iterator:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">fizzbuzz_generator</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">15</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="k">yield</span> <span class="s2">&#34;FizzBuzz&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="k">yield</span> <span class="s2">&#34;Fizz&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="k">yield</span> <span class="s2">&#34;Buzz&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="k">yield</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">fizzbuzz_generator</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span> <span class="nb">print</span> <span class="n">result</span></span></span></code></pre></div><p>Then, a simple function to calculate the correct fizz/buzz, combined with a list comprehension to create the result (man, I love list comprehensions):</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">fizzbuzz</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">15</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="s2">&#34;FizzBuzz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="s2">&#34;Fizz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">elif</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="s2">&#34;Buzz&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span> <span class="s2">&#34;</span><span class="se">\n</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">fizzbuzz</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">101</span><span class="p">)])</span></span></span></code></pre></div><p>Finally, here are two neat little Ruby one-liners, taken from the comments of the <a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/">original FizzBuzz article</a>. I hope the authors (<a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/#comment-2866">Brendan</a>, and <a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/#comment-2858">Brian</a> respectively) don&rsquo;t mind me reproducing them here:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ruby" data-lang="ruby"><span class="line"><span class="cl"><span class="nb">puts</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|</span><span class="p">(</span><span class="n">s</span><span class="o">=</span><span class="p">(</span><span class="n">i</span><span class="o">%</span><span class="mi">3</span><span class="o">==</span><span class="mi">0</span><span class="o">?</span><span class="s1">&#39;Fizz&#39;</span><span class="ss">:&#39;&#39;</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">i</span><span class="o">%</span><span class="mi">5</span><span class="o">==</span><span class="mi">0</span><span class="o">?</span><span class="s1">&#39;Buzz&#39;</span><span class="ss">:&#39;&#39;</span><span class="p">))</span><span class="o">==</span><span class="s1">&#39;&#39;</span><span class="sc">?i</span><span class="ss">:s</span><span class="p">}</span></span></span></code></pre></div>




<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ruby" data-lang="ruby"><span class="line"><span class="cl"><span class="nb">puts</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">100</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|</span><span class="n">i</span><span class="o">%</span><span class="mi">15</span><span class="o">==</span><span class="mi">0</span><span class="o">?</span><span class="s1">&#39;FizzBuzz&#39;</span><span class="ss">:i</span><span class="o">%</span><span class="mi">5</span><span class="o">==</span><span class="mi">0</span><span class="o">?</span><span class="s1">&#39;Buzz&#39;</span><span class="ss">:i</span><span class="o">%</span><span class="mi">3</span><span class="o">==</span><span class="mi">0</span><span class="o">?</span><span class="s1">&#39;Fizz&#39;</span><span class="ss">:i</span><span class="p">}</span></span></span></code></pre></div><p>How neat is that? Anyway, as I said, feel free to leave your own solutions below.</p>
]]></content:encoded></item><item><title>FizzBuzz and Other Problems</title><link>https://frosty.blog/2008/05/28/fizzbuzz-and-other-problems-2/</link><pubDate>Wed, 28 May 2008 23:00:00 +0000</pubDate><guid>https://frosty.blog/2008/05/28/fizzbuzz-and-other-problems-2/</guid><description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Check out the &lt;a href="http://www.jameswfrost.co.uk/2008/05/30/fizzbuzz-follow-up"&gt;follow-up&lt;/a&gt; post for some sample solutions!&lt;/p&gt;
&lt;p&gt;I love to code. Since a young age (I think I was about 7 when we first got our BBC B, and I would patiently tap BASIC code into the blinking prompt from childrens&amp;rsquo; books about programming), I&amp;rsquo;ve loved to write programs. I read somewhere recently (unfortunately, I forget where) that it&amp;rsquo;s not the act of coding itself we love, but rather it&amp;rsquo;s solving problems that we love. I think that&amp;rsquo;s one reason I like learning new programming languages, as it allows me to solve problems in different ways.&lt;/p&gt;</description><content:encoded><![CDATA[<p><strong>Update:</strong> Check out the <a href="http://www.jameswfrost.co.uk/2008/05/30/fizzbuzz-follow-up">follow-up</a> post for some sample solutions!</p>
<p>I love to code. Since a young age (I think I was about 7 when we first got our BBC B, and I would patiently tap BASIC code into the blinking prompt from childrens&rsquo; books about programming), I&rsquo;ve loved to write programs. I read somewhere recently (unfortunately, I forget where) that it&rsquo;s not the act of coding itself we love, but rather it&rsquo;s solving problems that we love. I think that&rsquo;s one reason I like learning new programming languages, as it allows me to solve problems in different ways.</p>
<p>A while back, I came upon the <a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/">FizzBuzz</a> idea. In short, it&rsquo;s a simple exercise for employers to give to potential employees at interviews; a very simple exercise, yet one that someone will only be able to answer quickly if they &lsquo;get&rsquo; programming. A suggested FizzBuzz problem is the following:</p>
<blockquote>
<p>Write a program that prints the numbers from 1 to 100. But for multiples of three print &lsquo;Fizz&rsquo; instead of the number and for the multiples of five print &lsquo;Buzz&rsquo;. For numbers which are multiples of both three and five print &lsquo;FizzBuzz&rsquo;.</p>
</blockquote>
<p>I invite everyone reading this to have a go now - time yourself, and see how quickly you can do it. Unfortunately, I only timed myself by looking at the clock, but I was glad to have completed it in under a minute. Apparently this shows I can code:</p>
<blockquote>
<p>Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.</p>
<p>Want to know something scary? - the majority of comp-sci graduates can&rsquo;t. I&rsquo;ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.</p>
</blockquote>
<p>That is a scary thought, but I expect it&rsquo;s almost certainly true.</p>
<p>So, this sort of simple problem excites me - I can work out a solution, and then try and improve that, perhaps. Myself and <a href="http://minus-zero.org/">Dave</a> spent a fair while earlier today trying to come up with more and more sophisticated solutions. And y&rsquo;know what? It was fun!</p>
<p>On a similar note, I thought I&rsquo;d mention a website that I found a while back (thanks to Matt Gwynne) that might be of interest to anybody else who enjoys these kind of problems. <a href="http://projecteuler.net/">Project Euler</a> is &ldquo;a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve&rdquo;. It contains a list of (at the time of writing) 195 mathematical problems, and the aim is to write code to solve them. You can then put in your answers, the website will tell you if you&rsquo;re correct, and check them off the list. Great fun, and it&rsquo;s neat to see some of the interesting solutions that people come up with (and the range of languages they use!). Hooray for being a geek! \o/</p>
]]></content:encoded></item></channel></rss>