tag:blogger.com,1999:blog-4442644934613653392024-03-17T05:14:57.281-04:00Kevin LadenheimKevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.comBlogger33125tag:blogger.com,1999:blog-444264493461365339.post-22032185144023435692016-01-23T11:12:00.002-05:002016-01-23T11:23:35.953-05:00Adding Locking Wheels to a Cast Iron Grinder StandHarbor Freight used to sell the same grinder stand as the Grizzly H7763 where the base is a hollow casting, complicating wheel mounting. Most stud mounted wheels only have a bit over an inch in stud length and more than two inches plus an enormous fender washer would be needed to mount on this base.<br />
<br />
I made aluminum adapters to go from the 1/2"-20 wheel studs on the 4 in. Hard Rubber Heavy Duty Swivel Casters with Brake Harbor Freight <i>Item #90999</i> to a 3/8"-16 tapped hole. The adapters are about two inches long and easily clear the the hollow casting. The mount is very solid but the assembly is top heavy and a bit tippy but still works well with the wheels locked.<br />
<br />
Click on any picture for a slideshow.<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNpcSywDbamaFThfyClJ602QqjuIxF3DUy81f6zpio7vq4OlE35EeABJbazQ6RxTsdNJvL1P-jEfIYZ6e39lMKuIusQ_tSTPeb_avJDZ9-bwo-fpMCAnk4rJ5d0lS3817o4LvzRI_jTHo/s1600/2016-01-22+22.15.12.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNpcSywDbamaFThfyClJ602QqjuIxF3DUy81f6zpio7vq4OlE35EeABJbazQ6RxTsdNJvL1P-jEfIYZ6e39lMKuIusQ_tSTPeb_avJDZ9-bwo-fpMCAnk4rJ5d0lS3817o4LvzRI_jTHo/s320/2016-01-22+22.15.12.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_4584AsRzlZt2-HDGNlg-zXVwNkMwWmSw2Kh1VAhSNcLFrY_uBhUkpEcUtHqONtiI98Y4uHSSbPxvTNXSCJKR8LBzsmlMKccxAcYcILtWBI3rdc_m_zSbFjrqv1P9VRvmOmAwAn0z_Pk/s1600/2016-01-22+22.13.58.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_4584AsRzlZt2-HDGNlg-zXVwNkMwWmSw2Kh1VAhSNcLFrY_uBhUkpEcUtHqONtiI98Y4uHSSbPxvTNXSCJKR8LBzsmlMKccxAcYcILtWBI3rdc_m_zSbFjrqv1P9VRvmOmAwAn0z_Pk/s320/2016-01-22+22.13.58.jpg" width="278" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih-1NdmFBvws4h_yQsZqkQkgDAou37mbNWc74iCL1mSjlufM0dd-IuYXSZmMV4PfFlaoSwnQXiOLJfoSoaAdfMRhdniRrc77gScF69Afx6YnQkVcEShC7VrX9I5XUynsUOkkpLiXzDiYg/s1600/2016-01-22+22.14.47.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih-1NdmFBvws4h_yQsZqkQkgDAou37mbNWc74iCL1mSjlufM0dd-IuYXSZmMV4PfFlaoSwnQXiOLJfoSoaAdfMRhdniRrc77gScF69Afx6YnQkVcEShC7VrX9I5XUynsUOkkpLiXzDiYg/s320/2016-01-22+22.14.47.jpg" width="164" /></a></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht0XmwDqJ0aLyG3JdtGs4QViBPzgo_jZGnqX1afAor06CyJv57uxfidMwb3agipC62gqdAlwnVtW1kGZR1RtYrKr1tRl06FWtS6LnnMrXr8PSo3QIp2r6O6tTIhlJPBVDtDC91uJ4ybos/s1600/2016-01-22+22.22.02.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht0XmwDqJ0aLyG3JdtGs4QViBPzgo_jZGnqX1afAor06CyJv57uxfidMwb3agipC62gqdAlwnVtW1kGZR1RtYrKr1tRl06FWtS6LnnMrXr8PSo3QIp2r6O6tTIhlJPBVDtDC91uJ4ybos/s320/2016-01-22+22.22.02.jpg" width="240" /></a></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBYocyesW68gq9T2tPCPCO2-CPNbLWMptyV_sgAln1f4btZDb0zIn5Lv-yzXqRQW2nrz6XfXNuedhbbx-Jgz6fsPgV0jc17PBlSS4hFEwKjL3PIXr9_tvUKeBJtw-DQVLANOiM_jqnzjQ/s1600/2016-01-22+22.24.06.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBYocyesW68gq9T2tPCPCO2-CPNbLWMptyV_sgAln1f4btZDb0zIn5Lv-yzXqRQW2nrz6XfXNuedhbbx-Jgz6fsPgV0jc17PBlSS4hFEwKjL3PIXr9_tvUKeBJtw-DQVLANOiM_jqnzjQ/s320/2016-01-22+22.24.06.jpg" width="240" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB1_i4AICWZ3X1HGdeGFdwY9WMu0n301MZDVDvBfmMyh5uRLyznpbn3RNus7UBEoU1iRk1bChx_RsAW1rrFlWx3d5RCVYtBoDosSzu4mecrfHH6JVvRF3MhXh2F8fRsW_j-sAuUzq31Og/s1600/2016-01-22+22.32.07.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB1_i4AICWZ3X1HGdeGFdwY9WMu0n301MZDVDvBfmMyh5uRLyznpbn3RNus7UBEoU1iRk1bChx_RsAW1rrFlWx3d5RCVYtBoDosSzu4mecrfHH6JVvRF3MhXh2F8fRsW_j-sAuUzq31Og/s320/2016-01-22+22.32.07.jpg" width="320" /></a></div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBYocyesW68gq9T2tPCPCO2-CPNbLWMptyV_sgAln1f4btZDb0zIn5Lv-yzXqRQW2nrz6XfXNuedhbbx-Jgz6fsPgV0jc17PBlSS4hFEwKjL3PIXr9_tvUKeBJtw-DQVLANOiM_jqnzjQ/s1600/2016-01-22+22.24.06.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"></a><br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com11tag:blogger.com,1999:blog-444264493461365339.post-49922643098366245632015-01-01T12:58:00.004-05:002015-01-01T13:03:47.536-05:00Number 5 is Alive<br />
A metal bodied wood plane that looks like a complete rust bucket can almost always be restored to be a good looking user. Other than heavy chips or rust pitting ahead of the mouth other wear and damage doesn't usually matter.<br />
<br />
This Stanley Number 5 jack plane has good finish on the knob and tote so it was retained. Good wax was just rubbed in with 0000 steel wool. If the knob and tote are really chewed up they can be stripped, sanded, oiled and waxed and will look terrific. The knob is quickly sanded on a lathe or drill press. The tote is all handwork. Strips of sandpaper used shoe shine fashion while the tote is in a vise are helpful.<br />
<br />
I use wire wheels in the drill press to remove rust and then Scotchbrite pads to give the<br />
steel a uniform scratch pattern. Brass parts are polished on a buffing wheel and with Simichrome metal polish. Steel screw heads can be buffed to a chrome-like luster with the buffing wheel.<br />
<br />
I used naval jelly on the rusty parts of the japanning and then wiped on some oil.<br />
<br />
I reassemble all the parts with a drop of oil on all threads.<br />
<br />
Click on the first picture for a slideshow:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC97AEWZkqfBa6N-47qfTnUolvOepBfnRKbrpBWcaRCWdMwCtU-DXCP2Y13sresBbOXTySEI-nRPd_V278gYpI76PZBb796d-df9VAD-ZDpVHirLQSBvm4pTPLEFoYQdIwM-SgwK-8VQA/s1600/five.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC97AEWZkqfBa6N-47qfTnUolvOepBfnRKbrpBWcaRCWdMwCtU-DXCP2Y13sresBbOXTySEI-nRPd_V278gYpI76PZBb796d-df9VAD-ZDpVHirLQSBvm4pTPLEFoYQdIwM-SgwK-8VQA/s1600/five.jpg" height="128" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8LfIrwkUsscDzkcLhGMhfroWSUGFX6k8wulLKZ0_1idvax0MzdSqIjUSOA5vlHY3gp65rpiDuAIDObrZbSgagtECcabH3rM4LBEfCdIWs_xocNIZnlVJR_XHKgygTgDTD24Yeo6q9YLs/s1600/four.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8LfIrwkUsscDzkcLhGMhfroWSUGFX6k8wulLKZ0_1idvax0MzdSqIjUSOA5vlHY3gp65rpiDuAIDObrZbSgagtECcabH3rM4LBEfCdIWs_xocNIZnlVJR_XHKgygTgDTD24Yeo6q9YLs/s1600/four.jpg" height="133" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPK-b9269HUDVMIuFSAii-XxsXr0nrWZIwcPGx335UbQXjWX5JK8ERwNC_gSTyq9OgvgGXjg2PTRfBi_jtqbO6BZa2TGuxCV7k1oid7EO5IT1eu1JI_0vXdoiioIPFa5zkaHfNA2G10lw/s1600/eight.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPK-b9269HUDVMIuFSAii-XxsXr0nrWZIwcPGx335UbQXjWX5JK8ERwNC_gSTyq9OgvgGXjg2PTRfBi_jtqbO6BZa2TGuxCV7k1oid7EO5IT1eu1JI_0vXdoiioIPFa5zkaHfNA2G10lw/s1600/eight.jpg" height="82" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJK7AKWPMnLwwbO78J6TdacXX5P4fp41xsPP-lvRpzAfUz5zOxV_HuxOWtAlh6AjsCFgG_eEkMRBTUASAVERE0ThdEvAhKOelXe4NgIgE2wr_ZG4nWKZCosW7ljNP-IbH_rz9WuziTLdM/s1600/seven.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJK7AKWPMnLwwbO78J6TdacXX5P4fp41xsPP-lvRpzAfUz5zOxV_HuxOWtAlh6AjsCFgG_eEkMRBTUASAVERE0ThdEvAhKOelXe4NgIgE2wr_ZG4nWKZCosW7ljNP-IbH_rz9WuziTLdM/s1600/seven.jpg" height="108" width="320" /></a></div>
<br />
<br />
All parts before and after cleaning:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisNnLl7_k1LcczJOOgQNejgCkpB68LmULaPdS3YkvZ1I9ke6smM3ZUDTCX3I2iWqDofBqOkmZW_ypt2HaaOtVYt9Y_bA0sSnLUYfKdIcdJ7-jx_pIj5tXvSbheqcY0r-V7dp9nXp1a3AI/s1600/nine.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisNnLl7_k1LcczJOOgQNejgCkpB68LmULaPdS3YkvZ1I9ke6smM3ZUDTCX3I2iWqDofBqOkmZW_ypt2HaaOtVYt9Y_bA0sSnLUYfKdIcdJ7-jx_pIj5tXvSbheqcY0r-V7dp9nXp1a3AI/s1600/nine.jpg" height="247" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuMG2Z_6dPY4-SkqeWwIDk5vpE6XGdoExuDG7tszf0zojFcDkIkZ3R_qFc5-xMXDyp_1REDgY02G0Bq27xmcftetST9JQONRVAoswGI3nsOzoHlfBvHZ4Gg_OeXFqHj4V_71C8RaEdLP0/s1600/ten.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuMG2Z_6dPY4-SkqeWwIDk5vpE6XGdoExuDG7tszf0zojFcDkIkZ3R_qFc5-xMXDyp_1REDgY02G0Bq27xmcftetST9JQONRVAoswGI3nsOzoHlfBvHZ4Gg_OeXFqHj4V_71C8RaEdLP0/s1600/ten.jpg" height="252" width="320" /></a></div>
<br />
<br />
<br />
The final result still has lots of patina, just no rust. The sides of the plane could have been lapped until they were bright as well.<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqZxqvfnWL7aLzZmfBwXrTwc9N3DIzoG7iME3uuPDzXDNYsFYVjA-Clulo_wXxei4Wr8Yhxcpcu_FRSscflE-zZgKtbi3mB3LYAHNGzp5Rf0A4paVO6NSwa8g9gY0o1jXIjH82v9nBZRk/s1600/one.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqZxqvfnWL7aLzZmfBwXrTwc9N3DIzoG7iME3uuPDzXDNYsFYVjA-Clulo_wXxei4Wr8Yhxcpcu_FRSscflE-zZgKtbi3mB3LYAHNGzp5Rf0A4paVO6NSwa8g9gY0o1jXIjH82v9nBZRk/s1600/one.jpg" height="122" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF0ccBvXHE5MerQJEqpi2XWBdx8NMUPtHQQlnIIzvNNCdOnkgfoDPQhk-JBHXZKonUDn7qvZ1JXY5u8RV9BToJLGuIaZyqn0w53ZKi6ssU1xO8mhf69G6BZPfhMTOxBU9lA8nK5NoRSX8/s1600/two.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF0ccBvXHE5MerQJEqpi2XWBdx8NMUPtHQQlnIIzvNNCdOnkgfoDPQhk-JBHXZKonUDn7qvZ1JXY5u8RV9BToJLGuIaZyqn0w53ZKi6ssU1xO8mhf69G6BZPfhMTOxBU9lA8nK5NoRSX8/s1600/two.jpg" height="133" width="320" /></a></div>
<br />
<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com2tag:blogger.com,1999:blog-444264493461365339.post-65594176663017966202013-06-25T15:07:00.005-04:002013-06-25T15:07:59.763-04:00A Django Management Command to Load CSV Data Into a Database Table<div>
<br /></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsBz8I-nv96NdE1kihx_FADXqt5BEDsYCW6uEfyYz_ovnzIJ1rh1U4TEPJDT4GeXpuJdhAPDz4yP-8jJHXUuTZcFnZMJ0zy5avddnqVxMuzsaN5yHsA1vfjKk-JE8chx13Qn0IRWs2RRU/s1600/1-DSC_1088.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="220" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsBz8I-nv96NdE1kihx_FADXqt5BEDsYCW6uEfyYz_ovnzIJ1rh1U4TEPJDT4GeXpuJdhAPDz4yP-8jJHXUuTZcFnZMJ0zy5avddnqVxMuzsaN5yHsA1vfjKk-JE8chx13Qn0IRWs2RRU/s320/1-DSC_1088.JPG" width="320" /></a></div>
<br /></div>
<div>
<br /></div>
This code expects input data that looks like this:<br />
<i><br /></i>
<i>First,Last,Age,Sex</i><br />
<i>John,Smith,26,Male</i><br />
<i>Jane,Doe,34,Female</i><br />
<i> .</i><br />
<i> .</i><br />
<i> .</i><br />
<br />
Assuming no irregular data, this sure is easy:<br />
<pre style="background: #ffffff; color: black;"><html><body style="background: #ffffff; color: black;">
<span style="color: maroon; font-weight: bold;">from</span> django<span style="color: #808030;">.</span>core<span style="color: #808030;">.</span>management<span style="color: #808030;">.</span>base <span style="color: maroon; font-weight: bold;">import</span> BaseCommand<span style="color: #808030;">,</span> CommandError
<span style="color: maroon; font-weight: bold;">from</span> app<span style="color: #808030;">.</span>models <span style="color: maroon; font-weight: bold;">import</span> Profile
<span style="color: maroon; font-weight: bold;">import</span> csv
<span style="color: maroon; font-weight: bold;">class</span> Command<span style="color: #808030;">(</span>BaseCommand<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">def</span> handle<span style="color: #808030;">(</span>self<span style="color: #808030;">,</span> <span style="color: #808030;">*</span>args<span style="color: #808030;">,</span> <span style="color: #808030;">*</span><span style="color: #808030;">*</span>options<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
self<span style="color: #808030;">.</span>stdout<span style="color: #808030;">.</span>write<span style="color: #808030;">(</span>args<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
with <span style="color: #e34adc;">open</span><span style="color: #808030;">(</span>args<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">,</span> <span style="color: #0000e6;">'rb'</span><span style="color: #808030;">)</span> as <span style="color: #e34adc;">file</span><span style="color: #808030;">:</span>
rows <span style="color: #808030;">=</span> csv<span style="color: #808030;">.</span>reader<span style="color: #808030;">(</span><span style="color: #e34adc;">file</span><span style="color: #808030;">,</span> delimiter<span style="color: #808030;">=</span><span style="color: #0000e6;">","</span><span style="color: #808030;">,</span> quotechar<span style="color: #808030;">=</span><span style="color: #0000e6;">'"'</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> row <span style="color: maroon; font-weight: bold;">in</span> rows<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> rows<span style="color: #808030;">.</span>line_num <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">continue</span>
row<span style="color: #808030;">[</span><span style="color: #008c00;">3</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> row<span style="color: #808030;">[</span><span style="color: #008c00;">3</span><span style="color: #808030;">]</span><span style="color: #808030;">.</span>replace<span style="color: #808030;">(</span><span style="color: #0000e6;">"\n"</span><span style="color: #808030;">,</span> <span style="color: #0000e6;">" "</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"First: "</span> <span style="color: #808030;">+</span> row<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Last: "</span> <span style="color: #808030;">+</span> row<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Age: "</span> <span style="color: #808030;">+</span> row<span style="color: #808030;">[</span><span style="color: #008c00;">2</span><span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Sex: "</span> <span style="color: #808030;">+</span> row<span style="color: #808030;">[</span><span style="color: #008c00;">3</span><span style="color: #808030;">]</span>
db_row <span style="color: #808030;">=</span> Profile<span style="color: #808030;">(</span>first<span style="color: #808030;">=</span>row<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">,</span> last<span style="color: #808030;">=</span>row<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span><span style="color: #808030;">,</span> age<span style="color: #808030;">=</span>row<span style="color: #808030;">[</span><span style="color: #008c00;">2</span><span style="color: #808030;">]</span><span style="color: #808030;">,</span> sex<span style="color: #808030;">=</span>row<span style="color: #808030;">[</span><span style="color: #008c00;">3</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
db_row<span style="color: #808030;">.</span>save<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: dimgrey;"># dump entire table</span>
<span style="color: maroon; font-weight: bold;">for</span> person <span style="color: maroon; font-weight: bold;">in</span> Profile<span style="color: #808030;">.</span>objects<span style="color: #808030;">.</span>all<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> person</body></html></pre>
<div>
<br /></div>
<div>
<br /></div>
Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com6tag:blogger.com,1999:blog-444264493461365339.post-31308853410958095062012-10-10T11:53:00.001-04:002012-10-10T12:01:02.065-04:00Linear Interpolation Calculator <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHogMkBcOb4hi5qVXx044vnYPmRObjcmlKyUdrXumWtpDHpeIUZFp8VEO3zLY7Vw2RZa0JTuZI8ovUPH2pB65C9lZjU_g_VlH5oLDzWWHabjUxqmibecyfiN-0Oyv67G7PHNctwceC1Qk/s1600/linterpmainscaled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Linear Interpolation" border="0" height="221" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHogMkBcOb4hi5qVXx044vnYPmRObjcmlKyUdrXumWtpDHpeIUZFp8VEO3zLY7Vw2RZa0JTuZI8ovUPH2pB65C9lZjU_g_VlH5oLDzWWHabjUxqmibecyfiN-0Oyv67G7PHNctwceC1Qk/s320/linterpmainscaled.png" title="Linear Interpolation" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.linterp.com/" target="_blank">www.linterp.com</a> Home Page</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This tiny calculator is an easy way to perform linear interpolation. Once you arrive on the page you can do all of your data entry using the keyboard by using the Tab key to move between entry boxes.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
The page is simple and uncluttered and offers a graphical proof of the result. The source is an example of HTML forms feeding variables into Javascript as well as basic use of the flot library (<a href="http://www.flotcharts.org/">http://www.flotcharts.org/</a>) which is built on top of jQuery. No effort has been made to validate entered values for size or legality.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO1HF6-nj2S0OiNg1l_p2k6silW18juyW6FAOt3UHCXyF0N04i6e3e-x2jh94Nqq1sosc25F9ue7qlffWctkhe2gF_c8G4ZOrLWHI4Zr1BwuA1KQ6AX9pr5L_VA85oT6oB-dk_Xxsop1k/s1600/linterpresultsscaled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO1HF6-nj2S0OiNg1l_p2k6silW18juyW6FAOt3UHCXyF0N04i6e3e-x2jh94Nqq1sosc25F9ue7qlffWctkhe2gF_c8G4ZOrLWHI4Zr1BwuA1KQ6AX9pr5L_VA85oT6oB-dk_Xxsop1k/s320/linterpresultsscaled.png" width="279" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
Results Page</div>
Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com2tag:blogger.com,1999:blog-444264493461365339.post-53235886638520465052012-07-16T14:38:00.001-04:002012-07-19T18:35:06.705-04:00Chaco Plots Using PySide Qt Bindings with Widgets in a QVBoxLayout<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpOgSKpdgM3yitcyFkuJY7ieS0O4ALcvtBvKxWjT-LxswfkiL6MHlmuGajXwIX1a8uIv7mq_Uud-5sLOZ-5A6u8keBU3S6KUoaikOEG2qC53Zh8UQoylgiEP6rRoHgE3RITdPjhP9t4ZI/s1600/sinx.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="230" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpOgSKpdgM3yitcyFkuJY7ieS0O4ALcvtBvKxWjT-LxswfkiL6MHlmuGajXwIX1a8uIv7mq_Uud-5sLOZ-5A6u8keBU3S6KUoaikOEG2qC53Zh8UQoylgiEP6rRoHgE3RITdPjhP9t4ZI/s320/sinx.png" width="320" /></a></div>
<br />
This small example builds on the example found at:<br />
<a href="http://caethan.wordpress.com/2011/09/05/chaco-plots-in-pyside/">http://caethan.wordpress.com/2011/09/05/chaco-plots-in-pyside/</a><br />
<br />
This example can be extended to easily include multiple plots and the adjustment of<br />
plot parameters using spin boxes.<br />
<br />
<pre style="background: #ffffff; color: black;">#!/usr/bin/python
import <span style="color: maroon; font-weight: bold;">sys</span>
<span style="color: maroon; font-weight: bold;">from</span> numpy import *
<span style="color: maroon; font-weight: bold;">from</span> PySide.QtCore import *
<span style="color: maroon; font-weight: bold;">from</span> PySide.QtGui import *
app <span style="color: #808030;">=</span> QApplication(sys.argv)
<span style="color: maroon; font-weight: bold;">from</span> traits.etsconfig.etsconfig import ETSConfig
ETSConfig.toolkit <span style="color: #808030;">=</span> <span style="color: #0000e6;">"qt4"</span>
<span style="color: maroon; font-weight: bold;">from</span> <span style="color: maroon; font-weight: bold;">enable</span>.api import Window
<span style="color: maroon; font-weight: bold;">from</span> chaco.api import ArrayPlotData<span style="color: #808030;">,</span> Plot
<span style="color: maroon; font-weight: bold;">class</span> Viewer(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.mainWidget <span style="color: #808030;">=</span> QWidget(self)<span style="color: dimgrey;"> # dummy widget to contain layout manager</span>
self.plotview <span style="color: #808030;">=</span> Plotter(self)
self.setCentralWidget(self.mainWidget)
self.amplitudeLabel <span style="color: #808030;">=</span> QLabel(<span style="color: #0000e6;">"Amplitude: "</span>)
self.amplitudeSpinBox <span style="color: #808030;">=</span> QDoubleSpinBox()
self.amplitudeSpinBox.setRange(-1000000.00<span style="color: #808030;">,</span> 1000000.00)
self.amplitudeSpinBox.setValue(1.00)
self.plotButton <span style="color: #808030;">=</span> QPushButton(<span style="color: #0000e6;">"Plot"</span>)
self.plotButton.clicked.<span style="color: maroon; font-weight: bold;">connect</span>(self.update)
self.statusBar().showMessage(<span style="color: #0000e6;">'Ready to rock!'</span>)
self.setWindowTitle(<span style="color: #0000e6;">'sin(x)'</span>)
x <span style="color: #808030;">=</span> arange(<span style="color: #008c00;">100</span>)
y <span style="color: #808030;">=</span> zeros(<span style="color: #008c00;">100</span>)
self.plotview.update_data(x<span style="color: #808030;">,</span> y)
layout <span style="color: #808030;">=</span> QVBoxLayout(self.mainWidget)
layout.addWidget(self.amplitudeLabel)
layout.addWidget(self.amplitudeSpinBox)
layout.addWidget(self.plotButton)
layout.addWidget(self.plotview.widget)
self.setLayout(layout)
def update(self):
print <span style="color: #0000e6;">"Ok"</span>
print self.amplitudeSpinBox.value()
x <span style="color: #808030;">=</span> arange(<span style="color: #008c00;">200</span>)
y <span style="color: #808030;">=</span> self.amplitudeSpinBox.value() * sin(x)
self.plotview.update_data(x<span style="color: #808030;">,</span> y)
<span style="color: maroon; font-weight: bold;">class</span> Plotter():
def __init__(self<span style="color: #808030;">,</span> parent):
self.plotdata <span style="color: #808030;">=</span> ArrayPlotData(x=array([])<span style="color: #808030;">,</span> y=array([]))
self.window <span style="color: #808030;">=</span> self.create_plot(parent)
self.widget <span style="color: #808030;">=</span> self.window.control
def update_data(self<span style="color: #808030;">,</span> x<span style="color: #808030;">,</span> y):
self.plotdata.set_data(<span style="color: #0000e6;">"x"</span><span style="color: #808030;">,</span> x)
self.plotdata.set_data(<span style="color: #0000e6;">"y"</span><span style="color: #808030;">,</span> y)
def create_plot(self<span style="color: #808030;">,</span> parent):
plot <span style="color: #808030;">=</span> Plot(self.plotdata<span style="color: #808030;">,</span> padding=<span style="color: #008c00;">50</span><span style="color: #808030;">,</span> border_visible=True)
plot.plot((<span style="color: #0000e6;">"x"</span><span style="color: #808030;">,</span> <span style="color: #0000e6;">"y"</span>)<span style="color: #808030;">,</span> <span style="color: maroon; font-weight: bold;">name</span><span style="color: #808030;">=</span><span style="color: #0000e6;">"data plot"</span><span style="color: #808030;">,</span> color=<span style="color: #0000e6;">"green"</span>)
return Window(parent<span style="color: #808030;">,</span> -1<span style="color: #808030;">,</span> component<span style="color: #808030;">=</span>plot)
if __name__ <span style="color: #808030;">=</span>= <span style="color: #0000e6;">"__main__"</span>:
plot <span style="color: #808030;">=</span> Viewer()
plot.resize(600<span style="color: #808030;">,</span> 400)
plot.show()
<span style="color: maroon; font-weight: bold;">sys</span>.<span style="color: maroon; font-weight: bold;">exit</span>(app.exec_())
</pre>
<pre style="background: #ffffff; color: black;"></pre>
<pre style="background: #ffffff; color: black;"></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com13tag:blogger.com,1999:blog-444264493461365339.post-26314930367823248462012-06-03T18:19:00.001-04:002012-06-03T18:19:36.734-04:00golang goroutine Example Sample Code<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiby8j7bmCHLH-e3e6SyvpnNiZCQ5u6HI2iaxwACKT3mPegL3nzoYvRjb027Gm_TTVQSKe-8636FCZ7Ogv6Yh1v0xkDnbj3ZZBvCF6DcbQW4ZKkyQ3-hferIxIBwgkJF-uekjFNlEJkFm0/s1600/DSC_0183.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiby8j7bmCHLH-e3e6SyvpnNiZCQ5u6HI2iaxwACKT3mPegL3nzoYvRjb027Gm_TTVQSKe-8636FCZ7Ogv6Yh1v0xkDnbj3ZZBvCF6DcbQW4ZKkyQ3-hferIxIBwgkJF-uekjFNlEJkFm0/s320/DSC_0183.JPG" width="320" /></a></div>
<br />
I don't think basic use of pthreads is that different in complexity from goroutines but it's neat to see lightweight threads as a built-in language feature. goroutine usage superficially looks a lot like costates in Dynamic C, a proprietary nonstandard compiler for the Rabbit series of Z80 derived CPUs from Digi International. It's a really useful feature on a single core 8-bit microcontroller that frees you having "one big loop" with brittle timing in an embedded application.<br />
<br />
<b>Code:</b><br />
<br />
package main<br />
<br />
import (<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>"fmt"<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>"runtime"<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>"time"<br />
)<br />
<br />
<br />
func thread1(counter chan int) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>count := 0<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>fmt.Println("Hello from thread 1")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>runtime.Gosched() // force yield<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>time.Sleep(5 * time.Second)<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>count++<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>counter <- count<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<br />
func thread2(counter chan int) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>count := 0<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> fmt.Println("Hello from thread 2")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> runtime.Gosched() // force yield<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> time.Sleep(5 * time.Second)<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> count++<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> counter <- count<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<br />
func main() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>c1 := make(chan int)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>c2 := make(chan int)<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>go thread1(c1)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>go thread2(c2)<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> count1 := <-c1<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> count2 := <-c2<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> fmt.Println("main() is alive ", count1, count2)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> time.Sleep(5 * time.Second)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> runtime.Gosched() // force yield<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<div>
<br /></div>
<div>
<b>Output:</b></div>
<div>
<br /></div>
<div>
<div>
Hello from thread 1</div>
<div>
Hello from thread 2</div>
<div>
Hello from thread 1</div>
<div>
Hello from thread 2</div>
<div>
main() is alive 1 1</div>
<div>
Hello from thread 2</div>
<div>
Hello from thread 1</div>
<div>
main() is alive 2 2</div>
<div>
main() is alive 3 3</div>
<div>
Hello from thread 2</div>
<div>
Hello from thread 1</div>
<div>
main() is alive 4 4</div>
<div>
Hello from thread 2</div>
<div>
Hello from thread 1</div>
<div>
main() is alive 5 5</div>
<div>
Hello from thread 2</div>
<div>
Hello from thread 1</div>
<div>
main() is alive 6 6</div>
<div>
Hello from thread 1</div>
<div>
Hello from thread 2</div>
</div>
<div>
<br /></div>
<br />
<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com1tag:blogger.com,1999:blog-444264493461365339.post-49920996431720291862012-06-03T14:52:00.003-04:002012-06-03T15:03:53.001-04:00Tour golang Exercise: Maps #45 WordCount Solution<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWGfp4CafWUGFulju_hOfbacbpfZj85E46N0GCpevIYvaxoqnQ9db69ci_FWA3sJ0GTyL-zi2unX7hA7sw42BznAzAnXRN4xozgearUCypErC9Xfkp9WabdCYdWDFffeWGSF43ireugsU/s1600/DSC_0670-001.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWGfp4CafWUGFulju_hOfbacbpfZj85E46N0GCpevIYvaxoqnQ9db69ci_FWA3sJ0GTyL-zi2unX7hA7sw42BznAzAnXRN4xozgearUCypErC9Xfkp9WabdCYdWDFffeWGSF43ireugsU/s320/DSC_0670-001.JPG" width="213" /></a></div>
<br />
<br />
This exercise from the golang tour asks for a function that will count the number of times each word appears in a string. I think of it as using a hash to contain a histogram of word frequency. The if/else could be tighter if go had a ternary operator.<br />
<br />
<br />
<b>Code:</b><br />
<br />
<br />
package main<br />
<br />
import "strings"<br />
import "fmt"<br />
<br />
func WordCount (s string) map[string]int {<br />
<br />
substrs := strings.Fields(s)<br />
fmt.Println(substrs)<br />
<br />
// key,value ==> word,count<br />
// iterate over substrs, if key in map, value++, else create<br />
<br />
counts := make(map[string]int)<br />
<br />
for _, word := range substrs {<br />
_, ok := counts[word]<br />
<br />
if ok == true {<br />
counts[word] += 1<br />
} else {<br />
counts[word] = 1<br />
} <br />
} <br />
<br />
return counts<br />
}<br />
<br />
func main() {<br />
fmt.Println(WordCount("now is the now is the go go go it a a a a a"))<br />
fmt.Println(WordCount("1 2 3 11 22 33 1 2 3"))<br />
<div>
}</div>
<br />
<b>Output:</b><br />
<br />
[now is the now is the go go go it a a a a a]<br />
map[now:2 the:2 go:3 a:5 is:2 it:1]<br />
[1 2 3 11 22 33 1 2 3]<br />
map[2:2 33:1 1:2 11:1 22:1 3:2]<br />
<br />
<b>Common Error:</b><br />
<br />
./wordcount2.go:20: cannot convert true to type int<br />
./wordcount2.go:20: invalid operation: ok == true (mismatched types int and bool)<br />
<div>
<br /></div>
<div>
This occurs when attempting: </div>
<div>
<br />
ok := counts[word]</div>
<div>
<br /></div>
<div>
Instead of:</div>
<div>
</div>
<div>
_, ok := counts[word]</div>
<div>
<br /></div>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com6tag:blogger.com,1999:blog-444264493461365339.post-63821726704732277682012-05-28T21:41:00.001-04:002012-05-28T22:54:21.122-04:00How to Drill a Hole in a Golf Ball Perfectly<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAvZi0FV5bAf9MKLA2atT-UBOP6TXktO5NBw90Lx3m0UyQQNgjrLKufTilDF9wwwalQ11Xq4n32U3ZmjuEE-ky3lgRY5Ht9APaP8LffAKWcASXIuLdSnPZ_lzl6mqYAAJJHgDVgltsx58/s1600/099.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAvZi0FV5bAf9MKLA2atT-UBOP6TXktO5NBw90Lx3m0UyQQNgjrLKufTilDF9wwwalQ11Xq4n32U3ZmjuEE-ky3lgRY5Ht9APaP8LffAKWcASXIuLdSnPZ_lzl6mqYAAJJHgDVgltsx58/s320/099.JPG" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">1935 South Bend Heavy 9x48</td></tr>
</tbody></table>
<br />
Holding a golf ball for drilling is pretty difficult with common tools. The ball wants to pop out of clamps and vises. One quick and dirty solution is to drill a hole smaller than the diameter of the ball in a board and hold the ball in place using the board and your foot.<br />
<br />
You can also build a small box to easily hold a ball for drilling at the bench vise or drill press using this concept. If you are able to hit the center of the portion of the ball exposed by the hole you will get a decent hole.<br />
<br />
But if you want a perfectly balanced ball, you want a dead center hole. And the easiest way to drill a dead center hole is hold the ball in a three jaw chuck in a lathe. I cranked down pretty tightly on the chuck and the ball still wanted to pull out of the jaws unless the lathe was in reverse when withdrawing the drill bit. There just isn't very much contact surface.<br />
<br />
Be sure to get solid core balls for safety, liquid core balls can eject liquid under high pressure.<br />
<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com25tag:blogger.com,1999:blog-444264493461365339.post-43594182667740285062012-05-22T19:46:00.002-04:002012-05-28T22:14:06.830-04:00Parse XML with Nokogiri then remove tags<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPJcTukYetcptN5BenujH6Mx-NAuRwQtEx9OnzKho4SXNhp-LSH2OAJ8PKZVRNCE-T0z7dpWNortwySJTuO_EkM_2e_L7FO4kCYXxrvQCf-X8K3_jET2Qcu4QuqBB9F0n50UUQCdS3TUE/s1600/DSC_0151.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="246" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPJcTukYetcptN5BenujH6Mx-NAuRwQtEx9OnzKho4SXNhp-LSH2OAJ8PKZVRNCE-T0z7dpWNortwySJTuO_EkM_2e_L7FO4kCYXxrvQCf-X8K3_jET2Qcu4QuqBB9F0n50UUQCdS3TUE/s320/DSC_0151.JPG" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><span style="background-color: white; font-family: sans-serif; line-height: 19px; text-align: -webkit-auto;">Galloping Ghost of the Japanese Coast</span>
</td></tr>
</tbody></table>
<br />
<br />
Let's say you have an XML document containing authors.<br />
<br />
The Nokogiri tutorial tells you can do this:<br />
<br />
authors = doc.xpath("//author")<br />
<br />
And it shows you will get output like this:<br />
<br />
<author>Kernighan</author><br />
<author>Ritchie</author><br />
<author>Matsumoto</author><br />
<br />
How do you get rid of all those tags?<br />
<br />
Instead of reading the Nokogiri documentation like I should have, I tried to further process this output.<br />
<br />
A regex worked fine but you have to worry about exceptions if there are no matches. And it's ugly.<br />
<br />
<pre style="background: #ffffff; color: black;">doc <span style="color: #808030;">=</span> Nokogiri::XML<span style="color: #808030;">(</span>body<span style="color: #808030;">)</span>
auths <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
authors <span style="color: #808030;">=</span> doc<span style="color: #808030;">.</span>xpath<span style="color: #808030;">(</span><span style="color: #0000e6;">"//author"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #008c00;">0</span><span style="color: #808030;">.</span><span style="color: #808030;">.</span>authors<span style="color: #808030;">.</span>length <span style="color: #808030;">-</span> <span style="color: #008c00;">1</span>
auths<span style="color: #808030;">[</span>i<span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #808030;">/</span><span style="color: #808030;">.</span><span style="color: #808030;">*</span><author><span style="color: #808030;">(</span><span style="color: #808030;">.</span><span style="color: #808030;">*</span><span style="color: #808030;">)</span><\<span style="color: #808030;">/</span>author><span style="color: #808030;">.</span><span style="color: #808030;">*</span><span style="color: #808030;">/</span><span style="color: #808030;">.</span>match<span style="color: #808030;">(</span>author<span style="color: #808030;">[</span>i<span style="color: #808030;">]</span><span style="color: #808030;">.</span>to_s<span style="color: #808030;">)</span><span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">end</span>
</pre>
<br />
I also tried string substitution, which also worked fine. I didn't test a no match case.<br />
<br />
<pre style="background: #ffffff; color: black;">doc <span style="color: #808030;">=</span> Nokogiri::XML<span style="color: #808030;">(</span>body<span style="color: #808030;">)</span>
auths <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
authors <span style="color: #808030;">=</span> doc<span style="color: #808030;">.</span>xpath<span style="color: #808030;">(</span><span style="color: #0000e6;">"//author"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #008c00;">0</span><span style="color: #808030;">.</span><span style="color: #808030;">.</span>authors<span style="color: #808030;">.</span>length <span style="color: #808030;">-</span> <span style="color: #008c00;">1</span>
auths<span style="color: #808030;">[</span>i<span style="color: #808030;">]</span> <span style="color: #808030;">=</span> author<span style="color: #808030;">[</span>i<span style="color: #808030;">]</span><span style="color: #808030;">.</span>to_s<span style="color: #808030;">.</span><span style="color: #400000;">sub</span><span style="color: #808030;">(</span><span style="color: #0000e6;">"<author>"</span>,<span style="color: #0000e6;">""</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span><span style="color: #400000;">sub</span><span style="color: #808030;">(</span><span style="color: #0000e6;">"</author>"</span>,<span style="color: #0000e6;">""</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">end</span>
</pre>
<br />
I knew I was parsing already parsed data and thought there should be an option to suppress the tags. I received some good advice to look more closely at Nokogiri and I came up with this approach of popping Nodes off of the NodeSet.<br />
<br />
If you need know how many Nodes there were originally you have to save a copy before the first pop.<br />
<br />
<pre style="background: #ffffff; color: black;">doc <span style="color: #808030;">=</span> Nokogiri::XML<span style="color: #808030;">(</span>body<span style="color: #808030;">)</span>
auths <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
authors <span style="color: #808030;">=</span> doc<span style="color: #808030;">.</span>xpath<span style="color: #808030;">(</span><span style="color: #0000e6;">"//author"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">while</span> authors<span style="color: #808030;">.</span>length<span style="color: #808030;">(</span><span style="color: #808030;">)</span> > <span style="color: #008c00;">0</span>
auths << authors<span style="color: #808030;">.</span>pop<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>inner_text<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">end</span>
</pre>
<br />
I think there might be an approach where you can iterate over the NodeSet without worrying about length and without using pop() but I haven't figured it out yet.<br />
<br />
<div>
<br /></div>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-85085178527062256342012-02-12T12:49:00.001-05:002012-02-12T16:41:33.022-05:00Precise Installation of a Drawer Knob on a Shaker Style Drawer Front in 6 Steps<div class="" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjHue475HtDZ8Oj90mo_pJW2wdIJvn0PF5xds8cHvtHON7uqFZE4w-jgDjuw3C8yVQsUuwzTTbcqNiMVKzNASp7qXUexMqpbUvk1Xe16bjk4hCtG8VEnWqLBE4cP5WRs_W96LnsigWvuw/s1600/002.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjHue475HtDZ8Oj90mo_pJW2wdIJvn0PF5xds8cHvtHON7uqFZE4w-jgDjuw3C8yVQsUuwzTTbcqNiMVKzNASp7qXUexMqpbUvk1Xe16bjk4hCtG8VEnWqLBE4cP5WRs_W96LnsigWvuw/s400/002.JPG" width="300" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 1 - Set a machinist adjustable parallel to half the vertical height: 1 1/4".</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZKie5_ZCGJVMbJltaZuj6t5mm-hlvaEIwp5AKxTVYkLUaaXS5A-A0VHq6yiM9KTFQju4XdcYfGbRSmvLJKqb8ZQ2etFoGnCEHWB0rUIvRHcdey57clqWXC3SLbHBeacA6Ltaq8rD5zDA/s1600/001.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZKie5_ZCGJVMbJltaZuj6t5mm-hlvaEIwp5AKxTVYkLUaaXS5A-A0VHq6yiM9KTFQju4XdcYfGbRSmvLJKqb8ZQ2etFoGnCEHWB0rUIvRHcdey57clqWXC3SLbHBeacA6Ltaq8rD5zDA/s400/001.JPG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 2 - Use the parallel to mark the horizontal center line.</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjTS0hOLZI30MYHv8ks6XFmIqqaDTP6r8NMuSruOQxL-ql20Szkdr-oPJnB2iT9xOaSWys5IJ4JzfY2za1ZNgR1qLYwZ71uoamPgYCaBFqzY5BUY4D_jT8zTvifJSLGMuSgZFnz5te90A/s1600/003.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjTS0hOLZI30MYHv8ks6XFmIqqaDTP6r8NMuSruOQxL-ql20Szkdr-oPJnB2iT9xOaSWys5IJ4JzfY2za1ZNgR1qLYwZ71uoamPgYCaBFqzY5BUY4D_jT8zTvifJSLGMuSgZFnz5te90A/s400/003.JPG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 3 - Use a machinist's 6" scale to mark the vertical center line.</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjltSh7O1Fo92GmQ5A4oChEdAijbiS2tXkmRPg2LFvFiTY_e6cHOLwUqZTmb-CkoAXnKPySKtiNkH2vUR8xafGgpfdVFbU6rcucToqicIp4eUg2eQRE0xQfmoCKAAd97nIjZ_k1UyUCjlw/s1600/004.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjltSh7O1Fo92GmQ5A4oChEdAijbiS2tXkmRPg2LFvFiTY_e6cHOLwUqZTmb-CkoAXnKPySKtiNkH2vUR8xafGgpfdVFbU6rcucToqicIp4eUg2eQRE0xQfmoCKAAd97nIjZ_k1UyUCjlw/s400/004.JPG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 4 - Use an awl to make a center punch mark.</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO_avqxx5ryJ4V-Q576tl3ZdUA-AukXj9RBiZeUffJytCm8lqShUig6TEb1dTpJxJOWCbvgXMpohlLYNJmZluKT0mk6ZnMxHXcEP857TiK6cDGpl3JTY-5RYUDq4-KiYCh9SOp6X0klW8/s1600/007.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO_avqxx5ryJ4V-Q576tl3ZdUA-AukXj9RBiZeUffJytCm8lqShUig6TEb1dTpJxJOWCbvgXMpohlLYNJmZluKT0mk6ZnMxHXcEP857TiK6cDGpl3JTY-5RYUDq4-KiYCh9SOp6X0klW8/s400/007.JPG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 5 - Use a brad point drill and a power drill to drill a clearance hole.</div>
<div class="separator" style="clear: both; text-align: center;">
Cover the drill fixture will masking tape to protect the finish.</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKu4FBFIxhPWCHnejbj3w8RospCzr1T4DewRZkxL6dM6ef1XBGTkZbTmUFarXojJq9Hk-_HcZvz-8oVbJx0uP6lvd21QWhnDMxm7aF7u3Hg04rYuATlolPIgtW2imCPz5A4Uk4OojkFhk/s1600/006.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKu4FBFIxhPWCHnejbj3w8RospCzr1T4DewRZkxL6dM6ef1XBGTkZbTmUFarXojJq9Hk-_HcZvz-8oVbJx0uP6lvd21QWhnDMxm7aF7u3Hg04rYuATlolPIgtW2imCPz5A4Uk4OojkFhk/s400/006.JPG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
Step 6 - Install knob. Repeat N times.</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKgoiSSWQOjNH5vL_Ap3VvARPkokJvaCvYxtVeBkYcWKYNrt8Hg-PYUqUU3KT_vaF5lZdlLTmlkpjyvtHAWx9c4xmUDdhHQljRHb6oTuipLTVQ8ctJd5vuRNaTg7T35Mxr6YtA4V8ZTrQ/s1600/009.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKgoiSSWQOjNH5vL_Ap3VvARPkokJvaCvYxtVeBkYcWKYNrt8Hg-PYUqUU3KT_vaF5lZdlLTmlkpjyvtHAWx9c4xmUDdhHQljRHb6oTuipLTVQ8ctJd5vuRNaTg7T35Mxr6YtA4V8ZTrQ/s400/009.JPG" width="300" /></a></div>
<div class="" style="clear: both; text-align: center;">
<br /></div>
<br />
NOTE - The drill fixture shown is a PORTALIGN from Sears. These have not been made in decades. It really creates a terrific portable drill press. All of the adjustable products that available today that look similar are complete junk. Taking off the drill chuck is pretty difficult so this Sears industrial 3/8" drill made by DeWalt is dedicated to the PORTALIGN.<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-13795049742724656362012-01-02T21:24:00.002-05:002012-01-02T22:00:22.589-05:00Word autocorrect can be a lot like emacs abbrev-mode<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2TtmPPwdSpn6MJjvF5pBtuVKLxdHHQ9Bs25VXQRIhEQ6X-XDuUutHY4YTCI9AYKMkL3-6zDSpV55rnFJxXfA9N5BaHNZXd_fsIs3QYO64QXfmh5w4qItD8f5sqNlnHLwff-TXT9HBsJQ/s1600/autocorrect.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="616" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2TtmPPwdSpn6MJjvF5pBtuVKLxdHHQ9Bs25VXQRIhEQ6X-XDuUutHY4YTCI9AYKMkL3-6zDSpV55rnFJxXfA9N5BaHNZXd_fsIs3QYO64QXfmh5w4qItD8f5sqNlnHLwff-TXT9HBsJQ/s640/autocorrect.jpg" width="640" /></a></div>
<br />
Tired of typing the same stock phrases over and over again in Word? You can use a stand-in word and global search and replace when your document is finished but it's a lot more fun to watch Word automatically type for you.<br />
<br />
For example, if you add an entry to your autocorrect dictionary as shown above, every time you type the key <i>qnato</i>, Word will instantly replace it with <i>North American Treaty Organization </i>just like emacs <i>abbrev-mode</i>.<br />
<br />
If you have different groups of abbreviations for different kinds of writing you can prefix them all with the same letter or number and they will stay grouped and separate in the Word autocorrect dictionary.<br />
<br />
Some general ideas for using this would be:<br />
<br />
<i> @gg = @gmail.com</i><br />
<i> @yy = @yahoo.com</i><br />
<i> @me = your.name@bigcorp.com</i><br />
<br />
You can also highlight an entire block of text and the entire block including all formatting can be assigned to an autocorrect key. This would be useful for having various signature blocks, addresses and boilerplate sentences and paragraphs for business letters.<br />
<br />
And the last thing you can do is select a photo or picture in a Word document and add an autocorrect key for it. For example if you paste a picture of The Dude into your document and add the key <i>dude</i> for it, every time you type <i>dude</i>, a picture of The Dude will be inserted.<br />
<br />
Teachers can use this for easy clip art insertion. Any bit of art that is regularly being inserted into your documents can be assigned to a key.<br />
<br />
You can build a dictionary with an image for every number and letter if you want for puzzle construction.<br />
<br />
If it turns out that this a seldom used feature of autocorrect I would worry about normal.dot corruption if the images are large, especially since the art is probably stored in normal.dot.<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com2tag:blogger.com,1999:blog-444264493461365339.post-15743108513031557912012-01-01T16:14:00.001-05:002012-01-02T10:14:34.207-05:00Samsung SyncMaster 204B Power Supply Repair<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI3G7uz7P4-dglfrIQLbT4TmkCEJJYBlJN14JkF_Tg_Zc32Z8IKzjdNlLaXb_qlCD7VKALdYfLYF-jldDenwPEXQI_2jf2eDIM9dpGAmTirjImGiuOqvPWkDaWMYNCZvkOJqWY1H81xic/s1600/004.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI3G7uz7P4-dglfrIQLbT4TmkCEJJYBlJN14JkF_Tg_Zc32Z8IKzjdNlLaXb_qlCD7VKALdYfLYF-jldDenwPEXQI_2jf2eDIM9dpGAmTirjImGiuOqvPWkDaWMYNCZvkOJqWY1H81xic/s320/004.JPG" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Samsung SyncMaster 204B PSU PCB</td></tr>
</tbody></table>
<br />
<br />
The SyncMaster 204B is a very nice 20.1" UXGA (1600x1200) panel circa 2006.<br />
I really like this aspect ratio but everything has gone widescreen and a monitor like this is basically no longer available.<br />
<br />
The CapXon caps bulge and fail and before complete failure the monitor flickers more and more since the caps are on the switching power supply that powers the backlights. I think only the three caps after the chopper were bulging on this unit.<br />
<br />
See: <a href="http://en.wikipedia.org/wiki/Capacitor_plague">http://en.wikipedia.org/wiki/Capacitor_plague</a><br />
<br />
Opening the monitor is a PITA and you don't want to do it twice so replace all the caps the first time. Use flush cutting side cutters to cut away as much of the leads and solder as you can. Solder wick rubbed with flux and pressed into what remains will loosen the caps to the extent that they will almost fall off the PCB.<br />
<br />
More info:<br />
<br />
<a href="http://tinymicros.com/wiki/Samsung_SyncMaster_204B_LCD_Repair">http://tinymicros.com/wiki/Samsung_SyncMaster_204B_LCD_Repair</a><br />
<br />
A proper parts list is a stumbling block for some people. These parts are perfect for my board rev 0.1 as of December 2011. These are not the cheapest possible parts but the parts with the longest lifetime ratings.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUSnjmd99UpTkiMWS9q_DuChJWFKrs3SRsvR7RL38sS6SKu-h1jkHz9xGMPHRcPBMoOf1DxekALXrHqFpHzq7UPWv4myJa2nN8hko-E9H5pDtBh9wBtLcfWi_Hj8Nwjffjby_lUJ_OMe0/s1600/syncmaster204bparts.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="106" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUSnjmd99UpTkiMWS9q_DuChJWFKrs3SRsvR7RL38sS6SKu-h1jkHz9xGMPHRcPBMoOf1DxekALXrHqFpHzq7UPWv4myJa2nN8hko-E9H5pDtBh9wBtLcfWi_Hj8Nwjffjby_lUJ_OMe0/s640/syncmaster204bparts.jpg" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Digi-key part numbers</td></tr>
</tbody></table>
<br />
<br />
<br />Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com12tag:blogger.com,1999:blog-444264493461365339.post-59754119071923573412011-09-29T16:55:00.025-04:002011-10-01T09:29:55.658-04:00Prox on, Prox off: Windows Proxy On, Windows Proxy Off with PowerShell<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3L0Hl6g_6yJR1Gm2GUQKZmiVvVl965dRYZcPVrsEwNl3IO0mIVjsM35NPeyPZasAnOgpfQlPUrVtQwylyuC97Q_Em4icEBSAZ_M3DW0dZhyRuZzC1suTZZaH1m_hMrCzsf47V9-4BAsg/s1600/DSC_0224.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3L0Hl6g_6yJR1Gm2GUQKZmiVvVl965dRYZcPVrsEwNl3IO0mIVjsM35NPeyPZasAnOgpfQlPUrVtQwylyuC97Q_Em4icEBSAZ_M3DW0dZhyRuZzC1suTZZaH1m_hMrCzsf47V9-4BAsg/s1600/DSC_0224.JPG" /></a></div>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">These four files will give you the ability to switch between
having a proxy server set in Windows and not having one set
without having to navigate a bunch of windows each time.
Because Windows won't allow a PowerShell script to execute by
default the scripts are called using batch files so the execution
policy can be altered before and after calling the script.</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
proxyon.ps1:
</span><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name MigrateProxy -value 1
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -value 1
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyHttp1.1 -value 0
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -value 'http://10.1.10.109:3128'
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyOverride -value '<local>'
</span>
proxyoff.ps1:
</pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'DejaVu Sans'; white-space: normal;"><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name MigrateProxy -value 1
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -value 0
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyHttp1.1 -value 0
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -value ''
set-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyOverride -value '<local>'
</span></pre>
<div>
<span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">
</span></div>
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">poff.bat:</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">powershell {Set-ExecutionPolicy Unrestricted}
powershell "& C:\proxyoff.ps1"
powershell {Set-ExecutionPolicy Restricted}
</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;">pon.bat:
</pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;"><span class="Apple-style-span" style="font-family: 'DejaVu Sans'; font-size: small; white-space: normal;"><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">powershell {Set-ExecutionPolicy Unrestricted}
powershell "& C:\proxyon.ps1"
powershell {Set-ExecutionPolicy Restricted}
</span></pre>
<div>
<span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: xx-small;">
</span></div>
</span></span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; text-align: left;"> </pre>
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br />
</span></div>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com19tag:blogger.com,1999:blog-444264493461365339.post-48247688155059752972011-08-18T17:13:00.002-04:002011-08-18T17:14:18.996-04:00Set Windows Network Settings From The Command Line Using Net Shell (netsh.exe)<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXZWhOHt_RNwBZTZDvCsl4f_zrGDh5zVwDfaF-92PG5dW36KkfgJbbxxw-_Wu-t3MqvH_vX6211BH1jITocRbAU9rrJRRzQQ6rEoSSZiwlyiafN3o9fYhKvaZ3aQGzLWq086TqNnfFwD0/s1600/001-1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXZWhOHt_RNwBZTZDvCsl4f_zrGDh5zVwDfaF-92PG5dW36KkfgJbbxxw-_Wu-t3MqvH_vX6211BH1jITocRbAU9rrJRRzQQ6rEoSSZiwlyiafN3o9fYhKvaZ3aQGzLWq086TqNnfFwD0/s320/001-1.JPG" width="320" /></a></div><br />
If you are changing network settings repeatedly on a test system it gets old having to point and click each time. Net Shell (netsh.exe) can be called as follows from the command line:<br />
<br />
<i> netsh interface ipv4 set address name="Local Area Connection" dhcp HOST_IP</i><br />
<i> SUBNETMASK GATEWAY_IP</i><br />
<i><br />
</i><br />
For example:<br />
<br />
<i> netsh interface ipv4 set address name="Local Area Connection" 10.0.0.2 </i><br />
<i> 255.255.255.0 10.0.0.1</i><br />
<br />
Typing this over and over will get old as well so you can make a simple batch file called<br />
ip.bat containing:<br />
<br />
<i> netsh interface ipv4 set address name="Local Area Connection" %1 %2 %3 %4</i><br />
<br />
Now you can type:<br />
<br />
<i> ip [dhcp | static] HOST_IP SUBNETMASK GATEWAY_IP</i><br />
<br />
For example:<br />
<br />
<i> ip dhcp 10.0.0.2 255.255.255.0 10.0.0.1</i><br />
<i><br />
</i><br />
Of course you can just create individual batch files for each test scenario.<br />
<br />
<br />
<i><br />
</i><br />
<i><br />
</i>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com11tag:blogger.com,1999:blog-444264493461365339.post-54509076316343959962011-08-18T12:31:00.000-04:002011-08-18T12:31:06.566-04:00Dell Laptop Power Jack Repair<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPos_cJ5dOFV3jrkH-G8dWfAaDoKd5lHlpQUEDvw1d316xhWBtuajuU3L1czsc09YwViCkhme0Ytt-ucH2Ighfkzj9KVdJsgv8eQshyl3PI4yiETuwvf9GdL9778bMHsNjy8p_e9-oKc/s1600/001.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjPos_cJ5dOFV3jrkH-G8dWfAaDoKd5lHlpQUEDvw1d316xhWBtuajuU3L1czsc09YwViCkhme0Ytt-ucH2Ighfkzj9KVdJsgv8eQshyl3PI4yiETuwvf9GdL9778bMHsNjy8p_e9-oKc/s320/001.JPG" width="320" /></a></div><br />
A common problem with laptops is that the power receptacle on the mainboard stops making a reliable connection to the barrel connector on the power brick. Often this leads to intermittent battery charging unless the connector is in just the "right" position. Eventually the connector fails completely and the laptop will not power on.<br />
<br />
The entire job took about three hours. Getting to the connector to desolder it is a bear. A complete disassembly was required. Thus the mountain of parts in the right side of the photo.<br />
<br />
Some newer laptops have daughterboards containing the power receptacle so that desoldering is not necessary. Not having good desoldering equipment made it difficult to get the through holes in the mainboard clean and ready for a new connector but a cheap solder sucker and solder wick eventually did the trick.<br />
<br />
If your laptop is worth less than the cost of a professional repair, give it a try. If your laptop is worth much more I would leave this repair to the professionals.Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com54tag:blogger.com,1999:blog-444264493461365339.post-77969863392302233222011-08-05T15:03:00.001-04:002011-08-05T15:06:34.919-04:00MIT 6.00 Problem Set 8 - Problem 2<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCLfzZbJ5onVDN0eenBYoyvupClVkSsX9qEjGkMSp3gMa2uBocOYahIL8VXWt7dacXnoCrBPYt-eZrPujQyXojwoDxYnB-ehiGJMnOFt0gswlS19FmlolyQ7VVLaRXZcIZhUEsxzlludw/s1600/DSC_0150.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCLfzZbJ5onVDN0eenBYoyvupClVkSsX9qEjGkMSp3gMa2uBocOYahIL8VXWt7dacXnoCrBPYt-eZrPujQyXojwoDxYnB-ehiGJMnOFt0gswlS19FmlolyQ7VVLaRXZcIZhUEsxzlludw/s1600/DSC_0150.JPG" /></a></div><br />
The problem statement does not mention sorting. But sorting the entire dict would be helpful. And the comparator could be used with sorted() but since there is no mention of sorting the data set I leave it unsorted. So this may be a completely wrong approach but I take two tuples at a time and whichever is best is added to the solution set where best is judged by the comparator.<br />
<br />
Python Code:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> greedyAdvisor<span style="color: #808030;">(</span>subjects<span style="color: #808030;">,</span> maxWork<span style="color: #808030;">,</span> comparator<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
schedule <span style="color: #808030;">=</span> <span style="color: purple;">{</span><span style="color: purple;">}</span> <span style="color: dimgrey;"># initial solution</span>
firstSubject <span style="color: #808030;">=</span> <span style="color: purple;">{</span><span style="color: purple;">}</span>
odd <span style="color: #808030;">=</span> <span style="color: #e34adc;">True</span>
work <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
test <span style="color: #808030;">=</span> subjects<span style="color: #808030;">.</span>copy<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> key <span style="color: maroon; font-weight: bold;">in</span> subjects<span style="color: #808030;">.</span>keys<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> odd <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #e34adc;">True</span><span style="color: #808030;">:</span>
firstSubject <span style="color: #808030;">=</span> subjects<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span>
odd <span style="color: #808030;">=</span> <span style="color: #e34adc;">False</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
odd <span style="color: #808030;">=</span> <span style="color: #e34adc;">True</span>
<span style="color: maroon; font-weight: bold;">if</span> comparator<span style="color: #808030;">(</span>firstSubject<span style="color: #808030;">,</span> subjects<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> work <span style="color: #808030;">+</span> firstSubject<span style="color: #808030;">[</span>WORK<span style="color: #808030;">]</span> <span style="color: #808030;"><</span><span style="color: #808030;">=</span> maxWork<span style="color: #808030;">:</span>
schedule<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span> <span style="color: #808030;">=</span> firstSubject
work <span style="color: #808030;">+</span><span style="color: #808030;">=</span> firstSubject<span style="color: #808030;">[</span>WORK<span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> work <span style="color: #808030;">+</span> subjects<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span><span style="color: #808030;">[</span>WORK<span style="color: #808030;">]</span> <span style="color: #808030;"><</span><span style="color: #808030;">=</span> maxWork<span style="color: #808030;">:</span>
schedule<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span> <span style="color: #808030;">=</span> subjects<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span>
work <span style="color: #808030;">+</span><span style="color: #808030;">=</span> subjects<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span><span style="color: #808030;">[</span>WORK<span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">if</span> test <span style="color: #808030;">!</span><span style="color: #808030;">=</span> subjects<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"SUBJECTS MUTATED"</span>
<span style="color: maroon; font-weight: bold;">return</span> schedule
</pre><pre style="background: #ffffff; color: black;"></pre><pre style="background: #ffffff; color: black;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background: #ffffff; color: black;"></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;">cmpValue
Course Value Work
====== ==== =====
12.05 7 1
14.01 6 1
22.12 10 18
6.09 8 4
7.11 7 4
7.15 3 3
7.17 10 1
7.19 10 5
8.10 4 6
8.14 3 7
8.18 4 10
Total Value: 72
Total Work: 60
cmpWork
Course Value Work
====== ==== =====
12.03 2 1
14.01 6 1
22.12 8 15
6.09 8 4
7.11 7 4
7.15 3 3
7.17 10 1
7.19 10 2
8.10 4 6
8.14 3 7
8.16 2 6
8.18 4 10
Total Value: 67
Total Work: 60
cmpRatio
Course Value Work
====== ==== =====
12.03 2 1
12.05 7 1
14.01 6 1
22.12 10 18
6.09 8 4
7.02 3 2
7.11 7 4
7.15 3 3
7.17 10 1
7.19 10 2
8.14 3 7
8.16 2 6
8.18 4 10
Total Value: 75
Total Work: 60<span class="Apple-style-span" style="color: black;">
</span></pre><div><br />
</div>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-36661176337323094662011-08-05T14:53:00.000-04:002011-08-05T14:53:19.506-04:00MIT 6.00 Problem Set 8 - Problem 1Python Code:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> loadSubjects<span style="color: #808030;">(</span>filename<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
catalog <span style="color: #808030;">=</span> <span style="color: purple;">{</span><span style="color: purple;">}</span>
inputFile <span style="color: #808030;">=</span> <span style="color: #e34adc;">open</span><span style="color: #808030;">(</span>filename<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> line <span style="color: maroon; font-weight: bold;">in</span> inputFile<span style="color: #808030;">:</span>
line <span style="color: #808030;">=</span> <span style="color: #e34adc;">string</span><span style="color: #808030;">.</span>strip<span style="color: #808030;">(</span>line<span style="color: #808030;">)</span>
lineList <span style="color: #808030;">=</span> <span style="color: #e34adc;">string</span><span style="color: #808030;">.</span>split<span style="color: #808030;">(</span>line<span style="color: #808030;">,</span> <span style="color: #0000e6;">','</span><span style="color: #808030;">)</span>
lineList<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #e34adc;">int</span><span style="color: #808030;">(</span>lineList<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
lineList<span style="color: #808030;">[</span><span style="color: #008c00;">2</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #e34adc;">int</span><span style="color: #808030;">(</span>lineList<span style="color: #808030;">[</span><span style="color: #008c00;">2</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
catalog<span style="color: #808030;">[</span>lineList<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> lineList<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">:</span><span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">return</span> catalog
</pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-3151563091476600532011-07-27T08:17:00.001-04:002011-07-27T08:27:38.098-04:00MIT 6.00 Lecture 8 - Stirling's approximation<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYn3A9jelO2J7rQc-M2n2wN97fEhwSs1M72MtGmQnI6n-24r6Xn6PBolavXZHte7T_CkkKK3-tzwdGcH9z-ehKM-r-hkzUSURnv-td-WAH0a8hUXI6VZ82e6VvHVzfIWW3nPW1whEgZHw/s1600/stirling.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYn3A9jelO2J7rQc-M2n2wN97fEhwSs1M72MtGmQnI6n-24r6Xn6PBolavXZHte7T_CkkKK3-tzwdGcH9z-ehKM-r-hkzUSURnv-td-WAH0a8hUXI6VZ82e6VvHVzfIWW3nPW1whEgZHw/s1600/stirling.png" /></a></div><br />
Approximating large factorials:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">import</span> math
<span style="color: maroon; font-weight: bold;">def</span> stirling<span style="color: #808030;">(</span>n<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> math<span style="color: #808030;">.</span>sqrt<span style="color: #808030;">(</span><span style="color: #008c00;">2</span><span style="color: #808030;">*</span>math<span style="color: #808030;">.</span>pi<span style="color: #808030;">*</span>n<span style="color: #808030;">)</span> <span style="color: #808030;">*</span> <span style="color: #808030;">(</span>n<span style="color: #808030;">/</span>math<span style="color: #808030;">.</span>e<span style="color: #808030;">)</span><span style="color: #808030;">*</span><span style="color: #808030;">*</span>n
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">__name__</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #0000e6;">'__main__'</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span><span style="color: #008c00;">1</span><span style="color: #808030;">,</span><span style="color: #008c00;">20</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
s <span style="color: #808030;">=</span> stirling<span style="color: #808030;">(</span>i<span style="color: #808030;">)</span>
f <span style="color: #808030;">=</span> math<span style="color: #808030;">.</span>factorial<span style="color: #808030;">(</span>i<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"stirling: %21.2f factorial: %21d error: %3.2f%%"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>s<span style="color: #808030;">,</span> f<span style="color: #808030;">,</span> <span style="color: #e34adc;">abs</span><span style="color: #808030;">(</span><span style="color: #808030;">(</span><span style="color: #808030;">(</span>f<span style="color: #808030;">-</span>s<span style="color: #808030;">)</span><span style="color: #808030;">/</span>s<span style="color: #808030;">)</span><span style="color: #808030;">*</span><span style="color: green;">100.0</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
</pre><pre style="background: #ffffff; color: black;"><span style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">stirling: 0.92 factorial: 1 error: 8.44%
stirling: 1.92 factorial: 2 error: 4.22%
stirling: 5.84 factorial: 6 error: 2.81%
stirling: 23.51 factorial: 24 error: 2.10%
stirling: 118.02 factorial: 120 error: 1.68%
stirling: 710.08 factorial: 720 error: 1.40%
stirling: 4980.40 factorial: 5040 error: 1.20%
stirling: 39902.40 factorial: 40320 error: 1.05%
stirling: 359536.87 factorial: 362880 error: 0.93%
stirling: 3598695.62 factorial: 3628800 error: 0.84%
stirling: 39615625.05 factorial: 39916800 error: 0.76%
stirling: 475687486.47 factorial: 479001600 error: 0.70%
stirling: 6187239475.19 factorial: 6227020800 error: 0.64%
stirling: 86661001740.60 factorial: 87178291200 error: 0.60%
stirling: 1300430722199.47 factorial: 1307674368000 error: 0.56%
stirling: 20814114415223.14 factorial: 20922789888000 error: 0.52%
stirling: 353948328666101.12 factorial: 355687428096000 error: 0.49%
stirling: 6372804626194313.00 factorial: 6402373705728000 error: 0.46%
stirling: 121112786592294176.00 factorial: 121645100408832000 error: 0.44%</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">As n increases the error decreases:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">n=165 error: 0.0505177446%
n=166 error: 0.0502133452%
n=167 error: 0.0499125922%
n=168 error: 0.0496154204%
n=169 error: 0.0493217663%</span></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-2471822762203557842011-07-27T07:43:00.000-04:002011-07-27T07:43:13.177-04:00MIT 6.00 Lecture 8 Notes on exp()<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgM4iVbdL0nZVB3e9gMDBGvxPKmbEt2-TC32a94laqcKLBFjIpYkfQuh9_pqVNx69albvE5IYLfsA9WLcu4pytGnnLWQ8CXxfT_sU9Y6XOJYpynF09WiBOpXpQKlQMZcrrHOcgj8S9vwo/s1600/DSC_0106.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="97" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgM4iVbdL0nZVB3e9gMDBGvxPKmbEt2-TC32a94laqcKLBFjIpYkfQuh9_pqVNx69albvE5IYLfsA9WLcu4pytGnnLWQ8CXxfT_sU9Y6XOJYpynF09WiBOpXpQKlQMZcrrHOcgj8S9vwo/s400/DSC_0106.JPG" width="400" /></a></div><br />
In Lecture 8 a recursive algorithm for finding a to the b is shown. The algorithm flip-flops between two cases (even, odd) until it reaches the base case of b == 0. For odd cases the problem is reduced by one, for even cases the problem is reduced by half.<br />
<br />
I think it can be hard to see where the final result comes from at first. What can exp() return? It can return 1, a or *a. 1 is returned for the edge case b == 0 so returning a or *a are the typical results. Any particular result is going to be of the form:<br />
<br />
result = a*a*a . . .<br />
<br />
<br />
For 2**15:<br />
<br />
exp3(2,15)<br />
2*exp3(2,14)<br />
exp3(4,7)<br />
4*exp3(4,6)<br />
exp3(16,3)<br />
16*exp3(16,2)<br />
exp3(256,1)<br />
b == 1<br />
a: 256<br />
result: 32768<br />
<br />
<br />
result = 2*4*16*256 => 32768<br />
<br />
Also notice that in the below code that the even test is (b%2)*2 == 0 not == b.<br />
<br />
Python Code:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> exp<span style="color: #808030;">(</span>a<span style="color: #808030;">,</span>b<span style="color: #808030;">,</span>mult<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Call %d*exp3(%d,%d)"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>mult<span style="color: #808030;">,</span>a<span style="color: #808030;">,</span>b<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> b <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">'b == 0'</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">if</span> b <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">'b == 1'</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"a: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>a<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">return</span> a
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>b<span style="color: #808030;">%</span><span style="color: #008c00;">2</span><span style="color: #808030;">)</span><span style="color: #808030;">*</span><span style="color: #008c00;">2</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">'\tif (b%2)*2 == 0: '</span><span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"[b: %d]"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>b<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">return</span> exp<span style="color: #808030;">(</span>a<span style="color: #808030;">*</span>a<span style="color: #808030;">,</span> b<span style="color: #808030;">/</span><span style="color: #008c00;">2</span><span style="color: #808030;">,</span> <span style="color: #008c00;">1</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"\telse [b: %d]"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>b<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">return</span> a<span style="color: #808030;">*</span>exp<span style="color: #808030;">(</span>a<span style="color: #808030;">,</span> b<span style="color: #808030;">-</span><span style="color: #008c00;">1</span><span style="color: #808030;">,</span> a<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">__name__</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #0000e6;">'__main__'</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">16</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"===> 2^%d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>i<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"result: %d\n"</span> <span style="color: #808030;">%</span> exp<span style="color: #808030;">(</span><span style="color: #008c00;">2</span><span style="color: #808030;">,</span>i<span style="color: #808030;">,</span><span style="color: #008c00;">1</span><span style="color: #808030;">)</span>
<span style="color: dimgrey;">'''</span>
<span style="color: dimgrey;">exp(2,4)</span>
<span style="color: dimgrey;"> exp(2*2, 4/2)</span>
<span style="color: dimgrey;"> exp(4*4, 2/2)</span>
<span style="color: dimgrey;"> b == 1</span>
<span style="color: dimgrey;"> return 16</span>
<span style="color: dimgrey;"></span>
<span style="color: dimgrey;">==> 16</span>
<span style="color: dimgrey;"></span>
<span style="color: dimgrey;">exp(2,5)</span>
<span style="color: dimgrey;"> 2*exp(2, 5-1)</span>
<span style="color: dimgrey;"> exp(2*2, 4/2)</span>
<span style="color: dimgrey;"> exp(4*4, 2/2)</span>
<span style="color: dimgrey;"> b == 1</span>
<span style="color: dimgrey;"> return 16</span>
<span style="color: dimgrey;"></span>
<span style="color: dimgrey;">==> 16 * 2 = 32</span>
<span style="color: dimgrey;"></span>
<span style="color: dimgrey;">exp(2,6)</span>
<span style="color: dimgrey;"> exp(2*2, 6/2)</span>
<span style="color: dimgrey;"> 4*exp(4, 3-1)</span>
<span style="color: dimgrey;"> exp(4*4, 2/2)</span>
<span style="color: dimgrey;"> b == 1</span>
<span style="color: dimgrey;"> return 16</span>
<span style="color: dimgrey;"></span>
<span style="color: dimgrey;">==> 16 * 4 = 64 </span>
<span style="color: dimgrey;">'''</span>
</pre><pre style="background: #ffffff; color: black;"><span style="color: dimgrey;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">===> 2^0
Call 1*exp3(2,0)
b == 0
result: 1
===> 2^1
Call 1*exp3(2,1)
b == 1
a: 2
result: 2
===> 2^2
Call 1*exp3(2,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(4,1)
b == 1
a: 4
result: 4
===> 2^3
Call 1*exp3(2,3)
else [b: 3]
Call 2*exp3(2,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(4,1)
b == 1
a: 4
result: 8
===> 2^4
Call 1*exp3(2,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(4,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(16,1)
b == 1
a: 16
result: 16
===> 2^5
Call 1*exp3(2,5)
else [b: 5]
Call 2*exp3(2,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(4,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(16,1)
b == 1
a: 16
result: 32
===> 2^6
Call 1*exp3(2,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(4,3)
else [b: 3]
Call 4*exp3(4,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(16,1)
b == 1
a: 16
result: 64
===> 2^7
Call 1*exp3(2,7)
else [b: 7]
Call 2*exp3(2,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(4,3)
else [b: 3]
Call 4*exp3(4,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(16,1)
b == 1
a: 16
result: 128
===> 2^8
Call 1*exp3(2,8)
if (b%2)*2 == 0: [b: 8]
Call 1*exp3(4,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 256
===> 2^9
Call 1*exp3(2,9)
else [b: 9]
Call 2*exp3(2,8)
if (b%2)*2 == 0: [b: 8]
Call 1*exp3(4,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 512
===> 2^10
Call 1*exp3(2,10)
if (b%2)*2 == 0: [b: 10]
Call 1*exp3(4,5)
else [b: 5]
Call 4*exp3(4,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 1024
===> 2^11
Call 1*exp3(2,11)
else [b: 11]
Call 2*exp3(2,10)
if (b%2)*2 == 0: [b: 10]
Call 1*exp3(4,5)
else [b: 5]
Call 4*exp3(4,4)
if (b%2)*2 == 0: [b: 4]
Call 1*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 2048
===> 2^12
Call 1*exp3(2,12)
if (b%2)*2 == 0: [b: 12]
Call 1*exp3(4,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(16,3)
else [b: 3]
Call 16*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 4096
===> 2^13
Call 1*exp3(2,13)
else [b: 13]
Call 2*exp3(2,12)
if (b%2)*2 == 0: [b: 12]
Call 1*exp3(4,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(16,3)
else [b: 3]
Call 16*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 8192
===> 2^14
Call 1*exp3(2,14)
if (b%2)*2 == 0: [b: 14]
Call 1*exp3(4,7)
else [b: 7]
Call 4*exp3(4,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(16,3)
else [b: 3]
Call 16*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 16384
===> 2^15
Call 1*exp3(2,15)
else [b: 15]
Call 2*exp3(2,14)
if (b%2)*2 == 0: [b: 14]
Call 1*exp3(4,7)
else [b: 7]
Call 4*exp3(4,6)
if (b%2)*2 == 0: [b: 6]
Call 1*exp3(16,3)
else [b: 3]
Call 16*exp3(16,2)
if (b%2)*2 == 0: [b: 2]
Call 1*exp3(256,1)
b == 1
a: 256
result: 32768</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com1tag:blogger.com,1999:blog-444264493461365339.post-90509396000348869012011-07-25T13:53:00.002-04:002011-07-25T18:29:18.263-04:00MIT 6.00 Problem Set 6 - Problem 5<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8xFo9LkUdxiaHghnaxZZQ9DDNjFPBsmf0toE2aKHPy0CMNGXBdfTR93UBQoqhRicNnU2yzcsjZN9j7sp4rtlPZe8my5gWahlK6cMJF0i1y6Br3nvdk6uzeA7Euz8QRMg_xvfUuz7JGm0/s1600/DSC_0034-1.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8xFo9LkUdxiaHghnaxZZQ9DDNjFPBsmf0toE2aKHPy0CMNGXBdfTR93UBQoqhRicNnU2yzcsjZN9j7sp4rtlPZe8my5gWahlK6cMJF0i1y6Br3nvdk6uzeA7Euz8QRMg_xvfUuz7JGm0/s1600/DSC_0034-1.JPG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">LPG Delivery, Seoul</td></tr>
</tbody></table><br />
Assume worst case. <i>pick_best_word()</i> is going to produce 13699 candidate words to be checked against the dictionary. <i>pick_best_word_faster()</i> is going to produce 127 candidate words to be checked against the dictionary.<br />
<br />
The original word list is length 83667 while the rearranged word list is length 69091. The reduction in length is from the collapse of sets of words made from the same letters into single words.<br />
<br />
I think the bulk of the performance improvement is from the reduction in candidate words, not dictionary length or access methods.<br />
<br />
<br />
Python Output:<br />
<br />
<br />
>>> len(list(itertools.permutations('abcdefg', 7)))<br />
5040<br />
>>> len(list(itertools.permutations('abcdefg', 6)))<br />
5040<br />
>>> len(list(itertools.permutations('abcdefg', 5)))<br />
2520<br />
>>> len(list(itertools.permutations('abcdefg', 4)))<br />
840<br />
>>> len(list(itertools.permutations('abcdefg', 3)))<br />
210<br />
>>> len(list(itertools.permutations('abcdefg', 2)))<br />
42<br />
>>> len(list(itertools.permutations('abcdefg', 1)))<br />
7<br />
>>> 5040+5040+2520+840+210+42+7<br />
13699<br />
<br />
>>> len(list(itertools.combinations('abcdefg', 7)))<br />
1<br />
>>> len(list(itertools.combinations('abcdefg', 6)))<br />
7<br />
>>> len(list(itertools.combinations('abcdefg', 5)))<br />
21<br />
>>> len(list(itertools.combinations('abcdefg', 4)))<br />
35<br />
>>> len(list(itertools.combinations('abcdefg', 3)))<br />
35<br />
>>> len(list(itertools.combinations('abcdefg', 2)))<br />
21<br />
>>> len(list(itertools.combinations('abcdefg', 1)))<br />
7<br />
>>> 1+7+21+35+35+21+7<br />
127Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com1tag:blogger.com,1999:blog-444264493461365339.post-80133692010534607922011-07-25T12:48:00.002-04:002011-07-25T13:56:37.771-04:00MIT 6.00 Problem Set 6 - Problem 4 Answer<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyF-fcrApQ16uV5ImDSHlVszPXcuUhFWFcpYShDNXR0t8YwFG0Rn74roMupVXuAZmqytECI67Bze-5BEGwwbrBTUdhTxNppWkRoLb8LEJPRO7nFBYuBQTACF2DCpaLZp9YUsUc_1wS4t4/s1600/DSC_0226.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyF-fcrApQ16uV5ImDSHlVszPXcuUhFWFcpYShDNXR0t8YwFG0Rn74roMupVXuAZmqytECI67Bze-5BEGwwbrBTUdhTxNppWkRoLb8LEJPRO7nFBYuBQTACF2DCpaLZp9YUsUc_1wS4t4/s1600/DSC_0226.JPG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Anhinga, Florida Everglades</td></tr>
</tbody></table><br />
This code is roughly twice as fast as the code from Problem 3. Two changes are responsible for the increased speed.<br />
<br />
The first change is the switch from <i>itertools.permutations()</i> to <i>itertools.combinations()</i>. In Problem 3 every permutation of n letters is checked against the dictionary. Since order doesn't mater with a sorted dictionary key permutations would be redundant.<br />
<br />
<br />
Python Code Answer:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> pick_best_word_faster<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> rearrange_dict<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
score <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
high_word <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span>
perms <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
attempts <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
local_hand <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
<span style="color: dimgrey;"># itertools.permutations is iterating for letters</span>
<span style="color: dimgrey;"># that are zero count in the hash, dump the zeros</span>
<span style="color: dimgrey;"># convert dict to list</span>
<span style="color: maroon; font-weight: bold;">for</span> k <span style="color: maroon; font-weight: bold;">in</span> hand<span style="color: #808030;">.</span>keys<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> hand<span style="color: #808030;">[</span>k<span style="color: #808030;">]</span> <span style="color: #808030;">></span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span>hand<span style="color: #808030;">[</span>k<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
local_hand<span style="color: #808030;">.</span>append<span style="color: #808030;">(</span>k<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"local_hand: "</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"\t"</span><span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">print</span> local_hand
<span style="color: maroon; font-weight: bold;">print</span>
take <span style="color: #808030;">=</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>local_hand<span style="color: #808030;">)</span>
<span style="color: dimgrey;"># try for a bingo, then one less than a bingo, etc.</span>
<span style="color: maroon; font-weight: bold;">while</span> take <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
perms <span style="color: #808030;">=</span> <span style="color: #e34adc;">list</span><span style="color: #808030;">(</span>itertools<span style="color: #808030;">.</span>combinations<span style="color: #808030;">(</span>local_hand<span style="color: #808030;">,</span> take<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
take <span style="color: #808030;">-</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">for</span> letter_list <span style="color: maroon; font-weight: bold;">in</span> perms<span style="color: #808030;">:</span>
attempts <span style="color: #808030;">+</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
sorted_word <span style="color: #808030;">=</span> <span style="color: #0000e6;">''</span><span style="color: #808030;">.</span>join<span style="color: #808030;">(</span>sorted<span style="color: #808030;">(</span>letter_list<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> rearrange_dict<span style="color: #808030;">.</span>get<span style="color: #808030;">(</span>sorted_word<span style="color: #808030;">,</span> <span style="color: #008c00;">0</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
word <span style="color: #808030;">=</span> rearrange_dict<span style="color: #808030;">[</span>sorted_word<span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">if</span> score <span style="color: #808030;"><</span> points_dict<span style="color: #808030;">[</span>word<span style="color: #808030;">]</span><span style="color: #808030;">:</span>
score <span style="color: #808030;">=</span> points_dict<span style="color: #808030;">[</span>word<span style="color: #808030;">]</span>
high_word <span style="color: #808030;">=</span> word
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"word: %s score: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>high_word<span style="color: #808030;">,</span> score<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"------------------------------------------------------------"</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"attempts: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>attempts<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> score <span style="color: #808030;"><</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #0000e6;">'.'</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> high_word
<span style="color: maroon; font-weight: bold;">def</span> get_word_rearrangements<span style="color: #808030;">(</span>word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
d <span style="color: #808030;">=</span> <span style="color: purple;">{</span><span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">for</span> word <span style="color: maroon; font-weight: bold;">in</span> word_list<span style="color: #808030;">:</span>
d<span style="color: #808030;">[</span><span style="color: #0000e6;">''</span><span style="color: #808030;">.</span>join<span style="color: #808030;">(</span>sorted<span style="color: #808030;">(</span>word<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> word
<span style="color: maroon; font-weight: bold;">return</span> d
</pre><br />
<br />
Output:<br />
<br />
<br />
d g i i z m t<br />
calling with:<br />
{'d': 1, 'g': 1, 'i': 2, 'z': 1, 'm': 1, 't': 1}<br />
local_hand:<br />
['d', 'g', 'i', 'i', 'z', 'm', 't']<br />
<br />
word: digit score: 7<br />
word: timid score: 8<br />
word: ditz score: 14<br />
------------------------------------------------------------<br />
attempts: 127<br />
0.0350000858307<br />
Score: 400.00 Total Score: 400.00<br />
Turn took 0.04 seconds, 0.31 seconds remaining<br />
<br />
g i m<br />
calling with:<br />
{'d': 0, 'g': 1, 'i': 1, 'z': 0, 'm': 1, 't': 0}<br />
local_hand:<br />
['g', 'i', 'm']<br />
<br />
word: mig score: 6<br />
------------------------------------------------------------<br />
attempts: 7<br />
0.0209999084473<br />
Score: 285.72 Total Score: 685.71<br />
Turn took 0.02 seconds, 0.29 seconds remaining<br />
<br />
Final Score: 685.71<br />
<br />
<br />
<br />
a c e j m n n<br />
calling with:<br />
{'a': 1, 'c': 1, 'e': 1, 'j': 1, 'm': 1, 'n': 2}<br />
local_hand:<br />
['a', 'c', 'e', 'j', 'm', 'n', 'n']<br />
<br />
word: nance score: 7<br />
word: mace score: 8<br />
word: jean score: 11<br />
word: jam score: 12<br />
------------------------------------------------------------<br />
attempts: 127<br />
0.0339999198914<br />
Time limit exceeded.<br />
Score: 0.00 Total Score: 0.00<br />
<br />
c e n n<br />
calling with:<br />
{'a': 0, 'c': 1, 'e': 1, 'j': 0, 'm': 0, 'n': 2}<br />
local_hand:<br />
['c', 'e', 'n', 'n']<br />
<br />
word: ne score: 2<br />
------------------------------------------------------------<br />
attempts: 15<br />
0.0210001468658<br />
Time limit exceeded.<br />
Score: 0.00 Total Score: 0.00<br />
<br />
c n<br />
calling with:<br />
{'a': 0, 'c': 1, 'e': 0, 'j': 0, 'm': 0, 'n': 1}<br />
local_hand:<br />
['c', 'n']<br />
<br />
------------------------------------------------------------<br />
attempts: 3<br />
0.018000125885<br />
No legal combination possible.Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-34618627144831011182011-07-22T15:16:00.000-04:002011-07-22T15:16:58.651-04:00MIT 6.00 Problem Set 6 Answers - Problem 3<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXNoy7DyKH_t44LlHd3KkiH4TL-uJ80AB3nCgIk2zE9M76JrT6XjWOF8cqM20Q9X-Z1a9qf4UDvWHVC0WcEtGmo3Kg-b588MJ-uKUN2waNHxkjyEvdIZuhJW6E7I21sCBa4ko8p34sFuY/s1600/DSC_0548.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXNoy7DyKH_t44LlHd3KkiH4TL-uJ80AB3nCgIk2zE9M76JrT6XjWOF8cqM20Q9X-Z1a9qf4UDvWHVC0WcEtGmo3Kg-b588MJ-uKUN2waNHxkjyEvdIZuhJW6E7I21sCBa4ko8p34sFuY/s1600/DSC_0548.JPG" /></a></div><br />
I don't think the intent of this exercise was to write a permutation routine or to use itertools but I used itertools. pick_best_word() is pretty much the whole assignment.<br />
<br />
Python Code Answer:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> pick_best_word<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> points_dict<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
score <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
length <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
high_word <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span>
perms <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
local_hand <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
attempts <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
<span style="color: dimgrey;"># itertools.permutations is iterating for letters</span>
<span style="color: dimgrey;"># that are zero count in the hash, dump the zeros</span>
<span style="color: dimgrey;"># convert dict to list</span>
<span style="color: maroon; font-weight: bold;">for</span> k <span style="color: maroon; font-weight: bold;">in</span> hand<span style="color: #808030;">.</span>keys<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> hand<span style="color: #808030;">[</span>k<span style="color: #808030;">]</span> <span style="color: #808030;">></span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span>hand<span style="color: #808030;">[</span>k<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
local_hand<span style="color: #808030;">.</span>append<span style="color: #808030;">(</span>k<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"local_hand: "</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"\t"</span><span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">print</span> local_hand
<span style="color: maroon; font-weight: bold;">print</span>
take <span style="color: #808030;">=</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>local_hand<span style="color: #808030;">)</span>
<span style="color: dimgrey;"># try for a bingo, then one less than a bingo, etc.</span>
<span style="color: maroon; font-weight: bold;">while</span> take <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
perms <span style="color: #808030;">=</span> <span style="color: #e34adc;">list</span><span style="color: #808030;">(</span>itertools<span style="color: #808030;">.</span>permutations<span style="color: #808030;">(</span>local_hand<span style="color: #808030;">,</span> take<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
take <span style="color: #808030;">-</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">for</span> letter_list <span style="color: maroon; font-weight: bold;">in</span> perms<span style="color: #808030;">:</span>
attempts <span style="color: #808030;">+</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
word <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span>
<span style="color: maroon; font-weight: bold;">for</span> c <span style="color: maroon; font-weight: bold;">in</span> letter_list<span style="color: #808030;">:</span>
word <span style="color: #808030;">+</span><span style="color: #808030;">=</span> c
<span style="color: maroon; font-weight: bold;">if</span> points_dict<span style="color: #808030;">.</span>has_key<span style="color: #808030;">(</span>word<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> score <span style="color: #808030;"><</span> points_dict<span style="color: #808030;">[</span>word<span style="color: #808030;">]</span><span style="color: #808030;">:</span>
score <span style="color: #808030;">=</span> points_dict<span style="color: #808030;">[</span>word<span style="color: #808030;">]</span>
high_word <span style="color: #808030;">=</span> word
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"word: %s score: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> score<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"------------------------------------------------------------"</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"attempts: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>attempts<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> score <span style="color: #808030;"><</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #0000e6;">'.'</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> high_word
</pre><br />
<br />
<br />
For each seven letter hand 13699 permutations are checked<br />
in the first pass:<br />
<br />
>>> import itertools<br />
>>> perms = list(itertools.permutations('1234567', 7))<br />
>>> len(perms)<br />
5040<br />
>>> perms = list(itertools.permutations('1234567', 6))<br />
>>> len(perms)<br />
5040<br />
>>> perms = list(itertools.permutations('1234567', 5))<br />
>>> len(perms)<br />
2520<br />
>>> perms = list(itertools.permutations('1234567', 4))<br />
>>> len(perms)<br />
840<br />
>>> perms = list(itertools.permutations('1234567', 3))<br />
>>> len(perms)<br />
210<br />
>>> perms = list(itertools.permutations('1234567', 2))<br />
>>> len(perms)<br />
42<br />
>>> perms = list(itertools.permutations('1234567', 1))<br />
>>> len(perms)<br />
7<br />
>>> 5040+5040+2520+840+210+42+7<br />
13699<br />
<br />
Output:<br />
<br />
<br />
s u u f y j m<br />
calling with:<br />
{'s': 1, 'u': 2, 'f': 1, 'y': 1, 'j': 1, 'm': 1}<br />
local_hand:<br />
['s', 'u', 'u', 'f', 'y', 'j', 'm']<br />
<br />
word: fumy score: 12<br />
------------------------------------------------------------<br />
attempts: 13699<br />
Score: 184.62 Total Score: 184.62<br />
Turn took 0.06 seconds, 1.75 seconds remaining<br />
<br />
s u j<br />
calling with:<br />
{'s': 1, 'u': 1, 'f': 0, 'y': 0, 'j': 1, 'm': 0}<br />
local_hand:<br />
['s', 'u', 'j']<br />
<br />
word: jus score: 10<br />
------------------------------------------------------------<br />
attempts: 15<br />
Score: 400.00 Total Score: 584.61<br />
Turn took 0.03 seconds, 1.73 seconds remaining<br />
<br />
Final Score: 584.61<br />
<br />
<br />
p s t z m o o<br />
calling with:<br />
{'p': 1, 's': 1, 't': 1, 'z': 1, 'm': 1, 'o': 2}<br />
local_hand:<br />
['p', 's', 't', 'z', 'm', 'o', 'o']<br />
<br />
word: pomos score: 9<br />
word: zooms score: 16<br />
------------------------------------------------------------<br />
attempts: 13699<br />
Score: 275.86 Total Score: 275.86<br />
Turn took 0.06 seconds, 1.59 seconds remaining<br />
<br />
p t<br />
calling with:<br />
{'p': 1, 's': 0, 't': 1, 'z': 0, 'm': 0, 'o': 0}<br />
local_hand:<br />
['p', 't']<br />
<br />
------------------------------------------------------------<br />
attempts: 4<br />
No legal combination possible.<br />
<br />
<br />
<br />
s s u d g i t<br />
calling with:<br />
{'s': 2, 'u': 1, 'd': 1, 'g': 1, 'i': 1, 't': 1}<br />
local_hand:<br />
['s', 's', 'u', 'd', 'g', 'i', 't']<br />
<br />
word: disgust score: 9<br />
------------------------------------------------------------<br />
attempts: 13699<br />
Score: 1053.57 Total Score: 1053.57<br />
Turn took 0.06 seconds, 1.36 seconds remaining<br />
<br />
Final Score: 1053.57<br />
<div><br />
</div><br />
<div><br />
</div><div><br />
</div><div><br />
</div>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-12238776206204697642011-07-21T14:37:00.002-04:002011-07-24T20:00:08.902-04:00MIT 6.00 Problem Set 6 Answer - Problems 1 & 2<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7kZXAEbkfTD9SdlYX9UxmZMf0Mu2QHMK9XdsuiEfkIHi7w9oO2UqdpgNTKQTKJLaeu_T4kd29e4ch1eTEh6_Kf9_C2RZ3AtQiReP0DRnX12puzQBpy5sJMs1VD1DROAVcbxXMvzT-Da4/s1600/DSC_0231.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7kZXAEbkfTD9SdlYX9UxmZMf0Mu2QHMK9XdsuiEfkIHi7w9oO2UqdpgNTKQTKJLaeu_T4kd29e4ch1eTEh6_Kf9_C2RZ3AtQiReP0DRnX12puzQBpy5sJMs1VD1DROAVcbxXMvzT-Da4/s1600/DSC_0231.JPG" /></a></div><br />
Python Code Answer:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> play_hand<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
total_score <span style="color: #808030;">=</span> <span style="color: green;">0.0</span>
min_elapsed <span style="color: #808030;">=</span> <span style="color: green;">0.010</span>
hand_score <span style="color: #808030;">=</span> <span style="color: green;">0.0</span>
time_limit <span style="color: #808030;">=</span> <span style="color: #e34adc;">raw_input</span><span style="color: #808030;">(</span><span style="color: #0000e6;">'Enter Time Limit: '</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> time_limit<span style="color: #808030;">.</span>isdigit<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
time_limit <span style="color: #808030;">=</span> <span style="color: #e34adc;">float</span><span style="color: #808030;">(</span>time_limit<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Enter Valid Time Limit"</span>
<span style="color: maroon; font-weight: bold;">return</span>
<span style="color: maroon; font-weight: bold;">while</span> <span style="color: #e34adc;">True</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span>
num_letters <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
<span style="color: maroon; font-weight: bold;">for</span> key <span style="color: maroon; font-weight: bold;">in</span> hand<span style="color: #808030;">.</span>keys<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span>hand<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
num_letters <span style="color: #808030;">+</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">print</span> key<span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">if</span> num_letters <span style="color: #808030;"><</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">break</span>
start <span style="color: #808030;">=</span> time<span style="color: #808030;">.</span>time<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
word <span style="color: #808030;">=</span> <span style="color: #e34adc;">raw_input</span><span style="color: #808030;">(</span><span style="color: #0000e6;">'Word: '</span><span style="color: #808030;">)</span>
stop <span style="color: #808030;">=</span> time<span style="color: #808030;">.</span>time<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
elapsed <span style="color: #808030;">=</span> stop <span style="color: #808030;">-</span> start
time_limit <span style="color: #808030;">-</span><span style="color: #808030;">=</span> elapsed
<span style="color: dimgrey;"># prevent divide by zero error, force min time</span>
<span style="color: maroon; font-weight: bold;">if</span> elapsed <span style="color: #808030;"><</span> min_elapsed<span style="color: #808030;">:</span>
elapsed <span style="color: #808030;">=</span> min_elapsed
<span style="color: maroon; font-weight: bold;">if</span> word <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #0000e6;">'.'</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">break</span>
<span style="color: maroon; font-weight: bold;">if</span> is_valid_word<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> hand<span style="color: #808030;">,</span> word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
update_hand<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> word<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> time_limit <span style="color: #808030;">></span> <span style="color: green;">0.0</span><span style="color: #808030;">:</span>
hand_score <span style="color: #808030;">=</span> get_word_score<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> num_letters<span style="color: #808030;">)</span> <span style="color: #808030;">/</span> elapsed
total_score <span style="color: #808030;">+</span><span style="color: #808030;">=</span> hand_score
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Time limit exceeded."</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Score: %0.2f Total Score: %0.2f"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>hand_score<span style="color: #808030;">,</span> total_score<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Invalid word. Try again."</span>
<span style="color: maroon; font-weight: bold;">if</span> time_limit <span style="color: #808030;">></span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Turn took %0.2f seconds, %0.2f seconds remaining"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>elapsed<span style="color: #808030;">,</span> time_limit<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Final Score: %0.2f"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>total_score<span style="color: #808030;">)</span>
</pre><pre style="background: #ffffff; color: black;"><span style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; color: black; font-family: 'DejaVu Sans'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"><span style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; color: black; font-family: 'DejaVu Sans'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="font-family: Verdana, sans-serif;">Loading word list from file...
83667 words loaded.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter Time Limit: 10
q e t h j o n Word: note
Score: 0.44 Total Score: 0.44
Turn took 9.05 seconds, 0.95 seconds remaining
q h j Word: .
Final Score: 0.44
Enter n to deal a new hand, r to replay the last hand, or e to end game:
Invalid command.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter Time Limit:
Enter Valid Time Limit
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter Time Limit: asdf
Enter Valid Time Limit
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter Time Limit: 10
a q v p j j o Word:
Invalid word. Try again.
a q v p j j o Word: .
Final Score: 0.00
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Enter Time Limit: 10
p u t i h z n Word: putz
Score: 3.28 Total Score: 3.28
Turn took 4.57 seconds, 5.43 seconds remaining
i h n Word: hin
Score: 1.51 Total Score: 4.79
Turn took 3.98 seconds, 1.45 seconds remaining
Final Score: 4.79
Enter n to deal a new hand, r to replay the last hand, or e to end game:
<span class="Apple-style-span" style="font-family: 'DejaVu Sans';"><span class="Apple-style-span" style="white-space: normal;">
</span></span></span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; color: black; font-family: 'DejaVu Sans'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"><span style="font-family: Verdana, sans-serif;">
</span></pre></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-51246278372883004692011-07-19T17:09:00.004-04:002011-07-24T19:57:49.843-04:00MIT 6.00 Problem Set 4 - Problem 5 Answer<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx0wT69WPO7xP3G8S-DAPeUIlW-wPJKREB3ZNMy05dshr8FvaIkWrnVJIiwYGgMCdS76KXcRWouXJhR3Yx31ZiOQVbF1TqVuJz2WySksGgkO_IUXAb7Wn1sZPt1K2MY1RCvDo4juKDNV8/s1600/DSC_0005-1.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx0wT69WPO7xP3G8S-DAPeUIlW-wPJKREB3ZNMy05dshr8FvaIkWrnVJIiwYGgMCdS76KXcRWouXJhR3Yx31ZiOQVbF1TqVuJz2WySksGgkO_IUXAb7Wn1sZPt1K2MY1RCvDo4juKDNV8/s1600/DSC_0005-1.JPG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Key Largo</td></tr>
</tbody></table><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">I spent a bunch of time thinking about how to do an exhaustive search with a given fragment by appending every letter of the alphabet all the way up to the maximum word length in the dictionary to see if the fragment was the beginning of a dictionary word. I thought there had to be a better way.</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;">This code below is looking for an exact match. </span><span class="Apple-style-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">If </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">fragment_str</span><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> is the beginning of a word that is in the </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">wordlist</span><span class="Apple-style-span" style="font-family: Verdana, sans-serif;"> but not a standalone word then this test will return False.</span></span><br />
<br />
<br />
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: black; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: maroon; font-weight: bold;">if</span> fragment_str <span style="color: maroon; font-weight: bold;">in</span> wordlist<span style="color: #808030;">:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The fragment of length n should be compared with the first n letters of each dictionary word. If there is a match then there is a word containing that fragment. </span><span class="Apple-style-span" style="font-family: Verdana, sans-serif;">The search itself is brute force. Words that are too short are checked. Words that don't even begin with the same first letter are checked. So the efficiency can be improved but the game play is correct. </span></pre><br />
<br />
Python Code Answer:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: maroon; font-weight: bold;">def</span> fragment_starts_word<span style="color: #808030;">(</span>fragment_str<span style="color: #808030;">,</span> wordlist<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
fragment_str_len <span style="color: #808030;">=</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>fragment_str<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> word <span style="color: maroon; font-weight: bold;">in</span> wordlist<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> word<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">:</span>fragment_str_len<span style="color: #808030;">]</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> fragment_str<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>word<span style="color: #808030;">)</span> <span style="color: #808030;">></span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>fragment_str<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Fragment (%s) is start of at least one longer word (%s)"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>fragment_str<span style="color: #808030;">,</span> word<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #e34adc;">True</span>
<span style="color: maroon; font-weight: bold;">def</span> other<span style="color: #808030;">(</span>player<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> player <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">2</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">def</span> ghost<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
wordlist <span style="color: #808030;">=</span> load_words<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
fragment <span style="color: #808030;">=</span> <span style="color: #808030;">[</span><span style="color: #808030;">]</span>
player <span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
fragment_str <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span>
<span style="color: maroon; font-weight: bold;">while</span> <span style="color: #e34adc;">True</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"\nFragment: "</span> <span style="color: #808030;">+</span> fragment_str
turn <span style="color: #808030;">=</span> <span style="color: #e34adc;">raw_input</span><span style="color: #808030;">(</span><span style="color: #0000e6;">"Player "</span> <span style="color: #808030;">+</span> <span style="color: #e34adc;">str</span><span style="color: #808030;">(</span>player<span style="color: #808030;">)</span> <span style="color: #808030;">+</span> <span style="color: #0000e6;">" Turn: "</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>turn<span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span> <span style="color: maroon; font-weight: bold;">and</span> turn <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">string</span><span style="color: #808030;">.</span>ascii_letters<span style="color: #808030;">:</span>
fragment<span style="color: #808030;">.</span>append<span style="color: #808030;">(</span>turn<span style="color: #808030;">.</span>lower<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
fragment_str <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span><span style="color: #808030;">.</span>join<span style="color: #808030;">(</span>fragment<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> fragment_str <span style="color: maroon; font-weight: bold;">in</span> wordlist<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> fragment_str <span style="color: #808030;">+</span> <span style="color: #0000e6;">" is a word."</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>fragment<span style="color: #808030;">)</span> <span style="color: #808030;">></span> <span style="color: #008c00;">3</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Player "</span> <span style="color: #808030;">+</span> <span style="color: #e34adc;">str</span><span style="color: #808030;">(</span>player<span style="color: #808030;">)</span> <span style="color: #808030;">+</span> <span style="color: #0000e6;">" loses."</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Player "</span> <span style="color: #808030;">+</span> <span style="color: #e34adc;">str</span><span style="color: #808030;">(</span>other<span style="color: #808030;">(</span>player<span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: #808030;">+</span> <span style="color: #0000e6;">" wins!"</span>
sys<span style="color: #808030;">.</span><span style="color: #e34adc;">exit</span><span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">elif</span> fragment_starts_word<span style="color: #808030;">(</span>fragment_str<span style="color: #808030;">,</span> wordlist<span style="color: #808030;">)</span> <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #e34adc;">True</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Fragment is not beginning of any word."</span>
sys<span style="color: #808030;">.</span><span style="color: #e34adc;">exit</span><span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Input must be a single letter."</span>
<span style="color: maroon; font-weight: bold;">break</span>
player <span style="color: #808030;">=</span> other<span style="color: #808030;">(</span>player<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">__name__</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #0000e6;">'__main__'</span><span style="color: #808030;">:</span>
ghost<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
</pre><br />
<pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="color: maroon;"><b>
</b></span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span class="Apple-style-span" style="color: maroon;"><b>
</b></span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Loading word list from file...
83667 words loaded.
Fragment:
Player 1 Turn: p
Fragment (p) is start of at least one longer word (pa)
Fragment: p
Player 2 Turn: e
pe is a word.
Fragment: pe
Player 1 Turn: a
pea is a word.
Fragment: pea
Player 2 Turn: f
Fragment (peaf) is start of at least one longer word (peafowl)
Fragment: peaf
Player 1 Turn: o
Fragment (peafo) is start of at least one longer word (peafowl)
Fragment: peafo
Player 2 Turn: w
Fragment (peafow) is start of at least one longer word (peafowl)
Fragment: peafow
Player 1 Turn: l
peafowl is a word.
Player 1 loses.
Player 2 wins!</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">******************************************************</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Fragment:
Player 1 Turn: p
Fragment (p) is start of at least one longer word (pa)
Fragment: p
Player 2 Turn: y
Fragment (py) is start of at least one longer word (pya)
Fragment: py
Player 1 Turn: t
Fragment (pyt) is start of at least one longer word (python)
Fragment: pyt
Player 2 Turn: h
Fragment (pyth) is start of at least one longer word (python)
Fragment: pyth
Player 1 Turn: o
Fragment (pytho) is start of at least one longer word (python)
Fragment: pytho
Player 2 Turn: n
python is a word.
Player 2 loses.
Player 1 wins!</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="font-family: 'DejaVu Sans'; white-space: normal;"><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="font-family: Verdana, sans-serif;">******************************************************</span></pre></span><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="font-family: Verdana, sans-serif;">Fragment:
Player 1 Turn: q
Fragment (q) is start of at least one longer word (qabala)
Fragment: q
Player 2 Turn: z
Fragment is not beginning of any word.</span></pre><span class="Apple-style-span" style="font-family: 'DejaVu Sans'; white-space: normal;"><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="font-family: Verdana, sans-serif;">
</span></pre></span></span></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com0tag:blogger.com,1999:blog-444264493461365339.post-72036125131534426452011-07-18T17:28:00.004-04:002011-07-24T20:00:44.776-04:00MIT 6.00 Problem Set 5 - Problems 1 - 4 Answers<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinlUPS3XNFeWcEaQfz368IhWgSVGYR6lrgikgD14PvNNTN3vNn7XjF_yY-Ccvdcs9KoG_cH6_XZRFzBBBxiiEbw_4b9EqNZ1JAQZ8CZ2UhnhMY-sgcpMUek_7A19OJ213MzLZXIV_IHhE/s1600/DSC_0333.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinlUPS3XNFeWcEaQfz368IhWgSVGYR6lrgikgD14PvNNTN3vNn7XjF_yY-Ccvdcs9KoG_cH6_XZRFzBBBxiiEbw_4b9EqNZ1JAQZ8CZ2UhnhMY-sgcpMUek_7A19OJ213MzLZXIV_IHhE/s1600/DSC_0333.JPG" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Juvenile Gull, Australia</td></tr>
</tbody></table>Problem set 5 has a framework of provided code to complete the assignment within. Below is the code that needs to be added to the framework to complete the assignment.<br />
<br />
Python Code Answer:<br />
<br />
<pre style="background: #ffffff; color: black;"><span style="color: dimgrey;"># Problem #1: Scoring a word</span>
<span style="color: maroon; font-weight: bold;">def</span> get_word_score<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> n<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
score <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
<span style="color: maroon; font-weight: bold;">for</span> c <span style="color: maroon; font-weight: bold;">in</span> word<span style="color: #808030;">:</span>
score <span style="color: #808030;">+</span><span style="color: #808030;">=</span> SCRABBLE_LETTER_VALUES<span style="color: #808030;">[</span>c<span style="color: #808030;">]</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #e34adc;">len</span><span style="color: #808030;">(</span>word<span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> HAND_SIZE<span style="color: #808030;">:</span>
score <span style="color: #808030;">+</span><span style="color: #808030;">=</span> <span style="color: #008c00;">50</span>
<span style="color: maroon; font-weight: bold;">return</span> score
<span style="color: dimgrey;"># Problem #2: Update a hand by removing letters</span>
<span style="color: maroon; font-weight: bold;">def</span> update_hand<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> word<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> c <span style="color: maroon; font-weight: bold;">in</span> word<span style="color: #808030;">:</span>
hand<span style="color: #808030;">[</span>c<span style="color: #808030;">]</span> <span style="color: #808030;">-</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">return</span> hand
<span style="color: dimgrey;"># Problem #3: Test word validity</span>
<span style="color: maroon; font-weight: bold;">def</span> is_valid_word<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> hand<span style="color: #808030;">,</span> word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
local_hand <span style="color: #808030;">=</span> hand<span style="color: #808030;">.</span>copy<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">for</span> c <span style="color: maroon; font-weight: bold;">in</span> word<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">if</span> local_hand<span style="color: #808030;">.</span>get<span style="color: #808030;">(</span>c<span style="color: #808030;">,</span> <span style="color: #008c00;">0</span><span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #e34adc;">False</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
local_hand<span style="color: #808030;">[</span>c<span style="color: #808030;">]</span> <span style="color: #808030;">-</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">if</span> word<span style="color: #808030;">.</span>lower<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">in</span> word_list<span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #e34adc;">True</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">return</span> <span style="color: #e34adc;">False</span>
<span style="color: dimgrey;"># Problem #4: Playing a hand</span>
<span style="color: maroon; font-weight: bold;">def</span> play_hand<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
total_score <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
<span style="color: maroon; font-weight: bold;">while</span> <span style="color: #e34adc;">True</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span>
num_letters <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span>
<span style="color: maroon; font-weight: bold;">for</span> key <span style="color: maroon; font-weight: bold;">in</span> hand<span style="color: #808030;">.</span>keys<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">for</span> i <span style="color: maroon; font-weight: bold;">in</span> <span style="color: #e34adc;">range</span><span style="color: #808030;">(</span>hand<span style="color: #808030;">[</span>key<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #808030;">:</span>
num_letters <span style="color: #808030;">+</span><span style="color: #808030;">=</span> <span style="color: #008c00;">1</span>
<span style="color: maroon; font-weight: bold;">print</span> key<span style="color: #808030;">,</span>
<span style="color: maroon; font-weight: bold;">if</span> num_letters <span style="color: #808030;"><</span> <span style="color: #008c00;">1</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">break</span>
word <span style="color: #808030;">=</span> <span style="color: #e34adc;">raw_input</span><span style="color: #808030;">(</span><span style="color: #0000e6;">'Word: '</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">if</span> word <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #0000e6;">'.'</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">break</span>
<span style="color: maroon; font-weight: bold;">if</span> is_valid_word<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> hand<span style="color: #808030;">,</span> word_list<span style="color: #808030;">)</span><span style="color: #808030;">:</span>
update_hand<span style="color: #808030;">(</span>hand<span style="color: #808030;">,</span> word<span style="color: #808030;">)</span>
hand_score <span style="color: #808030;">=</span> get_word_score<span style="color: #808030;">(</span>word<span style="color: #808030;">,</span> num_letters<span style="color: #808030;">)</span>
total_score <span style="color: #808030;">+</span><span style="color: #808030;">=</span> hand_score
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Score: %d Total Score: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>hand_score<span style="color: #808030;">,</span> total_score<span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">else</span><span style="color: #808030;">:</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Invalid word. Try again."</span>
<span style="color: maroon; font-weight: bold;">print</span> <span style="color: #0000e6;">"Final Score: %d"</span> <span style="color: #808030;">%</span> <span style="color: #808030;">(</span>total_score<span style="color: #808030;">)</span>
</pre><pre style="background: #ffffff; color: black;"><span style="color: #808030;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Output:</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">Loading word list from file...
83667 words loaded.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
u y h j o n n Word: joy
Score: 13 Total Score: 13
u h n n Word: hun
Score: 6 Total Score: 19
n Word: .
Final Score: 19
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
r r u v k l o Word: .
Final Score: 0
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
a r u w y h j Word: yaw
Score: 9 Total Score: 9
r u h j Word: .
Final Score: 9</span></pre><pre style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial;"><span style="font-family: Verdana, sans-serif;">
</span></pre><pre style="background: #ffffff; color: black;"><span style="color: #808030;">
</span></pre>Kevin Ladenheimhttp://www.blogger.com/profile/09035701407280328653noreply@blogger.com2