<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Codewar</title>
	<atom:link href="http://www.arbitraryuser.com/blog/2008/06/25/codewar/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/</link>
	<description>Jonathan Endersby, Recovering Technologist</description>
	<lastBuildDate>Thu, 11 Mar 2010 11:31:57 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Cyclone</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9931</link>
		<dc:creator>Cyclone</dc:creator>
		<pubDate>Thu, 26 Jun 2008 13:17:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9931</guid>
		<description>Here is another php version

function uniqRand($count=6, $from=1, $to=10, $maxInstances=1){
  $count=abs($count);
  $from=abs($from);
  $to=abs($to);
  $maxInstances=abs($maxInstances);
  $extracted=array();
  if(($to-$from+1)*$maxInstances=0){
    $r=mt_rand($from, $to);
	if(!isset($extracted[$r]) &#124;&#124; $extracted[$r]&lt;$maxInstances){$output[]=$r;}
	else{++$count;};
    ++$extracted[$r];
  };
  return $output;
}
</description>
		<content:encoded><![CDATA[<p>Here is another php version</p>
<p>function uniqRand($count=6, $from=1, $to=10, $maxInstances=1){<br />
  $count=abs($count);<br />
  $from=abs($from);<br />
  $to=abs($to);<br />
  $maxInstances=abs($maxInstances);<br />
  $extracted=array();<br />
  if(($to-$from+1)*$maxInstances=0){<br />
    $r=mt_rand($from, $to);<br />
	if(!isset($extracted[$r]) || $extracted[$r]&lt;$maxInstances){$output[]=$r;}<br />
	else{++$count;};<br />
    ++$extracted[$r];<br />
  };<br />
  return $output;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wynand Karsten</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9930</link>
		<dc:creator>Wynand Karsten</dc:creator>
		<pubDate>Thu, 26 Jun 2008 13:06:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9930</guid>
		<description>Brilliant. I&#039;m no programmer, but the concepts and ideas was nice to see in print (Nothing to do with the code).</description>
		<content:encoded><![CDATA[<p>Brilliant. I&#8217;m no programmer, but the concepts and ideas was nice to see in print (Nothing to do with the code).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jerith</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9904</link>
		<dc:creator>jerith</dc:creator>
		<pubDate>Wed, 25 Jun 2008 11:02:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9904</guid>
		<description>On education:

Your goal, when teaching programming, is to teach programming. You should not be teaching a specific language (although you need a language to teach in, the distinction is subtle but important) and you should not be teaching particular techniques except where they are important more generally.

With this in mind, you want a language that gets out of your way when you want it to but is flexible enough to demonstrate advanced concepts when necessary. Obrant: Java is not this language. Neither is Delphi. Python&#039;s a reasonable choice. (Yes, this is a dig at the local education department.)

With this in mind, you probably don&#039;t want to be teaching the One Right Way To Do It for pretty much anything. What methods you teach should depend on what concepts you are teaching. In this case, if the selection is incidental to the lesson, use the standard library function that does the hard work for you. If the implementation is central to the lesson for some reason, you need to cover that.

It is fairly important, in my opinion, to be able to ignore anything that isn&#039;t central to the concept being taught. This will never be universally possible, but it is far easier in some cases than in others. To use an example I&#039;m rather fond of, consider a student&#039;s first introduction to a programming language.

In Python:
&lt;pre&gt;
print &quot;Hello World!&quot;
&lt;/pre&gt;
In Pascal:
&lt;pre&gt;
program HelloWorld;
begin
    writeln(&#039;Hello world!&#039;);
end.
&lt;/pre&gt;

In Java:
&lt;pre&gt;
class HelloWorld {
    public static void main(String args[]) {
        System.out.println(&quot;Hello World!&quot;);
    }
}
&lt;/pre&gt;
The first two are fine. Neither contain more concepts than &quot;this is a program&quot; and &quot;we want to print &#039;Hello World!&#039;&quot;. Java, on the other hand, requires either explanation or glossing-over of classes, public/private methods, static methods, functions and parameters. All of these are important, to be sure, but the student should not have to deal with them all at once.

In conclusion, you need balance. Teach what it makes sense to teach. You don&#039;t want to gloss over everything, but neither do you want to dive immediately into the details of everything.

I should actually turn this into a post on my blog. Prod me with something appropriately pointy if I forget...</description>
		<content:encoded><![CDATA[<p>On education:</p>
<p>Your goal, when teaching programming, is to teach programming. You should not be teaching a specific language (although you need a language to teach in, the distinction is subtle but important) and you should not be teaching particular techniques except where they are important more generally.</p>
<p>With this in mind, you want a language that gets out of your way when you want it to but is flexible enough to demonstrate advanced concepts when necessary. Obrant: Java is not this language. Neither is Delphi. Python&#8217;s a reasonable choice. (Yes, this is a dig at the local education department.)</p>
<p>With this in mind, you probably don&#8217;t want to be teaching the One Right Way To Do It for pretty much anything. What methods you teach should depend on what concepts you are teaching. In this case, if the selection is incidental to the lesson, use the standard library function that does the hard work for you. If the implementation is central to the lesson for some reason, you need to cover that.</p>
<p>It is fairly important, in my opinion, to be able to ignore anything that isn&#8217;t central to the concept being taught. This will never be universally possible, but it is far easier in some cases than in others. To use an example I&#8217;m rather fond of, consider a student&#8217;s first introduction to a programming language.</p>
<p>In Python:</p>
<pre>
print "Hello World!"
</pre>
<p>In Pascal:</p>
<pre>
program HelloWorld;
begin
    writeln('Hello world!');
end.
</pre>
<p>In Java:</p>
<pre>
class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}
</pre>
<p>The first two are fine. Neither contain more concepts than &#8220;this is a program&#8221; and &#8220;we want to print &#8216;Hello World!&#8217;&#8221;. Java, on the other hand, requires either explanation or glossing-over of classes, public/private methods, static methods, functions and parameters. All of these are important, to be sure, but the student should not have to deal with them all at once.</p>
<p>In conclusion, you need balance. Teach what it makes sense to teach. You don&#8217;t want to gloss over everything, but neither do you want to dive immediately into the details of everything.</p>
<p>I should actually turn this into a post on my blog. Prod me with something appropriately pointy if I forget&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Hitchcock</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9903</link>
		<dc:creator>Jonathan Hitchcock</dc:creator>
		<pubDate>Wed, 25 Jun 2008 10:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9903</guid>
		<description>So, we have three basic methods for doing it so far:

1. Keep picking random numbers, discarding duplicates, until we have as many as we need.

2. Generate a list of all possible numbers, and throw away random ones until you have as many as you need.

3. Generate a list of all possible numbers, shuffle it, and throw away the first/last however many you don&#039;t need.

Oh, four:  4.  Use the builtin function to do what we need.

This is what I&#039;d explain to the kid I was teaching to code, rather than showing him array_splice()s and samples().</description>
		<content:encoded><![CDATA[<p>So, we have three basic methods for doing it so far:</p>
<p>1. Keep picking random numbers, discarding duplicates, until we have as many as we need.</p>
<p>2. Generate a list of all possible numbers, and throw away random ones until you have as many as you need.</p>
<p>3. Generate a list of all possible numbers, shuffle it, and throw away the first/last however many you don&#8217;t need.</p>
<p>Oh, four:  4.  Use the builtin function to do what we need.</p>
<p>This is what I&#8217;d explain to the kid I was teaching to code, rather than showing him array_splice()s and samples().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9902</link>
		<dc:creator>Brad</dc:creator>
		<pubDate>Wed, 25 Jun 2008 10:06:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9902</guid>
		<description>Another PHP solution, using only array functions:

$nums = array_keys(array_pad(array(),10,&#039;nom&#039;));
array_shift($nums);
shuffle($nums);
$nums = array_splice($nums,0,6);
print(implode(&#039; &#039;,$nums));
</description>
		<content:encoded><![CDATA[<p>Another PHP solution, using only array functions:</p>
<p>$nums = array_keys(array_pad(array(),10,&#8217;nom&#8217;));<br />
array_shift($nums);<br />
shuffle($nums);<br />
$nums = array_splice($nums,0,6);<br />
print(implode(&#8217; &#8216;,$nums));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jerith</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9899</link>
		<dc:creator>jerith</dc:creator>
		<pubDate>Wed, 25 Jun 2008 09:18:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9899</guid>
		<description>Good thing my Firefox crashed halfway through my duplication of Michael&#039;s solution, thus forcing a reload and giving me this most excellent comment thread.

I shall address the education question when I have more time to consider it, but here is the Erlang implementation you asked for. I specifically chose a method not yet used here:

&lt;pre&gt;uniquerandoms(Min, Max, Count) -&gt;
    ShuffledList = shuffle(lists:seq(Min, Max)),
    lists:sublist(ShuffledList, Count).

%% The shuffling implementation below was found on the interwebs:
%% http://www.trapexit.org/RandomShuffle
shuffle(List) -&gt;
    %% Determine the log n portion then randomize the list.
    randomize(round(math:log(length(List)) + 0.5), List).

randomize(1, List) -&gt;
    randomize(List);
randomize(T, List) -&gt;
    lists:foldl(fun(_E, Acc) -&gt;
                        randomize(Acc)
                end, randomize(List), lists:seq(1, (T - 1))).

randomize(List) -&gt;
    D = lists:map(fun(A) -&gt;
                          {random:uniform(), A}
                  end, List),
    {_, D1} = lists:unzip(lists:keysort(1, D)), 
    D1.&lt;/pre&gt;

Printing out the answer is left as an exercise for the reader.</description>
		<content:encoded><![CDATA[<p>Good thing my Firefox crashed halfway through my duplication of Michael&#8217;s solution, thus forcing a reload and giving me this most excellent comment thread.</p>
<p>I shall address the education question when I have more time to consider it, but here is the Erlang implementation you asked for. I specifically chose a method not yet used here:</p>
<pre>uniquerandoms(Min, Max, Count) -&gt;
    ShuffledList = shuffle(lists:seq(Min, Max)),
    lists:sublist(ShuffledList, Count).

%% The shuffling implementation below was found on the interwebs:
%% <a href="http://www.trapexit.org/RandomShuffle" rel="nofollow">http://www.trapexit.org/RandomShuffle</a>
shuffle(List) -&gt;
    %% Determine the log n portion then randomize the list.
    randomize(round(math:log(length(List)) + 0.5), List).

randomize(1, List) -&gt;
    randomize(List);
randomize(T, List) -&gt;
    lists:foldl(fun(_E, Acc) -&gt;
                        randomize(Acc)
                end, randomize(List), lists:seq(1, (T - 1))).

randomize(List) -&gt;
    D = lists:map(fun(A) -&gt;
                          {random:uniform(), A}
                  end, List),
    {_, D1} = lists:unzip(lists:keysort(1, D)),
    D1.</pre>
<p>Printing out the answer is left as an exercise for the reader.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9898</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Wed, 25 Jun 2008 09:08:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9898</guid>
		<description>Oh, and &quot; &quot;.join(...) is equivalent to implode(&#039; &#039;, ...).</description>
		<content:encoded><![CDATA[<p>Oh, and &#8221; &#8220;.join(&#8230;) is equivalent to implode(&#8217; &#8216;, &#8230;).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9897</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Wed, 25 Jun 2008 09:07:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9897</guid>
		<description>I&#039;d say random.sample() is easier to understand than a couple of loops, nested or otherwise :)

It&#039;s still useful to know how to do it if your language doesn&#039;t have a random.sample() equivalent.</description>
		<content:encoded><![CDATA[<p>I&#8217;d say random.sample() is easier to understand than a couple of loops, nested or otherwise :)</p>
<p>It&#8217;s still useful to know how to do it if your language doesn&#8217;t have a random.sample() equivalent.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9895</link>
		<dc:creator>Brad</dc:creator>
		<pubDate>Wed, 25 Jun 2008 09:06:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9895</guid>
		<description>My solution also wins because it includes nomnomnomnomnomnom</description>
		<content:encoded><![CDATA[<p>My solution also wins because it includes nomnomnomnomnomnom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad</title>
		<link>http://www.arbitraryuser.com/blog/2008/06/25/codewar/comment-page-1/#comment-9894</link>
		<dc:creator>Brad</dc:creator>
		<pubDate>Wed, 25 Jun 2008 08:58:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.arbitraryuser.com/blog/?p=257#comment-9894</guid>
		<description>Not the most efficient, but a short php solution:

$fu = array();
while (sizeof($fu)&lt;6) {
 $fu[rand(1,10)] = &#039;nom&#039;;
}
print_r(array_keys($fu));
</description>
		<content:encoded><![CDATA[<p>Not the most efficient, but a short php solution:</p>
<p>$fu = array();<br />
while (sizeof($fu)&lt;6) {<br />
 $fu[rand(1,10)] = &#8216;nom&#8217;;<br />
}<br />
print_r(array_keys($fu));</p>
]]></content:encoded>
	</item>
</channel>
</rss>
