<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>OJ's rants &#187; Challenges</title>
	<atom:link href="http://buffered.io/category/challenges/feed/" rel="self" type="application/rss+xml" />
	<link>http://buffered.io</link>
	<description>What would OJ do?</description>
	<lastBuildDate>Tue, 06 Jul 2010 20:32:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Another Quick Coding Challenge</title>
		<link>http://buffered.io/2008/07/25/another-quick-coding-challenge/</link>
		<comments>http://buffered.io/2008/07/25/another-quick-coding-challenge/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 18:46:35 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=406</guid>
		<description><![CDATA[I was about to head to bed when I stumbled across another interesting coding challenge. Since I had another half hour or so to kill I thought I'd give it a shot! The challenge was first posted here. The problem is as follows: Write a counter function that counts from 1 to max but only [...]]]></description>
			<content:encoded><![CDATA[<p>I was about to head to bed when I stumbled across another interesting coding challenge. Since I had another half hour or so to kill I thought I'd give it a shot! </p>
<p>The challenge was first posted <a href="http://beust.com/weblog/archives/000491.html">here</a>. The problem is as follows:</p>
<blockquote><p>Write a counter function that counts from 1 to max but only returns numbers whose digits don't repeat.</p>
<p>For example, part of the output would be:</p>
<p>    * 8, 9, 10, 12 (11 is not valid)<br />
    * 98, 102, 103 (99, 100 and 101 are not valid)<br />
    * 5432, 5436, 5437 (5433, 5434 and 5435 are not valid)</p>
<p>Also:</p>
<p>    * Display the biggest jump (in the sequences above, it's 4: 98 -&gt; 102)<br />
    * Display the total count of numbers<br />
    * Give these two values for max=10000
</p>
</blockquote>
<p>So without further ado, here's my solution:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List
&nbsp;
noDups <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">String</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span>
noDups l <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">length</span> l <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">length</span> <span style="color: green;">&#40;</span>nub l<span style="color: green;">&#41;</span>
&nbsp;
items <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
items <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span>noDups <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span>10000<span style="color: green;">&#93;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">maximum</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">zipWith</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> items<span style="color: green;">&#41;</span> items<span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="font-weight: bold;">length</span> items<span style="color: green;">&#41;</span></pre></div></div>

<p>It's probably not as concise as it could be, and I'm sure a more experienced Haskeller would be able to tidy it up a little bit. Despite that, it still performs quite well:</p>
<pre>Thu Jul 24 21:34 2008 Time and Allocation Profiling Report  (Final)

	   main +RTS -p -RTS

	total time  =        0.00 secs   (0 ticks @ 20 ms)
	total alloc =   4,121,500 bytes  (excludes profiling overheads)

COST CENTRE   MODULE   %time %alloc

CAF           Main       0.0  100.0

                                            individual    inherited
COST CENTRE   MODULE       no.    entries  %time %alloc   %time %alloc

MAIN          MAIN           1           0   0.0    0.0     0.0  100.0
 CAF          Main         152          14   0.0  100.0     0.0  100.0
 CAF          GHC.Handle    88           4   0.0    0.0     0.0    0.0</pre>
<p>Thoughts? How could I make this better/faster/more elegant? I'm keen to get some insight from a more experienced Haskell coder <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/25/another-quick-coding-challenge/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Validating use of Parenthesis</title>
		<link>http://buffered.io/2008/07/24/validating-use-of-parenthesis/</link>
		<comments>http://buffered.io/2008/07/24/validating-use-of-parenthesis/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 23:11:18 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[fsharp]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=394</guid>
		<description><![CDATA[Yet another programming challenge appeared on dev102 the other day, and I thought that this time I'd post my solution here in the blog rather than letting it get lost in the depths of the comment thread! The problem is as follows: Your input is a string which is composed from bracket characters. The allowed [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another <a href="http://www.dev102.com/2008/07/21/a-programming-job-interview-challenge-13-brackets/" title="Programming Challenge">programming challenge appeared on dev102</a> the other day, and I thought that this time I'd post my solution here in the blog rather than letting it get lost in the depths of the comment thread! </p>
<p>The problem is as follows:<br />
<blockquote>
<p>Your input is a string which is composed from bracket characters. The allowed characters are:’(', ‘)’, ‘[', ']‘, ‘{’, ‘}’, ‘&lt;’ and ‘&gt;’. Your mission is to determine whether the brackets structure is legal or not.</p>
<p>Example of a legal expression: “([](&lt;{}&gt;))”.</p>
<p>Example of an illegal expression: “({&lt;)&gt;}”.</p>
</blockquote>
<p>Is what I'm about to show the most efficient? No. Is it the most elegant? Hell no! But it works <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>It's surprisingly similar to <a href="http://www.fsharp.it/2008/07/22/balanced-parenthesis/" title="Balanced Parenthesis">this solution</a> over at Fsharp.it. It is different in that it allows non-bracket characters to be entered into the string as well.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;">#light
&nbsp;
let parens = [| ('(',')');('[',']');('{','}');('&lt;','&gt;') |]
let isOpen l = Array.exists (fun(o,c) -&gt; o = l) parens
let isClose l = Array.exists (fun(o,c) -&gt; c = l) parens
let isPair p = Array.exists (fun l -&gt; p = l) parens
&nbsp;
let validate inp =
  let rec v' str stack =
    match str with
    | [] -&gt; stack = []
    | c :: cs when isOpen c -&gt; v' cs (c :: stack)
    | c :: cs when isClose c -&gt;
        if isPair ((List.hd stack), c)
            then v' cs (List.tl stack)
            else false
    | c :: cs -&gt; v' cs stack
  v' (List.of_seq inp) []</pre></div></div>

<p>Call the function like so:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;">validate &quot;thisIsAFunction(int[] foo, delegate{}, bar()); // &lt; testing &gt;&quot;</pre></div></div>

<p>In short, the function uses an internal "stack" (which is actually a list) to keep track of open brackets. When a closed bracket is found, it's validated against the open bracket at the top of the stack.</p>
<p>Fairly simple stuff. There are optimisations that can be made around the searching for items in the <em>parens</em> array, but I couldn't be bothered changing it <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> For me at the moment it's more about playing with F#.</p>
<p>Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/24/validating-use-of-parenthesis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An Interesting Little Problem</title>
		<link>http://buffered.io/2008/06/14/an-interesting-little-problem/</link>
		<comments>http://buffered.io/2008/06/14/an-interesting-little-problem/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 10:39:38 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=365</guid>
		<description><![CDATA[This post was inspired by a recent interview question that was posted over at fsharp.it. It's one of those neat little questions which looks really simple on the surface but is quite tricky. The question apparently originates from an interview that someone had with Google, and goes something like this: There is an array A[N] [...]]]></description>
			<content:encoded><![CDATA[<p>This post was inspired by a recent interview question that was posted over at <a href="http://www.fsharp.it/2008/06/10/google-interview-question-product-of-other-elements-in-an-array-in-on/" title="Fsharp.it">fsharp.it</a>. It's one of those neat little questions which looks really simple on the surface but is quite tricky. </p>
<p>The question apparently originates from an interview that someone had with Google, and goes something like this:<br />
<blockquote>
<p>
There is an array A[N] of N integers. You have to compose an array Output[N] such that Output[i] will be equal to the product of all the elements of A[] except A[i].</p>
<p>Example:<br />
&nbsp;&nbsp;&nbsp;&nbsp;INPUT:[4, 3, 2, 1, 2]<br />
&nbsp;&nbsp;&nbsp;&nbsp;OUTPUT:[12, 16, 24, 48, 24]</p>
<p><strong>Note:</strong> Solve it <em>without</em> the division operator and in O(n).
</p>
</blockquote>
<p>Since I had a spare 10 minutes, I decided to give it a shot ... in Haskell.</p>
<p>I'll cut to the chase, here's the source to my solution:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">vals <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
vals <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span> <span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span> <span style="color: green;">&#93;</span>
&nbsp;
answers <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
answers <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span> front <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">*</span> back <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span>1<span style="color: #339933; font-weight: bold;">..</span><span style="font-weight: bold;">length</span> vals<span style="color: green;">&#93;</span> <span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span>
    front <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">scanl1</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> temp
    back <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">scanr1</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> temp
    temp <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> vals <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: green;">&#93;</span></pre></div></div>

<p>I'm hoping that this is quite self-explanatory. But in case it's not, I'll cover some of the gory bits.</p>
<p>The core of the problem is coming up with a way of determining the value of the product of numbers from the start of the list up to a given index, and to do the same at the other end of the list from that given index.</p>
<p>I thought that the easiest way would be to create two lists: both of them containing the compounded products of the numbers in the list, but each of them in different directions. To generate those lists, I thought that I'd add the value of 1 to the list, both at the start and at the end, as it would allow me to do two things:
<ol>
<li>Generate the lists using the <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Ascanl1" title="scanl1">scanl1</a> and scanr1 functions.</li>
<li>Index into the list using a counter that's based on the size of the original values without having to worry about going past the bounds of the list.</li>
</ol>
<p>Yup, quite lazy, but very handy.</p>
<p>Here's the output when I execute <em>answers</em> in <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci.html" title="GHCi">GHCi</a>:</p>
<pre lang="">Prelude> :l google.hs
[1 of 1] Compiling Main             ( google.hs, interpreted )
Ok, modules loaded: Main.
*Main> answers
[12,16,24,48,24]
*Main>
</pre>
<p>Problem solved in O(n). Neato! After feeling rather chuffed with myself I thought I'd go back to Fsharp.it and check out the answer posted there. The principle was similar, but the implementation listed was a little longer.</p>
<p>So I thought I'd have a go at writing up my solution using F#. It didn't seem like a stretch until I realised how little of the language I know (I'm currently reading through <a href="http://www.amazon.com/Expert-F-Experts-Voice-Net/dp/1590598504" title="Expert F#">Expert F#</a>, but I'm still far from being one myself). Here's what I came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #a52a2a;">#</span>light
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> vals <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span> <span style="color: #c6c;">4</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">3</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">1</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2</span><span style="color: #a52a2a;">;</span> <span style="color: #6c6;">&#93;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> mul a b <span style="color: #a52a2a;">=</span> a <span style="color: #a52a2a;">*</span> b
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">++</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">append</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> answers <span style="color: #a52a2a;">=</span>
  <span style="color: #06c; font-weight: bold;">let</span> temp <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">++</span> vals <span style="color: #a52a2a;">++</span> <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">let</span> front <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">scan1_left</span> mul temp <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">to_array</span>
  <span style="color: #06c; font-weight: bold;">let</span> back <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">scan1_right</span> mul temp <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">to_array</span>
  seq <span style="color: #6c6;">&#123;</span> <span style="color: #06c; font-weight: bold;">for</span> x <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span> <span style="color: #a52a2a;">..</span> <span style="color: #060;">vals</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Length</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> front<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>x<span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">*</span> back<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>x<span style="color: #a52a2a;">+</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span> <span style="color: #6c6;">&#125;</span></pre></div></div>

<p>A few things you might notice:
<ol>
<li>My syntax highlighter plugin doesn't currently support F# <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </li>
<li>I use a similar method to the Haskell solution, but ended up having to convert the <em>front</em> and <em>back</em> lists to arrays. The reason was because I need to be able to index into the resulting integer set, and I can't do that with lists (if I'm wrong, please let me know!)</li>
<li>I defined a function called <em>mul</em> which does a simple multiplication. I wanted to pass <em>(*)</em> as the first parameter to the scan1_* functions, but the interpreter took that as the start of a comment instead! So I had to resort to a dodgey hack. If you know a way around this, please let me know.</li>
<li>I wrote my own (++) operator because I didn't want to have to write List.append more than once <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </li>
</ol>
<p>In other words, my F# version smells like n00b. I'm sure there are so many better ways to implement this using the built-in features of the language and supporting libraries, but I'm yet to get to the level when I can write it. I'd love for someone to show me how <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>I did enjoy having a dabble with F# for the first time in ages, though I have to admit I much prefer using <a href="http://www.vim.org/" title="VIM">VIM</a> and <a href="http://research.microsoft.com/fsharp/manual/compiler.aspx" title="F# Interactive">fsi.exe</a> instead of <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx" title="Visual Studio">Visual Studio</a> and the <a href="http://blogs.msdn.com/dsyme/archive/2006/02/19/534925.aspx" title="A Taste of F# Interactive in Visual Studio">interactive F# add-in</a>.</p>
<p>As always, feedback and criticism welcomed (and needed).</p>
<h2>Update</h2>
<p>After some great feedback (see below), I've come to realise that the !! operator in Haskell is actually O(n) itself. Hence it was a bad choice for inclusion. Back to the drawing board for me!</p>
<p>Here are a couple of submitted Haskell solutions.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- by lf</span>
scanm f z xs <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">zipWith</span> f <span style="color: green;">&#40;</span><span style="font-weight: bold;">scanl</span> f z xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">scanr</span> f z xs<span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> scanm <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#91;</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- by Henning</span>
answers <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
answers vals <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">zipWith</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> front <span style="color: green;">&#40;</span><span style="font-weight: bold;">drop</span> <span style="color: red;">1</span> back<span style="color: green;">&#41;</span>
	<span style="color: #06c; font-weight: bold;">where</span>
	front <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">scanl</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> vals
	back <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">scanr</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> vals
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- by desp</span>
problem input <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">zipWith</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> front back <span style="color: #06c; font-weight: bold;">where</span>
  front <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">init</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">scanl</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> input<span style="color: green;">&#41;</span>
  back <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">tail</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">scanr</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> input<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- by foobar</span>
foo <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span>
foo <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> acc <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> m<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> foo xs <span style="color: green;">&#40;</span>acc<span style="color: #339933; font-weight: bold;">*</span>x<span style="color: green;">&#41;</span>
                 <span style="color: #06c; font-weight: bold;">in</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>m<span style="color: #339933; font-weight: bold;">*</span>acc<span style="color: green;">&#41;</span>:l <span style="color: #339933; font-weight: bold;">,</span> m<span style="color: #339933; font-weight: bold;">*</span>x<span style="color: green;">&#41;</span></pre></div></div>

<p>Thanks for the submissions guys <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Sorry for not including the imperative versions in the update.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/06/14/an-interesting-little-problem/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
		<item>
		<title>More RCE Goodness</title>
		<link>http://buffered.io/2007/03/06/more-rce-goodness/</link>
		<comments>http://buffered.io/2007/03/06/more-rce-goodness/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 02:45:19 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[RCE]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/03/06/more-rce-goodness/</guid>
		<description><![CDATA[I'm quite excited. My previous RCE solution and tutorial has been approved! Plus, the first keygenme that I've ever written is now approved and available for download. So if you're up for a 64-bit RCE challenge, go check it out (or download it directly from here). Feel free to let me know how good/bad/ugly you [...]]]></description>
			<content:encoded><![CDATA[<p>I'm quite excited.  My previous RCE solution and tutorial has been <a href="http://www.crackmes.de/users/cyclops/nts_crackme10/" title="My Solution" target="_blank">approved</a>!</p>
<p>Plus, the first keygenme that I've ever written is now <a href="http://www.crackmes.de/users/thecolonial/thecolonials_keygenme01_x64/" title="My Keygenme" target="_blank">approved and available for download</a>.  So if you're up for a 64-bit RCE challenge, go check it out (or download it directly from <a href="http://buffered.io/wp-content/uploads/2007/03/thecolonial_keygenme01_winx64.zip" title="My Keygenme">here</a>).</p>
<p>Feel free to let me know how good/bad/ugly you think this stuff is!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/03/06/more-rce-goodness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Challenge #3 &#8211; Day of Birth</title>
		<link>http://buffered.io/2007/02/04/challenge-3-day-of-birth/</link>
		<comments>http://buffered.io/2007/02/04/challenge-3-day-of-birth/#comments</comments>
		<pubDate>Sat, 03 Feb 2007 16:02:46 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/02/04/challenge-3-day-of-birth/</guid>
		<description><![CDATA[I haven't posted a challenge for a while, so I thought I'd get you all thinking again with another basic little program. It's simple, but there are a stack of possible solutions, so I'm expecting a bit of variety. So here we go. Write a program that asks the user for their date of birth [...]]]></description>
			<content:encoded><![CDATA[<p>I haven't posted a challenge for a while, so I thought I'd get you all thinking again with another basic little program.  It's simple, but there are a stack of possible solutions, so I'm expecting a bit of variety. So here we go.</p>
<p>Write a program that asks the user for their date of birth using the date format YYYY-MM-DD (eg. 1978-12-05 in my case). The program should respond by telling the user what <em>day of the week</em> they were born on.</p>
<p>You're allowed to use any language you want, but you're <strong>not</strong> allowed to use the built-in date object functionality other than to determine the current system date. That is, you can't date difference functions to determine time that's passed. You have to figure that out for yourself.</p>
<p>For those C# coders, don't even <em>think</em> about posting: <em>dateOfBirth.Day</em> <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> You're supposed to do the hard bit yourself</p>
<p>Anyway, it's easy peasy <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' />  Don't forget about leap years and stuff! I look forward to seeing your answers. I'll post up a solution in a week or so. Let me know if I haven't been clear.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/02/04/challenge-3-day-of-birth/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Challenge #2 &#8211; The Recursive Ruler</title>
		<link>http://buffered.io/2006/10/12/challenge-2-the-recursive-ruler/</link>
		<comments>http://buffered.io/2006/10/12/challenge-2-the-recursive-ruler/#comments</comments>
		<pubDate>Thu, 12 Oct 2006 13:18:25 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>

		<guid isPermaLink="false">http://buffered.io/2006/10/12/challenge-2-the-recursive-ruler/</guid>
		<description><![CDATA[I posted this problem a little while ago on the site I used to run, and it proved to be an interesting one that resulted in quite a large collection of solutions from the posters. So here it is again. Your job is to write a program that generates a text-based ruler. For a clearer [...]]]></description>
			<content:encoded><![CDATA[<p>I posted this problem a little while ago on the site I used to run, and it proved to be an interesting one that resulted in quite a large collection of solutions from the posters. So here it is again.</p>
<p>Your job is to write a program that generates a text-based ruler. For a clearer definition of what I mean, see the little text pic below:</p>
<pre>|                               |                               |
|               |               |               |               |
|       |       |       |       |       |       |       |       |
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</pre>
<p>The user of the program must be able to specify a couple of parameters: those being the <em>height</em> of the ruler and the <em>number of sections</em> in the ruler (at the top level).  In the above example, the values would be <strong>6</strong> and <strong>2</strong> respectively.  Another example, using the values <strong>3</strong> and <strong>3</strong> would result in:</p>
<pre>|   |   |   |
| | | | | | |
|||||||||||||</pre>
<p>The only other thing that you have to do is make sure that your solution uses <strong><u>recursion</u></strong> instead of iteration.  Feel free to use any language you want, you can write to screen or to file, it's your call. But you <strong>must</strong> use recursion.</p>
<p>I look forward to seeing your responses. <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2006/10/12/challenge-2-the-recursive-ruler/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Who&#8217;s up for a challenge?</title>
		<link>http://buffered.io/2006/09/15/whos-up-for-a-challenge/</link>
		<comments>http://buffered.io/2006/09/15/whos-up-for-a-challenge/#comments</comments>
		<pubDate>Fri, 15 Sep 2006 05:58:53 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=9</guid>
		<description><![CDATA[Before starting this blog up, I spent a bit of time thinking about the kind of things I would like to talk about which other people might find interesting. I also wanted to find some areas of discussion where people would be interested in giving me feedback and comments containing their thoughts and opinions so [...]]]></description>
			<content:encoded><![CDATA[<p>Before starting this blog up, I spent a bit of time thinking about the kind of things I would like to talk about which other people might find interesting. I also wanted to find some areas of discussion where people would be interested in giving me feedback and comments containing their thoughts and opinions so that I can learn something and possibly benefit from other people's experience. While this is all very enlightening, it can be fun and it can be, shall we say, political.</p>
<p>So as well as having lots of industry-specific rants, posts on what's considered good and bad, and large chunks of text relating to experiences while out on site working for clients, I thought it'd be a good thing to post some other bits and pieces that are a bit more light hearted, and dare I say it... FUN!</p>
<p>Way back before I went to the U.K. I used to have a site up and running that had a small community of people interested in programming - specifically game development. While this site is long gone, never to be remembered, there were a few things that we used to do there which I thought were really good. One of which was listing programming challenges. Every now and then I'd come up with (or rip off) a programming challenge that I thought would be a good exercise for members of the community to undertake. It was surprising how many people were interested, and we ended up with many responses and submissions containing solutions to the problems.</p>
<p>The beauty of it was that people learned a lot from the exercise - especially from each other's varied approaches. They would come up with a solution and be eager to post it. After noticing that someone else had created a "better" solution than them, members would go away and try and improve what they had. It was great, as it bred a mindset of iteration and improvement, and over time most of the community members ended up being comfortable with the idea that their first solution generally isn't perfect and they immediately look to make it better.</p>
<p>I'm of the view that the first solution that you write to solve a given problem is designed to give you a better understanding of the problem space. Sure, it's not always the case that you can throw away the code you've written (depending on what you're working on, and what timelines you have to adhere to), but if you can it's a good idea to do it and start again fresh with the knowledge you've gained from your first attempt.</p>
<p>I would like to get this type of thing going again. I'm going to get some ideas together for problems and start posting them here for other people to have a stab at. I'll be giving some of them a go as well, and if people are interested in sharing their solutions and discussing them, they can post their solutions as comments. If the idea becomes popular and the number of submissions goes up, then I may think about setting up a basic <em>Coding Challenges</em> forum for the discussion and submission of the problems and solutions. If people don't like it, I'll probably continue to post them anyway just in case someone at some stage finds them as interesting as I do <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> If anyone's interested in seeing my solution, then I'd be happy to post it.</p>
<p>The problems themselves will most likely be small. I won't be suggesting that people should go away and write a 3D rendering engine just to see the frames-per-second they can achieve on a 386! The problems will have a very small scope - meaning that solutions can be built quickly, and people won't feel that they need to put a massive amount of time in to get the results required. Hopefully they'll be quite challenging despite being small in stature.</p>
<p>One thing I will attempt to do when writing these problems up is make sure that the specification forces the programmer to do as much of the work as possible - without relying on the supporting framework or library set that comes with the language. In general the problems will not have a target language (or language set), but there may be cases where I state that the problem is intended to be solved using C++ or Miranda!</p>
<p>I hope that you'll find the challenges interesting, and that you'll perhaps find the time to give a few of them a go to exercise the mind. I think some of them will be more useful to beginners learning the ropes of development, but I can't see why experienced programmers shouldn't give them a shot. There's always the possibility that we'll learn <em>something</em>.</p>
<p>So let's begin with the first one that's popped into my head. It's a bit of a classic problem, but there's still a chance that people out there might not have heard it before - or more importantly attempted to solve it themselves in code. I first heard about this problem through a friend of a friend of a friend of a ... you get the idea <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>
<p><strong><u>Challenge #1 - Linked List Loops</u></strong></p>
<blockquote><p>"What do you think is the most efficient and resource-friendly way to determine if a singly-linked-list contains a loop?"</p></blockquote>
<p>When you have an instance of a linked list, it is usually <strong>NULL-terminated</strong>, that is, the last reference is a NULL reference which tells you that the end of the list has been found. The question asks how you determine if the list is <em>not</em> NULL-terminated, instead the "last" item in the list points back to another item in the list (thus creating a loop in the list).</p>
<p>To solve this problem you will need to have an understanding of <a href="http://en.wikipedia.org/wiki/Linked_list#Singly-linked_list" title="Singly-linked list" target="_blank">Singly-linked lists</a>. You don't need to write code, I'm just interested in the algorithm you'd use.</p>
<p>Sounds easy, and there are quite a few ways of doing it. So give it a shot! I'm interested to see what you guys come up with.</p>
<p>Happy problem solving!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2006/09/15/whos-up-for-a-challenge/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
