<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Binary Search and Vector Rotation - Jon Bentley Inspired</title>
	<atom:link href="http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/feed/" rel="self" type="application/rss+xml" />
	<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/</link>
	<description>Weblog of an Aspiring Computer Scientist</description>
	<pubDate>Thu, 04 Dec 2008 19:37:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Paul McGuire</title>
		<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/#comment-272</link>
		<dc:creator>Paul McGuire</dc:creator>
		<pubDate>Tue, 17 Jun 2008 16:39:01 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=154#comment-272</guid>
		<description>No, I studied Mechanical Engineering at RPI, the CS stuff came later.  I've been very fortunate to have worked with a number of *very* smart CS folks, and some of it rubbed off on me.</description>
		<content:encoded><![CDATA[<p>No, I studied Mechanical Engineering at RPI, the CS stuff came later.  I&#8217;ve been very fortunate to have worked with a number of *very* smart CS folks, and some of it rubbed off on me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shriphani</title>
		<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/#comment-265</link>
		<dc:creator>Shriphani</dc:creator>
		<pubDate>Sat, 14 Jun 2008 04:09:49 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=154#comment-265</guid>
		<description>Well, I agree I didn't use any brain when I copied Jon's implementation directly (the writing style is so cool that one can get carried away with ease), but thanks for such an excellent comment. 

Did you go to Purdue CS as well?

Shriphani</description>
		<content:encoded><![CDATA[<p>Well, I agree I didn&#8217;t use any brain when I copied Jon&#8217;s implementation directly (the writing style is so cool that one can get carried away with ease), but thanks for such an excellent comment. </p>
<p>Did you go to Purdue CS as well?</p>
<p>Shriphani</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul McGuire</title>
		<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/#comment-263</link>
		<dc:creator>Paul McGuire</dc:creator>
		<pubDate>Fri, 13 Jun 2008 18:33:12 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=154#comment-263</guid>
		<description>We used to have a saying when I was in college, "you can code FORTRAN in any language."  In this case, Jon Bentley's code, while beautiful, elegant, fast, etc., is very C-oriented.  Your example using .reverse() looks like a direct transliteration from C using strrev.  Yes, it's interesting, especially when designing optimal algorithms within the confines of C, but it is a poor way to teach yourself Python.  Just like the elegance of C's in-place of 2 integers or pointers, cleverly implemented as a C macro:
    
    #define swap(a,b) (a)^=(b);(b)^=(a);(a)^=(b)

is much more clearly implemented using Python's tuple assignment:

    a,b = b,a

Here are two renditions of rotate using more Pythonic idioms ("idia"?), first using list slices, and second using iterators:

def rotate(list_name, i):
    """Rotate i elements of the given list, using normal Python 
       list slicing"""
    return list_name[i:] + list_name[:i]
    
def iterrotate(list_name, i):
    """Rotate i elements of the given list, using iterators so no
       copies or slices are constructed"""
    retiter = iter(list_name)
    ii = i if i &#62;= 0 else len(list_name)+i
    try:
        while(ii):
            retiter.next()
            ii -= 1
    except StopIteration:
        pass
    else:
        for item in retiter:
            yield item

    retiter = iter(list_name)
    ii = i if i &#62;= 0 else len(list_name)+i
    while(ii):
        yield retiter.next()
        ii -= 1
    
I'm sure the iterator-based solution has room for improvement, this is left as an exercise for the reader. :)

Of course, test cases on the boundaries are always useful, not just testing the nominal case:
    
def test(title,rotsize):
    print "\n%s (%d)" % (title,rotsize)
    print rotate(list("abcdefghij"), rotsize)
    for c in iterrotate(list("abcdefghij"), rotsize):
        print c,
    print
    
test("test rotation", 3)
test("test rotation size longer than list", 50)
test("test rotation size = list length", 10)
test("test zero rotation size", 0)
test("test negative sizes", -4)
test("test rotation size = -list length", -10)
test("test rotation size &#60; -list length", -15)

Prints:
    
test rotation (3)
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'a', 'b', 'c']
d e f g h i j a b c

test rotation size longer than list (50)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a b c d e f g h i j

test rotation size = list length (10)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a b c d e f g h i j

test zero rotation size (0)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a b c d e f g h i j

test negative sizes (-4)
['g', 'h', 'i', 'j', 'a', 'b', 'c', 'd', 'e', 'f']
g h i j a b c d e f

test rotation size = -list length (-10)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a b c d e f g h i j

test rotation size &#60; -list length (-15)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a b c d e f g h i j

(Your originally posted rotate() method passed all of these tests, too.)

As for binary search, check out the bisect module.

Cheers, and good luck at Purdue!
-- Paul</description>
		<content:encoded><![CDATA[<p>We used to have a saying when I was in college, &#8220;you can code FORTRAN in any language.&#8221;  In this case, Jon Bentley&#8217;s code, while beautiful, elegant, fast, etc., is very C-oriented.  Your example using .reverse() looks like a direct transliteration from C using strrev.  Yes, it&#8217;s interesting, especially when designing optimal algorithms within the confines of C, but it is a poor way to teach yourself Python.  Just like the elegance of C&#8217;s in-place of 2 integers or pointers, cleverly implemented as a C macro:</p>
<p>    #define swap(a,b) (a)^=(b);(b)^=(a);(a)^=(b)</p>
<p>is much more clearly implemented using Python&#8217;s tuple assignment:</p>
<p>    a,b = b,a</p>
<p>Here are two renditions of rotate using more Pythonic idioms (&#8221;idia&#8221;?), first using list slices, and second using iterators:</p>
<p>def rotate(list_name, i):<br />
    &#8220;&#8221;"Rotate i elements of the given list, using normal Python<br />
       list slicing&#8221;"&#8221;<br />
    return list_name[i:] + list_name[:i]</p>
<p>def iterrotate(list_name, i):<br />
    &#8220;&#8221;"Rotate i elements of the given list, using iterators so no<br />
       copies or slices are constructed&#8221;"&#8221;<br />
    retiter = iter(list_name)<br />
    ii = i if i &gt;= 0 else len(list_name)+i<br />
    try:<br />
        while(ii):<br />
            retiter.next()<br />
            ii -= 1<br />
    except StopIteration:<br />
        pass<br />
    else:<br />
        for item in retiter:<br />
            yield item</p>
<p>    retiter = iter(list_name)<br />
    ii = i if i &gt;= 0 else len(list_name)+i<br />
    while(ii):<br />
        yield retiter.next()<br />
        ii -= 1</p>
<p>I&#8217;m sure the iterator-based solution has room for improvement, this is left as an exercise for the reader. <img src='http://shriphani.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Of course, test cases on the boundaries are always useful, not just testing the nominal case:</p>
<p>def test(title,rotsize):<br />
    print &#8220;\n%s (%d)&#8221; % (title,rotsize)<br />
    print rotate(list(&#8221;abcdefghij&#8221;), rotsize)<br />
    for c in iterrotate(list(&#8221;abcdefghij&#8221;), rotsize):<br />
        print c,<br />
    print</p>
<p>test(&#8221;test rotation&#8221;, 3)<br />
test(&#8221;test rotation size longer than list&#8221;, 50)<br />
test(&#8221;test rotation size = list length&#8221;, 10)<br />
test(&#8221;test zero rotation size&#8221;, 0)<br />
test(&#8221;test negative sizes&#8221;, -4)<br />
test(&#8221;test rotation size = -list length&#8221;, -10)<br />
test(&#8221;test rotation size &lt; -list length&#8221;, -15)</p>
<p>Prints:</p>
<p>test rotation (3)<br />
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'a', 'b', 'c']<br />
d e f g h i j a b c</p>
<p>test rotation size longer than list (50)<br />
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']<br />
a b c d e f g h i j</p>
<p>test rotation size = list length (10)<br />
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']<br />
a b c d e f g h i j</p>
<p>test zero rotation size (0)<br />
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']<br />
a b c d e f g h i j</p>
<p>test negative sizes (-4)<br />
['g', 'h', 'i', 'j', 'a', 'b', 'c', 'd', 'e', 'f']<br />
g h i j a b c d e f</p>
<p>test rotation size = -list length (-10)<br />
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']<br />
a b c d e f g h i j</p>
<p>test rotation size &lt; -list length (-15)<br />
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']<br />
a b c d e f g h i j</p>
<p>(Your originally posted rotate() method passed all of these tests, too.)</p>
<p>As for binary search, check out the bisect module.</p>
<p>Cheers, and good luck at Purdue!<br />
&#8211; Paul</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shriphani</title>
		<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/#comment-257</link>
		<dc:creator>Shriphani</dc:creator>
		<pubDate>Thu, 12 Jun 2008 10:39:04 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=154#comment-257</guid>
		<description>@Ben, 

Thanks for pointing it out. I have fixed it now and it seems to work.</description>
		<content:encoded><![CDATA[<p>@Ben, </p>
<p>Thanks for pointing it out. I have fixed it now and it seems to work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://shriphani.com/blog/2008/06/12/binary-search-vector-rotation/#comment-255</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Thu, 12 Jun 2008 10:21:04 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=154#comment-255</guid>
		<description>Yup. Jon Bentley is right: most binary search implementations are broken. Yours doesn't terminate when the value sought isn't in the list.</description>
		<content:encoded><![CDATA[<p>Yup. Jon Bentley is right: most binary search implementations are broken. Yours doesn&#8217;t terminate when the value sought isn&#8217;t in the list.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
