<?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"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Mars Rover in Python and Haskell</title> <atom:link href="http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/feed/" rel="self" type="application/rss+xml" /><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/</link> <description>Representing Anti-monotonistic Tendencies</description> <lastBuildDate>Thu, 29 Jul 2010 02:07:12 -0400</lastBuildDate> <generator>http://wordpress.org/?v=2.8.6</generator> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>By: Mars Rover in Java &#124; Veera Sundar - Java, Web and Design</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-4420</link> <dc:creator>Mars Rover in Java &#124; Veera Sundar - Java, Web and Design</dc:creator> <pubDate>Sun, 06 Jun 2010 17:14:04 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-4420</guid> <description>&lt;p&gt;[...] Java   Sometime back, when Arun posted a Python version of Mars Rover problem, I tried the same in Java. Btw, if you haven&#8217;t heard [...]&lt;/p&gt; </description> <content:encoded><![CDATA[<p>[...] Java   Sometime back, when Arun posted a Python version of Mars Rover problem, I tried the same in Java. Btw, if you haven&#8217;t heard [...]</p>]]></content:encoded> </item> <item><title>By: Erik Knowles</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1766</link> <dc:creator>Erik Knowles</dc:creator> <pubDate>Fri, 05 Feb 2010 19:16:45 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1766</guid> <description>&lt;p&gt;I have to say, your Python code is just damn beautiful. Wonderfully idiomatic. (Err...one little quibble...using &quot;i&quot; to parse the character instructions gave me a little pause -- I kept trying to figure out what you were doing with the integer inside the eval() expression. Woulda been better to use a more descriptive name.)&lt;/p&gt; </description> <content:encoded><![CDATA[<p>I have to say, your Python code is just damn beautiful. Wonderfully idiomatic. (Err&#8230;one little quibble&#8230;using &#8220;i&#8221; to parse the character instructions gave me a little pause &#8212; I kept trying to figure out what you were doing with the integer inside the eval() expression. Woulda been better to use a more descriptive name.)</p>]]></content:encoded> </item> <item><title>By: Arun Bhai</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1748</link> <dc:creator>Arun Bhai</dc:creator> <pubDate>Thu, 04 Feb 2010 05:18:11 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1748</guid> <description>&lt;p&gt;Apologies for not mentioning that comments are have markdown processing enabled. I have fixed the formatting in your original comment.&lt;/p&gt; </description> <content:encoded><![CDATA[<p>Apologies for not mentioning that comments are have markdown processing enabled. I have fixed the formatting in your original comment.</p>]]></content:encoded> </item> <item><title>By: Roger Gammans</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1745</link> <dc:creator>Roger Gammans</dc:creator> <pubDate>Wed, 03 Feb 2010 23:34:52 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1745</guid> <description>&lt;p&gt;Pah Thats:-
&lt;pre&gt;
r = lambda x, a: (x, -1j * a )
l = lambda x, a: (x,  1j *  a )
&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;And the interpreter line of :-
for i in instrns: pos = locals ( ) [ i ]( * pos )&lt;/p&gt;&lt;p&gt;which keeps getting eaten by the blog softtware,..&lt;/p&gt; </description> <content:encoded><![CDATA[<p>Pah Thats:-<pre>
r = lambda x, a: (x, -1j * a )
l = lambda x, a: (x,  1j *  a )
</pre></p><p>And the interpreter line of :-
for i in instrns: pos = locals ( ) [ i ]( * pos )</p><p>which keeps getting eaten by the blog softtware,..</p>]]></content:encoded> </item> <item><title>By: Roger Gammans</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1744</link> <dc:creator>Roger Gammans</dc:creator> <pubDate>Wed, 03 Feb 2010 23:31:01 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1744</guid> <description>&lt;p&gt;If your using complex arithmetic you can recode the the l() and r() functions as rotations in complex form are equivalent to multiplications.&lt;/p&gt;&lt;p&gt;That and making dirs a dict gives the following:-
&lt;pre&gt;
dirs =  { &#039;N&#039;:1j, &#039;E&#039;:1, &#039;S&#039;:-1j, &#039;W&#039;:-1j }
rdirs=dict(reversed(d) for d in dirs.items())
r = lambda x, a: (x, -1j * a )
l = lambda x, a: (x,  1j *  a )
m = lambda x, a: (x + a, a )
raw_input()                     # Ignore the grid size
while 1:
# parse initial position triplet
x, y, dir = raw_input().split()
pos = (int(x) + 1j * int(y),dirs[dir] )
# parse instructions
instrns = raw_input().lower()
# Invoke the corresponding functions passing prev position
for i in instrns: pos = locals ( ) [ i ]( * pos )
print int(pos[0].real), int(pos[0].imag), rdirs[pos[1]]
&lt;/pre&gt;
Tested aginst the test vector only.&lt;/p&gt; </description> <content:encoded><![CDATA[<p>If your using complex arithmetic you can recode the the l() and r() functions as rotations in complex form are equivalent to multiplications.</p><p>That and making dirs a dict gives the following:-<pre>
dirs =  { 'N':1j, 'E':1, 'S':-1j, 'W':-1j }
rdirs=dict(reversed(d) for d in dirs.items())
r = lambda x, a: (x, -1j * a )
l = lambda x, a: (x,  1j *  a )
m = lambda x, a: (x + a, a )
raw_input()                     # Ignore the grid size
while 1:
    # parse initial position triplet
    x, y, dir = raw_input().split() 
    pos = (int(x) + 1j * int(y),dirs[dir] )
    # parse instructions
    instrns = raw_input().lower() 
    # Invoke the corresponding functions passing prev position
    for i in instrns: pos = locals ( ) [ i ]( * pos )
    print int(pos[0].real), int(pos[0].imag), rdirs[pos[1]]
</pre>Tested aginst the test vector only.</p>]]></content:encoded> </item> <item><title>By: Arun Bhai</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1724</link> <dc:creator>Arun Bhai</dc:creator> <pubDate>Tue, 02 Feb 2010 03:08:24 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1724</guid> <description>&lt;p&gt;Thank you guillaum, for those lovely suggestions. I was not happy with using eval myself as it is not really safe. Given some more time a method-lookup was something I would&#039;ve preferred.&lt;/p&gt;&lt;p&gt;The complex arithmetic bit was really clever :)&lt;/p&gt; </description> <content:encoded><![CDATA[<p>Thank you guillaum, for those lovely suggestions. I was not happy with using eval myself as it is not really safe. Given some more time a method-lookup was something I would&#8217;ve preferred.</p><p>The complex arithmetic bit was really clever :)</p>]]></content:encoded> </item> <item><title>By: guillaum</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1714</link> <dc:creator>guillaum</dc:creator> <pubDate>Mon, 01 Feb 2010 15:37:25 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1714</guid> <description>&lt;p&gt;May I suggest some modifications :&lt;/p&gt;&lt;p&gt;1) I remove your eval (because eval is &lt;em&gt;evil&lt;/em&gt;) and replace it by a look in locals()
2) I remove your 2d vector arithmetic and replace it by complex arithmetic. Lot of people always forget about python builtin complex number support which is quite nice for 2d point operations ;)&lt;/p&gt;&lt;p&gt;&lt;pre&gt;
dirs = &quot;NESW&quot;                   # Notations for directions
shifts = [1j,1,-1j,-1] # delta vector for each direction
&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;One letter function names corresponding to each robot instruction&lt;/p&gt;&lt;p&gt;&lt;pre&gt;
r = lambda x, a: (x, (a + 1) % 4)
l = lambda x, a: (x, (a - 1) % 4)
m = lambda x, a: (x + shifts[a], a)
raw_input()                     # Ignore the grid size
while 1:
# parse initial position triplet
x, y, dir = raw_input().split()
pos = (int(x) + 1j * int(y),dirs.find(dir))
# parse instructions
instrns = raw_input().lower()
# Invoke the corresponding functions passing prev position
for i in instrns: pos = locals()&lt;a href=&quot;*pos&quot; rel=&quot;nofollow&quot;&gt;i&lt;/a&gt;
print int(pos[0].real), int(pos[0].imag), dirs[pos[1]]&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;May I also suggest replacing raw_input() / while 1 stuff by a more concise generator stuff:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;
import sys
dirs = &quot;NESW&quot;                   # Notations for directions
shifts = [1j,1,-1j,-1] # delta vector for each direction&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;One letter function names corresponding to each robot instruction&lt;/p&gt;&lt;p&gt;&lt;pre&gt;r = lambda x, a: (x, (a + 1) % 4)
l = lambda x, a: (x, (a - 1) % 4)
m = lambda x, a: (x + shifts[a], a)
def two_by_two(lines):
while True: yield next(lines).split(), next(lines).strip()
next(sys.stdin) # ignore the grid size
for (x, y, dir), actions in two_by_two(sys.stdin):
pos = (int(x) + 1j * int(y),dirs.find(dir))
# parse instructions
instrns = actions.lower()
# Invoke the corresponding functions passing prev position
for i in instrns: pos = locals()&lt;a href=&quot;*pos&quot; rel=&quot;nofollow&quot;&gt;i&lt;/a&gt;
print int(pos[0].real), int(pos[0].imag), dirs[pos[1]]&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;This remove the EOFError ;)&lt;/p&gt;&lt;p&gt;I like that kind of little snippets of code, there is always something fun to &lt;em&gt;enhance&lt;/em&gt; (in good or bad...)&lt;/p&gt; </description> <content:encoded><![CDATA[<p>May I suggest some modifications :</p><p>1) I remove your eval (because eval is <em>evil</em>) and replace it by a look in locals()
2) I remove your 2d vector arithmetic and replace it by complex arithmetic. Lot of people always forget about python builtin complex number support which is quite nice for 2d point operations ;)</p><p><pre>
dirs = "NESW"                   # Notations for directions
shifts = [1j,1,-1j,-1] # delta vector for each direction
</pre></p><p>One letter function names corresponding to each robot instruction</p><p><pre>
r = lambda x, a: (x, (a + 1) % 4)
l = lambda x, a: (x, (a - 1) % 4)
m = lambda x, a: (x + shifts[a], a)
raw_input()                     # Ignore the grid size
while 1:
    # parse initial position triplet
    x, y, dir = raw_input().split() 
    pos = (int(x) + 1j * int(y),dirs.find(dir))
    # parse instructions
    instrns = raw_input().lower() 
    # Invoke the corresponding functions passing prev position
    for i in instrns: pos = locals()<a href="*pos" rel="nofollow">i</a>
    print int(pos[0].real), int(pos[0].imag), dirs[pos[1]]</pre></p><p>May I also suggest replacing raw_input() / while 1 stuff by a more concise generator stuff:</p><p><pre>
import sys
dirs = "NESW"                   # Notations for directions
shifts = [1j,1,-1j,-1] # delta vector for each direction</pre></p><p>One letter function names corresponding to each robot instruction</p><p><pre>r = lambda x, a: (x, (a + 1) % 4)
l = lambda x, a: (x, (a - 1) % 4)
m = lambda x, a: (x + shifts[a], a)
def two_by_two(lines):
    while True: yield next(lines).split(), next(lines).strip()
next(sys.stdin) # ignore the grid size
for (x, y, dir), actions in two_by_two(sys.stdin):
    pos = (int(x) + 1j * int(y),dirs.find(dir))
    # parse instructions
    instrns = actions.lower()
    # Invoke the corresponding functions passing prev position
    for i in instrns: pos = locals()<a href="*pos" rel="nofollow">i</a>
    print int(pos[0].real), int(pos[0].imag), dirs[pos[1]]</pre></p><p>This remove the EOFError ;)</p><p>I like that kind of little snippets of code, there is always something fun to <em>enhance</em> (in good or bad&#8230;)</p>]]></content:encoded> </item> <item><title>By: Martin</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1713</link> <dc:creator>Martin</dc:creator> <pubDate>Mon, 01 Feb 2010 15:19:10 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1713</guid> <description>&lt;p&gt;Oh, I see. With javascript deactivated the code is not visible.&lt;/p&gt; </description> <content:encoded><![CDATA[<p>Oh, I see. With javascript deactivated the code is not visible.</p>]]></content:encoded> </item> <item><title>By: Arun Bhai</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1710</link> <dc:creator>Arun Bhai</dc:creator> <pubDate>Mon, 01 Feb 2010 11:18:22 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1710</guid> <description>&lt;p&gt;Hi Martin,&lt;/p&gt;&lt;p&gt;I suppose you saw this post at some aggregation site. The code snippets were embedded in the original post. The &lt;a href=&quot;http://gist.github.com/291495&quot; rel=&quot;nofollow&quot;&gt;Python code&lt;/a&gt; and &lt;a href=&quot;http://gist.github.com/291497&quot; rel=&quot;nofollow&quot;&gt;Haskell code&lt;/a&gt; were provided&lt;/p&gt; </description> <content:encoded><![CDATA[<p>Hi Martin,</p><p>I suppose you saw this post at some aggregation site. The code snippets were embedded in the original post. The <a
href="http://gist.github.com/291495" rel="nofollow">Python code</a> and <a
href="http://gist.github.com/291497" rel="nofollow">Haskell code</a> were provided</p>]]></content:encoded> </item> <item><title>By: Martin</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/comment-page-1/#comment-1709</link> <dc:creator>Martin</dc:creator> <pubDate>Mon, 01 Feb 2010 10:33:24 +0000</pubDate> <guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234#comment-1709</guid> <description>&lt;p&gt;So is it possible to see your solution anywhere? I didn&#039;t see any link.&lt;/p&gt; </description> <content:encoded><![CDATA[<p>So is it possible to see your solution anywhere? I didn&#8217;t see any link.</p>]]></content:encoded> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 9/23 queries in 0.009 seconds using disk

Served from: mshawking.asmallorange.com @ 2010-07-31 11:13:31 -->