<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-12914630</id><updated>2011-10-27T05:39:15.332-07:00</updated><title type='text'>Ivan Peev blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-12914630.post-457307379504475969</id><published>2010-11-05T04:44:00.000-07:00</published><updated>2010-11-05T07:29:29.115-07:00</updated><title type='text'>Wordhacking in Python</title><content type='html'>Reddit is hiring again and number 5 of the bonus points here:&lt;a href="http://blog.reddit.com/2010/08/reddit-is-hiring.html"&gt; http://blog.reddit.com/2010/08/reddit-is-hiring.html&lt;/a&gt; is:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;font-family:'Times New Roman';font-size:medium;"  &gt;&lt;span class="Apple-style-span" style="line-height: 25px;font-family:Arial,sans-serif;font-size:17px;"  &gt;5. ...compose us a piece of customized wordhacking, e.g.:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;font-family:'Times New Roman';font-size:medium;"  &gt;&lt;span class="Apple-style-span" style="line-height: 25px;font-family:Arial,sans-serif;font-size:17px;"  &gt;&lt;div   style="text-align: center; line-height: 1em;font-family:monospace;font-size:x-large;"&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;p&lt;/span&gt;it&lt;span style="font-weight: bold; background-color: rgb(221, 255, 221);"&gt;h&lt;/span&gt;ie&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;r&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;l&lt;/span&gt;eg&lt;span style="font-weight: bold; background-color: rgb(221, 255, 221);"&gt;i&lt;/span&gt;bl&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;e&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;e&lt;/span&gt;me&lt;span style="font-weight: bold; background-color: rgb(221, 255, 221);"&gt;r&lt;/span&gt;al&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;d&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;a&lt;/span&gt;ll&lt;span style="font-weight: bold; background-color: rgb(221, 255, 221);"&gt;e&lt;/span&gt;ge&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;d&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;s&lt;/span&gt;pu&lt;span style="font-weight: bold; background-color: rgb(255, 255, 170);"&gt;m&lt;/span&gt;on&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;i&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);"&gt;e&lt;/span&gt;xc&lt;span style="font-weight: bold; background-color: rgb(255, 255, 170);"&gt;e&lt;/span&gt;rp&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);"&gt;t&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;That sounds like an interesting task and here we will try solve it using Python and more specifically the &lt;tt class="descname"&gt;intersection&lt;/tt&gt; operation of the &lt;span style="font-weight: bold; font-style: italic;"&gt;set&lt;/span&gt; type. For example one solution to the text "&lt;span style="font-weight: bold; font-style: italic;"&gt;reddit is now wordhacking&lt;/span&gt;" is :&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;h&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;w&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;m&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;w&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;h&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;p&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;h&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;l&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;u&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;k&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;b&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;w&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;" &gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:courier new;"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;And another:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;u&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;w&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;y&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;h&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;m&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;h&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;t&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;u&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;c&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;l&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;k&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;a&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;m&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;o&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;d&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;n&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;e&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;s&lt;/span&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;w&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;r&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;i&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(255, 221, 221);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;g&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;font-size:130%;"  &gt;l&lt;/span&gt;&lt;span style="font-weight: bold; background-color: rgb(221, 255, 255);font-family:monospace;" &gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:courier new;"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Follows the source. The function that do all the work is &lt;span style="font-family:Lucida,Courier New;"&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;make_wordhacking&lt;/span&gt;&lt;/span&gt;. When run for the first time the program will get a dictionary with words from Internet. The program will print only one (the first) solution for each combination of columns and word size.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;font face="Lucida,Courier New"&gt;&lt;font color="#C00000"&gt;from&lt;/font&gt; &lt;font color="#000000"&gt;__future__&lt;/font&gt; &lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;division&lt;/font&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;from&lt;/font&gt; &lt;font color="#000000"&gt;urllib2&lt;/font&gt; &lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;urlopen&lt;/font&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;from&lt;/font&gt; &lt;font color="#000000"&gt;collections&lt;/font&gt; &lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;defaultdict&lt;/font&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;from&lt;/font&gt; &lt;font color="#000000"&gt;itertools&lt;/font&gt; &lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;combinations&lt;/font&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;from&lt;/font&gt; &lt;font color="#000000"&gt;zipfile&lt;/font&gt; &lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;ZipFile&lt;/font&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;import&lt;/font&gt; &lt;font color="#000000"&gt;os&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;iterate_in_group_by&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;num&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt; &lt;font color="#000000"&gt;cx&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#004080"&gt;"iterate_in_group_by(3, 'ABCDEFG') --&amp;gt; ABC DEF G"&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;c&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;while&lt;/font&gt; &lt;font color="#000000"&gt;c&lt;/font&gt; &lt;font color="#0000C0"&gt;&amp;lt;&lt;/font&gt; &lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;cx&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;yield&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;cx&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;c&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;font color="#000000"&gt;c&lt;/font&gt;&lt;font color="#0000C0"&gt;+&lt;/font&gt;&lt;font color="#000000"&gt;num&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;        &lt;font color="#000000"&gt;c&lt;/font&gt; &lt;font color="#0000C0"&gt;+=&lt;/font&gt; &lt;font color="#000000"&gt;num&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;get_words&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;fn&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#004080"&gt;"corncob_lowercase.zip"&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#C00000"&gt;not&lt;/font&gt; &lt;font color="#000000"&gt;os&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;path&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;isfile&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;fn&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#000000"&gt;data&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;urlopen&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt; &lt;font color="#004080"&gt;"http://www.mieliestronk.com/"&lt;/font&gt; &lt;font color="#0000C0"&gt;+&lt;/font&gt; &lt;font color="#000000"&gt;fn&lt;/font&gt; &lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;read&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;with&lt;/font&gt; &lt;font color="#000000"&gt;open&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;fn&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#004080"&gt;"w"&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt; &lt;font color="#C00000"&gt;as&lt;/font&gt; &lt;font color="#000000"&gt;fw&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                &lt;font color="#000000"&gt;fw&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;write&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;data&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;fz&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;ZipFile&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;fn&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;return&lt;/font&gt; &lt;font color="#000000"&gt;fz&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;open&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;fz&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;namelist&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;read&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;split&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;make_sets&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;W&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;S&lt;/font&gt;&lt;font color="#0000C0"&gt;=&lt;/font&gt;&lt;font color="#000000"&gt;defaultdict&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#C00000"&gt;lambda&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt; &lt;font color="#000000"&gt;defaultdict&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;set&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;word&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;W&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;c&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;enumerate&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;word&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#000000"&gt;S&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;c&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;add&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;word&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;return&lt;/font&gt; &lt;font color="#000000"&gt;S&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;factors&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;n&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;k&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0080C0"&gt;2&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;n&lt;/font&gt;&lt;font color="#0000C0"&gt;//&lt;/font&gt;&lt;font color="#0080C0"&gt;2&lt;/font&gt;&lt;font color="#0000C0"&gt;+&lt;/font&gt;&lt;font color="#0080C0"&gt;1&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;n&lt;/font&gt;&lt;font color="#0000C0"&gt;%&lt;/font&gt;&lt;font color="#000000"&gt;k&lt;/font&gt; &lt;font color="#0000C0"&gt;==&lt;/font&gt; &lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;yield&lt;/font&gt; &lt;font color="#000000"&gt;k&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;print_reddit_comment_solution&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;"---------------------------------------------------"&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;"We have a solution"&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#000000"&gt;P&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;v&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;values&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;w&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;list&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;a&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt; &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;a&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;v&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;p&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;p&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#004080"&gt;"`[%s](http://)`"&lt;/font&gt;&lt;font color="#0000C0"&gt;%&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;""&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;join&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#004080"&gt;"\n`%s`"&lt;/font&gt; &lt;font color="#0000C0"&gt;%&lt;/font&gt; &lt;font color="#004080"&gt;""&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;join&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;split&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#004080"&gt;"``"&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;break&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;print_html_solution&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;"""\n&amp;lt;br/&amp;gt;&amp;lt;div style="font-family: monospace; text-align: center; font-size: x-large; line-height: 1em;"&amp;gt;"""&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;v&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;values&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;w&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;list&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;a&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt; &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;a&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;v&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;p&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;p&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#004080"&gt;"""&amp;lt;span style="font-family:monospace; font-weight: bold; background-color: rgb(255, 221, 221);"&amp;gt;%s&amp;lt;/span&amp;gt;"""&lt;/font&gt;&lt;font color="#0000C0"&gt;%&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;                &lt;font color="#C00000"&gt;else&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#004080"&gt;"""&amp;lt;span style="font-family:monospace; font-weight: bold; background-color: rgb(221, 255, 255);"&amp;gt;%s&amp;lt;/span&amp;gt;"""&lt;/font&gt; &lt;font color="#0000C0"&gt;%&lt;/font&gt; &lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;p&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;"&amp;lt;br/&amp;gt;%s"&lt;/font&gt; &lt;font color="#0000C0"&gt;%&lt;/font&gt; &lt;font color="#004080"&gt;""&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;join&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;w&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;break&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#004080"&gt;"""&amp;lt;/div&amp;gt;"""&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;def&lt;/font&gt; &lt;font color="#000000"&gt;make_wordhacking&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;text&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;W&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;text&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;x&lt;/font&gt; &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;x&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;text&lt;/font&gt; &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;x&lt;/font&gt;&lt;font color="#0000C0"&gt;.&lt;/font&gt;&lt;font color="#000000"&gt;isalpha&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;colnum&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;factors&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;text&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;print&lt;/font&gt; &lt;font color="#000000"&gt;colnum&lt;/font&gt;&lt;br /&gt;        &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;w_len&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;colnum&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#0080C0"&gt;15&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;            &lt;font color="#000000"&gt;S&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;make_sets&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt; &lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;word&lt;/font&gt; &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;word&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;W&lt;/font&gt; &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;word&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;==&lt;/font&gt;&lt;font color="#000000"&gt;w_len&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt; &lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;            &lt;font color="#000000"&gt;T&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;list&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;iterate_in_group_by&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;text&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;//&lt;/font&gt;&lt;font color="#000000"&gt;colnum&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;text&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;            &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;P&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;combinations&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;w_len&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;colnum&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;=&lt;/font&gt;&lt;font color="#0000C0"&gt;{&lt;/font&gt;&lt;font color="#0000C0"&gt;}&lt;/font&gt;&lt;br /&gt;                &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;i&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;range&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;len&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;T&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;C&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;col&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;col&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;T&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;S&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;C&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#0080C0"&gt;0&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#C00000"&gt;for&lt;/font&gt; &lt;font color="#000000"&gt;ind&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;pos&lt;/font&gt; &lt;font color="#C00000"&gt;in&lt;/font&gt; &lt;font color="#000000"&gt;enumerate&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#0080C0"&gt;1&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                        &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt; &lt;font color="#0000C0"&gt;&amp;amp;&lt;/font&gt; &lt;font color="#000000"&gt;S&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;C&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;ind&lt;/font&gt;&lt;font color="#0000C0"&gt;+&lt;/font&gt;&lt;font color="#0080C0"&gt;1&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;pos&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#C00000"&gt;not&lt;/font&gt; &lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#0000C0"&gt;]&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                        &lt;font color="#C00000"&gt;break&lt;/font&gt;&lt;br /&gt;                &lt;font color="#C00000"&gt;else&lt;/font&gt; &lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;                    &lt;font color="#000000"&gt;print_reddit_comment_solution&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;R&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;P&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#C00000"&gt;if&lt;/font&gt; &lt;font color="#000000"&gt;__name__&lt;/font&gt; &lt;font color="#0000C0"&gt;==&lt;/font&gt; &lt;font color="#004080"&gt;"__main__"&lt;/font&gt;&lt;font color="#0000C0"&gt;:&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;W&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt;&lt;font color="#000000"&gt;get_words&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;text&lt;/font&gt; &lt;font color="#0000C0"&gt;=&lt;/font&gt; &lt;font color="#004080"&gt;"reddit is now wordhacking"&lt;/font&gt;&lt;br /&gt;    &lt;font color="#000000"&gt;make_wordhacking&lt;/font&gt;&lt;font color="#0000C0"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;text&lt;/font&gt;&lt;font color="#0000C0"&gt;,&lt;/font&gt;&lt;font color="#000000"&gt;W&lt;/font&gt;&lt;font color="#0000C0"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;br /&gt;Sometimes it will generate a lot of solutions, sometimes not at all. Try to choose an input text that has a length with a lot of factors.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-457307379504475969?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/457307379504475969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=457307379504475969' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/457307379504475969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/457307379504475969'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2010/11/wordhacking-in-python.html' title='Wordhacking in Python'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-9082691464284526105</id><published>2008-04-09T02:18:00.000-07:00</published><updated>2008-04-09T05:19:08.757-07:00</updated><title type='text'>Solving the Monty Hall problem with simulation in Python</title><content type='html'>On at least two occasions I've met the Monty Hall problem. The first time it was in the book &lt;a href="http://en.wikipedia.org/wiki/The_Curious_Incident_of_the_Dog_in_the_Night-time"&gt;"The Curious Incident of the Dog in the Night-time"&lt;/a&gt; from Mark Haddon. And today I found it in an article called &lt;a href="http://www.nytimes.com/2008/04/08/science/08tier.html?8dpc=&amp;amp;pagewanted=print"&gt;"Monty Hall strikes again - reveals fatal flaw in some of the most famous psychology experiments"&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;It is so counter intuitive that it is amazing. The problem is this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Monty_Hall_problem"&gt;http://en.wikipedia.org/wiki/Monty_Hall_problem&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;My intuition says there is no need to change doors. The probability to win a car is obvious and it is 1/3. But the mathematical solution says the probability to win a car if you change doors is 2/3. Amazing if it is true! So lets find out who is right? How? Will make a simulation, will run it many many times and will get the average. This is the Python program that solves the problem:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;from __future__ import division&lt;br /&gt;from random import shuffle, choice&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def get_random_doors():&lt;br /&gt;    doors = ["goat","goat","car"]&lt;br /&gt;    shuffle(doors)&lt;br /&gt;    return doors&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;def get_host_open(doors, guest_pick1):&lt;br /&gt;    host_open = 0&lt;br /&gt;    remaining_doors = [ k for k in [0,1,2] &lt;br /&gt;                                if k != guest_pick1]&lt;br /&gt;    if doors[guest_pick1] == "car":&lt;br /&gt;        host_open = choice(remaining_doors)&lt;br /&gt;    else:&lt;br /&gt;        [host_open] = [d for d in remaining_doors &lt;br /&gt;                                if doors[d] != "car"]&lt;br /&gt;    return host_open&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;def simulation(strategy):&lt;br /&gt;    N = 100000&lt;br /&gt;    cars_won = 0&lt;br /&gt;    for _ in range(N):&lt;br /&gt;        doors = get_random_doors()&lt;br /&gt;        guest_pick1 = choice([0,1,2])&lt;br /&gt;        host_open = get_host_open(doors, guest_pick1)&lt;br /&gt;        if strategy == "stay":&lt;br /&gt;            guest_pick_final = guest_pick1&lt;br /&gt;        elif strategy == "change":&lt;br /&gt;            [guest_pick_final] = [ k for k in [0,1,2] &lt;br /&gt;                                    if k != guest_pick1 and k != host_open]&lt;br /&gt;        else:&lt;br /&gt;            raise ("Unknown strategy")&lt;br /&gt;            &lt;br /&gt;        if doors[guest_pick_final] == "car":&lt;br /&gt;            cars_won += 1&lt;br /&gt;        &lt;br /&gt;    print strategy, cars_won/N&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;simulation("stay")&lt;br /&gt;simulation("change")&lt;br /&gt;    &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is a Python 2.4 and 2.5 code. The problem is written with the goal to be easy to read and verify and is not optimized for performance. Lets run it. The result is:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;stay 0.33478&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;change 0.66407&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So change doors, definitely change doors if you can!&lt;br /&gt;&lt;br /&gt;&lt;script&gt;reddit_url='http://ipeev.blogspot.com/2008/04/solving-monty-hall-problem-with.html'&lt;/script&gt;&lt;br /&gt;&lt;script&gt;reddit_title='Solving the Monty Hall problem with simulation in Python'&lt;/script&gt;&lt;br /&gt;&lt;script language="javascript" src="http://reddit.com/button.js?t=3"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-9082691464284526105?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/9082691464284526105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=9082691464284526105' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/9082691464284526105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/9082691464284526105'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2008/04/solving-monty-hall-problem-with.html' title='Solving the Monty Hall problem with simulation in Python'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-2912171611665330907</id><published>2007-04-14T03:04:00.000-07:00</published><updated>2009-09-04T03:23:22.851-07:00</updated><title type='text'>Peter Norvig's spell checker</title><content type='html'>I think that Peter Norvig changed slightly the implementation of his spell checker, because of me: &lt;a href="http://www.norvig.com/spell-correct.html"&gt;http://www.norvig.com/spell-correct.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;That happened during discussion on the reddit's comments thread about his spell checker: &lt;a href="http://programming.reddit.com/info/1gb59/comments"&gt;http://programming.reddit.com/info/1gb59/comments&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ipeev 2 points 4 days ago*&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;I see a small problem in the Norvig's code. This part is supposed to make 26n alterations:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[word[0:i]+c+word[i+1:] for i in range(n) for c in string.lowercase] + ## alteration&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;But not really. This is what string.lowercase returns on my computer:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;import string&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;string.lowercase&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;'abcdefghijklmnopqrstuvwxyz\x83\x9a\x9c\x9e\xaa\xb5\xba\xdf\xe0 \xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0 \xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Total of 65 characters. Same for insertion.&lt;br /&gt;&lt;br /&gt;Is this a real problem? It could be! One has to be aware of the Python's battery included philosophy.&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Then someone responds:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;br /&gt;vetinari 1 point 2 days ago&lt;br /&gt;&lt;br /&gt;Check your locale, mine is fine:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;&gt;&gt; import string&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;&gt;&gt; [c for c in string.lowercase]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;He is right too, but I am not going to change my locale, so I respond:&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;br /&gt;ipeev 1 point 2 days ago&lt;br /&gt;&lt;br /&gt;My point is that the Norvig's code is not aware of the localization features of Python. If he wants his program to run on any computer, the simplest think to do is use the first 26 letters only:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;string.lowercase[:26]&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But Norvig didn't exactly listened to me and implemented different solution:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;span class="pln"&gt;alphabet &lt;/span&gt;&lt;span class="pun"&gt;=&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="str"&gt;'abcdefghijklmnopqrstuvwxyz'&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd"&gt;def&lt;/span&gt;&lt;span class="pln"&gt; edits1&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;):&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;n &lt;/span&gt;&lt;span class="pun"&gt;=&lt;/span&gt;&lt;span class="pln"&gt; len&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;)&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd"&gt;return&lt;/span&gt;&lt;span class="pln"&gt; set&lt;/span&gt;&lt;span class="pun"&gt;([&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="lit"&gt;0&lt;/span&gt;&lt;span class="pun"&gt;:&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="lit"&gt;1&lt;/span&gt;&lt;span class="pun"&gt;:]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; i &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; range&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;n&lt;/span&gt;&lt;span class="pun"&gt;)]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="pln"&gt;                     &lt;/span&gt;&lt;span class="com"&gt;# deletion&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="lit"&gt;0&lt;/span&gt;&lt;span class="pun"&gt;:&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="lit"&gt;1&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="lit"&gt;2&lt;/span&gt;&lt;span class="pun"&gt;:]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; i &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; range&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;n&lt;/span&gt;&lt;span class="pun"&gt;-&lt;/span&gt;&lt;span class="lit"&gt;1&lt;/span&gt;&lt;span class="pun"&gt;)]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="com"&gt;# transposition&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="lit"&gt;0&lt;/span&gt;&lt;span class="pun"&gt;:&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;c&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="lit"&gt;1&lt;/span&gt;&lt;span class="pun"&gt;:]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; i &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; range&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;n&lt;/span&gt;&lt;span class="pun"&gt;)&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; c &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; alphabet&lt;/span&gt;&lt;span class="pun"&gt;]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="com"&gt;# alteration&lt;/span&gt;&lt;span class="pln"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="lit"&gt;0&lt;/span&gt;&lt;span class="pun"&gt;:&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;]+&lt;/span&gt;&lt;span class="pln"&gt;c&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="pln"&gt;word&lt;/span&gt;&lt;span class="pun"&gt;[&lt;/span&gt;&lt;span class="pln"&gt;i&lt;/span&gt;&lt;span class="pun"&gt;:]&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; i &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; range&lt;/span&gt;&lt;span class="pun"&gt;(&lt;/span&gt;&lt;span class="pln"&gt;n&lt;/span&gt;&lt;span class="pun"&gt;+&lt;/span&gt;&lt;span class="lit"&gt;1&lt;/span&gt;&lt;span class="pun"&gt;)&lt;/span&gt;&lt;span class="pln"&gt; &lt;/span&gt;&lt;span class="kwd"&gt;for&lt;/span&gt;&lt;span class="pln"&gt; c &lt;/span&gt;&lt;span class="kwd"&gt;in&lt;/span&gt;&lt;span class="pln"&gt; alphabet&lt;/span&gt;&lt;span class="pun"&gt;])&lt;/span&gt;&lt;span class="pln"&gt;  &lt;/span&gt;&lt;span class="com"&gt;# insertion&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pln"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So instead of choosing my cryptic syntax he implemented it with explicit alphabet instead. Apparently he also thinks that explicit is better than implicit.&lt;br /&gt;&lt;br /&gt;At the end of his page he writes:&lt;br /&gt;&lt;blockquote&gt; Originally my program was 20 lines, but a &lt;span style="color: rgb(204, 0, 0);"&gt;reader &lt;/span&gt;pointed out that I had used  &lt;tt&gt;string.lowercase&lt;/tt&gt;, which in some locales in some versions of Python, has more characters than just the &lt;tt&gt;a-z&lt;/tt&gt; I intended.  So I added the variable  &lt;tt&gt;alphabet&lt;/tt&gt; to make sure. &lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;I think I might have been that reader. Just for information the locale in question is Bulgarian. Sorry for causing the trouble with all those regional alphabets, I know it a pain in the ... . I like his site very much and enjoyed the "Tutorial on Good Lisp Programming Style" and even I have it printed on paper at home!&lt;br /&gt;&lt;br /&gt;UPDATE: I've sent an email to Norvig and asked him about this and he responded that indeed, he did read my comment on reddit and thats why he changed the implementation. Now my name is there too which is cool!&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-style: italic;"&gt; Originally my program was 20 lines, but &lt;span style="font-weight: bold;"&gt;Ivan Peev&lt;/span&gt; pointed out that I had used  &lt;/span&gt;&lt;tt style="font-style: italic;"&gt;string.lowercase&lt;/tt&gt;&lt;span style="font-style: italic;"&gt;, which in some locales in some versions of Python, has more characters than just the &lt;/span&gt;&lt;tt style="font-style: italic;"&gt;a-z&lt;/tt&gt;&lt;span style="font-style: italic;"&gt; I intended.  So I added the variable  &lt;/span&gt;&lt;tt style="font-style: italic;"&gt;alphabet&lt;/tt&gt;&lt;span style="font-style: italic;"&gt; to make sure.  I could have used &lt;/span&gt;&lt;tt style="font-style: italic;"&gt;string.ascii_lowercase&lt;/tt&gt;&lt;span style="font-style: italic;"&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-2912171611665330907?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/2912171611665330907/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=2912171611665330907' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/2912171611665330907'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/2912171611665330907'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2007/04/peter-norvigs-spell-checker.html' title='Peter Norvig&apos;s spell checker'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-3009702590076556879</id><published>2007-03-20T08:48:00.000-07:00</published><updated>2007-03-21T23:36:40.065-07:00</updated><title type='text'>Solving the "Mr.S and Mr.P" puzzle by John McCarthy in Python</title><content type='html'>I have found recently an interesting article on reddit, about solving a logic puzzle in Haskel:&lt;br /&gt;&lt;a href="http://okmij.org/ftp/Haskell/Mr-S-P.lhs"&gt;http://okmij.org/ftp/Haskell/Mr-S-P.lhs&lt;/a&gt;&lt;br /&gt;While the technique presented is interesting itself, I find it transferable to Python with even improved readability.&lt;br /&gt;And here is the original statement:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;Formalization of two Puzzles Involving Knowledge&lt;br /&gt; McCarthy, John (1987).&lt;br /&gt; http://www-formal.stanford.edu/jmc/puzzles.html&lt;br /&gt;&lt;br /&gt;We pick two numbers a and b, so that a&gt;=b and both numbers are within&lt;br /&gt;the range [2,99]. We give Mr.P the product a*b and give Mr.S the sum&lt;br /&gt;a+b.&lt;br /&gt;&lt;br /&gt;The following dialog takes place:&lt;br /&gt;&lt;br /&gt; Mr.P: I don't know the numbers&lt;br /&gt; Mr.S: I knew you didn't know. I don't know either.&lt;br /&gt; Mr.P: Now I know the numbers&lt;br /&gt; Mr.S: Now I know them too&lt;br /&gt;&lt;br /&gt;Can we find the numbers a and b?&lt;/pre&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Follows the solution written in Python 2.5. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;# all pairs of a,b&lt;br /&gt;pairs = [ (a,b) for a in range(2,99+1) for b in range(2,99+1) if a&gt;=b ]&lt;br /&gt;&lt;br /&gt;# calculates map of solutions&lt;br /&gt;def calc_map(oper):&lt;br /&gt;    M={}&lt;br /&gt;    for a,b in pairs:&lt;br /&gt;        m = oper(a,b)&lt;br /&gt;        if not m in M:&lt;br /&gt;            M[m] = []&lt;br /&gt;        M[m].append( (a,b) )&lt;br /&gt;    return M&lt;br /&gt;&lt;br /&gt;# function that tests for single solution&lt;br /&gt;single = lambda lx: len(lx) == 1&lt;br /&gt;&lt;br /&gt;# maps that hold the sum and the product solutions, &lt;br /&gt;# dictionaries with list values&lt;br /&gt;S = calc_map(lambda a,b: a+b)&lt;br /&gt;P = calc_map(lambda a,b: a*b)&lt;br /&gt;&lt;br /&gt;# Rules list&lt;br /&gt;rule_MrP_dont_know      = lambda p: not single (P[p])&lt;br /&gt;&lt;br /&gt;rule_MrS_dont_know      = lambda s: not single (S[s])&lt;br /&gt;&lt;br /&gt;rule_MrS_knew_MrP_doesnt_know = lambda s: all( [rule_MrP_dont_know( a*b ) &lt;br /&gt;                                                        for a,b in S[s] ] )&lt;br /&gt;        &lt;br /&gt;rule_MrP_now_knows      = lambda p: single( [ (a,b) for a,b in P[p] &lt;br /&gt;                                        if rule_MrS_knew_MrP_doesnt_know(a+b) ])&lt;br /&gt;&lt;br /&gt;rule_MrS_knows_MrP_now_know = lambda s: single([ (a,b) for a,b in S[s] &lt;br /&gt;                                            if rule_MrP_now_knows(a*b) ])&lt;br /&gt;# Solve it&lt;br /&gt;for a, b in pairs:&lt;br /&gt;    s,p = a+b, a*b&lt;br /&gt;    if rule_MrP_dont_know(p) \&lt;br /&gt;            and rule_MrS_dont_know(s) \&lt;br /&gt;            and rule_MrS_knew_MrP_doesnt_know(s)\&lt;br /&gt;            and rule_MrP_now_knows(p) \&lt;br /&gt;            and rule_MrS_knows_MrP_now_know(s):&lt;br /&gt;        print "Answer is:" , a,b&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;And if we run the program the answer we get is:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Answer is: 13 4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;I used the "all" function to increase readability and this is the only reason a 2.5 version is needed. Use own "all" function for a 2.4 version.&lt;br /&gt;&lt;br /&gt;Now what we have here is almost a short implementation of a rule engine with forward-chaining. It certainly misses a lot of functionality of a real rule engine, but the actual process of rules execution is probabbly the same. Is Python a good language for this kind of tasks? I think it is.&lt;br /&gt;&lt;br /&gt;The Python solution does not follow exactly the Haskell route neither the the paper by McCarthy. MsCarthy I find rather difficult to read and Haskel has its own oddity that makes me screem sometimes in the middle of the night.&lt;br /&gt;&lt;br /&gt;The Python rules try to be more directly mapped to the conditions in the task. Readability counts and of course beautiful is better than ugly.&lt;br /&gt;&lt;br /&gt;&lt;script&gt;reddit_url='http://ipeev.blogspot.com/2007/03/solving-mrs-and-mrp-puzzle-by-john.html'&lt;/script&gt;&lt;br /&gt;&lt;script language="javascript" src="http://reddit.com/button.js?t=3"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-3009702590076556879?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/3009702590076556879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=3009702590076556879' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/3009702590076556879'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/3009702590076556879'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2007/03/solving-mrs-and-mrp-puzzle-by-john.html' title='Solving the &quot;Mr.S and Mr.P&quot; puzzle by John McCarthy in Python'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-115806739614546416</id><published>2006-09-12T06:20:00.000-07:00</published><updated>2006-09-12T06:23:16.160-07:00</updated><title type='text'>Solving the Google Code Jam "countPaths" problem in Python</title><content type='html'>&lt;h2 class="title"&gt;Solving the Google Code Jam "countPaths" problem in Python&lt;/h2&gt; &lt;p class="postdate"&gt;Wednesday, 16. August 2006, 13:54:26&lt;/p&gt; &lt;p class="tags"&gt;&lt;a href="http://my.opera.com/ipeev/blog/index.dml/tag/Google%20Code%20Jam" rel="tag"&gt;Google Code Jam&lt;/a&gt;, &lt;a href="http://my.opera.com/ipeev/blog/index.dml/tag/Python" rel="tag"&gt;Python&lt;/a&gt; &lt;/p&gt; &lt;div class="content"&gt;I found this article about Haskell:&lt;br /&gt;&lt;a href="http://blog.moertel.com/articles/2006/08/15/solving-the-google-code-jam-countpaths-problem-in-haskell" target="_blank"&gt;Solving the Google Code Jam "countPaths" problem in Haskell&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Recently Guido van Rossum &lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=172054" target="_blank"&gt;announced&lt;/a&gt; that Python is one of the supported languages in the next Google Code Jam. I decided to write a solution to the puzzle in Python. I haven't looked at the Haskell code. It is too strange anyway.(*) Neither looked at the C code from the other site - too long. Here it is in Python in 25 lines. And it is very fast.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class WordPath:&lt;br /&gt;   def howMany(self, (x,y), word):&lt;br /&gt;       if x &lt;&gt;=self.N or y&lt;0&gt;=self.M or self.grid[x][y] != word[0]:&lt;br /&gt;           return 0&lt;br /&gt;       if len(word) == 1:&lt;br /&gt;           return 1&lt;br /&gt;       s = 0&lt;br /&gt;       for a in (x-1, x, x+1):&lt;br /&gt;           for b in (y-1, y, y+1):&lt;br /&gt;               if not (a == x and b ==y):&lt;br /&gt;                   if (a, b, word) not in self.cache:&lt;br /&gt;                       self.cache[ (a,b,word) ] = self.howMany( (a,b), word[1:])&lt;br /&gt;                   s += self.cache[ (a,b,word) ]&lt;br /&gt;       return s&lt;br /&gt;&lt;br /&gt;   def countPaths( self, grid, word):&lt;br /&gt;       self.grid = grid&lt;br /&gt;       self.N = len(grid)&lt;br /&gt;       self.M = len(grid[0])&lt;br /&gt;       self.cache = {}&lt;br /&gt;       s = sum ( [ self.howMany( (x,y), word) for x in range (self.N)&lt;br /&gt;                                                   for y in range(self.M)] )&lt;br /&gt;       if s &gt; 1000000000:&lt;br /&gt;           s = -1&lt;br /&gt;       return s&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;test = WordPath()&lt;br /&gt;assert 1 == test.countPaths( ("ABC","FED","GHI"), "ABCDEFGHI")&lt;br /&gt;assert 108 == test.countPaths( ("AA","AA"), "AAAA")&lt;br /&gt;assert 2 == test.countPaths( ("ABC","FED","GAI"), "ABCDEA")&lt;br /&gt;assert 0 == test.countPaths( ("ABC","DEF","GHI"), "ABCD")&lt;br /&gt;assert 56448 == test.countPaths( ("ABABA","BABAB","ABABA","BABAB","ABABA"), "ABABABBA")&lt;br /&gt;assert -1 == test.countPaths( ("AAAAA","AAAAA","AAAAA","AAAAA","AAAAA"), "AAAAAAAAAAA")&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cs.uic.edu/%7Ehnagaraj/articles/code-jam/problem.txt" target="_blank"&gt;Original Problem Statement here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The author of the Haskell article Tom Moertel investigates this extreme case:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;"to find a word composed of 50 “A” letters within a 50×50 grid of “A” cells.". &lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Let us see if we remove the check for exceeding 1 billion what will happen?&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print test.countPaths( [ "A"*50 for a in range(50)], "A"*50)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The result is 303835410591851117616135618108340196903254429200 and this is the same&lt;br /&gt;value that Tom Moertel found.&lt;br /&gt;&lt;br /&gt;The calculation took whole 8 seconds on oooold Athlon 1000Mhz with Win XP.&lt;br /&gt;&lt;br /&gt;Nice.&lt;br /&gt;&lt;br /&gt;Well not really. The Google Code Jam rules are as Tom Moertel pointed out &lt;i&gt;"All submissions have a maximum of 2 seconds of runtime per test case"&lt;/i&gt;. If Goggle is running the tests on 4Ghz Athlon we will be almost in limits. But lets not take chances. We have to stop the run earlier. That unfortunately will increase our code size. One very straightforward solution is with exceptions:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import timeit&lt;br /&gt;&lt;br /&gt;class WordPath:&lt;br /&gt;   def howMany(self, (x,y), word):&lt;br /&gt;       if x &lt;&gt;=self.N or y&lt;0&gt;=self.M or self.grid[x][y] != word[0]:&lt;br /&gt;           return 0&lt;br /&gt;       if len(word) == 1:&lt;br /&gt;           return 1&lt;br /&gt;       s = 0&lt;br /&gt;       for a in (x-1, x, x+1):&lt;br /&gt;           for b in (y-1, y, y+1):&lt;br /&gt;               if not (a == x and b ==y):&lt;br /&gt;                   if (a, b, word) not in self.cache:&lt;br /&gt;                       self.cache[ (a,b,word) ] = self.howMany( (a,b), word[1:])&lt;br /&gt;                   s += self.cache[ (a,b,word) ]&lt;br /&gt;                   if s &gt; 1000000000:&lt;br /&gt;                       raise OverflowError ('spam', 'eggs')&lt;br /&gt;       return s&lt;br /&gt;&lt;br /&gt;   def countPaths( self, grid, word):&lt;br /&gt;       self.grid = grid&lt;br /&gt;       self.N = len(grid)&lt;br /&gt;       self.M = len(grid[0])&lt;br /&gt;       self.cache = {}&lt;br /&gt;       try:&lt;br /&gt;           s = sum ( [ self.howMany( (x,y), word) for x in range (self.N)&lt;br /&gt;                                                   for y in range(self.M)] )&lt;br /&gt;           if s &gt; 1000000000:&lt;br /&gt;               s = -1&lt;br /&gt;           return s&lt;br /&gt;       except OverflowError :&lt;br /&gt;           return -1&lt;br /&gt;&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;t = timeit.Timer(stmt='WordPath().countPaths( [ "A"*50 for a in range(50)], "A"*50)',&lt;br /&gt;                   setup = 'from __main__ import WordPath')&lt;br /&gt;&lt;br /&gt;print "%.2f sec/pass" %  (t.timeit(number=100)/100)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the result on 1Ghz is the blazing speed of:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;0.03 sec/pass&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So the conclusion is that Python can be used in Google's Code Jam, but one must be carefull with the time limits!&lt;br /&gt;&lt;br /&gt;(*) Update. Some people are commenting on reddit my ignorance of the Haskell code. I actually learned most of Haskell some time ago, until I got to Monads. Then I found 267 articles expalining what Monads are from which 27 just tutorials. At that moment I tought "Enough! Maybe I will read it later.". The so called "Maybe" monad. That was 1.5 years ago.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt; &lt;a name="comments" id="comments"&gt;&lt;/a&gt; &lt;div class="comments"&gt;&lt;h2&gt;&lt;a href="http://my.opera.com/ipeev/xml/rss/comments/409336"&gt;&lt;img src="http://my.opera.com/community/graphics/feedicon16.gif" alt="" class="feedicon" height="16" width="16" /&gt;&lt;/a&gt; Comments&lt;/h2&gt;&lt;div class="comment1" id="comment1985327"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/steve_g/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;This is a neat solution to a problem that's combinatorially large.  I think the code is clean and easy to understand. &lt;br /&gt;&lt;br /&gt;I do have one question though - it appears to me that the WordPath class is used only for running the two methods. Wouldn't it be cleaner (two fewer lines and no "selfs") to just have the two methods in the global name space? What does the class structure bring to the party?&lt;br /&gt;&lt;br /&gt;Don't get me wrong; I'm not trying to trash the code. I'm sure I couldn't do as well. I'm just trying to improve my understanding of good python style.&lt;br /&gt;&lt;br /&gt;Thanks. &lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By &lt;a href="http://my.opera.com/steve_g/about/" class="username"&gt;steve_g&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment1985327"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 17. August 2006, 01:35:01&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="comment2 ownercomment" id="comment1985820"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/ipeev/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;Hi Steve,&lt;br /&gt;&lt;br /&gt;The solution is implemented with a class because of the Google Code Jam rules. They give the class structure and the competitor have to implement it. For this task the Java interface was:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Class:&lt;br /&gt;    WordPath&lt;br /&gt;    Method:&lt;br /&gt;    countPaths&lt;br /&gt;    Parameters:&lt;br /&gt;    String[], String&lt;br /&gt;    Returns:&lt;br /&gt;    int&lt;br /&gt;    Method signature:&lt;br /&gt;    int countPaths(String[] grid, String find)&lt;br /&gt;    (be sure your method is public)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="comment-user"&gt;By &lt;a href="http://my.opera.com/ipeev/about/" class="username"&gt;ipeev&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment1985820"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 17. August 2006, 04:41:58&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="comment1" id="comment1987705"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/almostobsolete/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;Here's a version which doesn't use a Dictionary to cache stuff :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class WordPath:&lt;br /&gt; def countPaths(self, grid, word):&lt;br /&gt;       last_round = [[((x == word[0] and 1) or 0) for x in y] for y in grid]&lt;br /&gt;       len_grid = len(grid)&lt;br /&gt;       len_grid_0 = len(grid[0])&lt;br /&gt;       for letter in word[1:]:&lt;br /&gt;           this_round  = [[0 for x in range(len_grid_0)] for y in range(len_grid)]&lt;br /&gt;           for x in range(len_grid):&lt;br /&gt;               for y in range(len_grid_0):&lt;br /&gt;                   if grid[x][y] == letter:&lt;br /&gt;                       for a in (x-1, x, x+1):&lt;br /&gt;                           for b in (y-1, y, y+1):                           &lt;br /&gt;                               if a &gt;= 0 and a &lt;&gt;= 0 and b &lt; len_grid_0 and not (a == x and b == y):&lt;br /&gt;                                   this_round[x][y] += last_round[a][ b]&lt;br /&gt;           last_round = this_round&lt;br /&gt;       total = sum(sum(x) for x in last_round)&lt;br /&gt;       if total &gt; 1000000000:&lt;br /&gt;           total = -1&lt;br /&gt;       return total&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Runs a little faster for the tests (on my machine anyway) but starts running a lot faster if you remove the "&gt; 1000000000" tests and use large test values.&lt;br /&gt;&lt;br /&gt;EDIT: Made it a bit more efficient&lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By &lt;a href="http://my.opera.com/almostobsolete/about/" class="username"&gt;almostobsolete&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment1987705"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 17. August 2006, 14:48:40&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="comment2 ownercomment" id="comment1988184"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/ipeev/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;Very interesting!&lt;br /&gt;&lt;br /&gt;After looking for several minutes at the code I think I understand now why and how this solution works.&lt;br /&gt;&lt;br /&gt;But not sure why it is faster. It aparently doesn't use cache. Maybe because it doesn't use recursion. I wish Python supports better recursive functions some day.&lt;br /&gt;&lt;br /&gt;Anyway. Measured 2.9 seconds on the same computer. Removing the check for "&gt; 1000000000" doesn't improve the time from what I see.&lt;br /&gt;&lt;br /&gt;Probably the trick with the early interruption can be used here too, because it is far behind the fastest 0.03 seconds solution with the exceptions.&lt;br /&gt;&lt;p class="editcomment"&gt;&lt;br /&gt;               &lt;/p&gt;&lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By &lt;a href="http://my.opera.com/ipeev/about/" class="username"&gt;ipeev&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment1988184"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 17. August 2006, 17:04:33&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="comment1" id="comment1988541"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/almostobsolete/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;Sorry, I wasn't very clear in my last message. What I meant was that it's quicker in getting the correct result (303835410591851117616135618108340196903254429200) for big all A's test (print test.countPaths( [ "A"*50 for a in range(50)], "A"*50)) and it gets relatively faster as the size of the grid or the word is increased.&lt;p class="editcomment"&gt;&lt;br /&gt;               &lt;/p&gt;&lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By &lt;a href="http://my.opera.com/almostobsolete/about/" class="username"&gt;almostobsolete&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment1988541"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 17. August 2006, 19:05:31&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="comment2" id="comment2003082"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/div&gt;&lt;div class="text"&gt;Thomas writes:&lt;br /&gt;&lt;br /&gt;I was coding this in the statistical language R (which is not fast enough, but comes surprisingly close with sparse transition matrix operations) and this got me thinking about the overflow exception case.&lt;br /&gt;&lt;br /&gt;I don't think that all A's is the hardest case by a long way. Suppose you have a 50x50 grid of As with a B in the last entry and that the test word is 49 As and a B. You won't know until you look at the last entry whether there are $BIGNUM solutions or none.&lt;p class="editcomment"&gt;&lt;br /&gt;               &lt;/p&gt;&lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By anonymous user,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment2003082"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 21. August 2006, 14:49:54&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="lastcomment" id="lastcomment"&gt;&lt;/a&gt;&lt;div class="comment1 ownercomment" id="comment2005668"&gt;&lt;div class="top"&gt;&lt;div class="bot"&gt;&lt;div class="c-avatar"&gt;&lt;a href="http://my.opera.com/ipeev/about/"&gt;&lt;img src="http://my.opera.com/community/graphics/avatar.gif" alt="avatar" class="avatar" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="text"&gt;Interesting observation Thomas. I checked to see how the execution time will change in the new extreme case for the solution with the exceptions.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;t = timeit.Timer(stmt='WordPath().countPaths( [ "A"*50 for a in range(50)], "A"*50)',&lt;br /&gt;                   setup = 'from __main__ import WordPath')&lt;br /&gt;print "%.4f sec/pass" %  (t.timeit(number=10)/10)&lt;br /&gt;&lt;br /&gt;A = [ "A"*50 for a in range(49)] + ["A"*49 + "B"]&lt;br /&gt;W = "A"*49 + "B"&lt;br /&gt;t = timeit.Timer(stmt='WordPath().countPaths( A,W)',&lt;br /&gt;                   setup = 'from __main__ import WordPath, A,W')&lt;br /&gt;print "%.4f sec/pass" %  (t.timeit(number=10)/10)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The first test is with all "A"s and the second is with "A"s and only 1 "B" at the end.&lt;br /&gt;&lt;br /&gt;Here are the measured results:&lt;br /&gt;&lt;pre&gt;0.0114 sec/pass&lt;br /&gt;0.9282 sec/pass&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We see that indeed the time increased about 100 times. But it is still under 1 second and much better than 2.4545 sec/pass for my first solution without the exceptions.&lt;br /&gt;&lt;br /&gt;But let see how the solution provided by almostobsolete will handle this case. Running the same 2 tests gives:&lt;br /&gt;&lt;pre&gt;1.3069 sec/pass&lt;br /&gt;1.2378 sec/pass&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;His algorythm handles a little better the new extreme case. Apparently it is using time to sum all solutions in the "A"s only case.&lt;br /&gt;&lt;p class="editcomment"&gt;&lt;br /&gt;               &lt;/p&gt;&lt;/div&gt;&lt;p class="comment-by"&gt;&lt;span class="comment-user"&gt;                   By &lt;a href="http://my.opera.com/ipeev/about/" class="username"&gt;ipeev&lt;/a&gt;,                 &lt;/span&gt; &lt;span class="permalink hidemobile"&gt;&lt;a href="http://my.opera.com/ipeev/blog/show.dml/409336#comment2005668"&gt;#&lt;/a&gt;&lt;/span&gt; &lt;span class="commentdate"&gt; 22. August 2006, 06:53:36&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-115806739614546416?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/115806739614546416/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=115806739614546416' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/115806739614546416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/115806739614546416'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2006/09/solving-google-code-jam-countpaths.html' title='Solving the Google Code Jam &quot;countPaths&quot; problem in Python'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-113485805293284313</id><published>2005-12-17T14:19:00.000-08:00</published><updated>2006-05-08T17:46:34.476-07:00</updated><title type='text'>За Функционалното Програмиране</title><content type='html'>От известно време се интересувам от функционално програмираме. Много интересно.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-113485805293284313?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/113485805293284313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=113485805293284313' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/113485805293284313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/113485805293284313'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2005/12/blog-post.html' title='За Функционалното Програмиране'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12914630.post-111616988961547817</id><published>2005-05-15T08:09:00.000-07:00</published><updated>2005-05-15T08:11:29.616-07:00</updated><title type='text'>Начален старт</title><content type='html'>Това е новия ми блог.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12914630-111616988961547817?l=ipeev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ipeev.blogspot.com/feeds/111616988961547817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12914630&amp;postID=111616988961547817' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/111616988961547817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12914630/posts/default/111616988961547817'/><link rel='alternate' type='text/html' href='http://ipeev.blogspot.com/2005/05/blog-post.html' title='Начален старт'/><author><name>Ivan Peev</name><uri>http://www.blogger.com/profile/09778889341100738638</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
