<?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>Algorithm Blogs &#187; Programming</title>
	<atom:link href="http://www.algorithm.co.il/blogs/index.php/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 22 Apr 2010 21:04:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>One-liner Guitar Tuner in Python</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/one-liner-guitar-tuner-in-python/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/one-liner-guitar-tuner-in-python/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:04:32 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[guitar]]></category>
		<category><![CDATA[guitar tuner]]></category>
		<category><![CDATA[one-liner]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=611</guid>
		<description><![CDATA[
On windows, assuming imports are free:


import winsound
winsound.Beep&#40;220*&#40;&#40;2**&#40;1/12.0&#41;&#41;**7&#41;, 2000&#41;



But that's just because I like to tune to E. If you prefer a more "natural looking" note, you can use A:


winsound.Beep&#40;110, 1000&#41;



But why choose at all when you can go for all of them?


&#91;winsound.Beep&#40;220*&#40;&#40;2**&#40;1/12.0&#41;&#41;**i&#41;, 500&#41; for i in &#91;7, 2, -2, -7, -12, -17&#93;&#93;



Image by Keela84
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.algorithm.co.il/sitecode/guitar.jpg" alt="Guitar" style="border: 2px solid black;"/></p>
<p>On windows, assuming imports are free:</p>
<div class="syntax_hilite">
<div id="python-4">
<div class="python"><span style="color: #00007f;font-weight:bold;">import</span> <span style="color: #dc143c;">winsound</span><br />
<span style="color: #dc143c;">winsound</span>.<span style="color: #000000;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">220</span>*<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>**<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">12</span>.<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>**<span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">2000</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>But that's just because I like to tune to E. If you prefer a more "natural looking" note, you can use A:</p>
<div class="syntax_hilite">
<div id="python-5">
<div class="python"><span style="color: #dc143c;">winsound</span>.<span style="color: #000000;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">110</span>, <span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>But why choose at all when you can go for all of them?</p>
<div class="syntax_hilite">
<div id="python-6">
<div class="python"><span style="color: black;">&#91;</span><span style="color: #dc143c;">winsound</span>.<span style="color: #000000;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">220</span>*<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>**<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">12</span>.<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>**i<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span> <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">2</span>, -<span style="color: #ff4500;">2</span>, -<span style="color: #ff4500;">7</span>, -<span style="color: #ff4500;">12</span>, -<span style="color: #ff4500;">17</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span></div>
</div>
</div>
<p></p>
<p><small>Image by <a href="http://www.flickr.com/photos/keela84/8075445/">Keela84</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/one-liner-guitar-tuner-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Visualizing Data Using the Hilbert Curve</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/visualizing-data-using-the-hilbert-curve/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/visualizing-data-using-the-hilbert-curve/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 06:57:30 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hilbert curve]]></category>
		<category><![CDATA[PIL]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=525</guid>
		<description><![CDATA[Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he asked for my help.
I installed Python &#038; PIL on his machine, and not too long [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he asked for my help.<br />
I installed Python &#038; PIL on his machine, and not too long after, he had the image plotted. The script looked something like:</p>
<div class="syntax_hilite">
<div id="python-9">
<div class="python">data_points = get_data_points<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
n =&nbsp; <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>**<span style="color: #ff4500;">0</span>.<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0</span>.<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span></p>
<p>image = Image<span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span>, <span style="color: black;">&#40;</span>n, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<span style="color: #00007f;font-weight:bold;">for</span> idx, pt <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; image.<span style="color: #000000;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>idx/n, idx%n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
image.<span style="color: #000000;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bla.png'</span>, <span style="color: #483d8b;">'png'</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p>
Easy enough to do. Well, easy enough if you have enough memory to handle very large data sets. Luckily enough, we had just enough memory for this script &#038; data series, and we were happy. The image was generated, and everything worked fine.</p>
<p>Still, we wanted to improve on that. One problem with this visualization is that two horizontally adjacent pixels don’t have anything to do with each other. Remembering xkcd’s "<a href="http://xkcd.com/195/">Map of the Internet</a>", I decided to use the <a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert Curve</a>. I started with wikipedia's version of the code for the Python turtle and changed it to generate a string of instructions of where to put pixels. On the way I improved the time complexity by changing it to have only two recursion calls instead of four. (It can probably be taken down to one by the way, I leave that as a challenge to the reader :)</p>
<p>Unfortunately, at this point we didn’t have enough memory to hold all of those instructions, so I changed it into a generator. Now it was too slow. I cached the lower levels of the recursion, and now it worked in reasonable time (about 3-5 minutes), with reasonable memory requirements (no OutOfMemory exceptions). Of course, I'm skipping a bit of exceptions and debugging along the way. Still, it was relatively straightforward.</p>
<p>Writing the generator wasn't enough - there were still pixels to draw! It took a few more minutes to write a simple "turtle", that walks the generated hilbert curve.<br />
Now, we were ready:</p>
<div class="syntax_hilite">
<div id="python-10">
<div class="python">hilbert = Hilbert<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: #000000;">log</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0</span>.<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<span style="color: #00007f;font-weight:bold;">for</span> pt <span style="color: #00007f;font-weight:bold;">in</span> data_points:<br />
&nbsp; &nbsp; x,y = hilbert.<span style="color: #000000;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; image.<span style="color: #000000;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p>
A few minutes later, the image was generated. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/visualizing-data-using-the-hilbert-curve/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fuzz-Testing With Nose</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/fuzz-testing-with-nose/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/fuzz-testing-with-nose/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 07:49:01 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[nose]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=587</guid>
		<description><![CDATA[A few days ago, I found a in my website, plnnr.com. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test in hand, I then worked to fix the bug, and got this unit-test to pass. [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I found a in my website, <a href="http://plnnr.com">plnnr.com</a>. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test in hand, I then worked to fix the bug, and got this unit-test to pass. </p>
<p>As I previously persumed this feature to be (relatively :) bug free, I decided that more testing was in order. This time however, a single test-case would not be enough - I needed to make sure that the trip-generation algorithm works in many cases. Enter <a href="http://en.wikipedia.org/wiki/Fuzz_testing">fuzzing</a>.</p>
<p>Plnnr.com generates trips according to trip preferences. Why not generate the trip preferences with a fuzzer, and then check if the planning algorithm chokes on them? While fuzzing is usually used to generate invalid input with the goal of causing the program to crash, in this case I'm generating valid input with the goal of causing the planning algorithm to fail.</p>
<p>Usually fuzzing is done with one of two techniques - exhaustive fuzzing, that goes systematically (possibly selectively) over the input space and random fuzzing, which picks inputs at random - or "somewhat" randomly. In my case, the input space consists of "world data" - locations of attractions, restaurants, etc, and trip preferences - intensity, required attractions, and so on. Since the input space is so large and "unstructured", I found it much easier to go with random fuzzing. </p>
<p>In each test-case, I will generate a "random world", and random trip preferences for that world.<br />
Here is some sample code that shows how this might look:</p>
<div class="syntax_hilite">
<div id="python-15">
<div class="python">trip_prefs.<span style="color: #000000;">num_days</span> = <span style="color: #dc143c;">random</span>.<span style="color: #000000;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><br />
trip_prefs.<span style="color: #000000;">intensity</span> = <span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><br />
<span style="color: #00007f;font-weight:bold;">if</span> randbit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; trip_prefs.<span style="color: #000000;">schedule_lunch</span> = <span style="color: #008000;">True</span></div>
</div>
</div>
<p></p>
<p>Where randbit is defined like so:</p>
<div class="syntax_hilite">
<div id="python-16">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> randbit<span style="color: black;">&#40;</span>prob = <span style="color: #ff4500;">0</span>.<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> &lt; prob</div>
</div>
</div>
<p></p>
<p>This is all very well, but tests need to be reproducible. If a fuzzer-generated test case fails and I can't recreate it to analyze the error and later verify that it is fixed, it isn't of much use. To solve this issue, the input generation function receives some value, and sets the random seed with this parameter. Now, generating test cases is just a matter of generating a sequence of random values. Here is my code to do that:</p>
<div class="syntax_hilite">
<div id="python-17">
<div class="python"><span style="color: #00007f;font-weight:bold;">class</span> FuzzTestBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; __test__ = <span style="color: #008000;">False</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> run_single_fuzz<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, random_seed<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> fuzz_test<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">random</span>.<span style="color: #000000;">seed</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; random_seeds = <span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>NUM_FUZZ_TESTS<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> seed <span style="color: #00007f;font-weight:bold;">in</span> random_seeds:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">yield</span> <span style="color: #008000;">self</span>.<span style="color: #000000;">run_single_fuzz</span>, seed</div>
</div>
</div>
<p></p>
<p>FuzzTestBase is a base-class for actual test classes. Each test class just needs to define its own version of run_single_fuzz, and in it call random.seed(random_seed) and log random_seed.</p>
<p>This code uses <a href="http://somethingaboutorange.com/mrl/projects/nose/0.11.3/">nose</a>'s ability to test generators: it assumes that a test generator yields test functions and their parameters.</p>
<p>A few interesting issues:<br />
* I generate the random seeds beforehand, so that calling random.seed() in the actual test case doesn't affect the seed sequence.<br />
* Originally I used just random.random() as a seed instead of str(random.random()). The problem with that is that this way it's not reproducible. random.random() returns a floating point value x, for which usually x != eval(str(x)):</p>
<div class="syntax_hilite">
<div id="python-18">
<div class="python">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>: x = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: x == <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span></div>
</div>
</div>
<p></p>
<p>Even though x == eval(repr(x)) for that case, there's still room for error. Unlike floating point numbers, it's harder to go wrong with string equality. So str(random.random()) is just a cheap way to generate random strings.</p>
<p>I'd recommend that if your testing mostly consists of selected test cases based on what you think is possible user behavior, you might want to add some fuzzed inputs. I originally started the fuzz-testing described in this blog-post to better test for a specific bug. After adding the fuzz-testing, I found another bug I didn't know was there. This just goes to show how useful fuzzing is as a testing tool. The fact that it's so easy to implement is just a bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/fuzz-testing-with-nose/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Open Redirects</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/open-redirects/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/open-redirects/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 19:59:18 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[web-design]]></category>
		<category><![CDATA[cross site scripting]]></category>
		<category><![CDATA[csrf]]></category>
		<category><![CDATA[open redirect]]></category>
		<category><![CDATA[robots.txt]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=560</guid>
		<description><![CDATA[In this post I'll discuss an issue I tackled a short while ago - open redirects. But first, the story of how I got to it. Feel free to skip ahead to the technical discussion.
Background
Our analytics for plnnr.com - the website for trip planning wasn't working as well as we wanted. We're using Google Analytics, [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I'll discuss an issue I tackled a short while ago - open redirects. But first, the story of how I got to it. Feel free to skip ahead to the technical discussion.</p>
<h4>Background</h4>
<p>Our analytics for <a href="http://plnnr.com/">plnnr.com - the website for trip planning</a> wasn't working as well as we wanted. We're using Google Analytics, and it's hard generating the specific report we want, and when we did get it, it seemed to show inaccurate numbers. To partially alleviate the issue, I was required to add tracking pixels for facebook &#038; adwords, so we can better track conversions.<br />
For us, an "internal" conversion is when a user clicks on a link to a booking url (for a hotel, or any other "bookable" attraction).<br />
After reviewing it, I decided that the best course of action would be to create an intermediate page, on which the tracking pixels would appear. Such a page would receive as a parameter the url to redirect to, and will contain the appropriate tracking pixels.</p>
<h4>Description of problem</h4>
<p>Let's say we build the url for the page like so:</p>
<div class="syntax_hilite">
<div id="code-21">
<div class="code">/redirect/?url=X</div>
</div>
</div>
<p></p>
<p>This page will load the appropriate tracking pixels, and then redirect to the given url (the X).<br />
The problems are:<br />
1. We are potentially exposing ourselves to <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross Site Scripting (XSS)</a>, if we don't filter the redirect url correctly. A malicious website could create links to our page that will run scripts in our context.</p>
<p>2. A malicious webmaster could steal search engine authority. Let's say he has two domains: a.com and b.com, of which he cares about b.com. He creates links on a.com to:</p>
<div class="syntax_hilite">
<div id="code-22">
<div class="code">ourdomain.<span style="">com</span>/redirect/?url=b.<span style="">com</span></div>
</div>
</div>
<p>
A search engine crawls his page, sees the links to his domain, and gives ourdomain.com authority to b.com. Not nice.</p>
<p>3. A malicious website could create links to ourdomain.com that redirect to some malware site, this way harming the reputation of ourdomain.com, or creating better phishing links for ourdomain.com.</p>
<h4>Possible solutions</h4>
<p>Before we handle the open-redirect issues it's important to block cross site scripting attacks. Since the attack might be possible by inject code into the url string, this is doable by correctly filtering the urls, and by using existing solutions for XSS. </p>
<p>As for the open redirect:</p>
<p>1. Non solution: cookies. We can pass the url we want in a cookie. Since cookies may only be set by our domain, other websites would not be able to set the redirect url. This doesn't work well if you want more than one redirect link, or with multiple pages open, etc.</p>
<p>2. Checking the <a href="http://en.wikipedia.org/wiki/HTTP_referrer">referrer ("referer")</a>, and allowing redirects to come only from our domain. This will break for all users who use a browser that hides referrer information, for example, those using zone-alarm. Google also suggests that if the referrer information is available, block if it's external. That way we are permissive for clients that hide it.</p>
<p>3. Whitelisting redirect urls. This solutions actually comes in two flavors - one is keeping a list of all possible urls, and then checking urls against it. The other is keeping a list of allowed specific url parts, for example, domains. While keeping track of all allowed urls may be impractical, keeping track of allowed domains is quite doable. Downside is that you have to update that piece of the code as well each time you want to allow another domain.</p>
<p>4. Signing urls. Let the server keep a secret, and generate a (sha1) hash for each url of "url + secret". In the redirect page, require the hash, and if it doesn't match the desired hash, don't redirect to that url. This solution is quite elegant, but it means that the client code (the javascript) can't generate redirect URLs. In my case this incurs a design change, a bandwidth cost, and a general complication of the design.</p>
<p>5. <a href="http://en.wikipedia.org/wiki/Robots_exclusion_standard">Robots.txt</a>. Use the robots.txt file to prevent search engines from indexing the redirect page, thereby mitigating at least risk number 2.</p>
<p>6. Generating a token for the entire session, much like <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">CSRF</a> protection. The session token is added to all links, and is later checked by the redirect page (on the server side).  This is especially easy to implement if you already have an existing anti-csrf mechanism in place.</p>
<p>7. A combination of the above.</p>
<h4>Discussion and my thoughts</h4>
<p>It seems to me, that blocking real users is unacceptable. Therefor, only filtering according to referrer information is unacceptable if you block users with no referrer information.<br />
At first I started to implement the url signing mechanism, but then I saw the cost associated with it, and reassessed the risks. Given that cross-site-scripting is solved, the biggest risk is stealing search-engine-authority. Right now I don't consider the last risk (harming our reputation) as important enough, but this will become more acute in the future.</p>
<p>Handling this in a robots.txt file is very easy, and that was the solution I chose. I will probably add more defense mechanisms in the future. When I do add another defense mechanism, it seems that using permissive referrer filtering, and the existing anti-csrf code will be the easiest to implement. A whitelist of domains might also be acceptable in the future.</p>
<p>If you think I missed a possible risk, or a possible solution, or you have differing opinions regarding my assessments, I'll be happy to hear about it. </p>
<p>My thanks go to Rafel, who discussed this issue with me.</p>
<h4><a name="further_reading">Further reading</a></h4>
<p>* <a href="http://www.owasp.org/index.php/Open_redirect">http://www.owasp.org/index.php/Open_redirect</a><br />
* <a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&#038;answer=171297">http://www.google.com/support/webmasters/bin/answer.py?hl=en&#038;answer=171297</a><br />
* <a href="http://muffinresearch.co.uk/archives/2009/09/30/open-redirects-and-phishing-vectors/">Open Redirects and Phishing Vectors/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/open-redirects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pyweb-il Presentation on Optimization Slides</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-oresentation-on-optimization-slides/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-oresentation-on-optimization-slides/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 18:31:50 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[presentation slides]]></category>
		<category><![CDATA[pyweb-il]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=570</guid>
		<description><![CDATA[Last Monday I gave a presentation in pywebil on optimization, that's loosely based on my blog post on the same subject. Here are the slides for that presentation.
]]></description>
			<content:encoded><![CDATA[<p>Last Monday I gave a presentation in <a href="http://groups.google.com/group/pyweb-il">pywebil</a> on optimization, that's loosely based on my <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/10-python-optimization-tips-and-issues/">blog post on the same subject</a>. Here are the <a href="http://www.algorithm.co.il/sitecode/optimization.pdf">slides for that presentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-oresentation-on-optimization-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Element Creator</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/javascript-element-creator/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/javascript-element-creator/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 14:29:22 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[startup]]></category>
		<category><![CDATA[web-design]]></category>
		<category><![CDATA[element creator]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=542</guid>
		<description><![CDATA[Some time ago I was working on optimizing the client side code of my website, plnnr.com, an online trip planner.
This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out that most of the time wasn't actually taken up by the algorithm, but by the UI. [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I was working on optimizing the client side code of my website, <a href="http://plnnr.com/">plnnr.com, an online trip planner</a>.<br />
This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out that most of the time wasn't actually taken up by the algorithm, but by the UI. Rendering the trip to html was the costly part. The process was like so:</p>
<p>Client-side Javascript code generates new trip prefs -> application calculates new trip -> Client-side Javascript gets the new trip, and creates new html.</p>
<p>It's important to note that the app is "ajax based", so the actual trip html was generated by the Javascript code, and not the server. At the time I was using Mochikit to generate the new html. Mochikit has a pretty nifty API for generating html, but it's quite a bit on the slow side. Basically, this API is a wrapper around createElement.</p>
<p>Well, first I did a little test, and found out that generating html with cloneNode and innerHTML is much faster than createElement. Still, there was a problem - I needed to generate many similar elements - similar but not identical. Consider entries on a trip itinerary - they all look the same, yet each one has a different name, a different time string, and a different onclick event.</p>
<p>What I needed was a Javascript based html template library. My requirements:<br />
1. Speed. Html had to be generated quickly.<br />
2.  Expressiveness. It had to be able to create pretty arbitrary html with a given context. For example, an anchor element (&lt;a> tag) with a given href property, and a given text content.<br />
3. References to inner elements: Many elements inside the generated html need various events attached to them, or various code processing. This should be easy to achieve.<br />
4. The library has to allow the template html to be written as html, and not only as javascript strings.</p>
<p>So, I sat down with <a href="http://twitter.com/daonb">Benny</a>, and we wrote the Javascript Element Creator, which we are now releasing under the BSD license. I originally wrote it to work with Mochikit and the Sizzle library, and Benny changed his version to worked with jquery.</p>
<p>After adding the code to my project, I got two things: first, everything worked much, much faster. Second, it was much easier changing the generated html when it was generated according to a template, and not directly in code.</p>
<h4>Instructions</h4>
<p>1. Write your html somewhere visible to the javascript code. Add the "template" class to the upper node, and the id will be the name of the template. For example:</p>
<div class="syntax_hilite">
<div id="xml-27">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;body<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"some_div"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"template"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/body<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>2. Similarly to other template engines, add double brackets to signify where text should be inserted:</p>
<div class="syntax_hilite">
<div id="xml-28">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;body<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"some_div"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"template"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"[[link_url]]"</span><span style="font-weight: bold; color: black;">&gt;</span></span>[[link_text]]<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/a<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/div<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/body<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p>
3. Create a creator object. It will "collect" your template, and will make it available to your code. </p>
<div class="syntax_hilite">
<div id="javascript-29">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> creator = <span style="color: #003366; font-weight: bold;">new</span> ElementCreator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>4. Generate your DOM object, and add it to the document;</p>
<div class="syntax_hilite">
<div id="javascript-30">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> obj = creator.<span style="color: #006600;">generate</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"some_div"</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span>link_url: <span style="color: #3366CC;">'/url/'</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link_text: <span style="color: #3366CC;">'hello world'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
appendChildNodes<span style="color: #66cc66;">&#40;</span>foo, obj<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h4>The code</h4>
<p>We decided to publish for now only the jquery version. I might publish the mochikit version as well at a later date. Since Benny wrote the jquery version, he also wrote the tests for that version.</p>
<p>All in all, the final code is pretty short, and could probably be even shorter. Still, it's good enough, and gave me a very real performance boost.</p>
<p>Here is the <a href="http://www.algorithm.co.il/sitecode/elementcreator.zip">code</a>, have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/javascript-element-creator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ethics in Programming</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/ethics-in-programming/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/ethics-in-programming/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:23:00 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Philosophy]]></category>
		<category><![CDATA[Programming ethics]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=511</guid>
		<description><![CDATA[Some time ago I was bothered by the issue of ethics in programming.
I heard the question best raised during a "game unconference" I attended. There was a panel about monetary systems for games, and people talked about the issues faced when adding money to an online game.
At one point someone from the audience said about [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I was bothered by the issue of ethics in programming.<br />
I heard the question best raised during a "game unconference" I attended. There was a panel about monetary systems for games, and people talked about the issues faced when adding money to an online game.<br />
At one point someone from the audience said about ingame monetary systems (such as in WoW) "it's like gambling and drugs!", to which one panelist jokingly replied "so we have a proven business model", and another said "except it's legal".</p>
<p>This was all in good spirit, but it got me thinking: </p>
<h4>What are the programming jobs I will not take?</h4>
<p><span id="more-511"></span></p>
<p>To answer this question I gave the subject some more thought, and discussed it with my friends. To make the discussion more concrete, here is a short (partial) list of jobs of which at least one is probably problematical for you:</p>
<ul>
<li>Advertising</li>
<li>SEO</li>
<li>Pornography</li>
<li>Gambling</li>
<li>Spam and spam related
<ul>
<li>regular advertising</li>
<li>Botnet based spam</li>
<li>scams</li>
<li>harvesting email addresses</li>
</ul>
</li>
<li>Hacking
<ul>
<li>en masse</li>
<li>commercial espionage</li>
<li>targeted “cons”</li>
</ul>
</li>
<li>Costly addictive games</li>
<li>Affiliate marketing</li>
<li>DRM</li>
<li>Weapon R&#038;D</li>
<li>Lawful Interception</li>
<li>guerilla marketing, specifically <a href="http://en.wikipedia.org/wiki/Astroturfing">astroturfing</a></li>
</ul>
<p>A critical issue that came up in discussions is the "victim". "Victimless" jobs were perceived as ethically better than ones with a victim. Also some people considered gambling ok, because the player agreed to play. Some people considered spam victimless.</p>
<p>Another argument was practicality. Someone argued that while spam is marginally ethical, he still wouldn't do it, as the returns on doing spam are not worth it. Similarly, many people said that while they don't see working on pornography as ethically wrong, they would still not do it because of the stigma attached to it.</p>
<p>Still, all the people I talked to pointed out jobs they will not do.<br />
When I tried to reason what jobs are not for me, I came up with the following hypothetical questions to ask:</p>
<ul>
<li>Would you use the product yourself?</li>
<li>If appropriate, will you let your children use it?</li>
<li>Would you let your spouse use it and pay for it?</li>
<li>Would you partner with someone who has that work experience?</li>
</ul>
<p>Using this guide, it's easier to think about which jobs I'd rather avoid.</p>
<p>One last note: many times, morality is a luxury that not everyone has. In dire times, I believe many gray-area jobs would be considered less ambiguous. After all, <a href="http://www.imdb.com/title/tt0427944/">everyone has to pay the mortgage</a>.</p>
<h4>Further reading:</h4>
<p><a href="http://www.acm.org/about/code-of-ethics">ACM's Code of Ethics</a><br />
<a href="http://www.techcrunch.com/2009/10/31/scamville-the-social-gaming-ecosystem-of-hell/">Scamville</a><br />
<a href="http://www.sjtrek.com/trek/rules/">The Rules of Acquisition</a><br />
<a href="http://www.codinghorror.com/blog/archives/001253.html">Jeff Atwood's take on the subject</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/ethics-in-programming/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Simple SQLObject DB Migration how-to</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/simple-sqlobject-db-migration-how-to/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/simple-sqlobject-db-migration-how-to/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 05:50:58 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[database migration]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[sqlobject]]></category>
		<category><![CDATA[turbogears]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=507</guid>
		<description><![CDATA[I've been using sqlobject for plnnr.com for quite some time now. So far my experience with it has been positive. Although I'll probably change ORM when I move to django, for now it stays. While it stays, I need to be able to upgrade my schema to add features.
SQLObject already has a tool for the [...]]]></description>
			<content:encoded><![CDATA[<p>I've been using sqlobject for <a href="http://plnnr.com">plnnr.com</a> for quite some time now. So far my experience with it has been positive. Although I'll probably change ORM when I move to django, for now it stays. While it stays, I need to be able to upgrade my schema to add features.<br />
SQLObject already has a tool for the job, sqlobject-admin. There are instructions on how to use it, but I found them unsatisfactory.<br />
(By the way, both django's ORM and sqlalchemy also have tools for that, django-south and sqlalchemy-migrate respectively.)</p>
<p>So here is how I use sqlobject-admin to do migrations. Note that if you're using turbogears 1.0, you would probably be using tg-admin. In that case, bear in mind that tg-admin just simplifies the job for you by adding various standard parameters, but apart from that, the idea stays the same.<br />
Notes:<br />
* I wrote these instructions on a windows machine. On linux machines it should be almost the same, but might require tweaking.<br />
* I used a specific db URI in the examples. You can change it to whatever you want.<br />
* I once had to tweak the main sqlobject-admin file to add the current dir to sys.path. YMMV.</p>
<p><strong>1. Example project:</strong><br />
Let's setup a project that uses sqlobject. We'll create a single file, 'main.py' with the following content:</p>
<div class="syntax_hilite">
<div id="python-34">
<div class="python"><span style="color: #00007f;font-weight:bold;">import</span> sqlobject</p>
<p>sqlobject.<span style="color: #000000;">sqlhub</span>.<span style="color: #000000;">processConnection</span> = sqlobject.<span style="color: #000000;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span></p>
<p><span style="color: #00007f;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: #000000;">SQLObject</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; bla = sqlobject.<span style="color: #000000;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>This is about as simple as I could get it with sqlobject.</p>
<p><strong>2. Starting to use sqlobject-admin</strong><br />
Sqlobject-admin has quite a bit of bureaucracy to go through before you get everything to work right. For a simple project, I cheat (i.e. fake an egg :), and do the following:<br />
a. Create a directory in your project called sqlobject-history<br />
b. If your project name is sotest, create a directory inside your project called sotest.egg-info<br />
c. Inside that dir create a file called sqlobject.txt<br />
d. Inside that file write:</p>
<div class="syntax_hilite">
<div id="python-35">
<div class="python">db_module=main<br />
history_dir=$base/sqlobject-history</div>
</div>
</div>
<p></p>
<p>(note that the main here is the name of the module we created earlier).</p>
<p><strong>3. Start using sqlobject-admin</strong><br />
This will be the workflow with sqlobject-admin:<br />
1. Have the creation sql for the current code version.<br />
2. Update your code<br />
3. Generate the creation sql for the new code version, *without updating the db*<br />
4. Create an upgrade script using the diff between the versions<br />
5. Use the upgrade script.</p>
<p>More specifically:<br />
1. First time - do:</p>
<blockquote><p>sqlobject-admin record --egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>2. To see that everything works, do:</p>
<blockquote><p>sqlobject-admin list --egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>and:</p>
<blockquote><p>sqlobject-admin status --egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>3. Update your database definition (in the Python file). For example, change the contents of main.py to:</p>
<div class="syntax_hilite">
<div id="python-36">
<div class="python"><span style="color: #00007f;font-weight:bold;">import</span> sqlobject</p>
<p>sqlobject.<span style="color: #000000;">sqlhub</span>.<span style="color: #000000;">processConnection</span> = sqlobject.<span style="color: #000000;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span></p>
<p><span style="color: #00007f;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: #000000;">SQLObject</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; bla = sqlobject.<span style="color: #000000;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; bla2 = sqlobject.<span style="color: #000000;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>4. Here is the critical part. Do</p>
<blockquote><p>sqlobject-admin record --egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite --no-db-record</p></blockquote>
<p>In the sqlobject-history directory there should be now two subdirectories, for each version. Let's call the old version X and the new version Y. In the old version directory create a file:<br />
upgrade_sqlite_Y.sql (where Y is the new version's name).<br />
In this file, write down the sql to add the bla2 column to the MyThing table. You can use the creation sql commands in the respective versions' directories to write it.</p>
<p>(note: if we used --edit we would get an editor opened, and if the edited file has any content when you close it, it will be saved as the upgrade script. I don't like using this method. Note that if you're on windows you'll have to fix sqlobject-admin to open your editor, as the command it uses works only on linux machines.)</p>
<p>5. run<br />
<blockquote>sqlobject-admin upgrade --egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>6. Make sure everything is OK with sqlobject-admin status.</p>
<p><strong>3. After using the upgrade script</strong><br />
You can use the same upgrade script for other instances of your project. Just make sure that you have the versions numbers correct, and the first version recorded in the database.</p>
<p>I hope this will be useful for someone using sqlobject, I know I needed this kind of how-to. If you have any questions, feel free to ask them in comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/simple-sqlobject-db-migration-how-to/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The mathematics behind the solution for Challenge No. 5</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/the-mathematics-behind-the-solution-for-challenge-no-5/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/the-mathematics-behind-the-solution-for-challenge-no-5/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 23:49:15 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[base systems]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[Knuth]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=500</guid>
		<description><![CDATA[If you take a look at the various solutions people proposed for the last challenge of generating a specific permutation, you'll see that they are very similar. Most of them are based on some form of div-mod usage. The reason this is so, is because all of these solutions are using the Factorial Base.
What does [...]]]></description>
			<content:encoded><![CDATA[<p>If you take a look at the various solutions people proposed for the <a href="http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/">last challenge of generating a specific permutation</a>, you'll see that they are very similar. Most of them are based on some form of div-mod usage. The reason this is so, is because all of these solutions are using the <a href="http://en.wikipedia.org/wiki/Factorial_base">Factorial Base</a>.</p>
<p>What does that mean?<br />
Note that we usually encounter div-mods when we want to find the representation of a number in a certain base. That should already pique your interest. Now consider that a base's digits need not have the same weight. For example, consider how we count the number of seconds since the start of the week:</p>
<p>seconds of the last minute, A (at most 60-1)<br />
minutes of the last hour, B (at most 60-1)<br />
hours of the last day, C (at most (24-1)<br />
days of the last week, D (at most 7-1)</p>
<p>So given A, B, C, D, we would say that the number of seconds is:<br />
A + 60*B + 24*C + 7*D. This certainly looks like a base transformation. To go back, we would use divmod.</p>
<p>The factorial base is just the same, with the numbers n, n-1, ... 1. Note that in the factorial base, you can only represent a finite number of numbers - n!. This should not be surprising - this is what we set out to do in the first place!<br />
The thing that I found really amazing about this is that all the people to whom I posed this challenge came up with almost the same "way" of solving it. </p>
<p>Other interesting curiosities regarding bases can be found in Knuth's book, "The Art of Computer Programming", volume 2, Section 4.1. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/the-mathematics-behind-the-solution-for-challenge-no-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Small Programming Challenge no. 5 &#8211; Generating a Permutation</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:43:45 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[challenge]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493</guid>
		<description><![CDATA[I thought of this one quite a long time ago, and I believe that the idea behind it is pretty nice mathematically. I got the idea for it from Knuth's "The Art of Computer Programming".
The challenge is simple:
write a function that receives as arguments two numbers, n, and num such that 0 ]]></description>
			<content:encoded><![CDATA[<p>I thought of this one quite a long time ago, and I believe that the idea behind it is pretty nice mathematically. I got the idea for it from Knuth's "The Art of Computer Programming".</p>
<p>The challenge is simple:<br />
write a function that receives as arguments two numbers, n, and num such that 0 <= num < n!. This function needs to return an array (list) representing a permutation of the numbers 0..n-1. For each possible num, the function needs to return a different permutation, such that over all values of num, all possible permutations are generated. The order of permutations is up to you.</p>
<p>The function you write should do this in at most O(n) time &#038; space (Various O(nlogn) are also acceptable).<br />
Write your solutions in the comments, in [ LANG ] [/ LANG ] blocks (without the spaces) where LANG is preferably Python :). I will post my solution in a few days. As usual, the most efficient &#038; elegant solution wins.</p>
<p>Go!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
