<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Algorithm Blogs</title>
	
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 11 Mar 2010 20:05:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/co/Ylcg" /><feedburner:info uri="co/ylcg" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Call for Volunteers: Open Knesset – oknesset.org</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/F__GfAvLsiM/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/misc/call-for-volunteers-open-knesset-oknesset-org/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 20:05:10 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[open knesset]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=533</guid>
		<description><![CDATA[Over the last few weeks, I&#8217;ve been lightly involved in work on open knesset.
Mostly I&#8217;ve been helping two of the main developers, Benny and Ofri, and joining the discussions on the discussion group.
(For the non-Israelis: the Knesset is Israel&#8217;s congress, where laws are passed.)
The website&#8217;s mission is to improve Israeli citizens&#8217; involvement in our democracy, [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few weeks, I&#8217;ve been lightly involved in work on <a href="http://oknesset.org/">open knesset</a>.<br />
Mostly I&#8217;ve been helping two of the main developers, <a href="http://twitter.com/daonb">Benny</a> and <a href="http://twitter.com/ofri">Ofri</a>, and joining the discussions on the <a href="http://groups.google.com/group/open-knesset/">discussion group</a>.</p>
<p>(For the non-Israelis: <a href="http://en.wikipedia.org/wiki/Knesset">the Knesset</a> is Israel&#8217;s congress, where laws are passed.)</p>
<p>The website&#8217;s mission is to improve Israeli citizens&#8217; involvement in our democracy, and the first step in doing so is giving people more information. Ever wanted to know who keeps his promises? Who voted how? Who never votes? Which Member of Knesset is never present in discussions and votes? </p>
<p>Open Knesset is the place to put this information. If you know a bit of Python &#038; Django, you can join development either on the content harvesting front, or on the front-end front (pun not intended :).</p>
<p>&#8220;What about algorithms?&#8221; you may ask, or &#8220;what does this project has to do with algorithm.co.il?&#8221;. Well, there&#8217;s also plenty of room for innovation and interesting features. For example, finding the correlation between Members of Knesset that always vote together. Knowing which vote is for which law. Understanding if the vote is for or against that law. Plotting the party graph, and working with it. These are all things that still need to be done. See <a href="http://wiki.hamakor.org.il/index.php/Open-Knesset/%D7%AA%D7%A6%D7%95%D7%92%D7%95%D7%AA">these graphical examples</a> to see what I&#8217;m talking about.</p>
<p>The website is still in its infancy, but it already has lots of content and lots of features. Nevertheless, it still needs more work.<br />
If you&#8217;re looking for an open-source project where your work will have great impact, this might just be it.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=F__GfAvLsiM:4HzzMbRzfc4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=F__GfAvLsiM:4HzzMbRzfc4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=F__GfAvLsiM:4HzzMbRzfc4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/F__GfAvLsiM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/misc/call-for-volunteers-open-knesset-oknesset-org/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/misc/call-for-volunteers-open-knesset-oknesset-org/</feedburner:origLink></item>
		<item>
		<title>Ethics in Programming</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/vzWGts9_mVc/</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 &#8220;game unconference&#8221; 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 &#8220;game unconference&#8221; 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) &#8220;it&#8217;s like gambling and drugs!&#8221;, to which one panelist jokingly replied &#8220;so we have a proven business model&#8221;, and another said &#8220;except it&#8217;s legal&#8221;.</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 &#8220;victim&#8221;. &#8220;Victimless&#8221; 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&#8217;t do it, as the returns on doing spam are not worth it. Similarly, many people said that while they don&#8217;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&#8217;s easier to think about which jobs I&#8217;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&#8217;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&#8217;s take on the subject</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=vzWGts9_mVc:dtUkwTkyJDM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=vzWGts9_mVc:dtUkwTkyJDM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=vzWGts9_mVc:dtUkwTkyJDM:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/vzWGts9_mVc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/ethics-in-programming/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/ethics-in-programming/</feedburner:origLink></item>
		<item>
		<title>Simple SQLObject DB Migration how-to</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/0EapuuPDCIM/</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-4">
<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-5">
<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-6">
<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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=0EapuuPDCIM:gNn-Wzkscx8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=0EapuuPDCIM:gNn-Wzkscx8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=0EapuuPDCIM:gNn-Wzkscx8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/0EapuuPDCIM" height="1" width="1"/>]]></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>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/simple-sqlobject-db-migration-how-to/</feedburner:origLink></item>
		<item>
		<title>The mathematics behind the solution for Challenge No. 5</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/44lsTpGnbM8/</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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=44lsTpGnbM8:jBYXyR8DbEs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=44lsTpGnbM8:jBYXyR8DbEs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=44lsTpGnbM8:jBYXyR8DbEs:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/44lsTpGnbM8" height="1" width="1"/>]]></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>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/the-mathematics-behind-the-solution-for-challenge-no-5/</feedburner:origLink></item>
		<item>
		<title>Small Programming Challenge no. 5 – Generating a Permutation</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/G9HNr_XGR5U/</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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=G9HNr_XGR5U:iA-DUcSEO3A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=G9HNr_XGR5U:iA-DUcSEO3A:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=G9HNr_XGR5U:iA-DUcSEO3A:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/G9HNr_XGR5U" height="1" width="1"/>]]></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>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/</feedburner:origLink></item>
		<item>
		<title>And now for something completely different</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/Av2cYVfjzmo/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/and-now-for-something-completely-different/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 18:28:42 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Humour]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[clip]]></category>
		<category><![CDATA[humor]]></category>
		<category><![CDATA[python zen]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=484</guid>
		<description><![CDATA[My friend Yuval whom you might already know from the comments here, apparently composed music for the Python Zen. It made me laugh today, and as it's been a long day, I thought it's worth sharing here. Especially as it is Python related.

]]></description>
			<content:encoded><![CDATA[<p>My friend <a href="http://uberpython.wordpress.com/">Yuval</a> whom you might already know from the comments here, apparently composed music for the <a href="http://www.python.org/dev/peps/pep-0020/">Python Zen</a>. It made me laugh today, and as it's been a long day, I thought it's worth sharing here. Especially as it is Python related.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/kYB72Qa6F9I&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/kYB72Qa6F9I&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=Av2cYVfjzmo:05n7a4C-fwU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=Av2cYVfjzmo:05n7a4C-fwU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=Av2cYVfjzmo:05n7a4C-fwU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/Av2cYVfjzmo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/and-now-for-something-completely-different/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/and-now-for-something-completely-different/</feedburner:origLink></item>
		<item>
		<title>Leaky method references</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/cbg6KnTOktA/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/leaky-method-references/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 08:39:25 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Gotcha]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[__del__]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=472</guid>
		<description><![CDATA[After reading my last post regarding __del__, you should know that __del__ + reference cycle = leak.
Let's say that you do need to use __del__, so you decide to avoid reference cycles. You write your code in such a way as to use the minimum necessary cycles, and for the ones that remain you use [...]]]></description>
			<content:encoded><![CDATA[<p>After reading my last post <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/python-gotchas-no-2-garbage-collection-oddities/">regarding __del__</a>, you should know that __del__ + reference cycle = leak.<br />
Let's say that you do need to use __del__, so you decide to avoid reference cycles. You write your code in such a way as to use the minimum necessary cycles, and for the ones that remain you use the weakref module.</p>
<p>You might still have cycles where you don't expect it - in references to methods.<br />
Consider the following piece of code. Can you spot the reference cycle?</p>
<div class="syntax_hilite">
<div id="python-9">
<div class="python"><span style="color: #00007f;font-weight:bold;">class</span> A<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> f<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: #00007f;font-weight:bold;">return</span></p>
<p>a = A<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
a.<span style="color: #000000;">g</span> = a.<span style="color: #000000;">f</span></div>
</div>
</div>
<p></p>
<p>This code has the following reference cycle: a -> a.g -> a.f -> a.<br />
How come?<br />
When you call a.f like so: "a.f()" <strong>two</strong> things happen:<br />
1. A.f is bounded to a<br />
2. The bounded A.f is called with the first parameter getting the bounded value.</p>
<p>You may consider that "a.f" is syntactic sugar for the partial function application, A.f gets a as a first argument but doesn't get called yet.</p>
<p>When you use "a.g = a.f" what actually happens is a holding a reference to a bounded method, which holds a reference to a.</p>
<p>An idiom that uses these cycles is implementing state machines. Consider the following example code:</p>
<div class="syntax_hilite">
<div id="python-10">
<div class="python"><span style="color: #00007f;font-weight:bold;">class</span> MyMachine<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><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: #008000;">self</span>.<span style="color: #000000;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: #000000;">state_a</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">input</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> x <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">input</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: #000000;">next_func</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> state_a<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">print</span> <span style="color: #483d8b;">'a: '</span>, value<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: #000000;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: #000000;">state_b</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">def</span> state_b<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">print</span> <span style="color: #483d8b;">'b: '</span>, value<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: #000000;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: #000000;">state_a</span></div>
</div>
</div>
<p></p>
<p>Of course, my code was a bit more complicated than that, but the basic idea remains. (My code usually created some kind of function table in __init__ used to lookup the next function, and lookups happened outside "state functions"). I've seen many state machine recipes include method references - and rightly so. It's a clear and easy way to code a state machine. (For example, <a href="http://code.activestate.com/recipes/574430/">this state machine recipe</a>).<br />
Be careful though - once you add __del__ to these simple recipes you might end up with a memory leak. </p>
<p>Short note: I was going to publish this post a few days ago, but <a href="http://www.kylev.com/2009/11/03/finding-my-first-python-reference-cycle/">kylev</a> beat me to it. This just goes to show that other people encountered this kind of cycle.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=cbg6KnTOktA:YyMpFFWtBDA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=cbg6KnTOktA:YyMpFFWtBDA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=cbg6KnTOktA:YyMpFFWtBDA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/cbg6KnTOktA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/leaky-method-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/leaky-method-references/</feedburner:origLink></item>
		<item>
		<title>Python Gotchas No. 2: Garbage Collection Oddities</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/MDirtamqMw8/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/python-gotchas-no-2-garbage-collection-oddities/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 13:10:22 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[gc]]></category>
		<category><![CDATA[Gotcha]]></category>
		<category><![CDATA[__del__]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=462</guid>
		<description><![CDATA[Python is a garbage collected language. The garbage collector will collect orphaned objects. These are objects that have no references.
If an object has a __del__ method, it will be called when that object is collected. Note however, that there are no guarantees as to when this will take place. This means that while you should [...]]]></description>
			<content:encoded><![CDATA[<p>Python is a garbage collected language. The garbage collector will collect orphaned objects. These are objects that have no references.<br />
If an object has a __del__ method, it will be called when that object is collected. Note however, that there are no guarantees as to when this will take place. This means that while you should release all owned resources in the __del__ method, you should not depend on it to release these resources when the object "goes out of scope" as in C++.<br />
Specifically note that del x <strong>does not</strong> call x's __del__ method, just removes this specific reference to x.<br />
To be explicit about resource management, call the appropriate function yourself in the flow of your program, or better yet, use try-finally or the with statement. (For more information about those, see my presentation about <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-presentation-advanced-subjects-in-python/">advanced python subjects</a>).</p>
<p>There is another interesting caveat regarding Python's gc. Consider the following code:</p>
<div class="syntax_hilite">
<div id="python-12">
<div class="python"><span style="color: #00007f;font-weight:bold;">class</span> A<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">pass</span></p>
<p>a = A<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
b = A<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
a.<span style="color: #000000;">x</span> = b<br />
b.<span style="color: #000000;">x</span> = a<br />
<span style="color: #00007f;font-weight:bold;">del</span> a<br />
<span style="color: #00007f;font-weight:bold;">del</span> b</div>
</div>
</div>
<p></p>
<p>Did a and b lose their references? Well, they didn't. They're still pointing at each other, creating a <strong>reference cycle</strong>. Happily, Python's garbage collector can still handle those. However, it won't be able to handle reference cycles if at least one object in the cycle has a __del__ method.<br />
To understand why, consider the above example, only this time assume A has a __del__ method that calls self.x.release().</p>
<p>Now, which __del__ method should be called first? if it is called, is the other one still valid? Python refuses the temptation to guess, and leaves the cycle be, creating a memory (or resource) leak.<br />
The solution?<br />
1. Avoid data structures with reference cycles. For instance, there are very few instances where you'd need a <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/lru-cache-solution-a-case-for-linked-lists-in-python/">doubly linked list</a> :)<br />
2. If you do need reference cycles, consider using the <a href="http://www.python.org/doc/2.6/library/weakref.html#module-weakref">weakref module</a>, which allows you to create references that "don't count" in the eyes of the gc.</p>
<p>Further reading material:<br />
1. <a href="http://docs.python.org/reference/datamodel.html#object.__del__">The __del__ method</a>.<br />
2. <a href="http://docs.python.org/library/gc.html#module-gc">The gc (garbage collector) module</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=MDirtamqMw8:pl_xXrgJiVc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=MDirtamqMw8:pl_xXrgJiVc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=MDirtamqMw8:pl_xXrgJiVc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/MDirtamqMw8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/python-gotchas-no-2-garbage-collection-oddities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/python-gotchas-no-2-garbage-collection-oddities/</feedburner:origLink></item>
		<item>
		<title>PyWeb-IL presentation: Advanced subjects in Python</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/iOoJPTnYBnk/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-presentation-advanced-subjects-in-python/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 19:47:49 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Teaching Programming]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=457</guid>
		<description><![CDATA[Yesterday I gave a presentation at PyWeb-IL, which took place at Google's offices in Tel-Aviv. The presentation went really well, and interested many people.
Here are the slides for the presentation, "Advanced Python Subjects".
]]></description>
			<content:encoded><![CDATA[<p>Yesterday I gave a presentation at PyWeb-IL, which took place at Google's offices in Tel-Aviv. The presentation went really well, and interested many people.</p>
<p>Here are the slides for the presentation, <a href="http://www.algorithm.co.il/sitecode/advanced_python_subjects.pdf">"Advanced Python Subjects"</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=iOoJPTnYBnk:PVOJL-C7RvA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=iOoJPTnYBnk:PVOJL-C7RvA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=iOoJPTnYBnk:PVOJL-C7RvA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/iOoJPTnYBnk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-presentation-advanced-subjects-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/pyweb-il-presentation-advanced-subjects-in-python/</feedburner:origLink></item>
		<item>
		<title>PythonTurtle delivers!</title>
		<link>http://feedproxy.google.com/~r/co/Ylcg/~3/peALQvkvO8s/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/pythonturtle-delivers/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 08:18:25 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Teaching Programming]]></category>
		<category><![CDATA[teach]]></category>
		<category><![CDATA[turtle]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=449</guid>
		<description><![CDATA[A few days ago, a coworker asked me what tool he should use to teach another non-programming coworker some programming.
I thought a little, and suggested PythonTurtle, and then also demonstrated the builtin turtle.
I thought nothing much of it, but a few days later, when I entered his office, the non-programming coworker was sitting there, trying [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, a coworker asked me what tool he should use to teach another non-programming coworker some programming.<img src="http://www.algorithm.co.il/sitecode/teach_me.png" alt="PythonTurtle" style="float: right; border: 1px solid black; margin-left: 10px; margin-top: 10px; margin-bottom: 10px;" /></p>
<p>I thought a little, and suggested <a href="http://pythonturtle.org/">PythonTurtle</a>, and then also <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-6-turtle-snowflake/">demonstrated</a> the builtin turtle.</p>
<p>I thought nothing much of it, but a few days later, when I entered his office, the non-programming coworker was sitting there, trying to draw a star of david! She said he left her to do this, and that it was really cool, and then she asked me "how can I repeat commands instead of typing them again?".</p>
<p>I showed her "for i in range(10)", and then a function definition. She was really excited about those. I then helped her a little with the star of david, showed her some fractals, and gave her some new homework.<br />
My conclusions from her reactions:</p>
<ol>
<li>PythonTurtle is especially suited for non-programmers. Because of the non-intimidating UI, she did not realize she was programming until we specifically told her she was. She thought this was some introductory stuff before she starts "real programming". I believe this is very important, as while the classic turtle has the same capabilities, it would have been very different working from a "scary" interactive prompt. (The friendly turtle image is a definite plus in that regard! :)</li>
<li>When teaching with PythonTurtle and using "for i in range(10)" and "def triangle():", loops and functions were very intuitive. She was actually asking for such tools, and when I told her about them, she understood them almost immediately!</li>
<li>Any programming teaching aid that makes the student actually excited about loops and functions should be respected :)</li>
<li>Since the goal was to draw specific shapes, it was easy getting the "success high" associated with programming, and it kept her really excited. It was amazing to see her reaction when she finished writing the triangle function, not sure if she wrote it right, and then calling it, and seeing it draw a triangle correctly.</li>
<li>Even with all the praise I write here, it should still be taken with a grain of salt. It's possible that a lot of her enthusiasm was because of her personality, and less because of PythonTurtle itself. </li>
</ol>
<p>Final verdict: next time I need to teach someone to program, I'm going to reach for PythonTurtle.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=peALQvkvO8s:TB2YBVgaDmE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/co/Ylcg?a=peALQvkvO8s:TB2YBVgaDmE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/co/Ylcg?i=peALQvkvO8s:TB2YBVgaDmE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/co/Ylcg/~4/peALQvkvO8s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/pythonturtle-delivers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.algorithm.co.il/blogs/index.php/programming/python/pythonturtle-delivers/</feedburner:origLink></item>
	</channel>
</rss>
