<?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>Joe Doliner &#187; programming</title>
	<atom:link href="http://joedoliner.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://joedoliner.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 Feb 2011 07:58:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Programming Praxis Growable Arrays</title>
		<link>http://joedoliner.com/2009/10/16/programming-praxis-growable-arrays/</link>
		<comments>http://joedoliner.com/2009/10/16/programming-praxis-growable-arrays/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 00:07:53 +0000</pubDate>
		<dc:creator>Joe Doliner</dc:creator>
				<category><![CDATA[Programming Praxis]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://joedoliner.com/?p=79</guid>
		<description><![CDATA[Exercise from: http://programmingpraxis.com/2009/10/16/growable-arrays/ Our goal here is to implement an array which is capable of dynamically growing as we add elements to it at the cost of logarithmic runtime for put and get functions instead of the constant runtime of standard arrays. The indexing scheme seems a bit weird at first until we notice the [...]]]></description>
			<content:encoded><![CDATA[<p>Exercise from: <a href="http://programmingpraxis.com/2009/10/16/growable-arrays/"> http://programmingpraxis.com/2009/10/16/growable-arrays/ </a></p>
<p>Our goal here is to implement an array which is capable of dynamically growing as we add elements to it at the cost of logarithmic runtime for put and get functions instead of the constant runtime of standard arrays. <img class="alignleft size-full wp-image-80" title="growable_arrays" src="http://joedoliner.com/wp-content/uploads/2009/10/growable_arrays.jpg" alt="growable_arrays" width="600" height="254" /></p>
<p>The indexing scheme seems a bit weird at first until we notice the pattern that the left subtree of 1 is all even and the right subtree is all odd. So given and index we simply consider its parity and choose the correct subtree. Then we apply this recursively to the subtree with the index halved (rounded down). Here it is implemented in python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> exArray<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	key = <span style="color: #008000;">None</span>
	left = <span style="color: #008000;">None</span>
	right = <span style="color: #008000;">None</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, index<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>index == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'index out of bounds'</span>
		<span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>index == <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">key</span> == <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'index out of bounds'</span>
			<span style="color: #ff7700;font-weight:bold;">else</span>:
				<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">key</span>
		<span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>index<span style="color: #66cc66;">%</span>2 == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">left</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;">left</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>index <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">else</span>:
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'index out of bounds'</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">right</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;">right</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>index <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">else</span>:
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'index out of bounds'</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, index, key<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>index == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'index out of bounds'</span>
		<span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>index == <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">key</span> = key
		<span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>index<span style="color: #66cc66;">%</span>2 == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">left</span><span style="color: black;">&#41;</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">left</span> = exArray<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">left</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span>index <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span>, key<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">right</span><span style="color: black;">&#41;</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">right</span> = exArray<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">right</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span>index <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span>, key<span style="color: black;">&#41;</span></pre></div></div>

<p>Full code: <a href="http://joedoliner.com/wp-content/uploads/2009/10/exArray.py">exArray.py</a><br />
To use:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> exArray
exArray.<span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span>array_size<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://joedoliner.com/2009/10/16/programming-praxis-growable-arrays/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

