<?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>skipperkongen.dk</title>
	<atom:link href="http://skipperkongen.dk/feed/" rel="self" type="application/rss+xml" />
	<link>http://skipperkongen.dk</link>
	<description>Trying stuff with code</description>
	<lastBuildDate>Sun, 13 May 2012 14:02:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Plotting data on maps with matplotlib</title>
		<link>http://skipperkongen.dk/2012/05/10/plotting-data-on-maps-with-matplotlib/</link>
		<comments>http://skipperkongen.dk/2012/05/10/plotting-data-on-maps-with-matplotlib/#comments</comments>
		<pubDate>Thu, 10 May 2012 15:52:20 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=2016</guid>
		<description><![CDATA[I&#8217;m learning about matplotlib, and actually just bought the book Matplotlib for Python Developers. Geographical plots Browsing stackoverflow, the matplotlib homepage, and other resources, I eventually came by this stackoverflow post, which mentions BaseMap. Since the data that I&#8217;m plotting is inherently geographical, it makes sense to show the data on a map. There are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m learning about matplotlib, and actually just bought the book <a href="http://www.packtpub.com/matplotlib-python-development/book">Matplotlib for Python Developers</a>. </p>
<h2>Geographical plots</h2>
<p>Browsing stackoverflow, the matplotlib homepage, and other resources, I eventually came by this stackoverflow post, which mentions <a href="http://matplotlib.github.com/basemap/">BaseMap</a>. Since the data that I&#8217;m plotting is inherently geographical, it makes sense to show the data on a map.</p>
<p><img alt="" src="http://matplotlib.github.com/basemap/_images/contour1.png" title="Plotting data geographically" class="alignnone" width="400" height="300" /></p>
<p>There are several <a href="http://matplotlib.github.com/basemap/users/examples.html">nice examples</a> on the <a href="http://matplotlib.github.com/basemap/">basemap</a> Github page.</p>
<h2>Heatmaps</h2>
<p>Often I want to create <a href="http://matplotlib.sourceforge.net/examples/pylab_examples/pcolor_demo2.html">heatmaps</a> of the data, using matplotlib. </p>
<p><img alt="" src="http://matplotlib.sourceforge.net/_images/pcolor_demo2.png" title="Heatmap" class="alignnone" width="440" height="360" /></p>
<p>On stackoverflow there are several posts on this topic:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/6387819/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set">Generate a heatmap in MatPlotLib using a scatter data set</a></li>
<li><a href="http://stackoverflow.com/questions/2971653/pyplot-really-slow-creating-heatmaps">pyplot: really slow creating heatmaps</a></li>
<li><a href="http://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set">Generate a heatmap in MatPlotLib using a scatter data set</a></li>
</ul>
<p>There are different <a href="http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html">colormaps</a> available for matplotlib, if you want to try different colorschemes.</p>
<p><img alt="" src="http://matplotlib.sourceforge.net/_images/show_colormaps.png" title="Color maps" class="alignnone" width="400" height="800" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/05/10/plotting-data-on-maps-with-matplotlib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making your Python code installable with PIP</title>
		<link>http://skipperkongen.dk/2012/05/02/publish-python-on-pypi/</link>
		<comments>http://skipperkongen.dk/2012/05/02/publish-python-on-pypi/#comments</comments>
		<pubDate>Wed, 02 May 2012 14:21:28 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1997</guid>
		<description><![CDATA[I like to install 3rd party Python libraries using pip. Pip and easy_install can automatically download and install Python code from PyPi (also known as The Cheese Shop). This is how to publish your own Python code on PyPi, so people can do this: pip install yourawesomeproject The packaging chapter in dive into python 3 [...]]]></description>
			<content:encoded><![CDATA[<p>I like to install 3rd party Python libraries using <a href="http://pypi.python.org/pypi/pip">pip</a>. Pip and easy_install can automatically download and install Python code from <a href="http://pypi.python.org/pypi">PyPi</a> (also known as <a href="http://youtu.be/B3KBuQHHKx0">The Cheese Shop</a>). This is how to publish your own Python code on PyPi, so people can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pip <span style="color: #c20cb9; font-weight: bold;">install</span> yourawesomeproject</pre></div></div>

<p>The <a href="http://getpython3.com/diveintopython3/packaging.html">packaging chapter </a>in dive into python 3 is a good starting point, also for Python 2, and this guide is a shorter rehash of that chapter. Just enough to get you started with publishing code on PyPi.</p>
<h2>Step 1: Structure your code</h2>
<p>Follow the tutorial, and you should have the right structure. Let&#8217;s assume that you have the following basic structure for your project &#8220;foolib&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span>
.<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span> 
    __init__.py
    foo.py
    bar.py</pre></div></div>

<p>Add <a href="http://www.gnu.org/licenses/gpl-3.0.txt">COPYING.txt</a>, <a href="http://en.wikipedia.org/wiki/README">README.txt</a>, MANIFEST.in, setup.py:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span>
    COPYING.txt
    README.txt
    MANIFEST.in
    setup.py
.<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span>foolib<span style="color: #000000; font-weight: bold;">/</span>
    __init__.py
    foo.py
    bar.py</pre></div></div>

<p>Example setup.py (choose classifier strings from <a href="http://pypi.python.org/pypi?:action=list_classifiers">this list</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> setuptools <span style="color: #ff7700;font-weight:bold;">import</span> setup
&nbsp;
setup<span style="color: black;">&#40;</span>
    name = <span style="color: #483d8b;">'foolib'</span>,
    packages = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'foolib'</span><span style="color: black;">&#93;</span>,
    version = <span style="color: #483d8b;">'0.0.1'</span>,
    description = <span style="color: #483d8b;">'Does something foo'</span>,
    author=<span style="color: #483d8b;">'Your Name'</span>,
    author_email=<span style="color: #483d8b;">'youremail@example.com'</span>,
    url=<span style="color: #483d8b;">'https://github.com/youraccount/foolib'</span>,
    classifiers=<span style="color: black;">&#91;</span>
        <span style="color: #483d8b;">'Programming Language :: Python'</span>,
        <span style="color: #483d8b;">'Programming Language :: Python :: 2'</span>,
        <span style="color: #483d8b;">'License :: OSI Approved :: GNU General Public License (GPL)'</span>,
        <span style="color: #483d8b;">'Operating System :: OS Independent'</span>,
        <span style="color: #483d8b;">'Development Status :: 1 - Planning'</span>,
        <span style="color: #483d8b;">'Environment :: Console'</span>,
        <span style="color: #483d8b;">'Intended Audience :: Science/Research'</span>,
        <span style="color: #483d8b;">'Topic :: Scientific/Engineering :: GIS'</span>
    <span style="color: black;">&#93;</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>Example MANIFEST.in</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">include COPYING.txt</pre></div></div>

<h2>Step 2: Build distribution files</h2>
<p>This is how to publish a binary distribution of your code on PyPi</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># check the configuration</span>
python setup.py check
<span style="color: #666666; font-style: italic;"># build Python eggs and publish in PyPi</span>
python setup.py register bdist_egg upload</pre></div></div>

<p>This has added some new folders in the root of your library:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>build
.<span style="color: #000000; font-weight: bold;">/</span>dist
...</pre></div></div>

<p>And now, anyone can install your Python library using PIP or easy_install</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pip <span style="color: #c20cb9; font-weight: bold;">install</span> foolib</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/05/02/publish-python-on-pypi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trying SQLAlchemy for Python</title>
		<link>http://skipperkongen.dk/2012/04/27/trying-sqlalchemy-for-python/</link>
		<comments>http://skipperkongen.dk/2012/04/27/trying-sqlalchemy-for-python/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 21:49:14 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1994</guid>
		<description><![CDATA[Install SQLAlchemy This is easy (using pip): pip install sqlalchemy Learn SQLAlchemy http://docs.sqlalchemy.org/en/latest/core/tutorial.html This tutorial uses an in memory SQLite database, which is cool in itself. Various topics: Indexes]]></description>
			<content:encoded><![CDATA[<h2>Install SQLAlchemy</h2>
<p>This is easy (using pip):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pip <span style="color: #c20cb9; font-weight: bold;">install</span> sqlalchemy</pre></div></div>

<h2>Learn SQLAlchemy</h2>
<p><a href="http://docs.sqlalchemy.org/en/latest/core/tutorial.html">http://docs.sqlalchemy.org/en/latest/core/tutorial.html</a></p>
<p>This tutorial uses an in memory SQLite database, which is cool in itself.</p>
<p>Various topics:</p>
<ul>
<li><a href="http://docs.sqlalchemy.org/en/latest/core/schema.html#indexes">Indexes</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/27/trying-sqlalchemy-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to install gfortran on Mac OS X</title>
		<link>http://skipperkongen.dk/2012/04/27/how-to-install-gfortran-on-mac-os-x/</link>
		<comments>http://skipperkongen.dk/2012/04/27/how-to-install-gfortran-on-mac-os-x/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 21:25:16 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Linux fu]]></category>
		<category><![CDATA[Mac OS X fu]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1985</guid>
		<description><![CDATA[Why did I install gfortran? Well, not to write Fortran programs. I tried installing SciPy using pip install scipy, and I got a message that a Fortran compiler was needed. Install This is how I installed gfortran on my Mac: Visit hpc.sourceforge.net, and select a binary distribution for your version of Mac OS X, e.g. [...]]]></description>
			<content:encoded><![CDATA[<p>Why did I install gfortran? Well, not to write Fortran programs. I tried installing SciPy using <tt>pip install scipy</tt>, and I got a message that a Fortran compiler was needed.</p>
<h2>Install</h2>
<p>This is how I installed gfortran on my Mac:</p>
<p>Visit <a href="http://hpc.sourceforge.net/">hpc.sourceforge.net</a>, and select a binary distribution for your version of Mac OS X, e.g. <a href="http://prdownloads.sourceforge.net/hpc/gfortran-snwleo-intel-bin.tar.gz?download">gfortran-snwleo-intel-bin.tar.gz</a> for Snow Leopard.</p>
<p>Download the file and open the download directory in Terminal. </p>
<p>To install gfortran do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">gunzip</span> gfortran-snwleo-intel-bin.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> xvf gfortran-snwleo-intel-bin.tar <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<h2>Compile a hello world</h2>
<p>Let&#8217;s do a quick test that gfortran was installed.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gfortran <span style="color: #660033;">--version</span></pre></div></div>

<p>To try compiling a small testprogram, enter the <a href="http://www.stanford.edu/class/me200c/tutorial_77/03_basics.html">following</a> into a file called <tt>radius.for</tt> (btw, the whitespace matters):</p>

<div class="wp_syntax"><div class="code"><pre class="fortran" style="font-family:monospace;">      <span style="color: #b1b100;">program</span> circle
      <span style="color: #000066;">real</span> r, area
&nbsp;
<span style="color: #666666; font-style: italic;">c This program reads a real number r and prints</span>
<span style="color: #666666; font-style: italic;">c the area of a circle with radius r.</span>
&nbsp;
      write <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Give radius r:'</span>
      read  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> r
      area <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3.14159</span><span style="color: #339933;">*</span>r<span style="color: #339933;">*</span>r
      write <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Area = '</span>, area
&nbsp;
      <span style="color: #b1b100;">stop</span>
      <span style="color: #b1b100;">end</span></pre></div></div>

<p>Compile and run the program:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gfortran <span style="color: #660033;">-o</span> radius radius.for
.<span style="color: #000000; font-weight: bold;">/</span>radius</pre></div></div>

<p>Enter a number, e.g. 3, to see the area of a circle with this radius.</p>
<h2>Uninstall</h2>
<p>To <a href="http://www.webmo.net/support/fortran_osx.html">uninstall</a> again (warning, I haven&#8217;t tried it):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-tf</span> gfortran-snwleo-intel-bin.tar <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>; <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-p</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> -d<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>or do the following to delete without prompting:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-tf</span> gfortran-snwleo-intel-bin.tar <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>; <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-t</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> -d<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/27/how-to-install-gfortran-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling __init__() on all base classes in Python multiple heritance</title>
		<link>http://skipperkongen.dk/2012/04/27/python-init-multiple-heritance/</link>
		<comments>http://skipperkongen.dk/2012/04/27/python-init-multiple-heritance/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 08:44:23 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1975</guid>
		<description><![CDATA[Here is an example of calling __init__&#40;&#41; for all base classes, when doing multiple inheritance in Python (mixins). It uses *args and **kwargs syntax: mrotest.py: class A&#40;object&#41;: &#160; def __init__&#40;self, *args, **kwargs&#41;: super&#40;A, self&#41;.__init__&#40;*args, **kwargs&#41; self.number = kwargs&#91;'number'&#93; &#160; def helloA&#40;self&#41;: return self.number &#160; class B&#40;object&#41;: &#160; def __init__&#40;self, *args, **kwargs&#41;: super&#40;B, self&#41;.__init__&#40;&#41; self.message = [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an example of calling</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>for all base classes, when doing multiple inheritance in Python (mixins). It uses <a href="http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/">*args and **kwargs syntax</a>:</p>
<p><tt>mrotest.py:</tt></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> A<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;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: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>A, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">number</span> = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'number'</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> helloA<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">number</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> B<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;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: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>B, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">message</span> = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'message'</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> helloB<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">message</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> C<span style="color: black;">&#40;</span>A,B<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;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: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># Notice: only one call to super... due to mro</span>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>C, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> helloC<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Calling A: &quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">helloA</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Calling B: &quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">helloB</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    c = C<span style="color: black;">&#40;</span>number=<span style="color: #ff4500;">42</span>, message=<span style="color: #483d8b;">&quot;Joe&quot;</span><span style="color: black;">&#41;</span>
    c.<span style="color: black;">helloC</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Running the program gives this output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python mrotest.py 
Calling A:  <span style="color: #000000;">42</span>
Calling B:  Joe</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/27/python-init-multiple-heritance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting up to speed with Git</title>
		<link>http://skipperkongen.dk/2012/04/26/getting-up-to-speed-with-git/</link>
		<comments>http://skipperkongen.dk/2012/04/26/getting-up-to-speed-with-git/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 08:38:08 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Linux fu]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1961</guid>
		<description><![CDATA[This is how you can get started successfully using Git. Basic concepts: Git Tutorial (by Lars Vogel) how successful developers are using Git:A successful branching model for Git (Gitflow) Online books: Pro Git and Git Community Book Additional things to read: Using Git with DropBox Remote tracking branches]]></description>
			<content:encoded><![CDATA[<p>This is how you can get started successfully using Git.</p>
<ul>
<li>Basic concepts: <br/><a href="http://www.vogella.com/articles/Git/article.html">Git Tutorial</a> (by Lars Vogel)</li>
<li>how successful developers are using Git:<br/><a href="http://nvie.com/posts/a-successful-git-branching-model/">A successful branching model for Git</a> (Gitflow)</li>
<li>Online books: <br/><a href="http://progit.org/book/">Pro Git</a> and <a href="http://book.git-scm.com/index.html">Git Community Book</a></li>
</ul>
<p>Additional things to read:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/1960799/using-gitdropbox-together-effectively">Using Git with DropBox</a></li>
<li><a href="http://gitready.com/beginner/2009/03/09/remote-tracking-branches.html">Remote tracking branches</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/26/getting-up-to-speed-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pyramidal tile cache cheat sheet</title>
		<link>http://skipperkongen.dk/2012/04/24/pyramidal-tile-cache-cheat-sheet/</link>
		<comments>http://skipperkongen.dk/2012/04/24/pyramidal-tile-cache-cheat-sheet/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 17:29:01 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[GIS]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1952</guid>
		<description><![CDATA[This table lists the number of total tiles for increasing zoom-level in a tile cache. The tile cache is assumed to be pyramidal: . level 1: 1 tile level 2: 5 tiles level 3: 21 tiles level 4: 85 tiles level 5: 341 tiles level 6: 1,365 tiles level 7: 5,461 tiles level 8: 21,845 [...]]]></description>
			<content:encoded><![CDATA[<p>This table lists the number of total tiles for increasing zoom-level in a tile cache. The tile cache is assumed to be pyramidal: <img src='http://s.wordpress.com/latex.php?latex=%5Cleft%7Cz_%7Bi%7D%5Cright%7C%20%3D%20n%20%5CRightarrow%20%5Cleft%7Cz_%7Bi%2B1%7D%5Cright%7C%20%3D%20n%20%5Cdot%204&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\left|z_{i}\right| = n \Rightarrow \left|z_{i+1}\right| = n \dot 4' title='\left|z_{i}\right| = n \Rightarrow \left|z_{i+1}\right| = n \dot 4' class='latex' />.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">level <span style="color: #000000;">1</span>: <span style="color: #000000;">1</span> tile
level <span style="color: #000000;">2</span>: <span style="color: #000000;">5</span> tiles
level <span style="color: #000000;">3</span>: <span style="color: #000000;">21</span> tiles
level <span style="color: #000000;">4</span>: <span style="color: #000000;">85</span> tiles
level <span style="color: #000000;">5</span>: <span style="color: #000000;">341</span> tiles
level <span style="color: #000000;">6</span>: <span style="color: #000000;">1</span>,<span style="color: #000000;">365</span> tiles
level <span style="color: #000000;">7</span>: <span style="color: #000000;">5</span>,<span style="color: #000000;">461</span> tiles
level <span style="color: #000000;">8</span>: <span style="color: #000000;">21</span>,<span style="color: #000000;">845</span> tiles
level <span style="color: #000000;">9</span>: <span style="color: #000000;">87</span>,<span style="color: #000000;">381</span> tiles
level <span style="color: #000000;">10</span>: <span style="color: #000000;">349</span>,<span style="color: #000000;">525</span> tiles
level <span style="color: #000000;">11</span>: <span style="color: #000000;">1</span>,<span style="color: #000000;">398</span>,<span style="color: #000000;">101</span> tiles
level <span style="color: #000000;">12</span>: <span style="color: #000000;">5</span>,<span style="color: #000000;">592</span>,<span style="color: #000000;">405</span> tiles
level <span style="color: #000000;">13</span>: <span style="color: #000000;">22</span>,<span style="color: #000000;">369</span>,<span style="color: #000000;">621</span> tiles
level <span style="color: #000000;">14</span>: <span style="color: #000000;">89</span>,<span style="color: #000000;">478</span>,<span style="color: #000000;">485</span> tiles
level <span style="color: #000000;">15</span>: <span style="color: #000000;">357</span>,<span style="color: #000000;">913</span>,<span style="color: #000000;">941</span> tiles
level <span style="color: #000000;">16</span>: <span style="color: #000000;">1</span>,<span style="color: #000000;">431</span>,<span style="color: #000000;">655</span>,<span style="color: #000000;">765</span> tiles
level <span style="color: #000000;">17</span>: <span style="color: #000000;">5</span>,<span style="color: #000000;">726</span>,<span style="color: #000000;">623</span>,061 tiles
level <span style="color: #000000;">18</span>: <span style="color: #000000;">22</span>,<span style="color: #000000;">906</span>,<span style="color: #000000;">492</span>,<span style="color: #000000;">245</span> tiles
level <span style="color: #000000;">19</span>: <span style="color: #000000;">91</span>,<span style="color: #000000;">625</span>,<span style="color: #000000;">968</span>,<span style="color: #000000;">981</span> tiles
level <span style="color: #000000;">20</span>: <span style="color: #000000;">366</span>,<span style="color: #000000;">503</span>,<span style="color: #000000;">875</span>,<span style="color: #000000;">925</span> tiles
level <span style="color: #000000;">21</span>: <span style="color: #000000;">1</span>,<span style="color: #000000;">466</span>,015,<span style="color: #000000;">503</span>,<span style="color: #000000;">701</span> tiles</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/24/pyramidal-tile-cache-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating plots in LaTeX with Gnuplot and Tikz</title>
		<link>http://skipperkongen.dk/2012/04/23/latex-plots-with-gnuplot-and-tikz/</link>
		<comments>http://skipperkongen.dk/2012/04/23/latex-plots-with-gnuplot-and-tikz/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 12:41:01 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[LaTeX]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1940</guid>
		<description><![CDATA[Plotting data in LaTeX is a useful thing to do. Xavier Delaunay shows how to do it on his homepage: http://d.xav.free.fr/tikz/index.html The plot looks like this: The advantage of using Tikz to draw plots is, that things like fonts and arrow-heads will be the same in the document and in the figures. Below I&#8217;ve replicated [...]]]></description>
			<content:encoded><![CDATA[<p>Plotting data in LaTeX is a useful thing to do. Xavier Delaunay shows how to do it on his homepage:</p>
<p><a href="http://d.xav.free.fr/tikz/index.html">http://d.xav.free.fr/tikz/index.html</a></p>
<p>The plot looks like this:</p>
<p><a href="http://skipperkongen.dk/wp-content/uploads/2012/04/latexplot_example.png"><img src="http://skipperkongen.dk/wp-content/uploads/2012/04/latexplot_example.png" alt="" title="latexplot_example" width="348" height="233" class="alignnone size-full wp-image-1943" /></a></p>
<p>The advantage of using Tikz to draw plots is, that things like fonts and arrow-heads will be the same in the document and in the figures.</p>
<p>Below I&#8217;ve replicated <a href="http://d.xav.free.fr/">Xavier Delaunays</a> LaTeX code, in the unlikely case that his site goes offline (Xavier, hope you don&#8217;t mind).</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #E02020; ">\</span><span style="color: #800000;">documentclass</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">article</span><span style="color: #E02020; ">}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">tikz</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span>
<span style="color: #E02020; ">\</span><span style="color: #800000;">pagestyle</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">empty</span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">tikzpicture</span></span><span style="color: #E02020; ">}[</span><span style="color: #C08020; font-weight: normal;">x=1cm,y=0.4cm</span><span style="color: #E02020; ">]</span>
&nbsp;
  <span style="color: #E02020; ">\</span><span style="color: #800000;">def</span><span style="color: #800000; font-weight: normal;">\xmin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">3</span><span style="color: #E02020; ">}</span>
  <span style="color: #E02020; ">\</span><span style="color: #800000;">def</span><span style="color: #800000; font-weight: normal;">\xmax</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">9.2</span><span style="color: #E02020; ">}</span>
  <span style="color: #E02020; ">\</span><span style="color: #800000;">def</span><span style="color: #800000; font-weight: normal;">\ymin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">2</span><span style="color: #E02020; ">}</span>
  <span style="color: #E02020; ">\</span><span style="color: #800000;">def</span><span style="color: #800000; font-weight: normal;">\ymax</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">15.5</span><span style="color: #E02020; ">}</span>
&nbsp;
  <span style="color: #2C922C; font-style: italic;">% grid</span>
  <span style="color: #800000; font-weight: normal;">\draw</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">style=help lines, ystep=2, xstep=1</span><span style="color: #E02020; ">]</span> (<span style="color: #800000; font-weight: normal;">\xmin</span>,<span style="color: #800000; font-weight: normal;">\ymin</span>) grid
  (<span style="color: #800000; font-weight: normal;">\xmax</span>,<span style="color: #800000; font-weight: normal;">\ymax</span>);
&nbsp;
  <span style="color: #2C922C; font-style: italic;">% axes</span>
  <span style="color: #800000; font-weight: normal;">\draw</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">-&gt;</span><span style="color: #E02020; ">]</span> (<span style="color: #800000; font-weight: normal;">\xmin</span>,<span style="color: #800000; font-weight: normal;">\ymin</span>) -- (<span style="color: #800000; font-weight: normal;">\xmax</span>,<span style="color: #800000; font-weight: normal;">\ymin</span>) node<span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">right</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #8020E0; font-weight: normal;">$x$</span></span><span style="color: #E02020; ">}</span>;
  <span style="color: #800000; font-weight: normal;">\draw</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">-&gt;</span><span style="color: #E02020; ">]</span> (<span style="color: #800000; font-weight: normal;">\xmin</span>,<span style="color: #800000; font-weight: normal;">\ymin</span>) -- (<span style="color: #800000; font-weight: normal;">\xmin</span>,<span style="color: #800000; font-weight: normal;">\ymax</span>) node<span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">above</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #8020E0; font-weight: normal;">$y$</span></span><span style="color: #E02020; ">}</span>;
&nbsp;
  <span style="color: #2C922C; font-style: italic;">% xticks and yticks</span>
  <span style="color: #800000; font-weight: normal;">\foreach</span> <span style="color: #800000; font-weight: normal;">\x</span> in <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">3,4,...,9</span><span style="color: #E02020; ">}</span>
    <span style="color: #800000; font-weight: normal;">\node</span> at (<span style="color: #800000; font-weight: normal;">\x</span>, <span style="color: #800000; font-weight: normal;">\ymin</span>) <span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">below</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\x</span></span><span style="color: #E02020; ">}</span>;
  <span style="color: #800000; font-weight: normal;">\foreach</span> <span style="color: #800000; font-weight: normal;">\y</span> in <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">2,4,...,14</span><span style="color: #E02020; ">}</span>
    <span style="color: #800000; font-weight: normal;">\node</span> at (<span style="color: #800000; font-weight: normal;">\xmin</span>,<span style="color: #800000; font-weight: normal;">\y</span>) <span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">left</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\y</span></span><span style="color: #E02020; ">}</span>;
&nbsp;
  <span style="color: #2C922C; font-style: italic;">% plot the data from the file data.dat</span>
  <span style="color: #2C922C; font-style: italic;">% smooth the curve and mark the data point with a dot</span>
  <span style="color: #800000; font-weight: normal;">\draw</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">color=blue</span><span style="color: #E02020; ">]</span> plot<span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">smooth,mark=*,mark size=1pt</span><span style="color: #E02020; ">]</span> file <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">data.dat</span><span style="color: #E02020; ">}</span>
   node <span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">right</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">data</span><span style="color: #E02020; ">}</span>;
&nbsp;
  <span style="color: #2C922C; font-style: italic;">% generate and plot another a curve y = 0.1 x^2 + 2.5</span>
  <span style="color: #2C922C; font-style: italic;">% this generates the files figure.parabola.gnuplot and figure.parabola.table </span>
  <span style="color: #800000; font-weight: normal;">\draw</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">color=red, domain=<span style="color: #800000; font-weight: normal;">\xmin</span>:<span style="color: #800000; font-weight: normal;">\xmax</span></span><span style="color: #E02020; ">]</span> plot<span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">id=parabola</span><span style="color: #E02020; ">]</span>
  function<span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">0.1*x**2 + 2.5</span><span style="color: #E02020; ">}</span> node <span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">right</span><span style="color: #E02020; ">]</span> <span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #8020E0; font-weight: normal;">$y=0.1\,x^2 + 2.5$</span></span><span style="color: #E02020; ">}</span>;
&nbsp;
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">tikzpicture</span></span><span style="color: #E02020; ">}</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span></pre></div></div>

<p>The data.dat file is simply a tab-separated file with <img src='http://s.wordpress.com/latex.php?latex=%28x%2Cy%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='(x,y)' title='(x,y)' class='latex' /> coordinates</p>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/23/latex-plots-with-gnuplot-and-tikz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simulating the Golden Balls game show</title>
		<link>http://skipperkongen.dk/2012/04/22/simulating-golden-balls/</link>
		<comments>http://skipperkongen.dk/2012/04/22/simulating-golden-balls/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 21:22:24 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1920</guid>
		<description><![CDATA[In this eposide of Golden Balls, an inspired event takes place: I retold the event at DIKU APL lunch (nice to have a job where game theory is a valid conversation topic), and we had a conversation about it. At first I thought this was prisoners dilemma, but it was quickly revealed that it is [...]]]></description>
			<content:encoded><![CDATA[<p>In this eposide of Golden Balls, an inspired event takes place:</p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/S0qjK3TWZE8" frameborder="0" allowfullscreen></iframe></p>
<p>I retold the event at DIKU APL lunch (nice to have a job where game theory is a valid conversation topic), and we had a conversation about it. At first I thought this was prisoners dilemma, but it was quickly revealed that it is a different game. What is cool about it is, that Nick basically forces Abraham to pick split. I don&#8217;t think the same approach would work again though. The person in Abrahams position might be tempted to try a counter-steal. The person in Nicks position might be tempted to actually steal the money, which would be a dirty thing to do, but not completely unlikely.</p>
<p> Someone remarked that if each player was a random variable, stealing would be twice as successful as splitting, as on average you get half the money by stealing, while splitting only gets you one quarter of the money on average. </p>
<p>Clearly players are not random variables, so we talked a bit about what a genetic algorithm simulation would reveal. Obviously, if all people were stealers, everyone would loose all the time, so that might inspire people to try splitting. If most people were splitters, stealers would have a field day, which might inspire people to become stealers. Someone speculated that running a simulation would reveal some kind of oscillating pattern. I decided to build a model and implement a simulation in Python. </p>
<p>The way the simulation works is this: A population is initialized, with a certain ratio between stealers and splitters (I use a 50/50 split). A game of golden balls is played, by drawing two participants randomly from the population. If the outcome is a steal, it will have an propability of converting splitters in the population into stealers. If the outcome is a split, it will conversely have a propability of converting stealers into splitters. If the outcome is a mutual steal, and hence loss, it will have a (slightly bigger) propability of converting stealers into splitters.</p>
<p><tt>golden_balls.py</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># constants</span>
    POP_SIZE = <span style="color: #ff4500;">1000</span>
    INITIAL_STEALERS = <span style="color: #ff4500;">0.5</span>
    population = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>POP_SIZE<span style="color: #66cc66;">*</span>INITIAL_STEALERS<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    population.<span style="color: black;">extend</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>POP_SIZE<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>-INITIAL_STEALERS<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    GAMES = <span style="color: #ff4500;">100</span>
    CONVERT_TO_STEALER = <span style="color: #ff4500;">0.1</span>
    CONVERT_TO_SPLITTER = <span style="color: #ff4500;">0.05</span>
    CONVERT_TO_SPLITTER_ON_LOOSE = <span style="color: #ff4500;">0.1</span>
&nbsp;
    ts = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'Generation'</span>, <span style="color: #483d8b;">'Percent stealers in population'</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># generations</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> game <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>GAMES<span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #808080; font-style: italic;"># calc stealer percent</span>
        stealers = <span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: x+y, population<span style="color: black;">&#41;</span> / <span style="color: #008000;">float</span><span style="color: black;">&#40;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>population<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># record last generation</span>
        ts.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>game, stealers<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># play game</span>
        p1 = <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: #66cc66;">&lt;</span>= stealers
        p2 = <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: #66cc66;">&lt;</span>= stealers
        new_pop = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> person <span style="color: #ff7700;font-weight:bold;">in</span> population:
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>p1 <span style="color: #ff7700;font-weight:bold;">or</span> p2<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span>p1 <span style="color: #ff7700;font-weight:bold;">and</span> p2<span style="color: black;">&#41;</span>:
                <span style="color: #808080; font-style: italic;"># steal</span>
                <span style="color: #ff7700;font-weight:bold;">if</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: #66cc66;">&lt;</span>= CONVERT_TO_STEALER:
                    <span style="color: #808080; font-style: italic;"># converted</span>
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span>person<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> p1<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> p2<span style="color: black;">&#41;</span>:
                <span style="color: #808080; font-style: italic;"># split</span>
                <span style="color: #ff7700;font-weight:bold;">if</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: #66cc66;">&lt;</span>= CONVERT_TO_SPLITTER:
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span>person<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #808080; font-style: italic;"># loose</span>
                <span style="color: #ff7700;font-weight:bold;">if</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: #66cc66;">&lt;</span>= CONVERT_TO_SPLITTER_ON_LOOSE:
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    new_pop.<span style="color: black;">append</span><span style="color: black;">&#40;</span>person<span style="color: black;">&#41;</span>
&nbsp;
        population = new_pop
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> ts
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The outcome of the simulation:</p>
<p><iframe width="550" height="350" src="http://skipperkongen.dk/files/golden_balls.html" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/22/simulating-golden-balls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple programs for simple turing machines</title>
		<link>http://skipperkongen.dk/2012/04/08/simple-programs-for-simple-turing-machines/</link>
		<comments>http://skipperkongen.dk/2012/04/08/simple-programs-for-simple-turing-machines/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 14:36:42 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://skipperkongen.dk/?p=1880</guid>
		<description><![CDATA[I&#8217;ve always wanted to understand Turing Machines better. I believe that the best understanding comes from the intuition developed by solving puzzles. So here, I give myself some challenges. Solve simple problems by writing programs for a simple Turing Machine, using this simulator: http://ironphoenix.org/tril/tm/ Challenge 1: Odd or even characters? Challenge: Make a program for [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve always wanted to understand Turing Machines better. I believe that the best understanding comes from the intuition developed by solving puzzles.
</p>
<p>
So here, I give myself some challenges. Solve simple problems by writing programs for a simple Turing Machine, using this simulator: <a href="http://ironphoenix.org/tril/tm/">http://ironphoenix.org/tril/tm/</a>
</p>
<p><a href="http://skipperkongen.dk/wp-content/uploads/2012/04/turing_machine.jpg"><img src="http://skipperkongen.dk/wp-content/uploads/2012/04/turing_machine-300x106.jpg" alt="" title="turing_machine" width="300" height="106" class="alignnone size-medium wp-image-1891" /></a><br />
<strong>Challenge 1: Odd or even characters?</strong></p>
<p>Challenge: </p>
<blockquote><p>Make a program for a Turing Machine, such that the program:</p>
<ol>
<li>outputs E if there is an even number of sequential x&#8217;es on tape</li>
<li>outputs O if there is an odd number of sequential x&#8217;es on tape</li>
<li>the program does not have to handle the empty string</li>
<li>running time not important</li>
</ol>
</blockquote>
<p>For example, the program should output &#8216;E&#8217; for the following input:</p>

<div class="wp_syntax"><div class="code"><pre class="turing" style="font-family:monospace;">xxxx</pre></div></div>

<p>My solution to challenge 1:</p>

<div class="wp_syntax"><div class="code"><pre class="turing" style="font-family:monospace;">1,x 2,1 &gt;
2,x 3,. &lt;
2,. 2,. &gt;
2,_ 4,_ &lt;
3,. 3,. &lt;
3,0 2,1 &gt;
3,1 2,0 &gt;
4,. 4,_ &lt;
4,0 5,E &gt;
4,1 8,O &gt;
5,_ 6,V, &gt;
6,_ 7,E &gt;
7,_ H,N &gt;
8,_ 9,D &gt;
9,_ H,D &gt;</pre></div></div>

<p>The program has O(n^2) running time.</p>
<h2>Challenge 2: Improve the solution to linear running time</h2>
<p>In the above only the character &#8216;x&#8217; is matched. Matching <em>n</em> characters would mean adding n transitions to state 2. That is straight forward, and I&#8217;ll not do it in the following. </p>
<p>Challenge: </p>
<blockquote><p>Improve the solution to challenge 1: </p>
<ol>
<li>Algorithm must have O(n) running time</li>
<li>It should handle the empty string (output &#8216;E&#8217;)</li>
</ol>
</blockquote>
<p>My solution to challenge 2:</p>

<div class="wp_syntax"><div class="code"><pre class="turing" style="font-family:monospace;">1,_ 9,_ &gt;
1,x 2,# &gt;
2,x 3,x &lt;
2,_ 6,_ &lt;
3,0 5,0 &gt;
3,1 4,1 &gt;
3,# 4,# &gt;
4,x 2,0 &gt;
5,x 2,1 &gt;
6,0 7,_ &lt;
6,1 8,_ &lt;
6,# 10,_ &gt;
7,0 7,_ &lt;
7,1 7,_ &lt;
7,# 9,_ &gt;
8,0 8,_ &lt;
8,1 8,_ &lt;
8,# 10,_ &gt;
9,_ H,E &gt;
10,_ H,O &gt;</pre></div></div>

<h2>Challenge 3: Sorting</h2>
<p>Ok, let&#8217;s up the ante a little bit. Let&#8217;s do sorting.</p>
<p>Challenge:</p>
<blockquote>
<ol>
<li>Sort a scrambled sequence of numbers from 0-9</li>
<li>Running time should be no worse than bubble sort, O(n^2) worst case</li>
</ol>
</blockquote>
<p>TODO</p>
]]></content:encoded>
			<wfw:commentRss>http://skipperkongen.dk/2012/04/08/simple-programs-for-simple-turing-machines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

