<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1105</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1105" rel="self"/>
  <title>Leif Ryge</title>
  <updated>Mon Jul 18 18:31:21 -0700 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1741</id>
    <published>2011-07-18T18:31:21-07:00</published>
    <updated>2011-07-18T19:00:00-07:00</updated>
    <title>[Python] changeWordSize</title>
    <content type="html">&lt;p&gt;Generator which transforms a sequence of n-bit &amp;quot;words&amp;quot; (0 &amp;lt;= i &amp;lt; 2**n) into a sequence of words of some other size. This works, but I feel like it could be made a lot clearer.&lt;/p&gt;

&lt;pre&gt;def changeWordSize ( words, inSize, outSize ):
    &amp;quot;&amp;quot;&amp;quot;
    &amp;gt;&amp;gt;&amp;gt; list( changeWordSize( [255, 18], 8, 1 ) )
    [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0]
    &amp;gt;&amp;gt;&amp;gt; list( changeWordSize( [255, 18], 8, 4 ) )
    [15, 15, 1, 2]
    &amp;gt;&amp;gt;&amp;gt; list( changeWordSize( [15, 15, 1, 2], 4, 8 ) )
    [255, 18]
    &amp;quot;&amp;quot;&amp;quot;
    assert inSize  &amp;gt; 0
    assert outSize &amp;gt; 0
    bits  = 0
    value = 0
    for word in words:
        assert type(word) in (int, long), &amp;quot;words must be int or long, got %s&amp;quot; % ( type( word ), )
        assert 0 &amp;lt;= word &amp;lt; 2 ** inSize  , &amp;quot;words must be in range 0 to 2**%s-1, got %s&amp;quot; % ( inSize, word )
        value  = value &amp;lt;&amp;lt; inSize
        value += word
        bits  += inSize
        while outSize &amp;lt;= bits:
            bits    -= outSize
            newWord  = value &amp;gt;&amp;gt; bits
            value   -= newWord &amp;lt;&amp;lt; bits
            yield newWord
    if bits and inSize &amp;lt; outSize:
        yield value &amp;lt;&amp;lt; (outSize - bits)
&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1741-changewordsize" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor57168</id>
    <published>2008-10-31T20:37:23-07:00</published>
    <title>[Python] On Count word occurrences in a string</title>
    <content type="html">&lt;p&gt;Here is another version which uses the handy itertools.groupby function.&lt;/p&gt;

&lt;pre&gt;from itertools import groupby
import doctest

def printWordFrequencies(text):
    &amp;quot;&amp;quot;&amp;quot;
    &amp;gt;&amp;gt;&amp;gt; printWordFrequencies(&amp;quot;Ob la di ob la da&amp;quot;)
    1 da
    1 di
    2 la
    2 ob&amp;quot;&amp;quot;&amp;quot;
    for w, g in groupby(sorted(text.lower().split())):
        print &amp;quot;%s %s&amp;quot; % (len(list(g)), w)

doctest.testmod(verbose=True)
&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/176-count-word-occurrences-in-a-string/refactors/57168" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor57150</id>
    <published>2008-10-31T19:58:08-07:00</published>
    <title>[Python] On Count word occurrences in a string</title>
    <content type="html">&lt;p&gt;This prints not only the count but also the locations of each word.&lt;/p&gt;

&lt;pre&gt;def mk_index(seq):
    &amp;quot;&amp;quot;&amp;quot;Index a sequence
    &amp;gt;&amp;gt;&amp;gt; sorted(mk_index(&amp;quot;abcba&amp;quot;).items())
    [('a', [0, 4]), ('b', [1, 3]), ('c', [2])]&amp;quot;&amp;quot;&amp;quot;
    result={}
    for location, item in enumerate(seq):
        result.setdefault(item,[]).append(location)
    return result

def printWordFrequencyAndLocationReport(text):
    &amp;quot;&amp;quot;&amp;quot;Report count and locations of each word in text
    &amp;gt;&amp;gt;&amp;gt; printWordFrequencyAndLocationReport('Ob la di ob la da')
    ob occurred 2 times [0, 3]
    la occurred 2 times [1, 4]
    da occurred 1 time [5]
    di occurred 1 time [2]&amp;quot;&amp;quot;&amp;quot;
    for word, locs in sorted(mk_index(text.lower().split()).items(),
                             key=lambda (w,l): len(l), reverse=True):
        print &amp;quot;%s occurred %s time%s %s&amp;quot; % (word, len(locs),
                                            ('','s')[len(locs)&amp;gt;1], locs)

import doctest
doctest.testmod(verbose=True)
&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/176-count-word-occurrences-in-a-string/refactors/57150" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56822</id>
    <published>2008-10-31T06:55:07-07:00</published>
    <title>[Python] On Computing permutations with a recursive generator expression</title>
    <content type="html">&lt;p&gt;Answering my own question: the caveat to idiom_B is that it isn't short-circuiting. So, if instead of z and y we had z() and y() then evaluating the expression causes BOTH functions to be called (regardless of the value of x) in idiom_B, while in A and C only the desired function is called.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/551-computing-permutations-with-a-recursive-generator-expression/refactors/56822" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56581</id>
    <published>2008-10-30T20:23:38-07:00</published>
    <title>[Python] On Computing permutations with a recursive generator expression</title>
    <content type="html">&lt;p&gt;Thanks! I was actually unaware that booleans could be used as getitem keys, which is a very useful trick. I wonder why I've seen this ugly idiom A in wide use instead of the much more elegant B:&lt;/p&gt;

&lt;pre&gt;idiom_A = lambda x,y,z: (x and [y] or [z])[0]
idiom_B = lambda x,y,z: (z, y)[x]
idiom_C = lambda x,y,z: y if x else z # introduced in Python 2.5&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/551-computing-permutations-with-a-recursive-generator-expression/refactors/56581" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55897</id>
    <published>2008-10-22T04:55:08-07:00</published>
    <title>[Python] On Permutation of values</title>
    <content type="html">&lt;p&gt;[edit: redacted, sorry]&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/523-permutation-of-values/refactors/55897" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code551</id>
    <published>2008-10-22T04:16:41-07:00</published>
    <updated>2010-03-15T23:03:11-07:00</updated>
    <title>[Python] Computing permutations with a recursive generator expression</title>
    <content type="html">&lt;p&gt;I suspect there is a better way to deal with the end of the sequence.&lt;/p&gt;

&lt;pre&gt;#!/usr/bin/python2.5
def permute(li):
    &amp;quot;&amp;quot;&amp;quot;Generate all permutations of a sequence
    &amp;gt;&amp;gt;&amp;gt; for i in permute([1,2,3]): print i
    (1, 2, 3)
    (1, 3, 2)
    (2, 1, 3)
    (2, 3, 1)
    (3, 1, 2)
    (3, 2, 1)&amp;quot;&amp;quot;&amp;quot;
    return ((i,)+j for i in li for j in (permute([k for k in li if k is not i])
                                         if len(li) &amp;gt; 1 else [()] ) )

import doctest
doctest.testmod(verbose=True)&lt;/pre&gt;</content>
    <author>
      <name>Leif Ryge</name>
      <email>leif@synthesize.us</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/551-computing-permutations-with-a-recursive-generator-expression" rel="alternate"/>
  </entry>
</feed>

