<?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:users402</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/402" rel="self"/>
  <title>armandino.myopenid.com</title>
  <updated>Sat Jan 19 04:33:14 -0800 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1645</id>
    <published>2008-01-19T04:33:14-08:00</published>
    <title>[Java] On clean up nested loops?</title>
    <content type="html">&lt;p&gt;I haven't tested this properly but I think it should work.&lt;/p&gt;

&lt;pre&gt;## java

    public Collection findAllDuplicates(List&amp;lt;NameAddress&amp;gt; list, int threshold) {
        Collection&amp;lt;NameAddress&amp;gt; duplicates = new HashSet&amp;lt;NameAddress&amp;gt;();
        boolean[] isDuplicate = new boolean[list.size()];
        
        for(int i = 0; i &amp;lt; list.size(); i++) {
            for(int k = i+1; k &amp;lt; list.size() &amp;amp;&amp;amp; !isDuplicate[i]; k++) {
                if(!isDuplicate[k] &amp;amp;&amp;amp; rateSimilarity(list.get(i), list.get(k)) &amp;gt;= threshold) {
                    duplicates.add(list.get(k));
                    isDuplicate[k] = true;
                }
            }
        }
        
        return duplicates;
    }
&lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/215-clean-up-nested-loops/refactors/1645" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1188</id>
    <published>2007-12-18T07:06:15-08:00</published>
    <title>[Ruby] On script for subversion status manipulation</title>
    <content type="html">&lt;p&gt;That's a handy script Philippe!&lt;/p&gt;

&lt;p&gt;I used to do batch operations using xargs but this is a lot simpler and cleaner. Good idea! :)&lt;/p&gt;

&lt;pre&gt;## 'svn add' all new files [shell-unix-generic]

svn st | grep ? | sed -e 's/?//g' | xargs svn add&lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/186-script-for-subversion-status-manipulation/refactors/1188" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1094</id>
    <published>2007-12-12T03:00:47-08:00</published>
    <title>[Python] On Custom sort  a list of words</title>
    <content type="html">&lt;p&gt;Hi Jared,&lt;/p&gt;

&lt;p&gt;There seems to be a problem with the last two return statements. I'm not that familiar with python syntax to fix it tho :)&lt;/p&gt;

&lt;p&gt;An unrelated question.. what's the benefit of using alphabet.index(...) instead of the find method? The latter, I understand, doesn't throw an error.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/179-custom-sort-a-list-of-words/refactors/1094" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code179</id>
    <published>2007-12-10T02:36:13-08:00</published>
    <updated>2011-01-10T11:00:08-08:00</updated>
    <title>[Python] Custom sort  a list of words</title>
    <content type="html">&lt;p&gt;My second python script. :)&lt;/p&gt;

&lt;p&gt;This one sorts a list of words in a particular order, which depends on the order of characters in the 'alphabet' string.&lt;/p&gt;

&lt;p&gt;Any suggestions on how to improve / simplify it?
&lt;/p&gt;

&lt;pre&gt;

def sort(words):

    sortedList = []

    for i in range(len(words)):

        word = words[i]

        if not sortedList:

            sortedList.append(word)

        else:

            for j in range(len(sortedList)):

                sortedWord = sortedList[j]

                if isLeftWordBeforeRight(word, sortedWord):

                    sortedList.insert(j, word)

                    break

                elif j == (len(sortedList) - 1):

                    sortedList.append(word)

    return sortedList





def isLeftWordBeforeRight(left, right):

    isBefore = False

    if right.startswith(left):

        isBefore = True

    else:

        for n in range(len(left)):

            if n &amp;lt; len(right):

                comparison = compareChars(left[n], right[n])

                if comparison != 0:

                    isBefore = comparison &amp;lt; 0

                    break    

    return isBefore



def compareChars(left, right):

    return  alphabet.find(left) - alphabet.find(right)





alphabet = &amp;quot;zyxwvutsrqpomnlkjihgfedcba&amp;quot;



inputWords = [&amp;quot;england&amp;quot;, &amp;quot;france&amp;quot;, &amp;quot;spain&amp;quot;, &amp;quot;italy&amp;quot;, &amp;quot;greece&amp;quot;, &amp;quot;portugal&amp;quot;,

              &amp;quot;canada&amp;quot;, &amp;quot;usa&amp;quot;, &amp;quot;mexico&amp;quot;, &amp;quot;peru&amp;quot;, &amp;quot;cuba&amp;quot;, &amp;quot;chile&amp;quot;, &amp;quot;argentina&amp;quot;,

              &amp;quot;zimbabwe&amp;quot;, &amp;quot;uganda&amp;quot;, &amp;quot;congo&amp;quot;, &amp;quot;zambia&amp;quot;, &amp;quot;namibia&amp;quot;, &amp;quot;ghana&amp;quot;]



print sort(inputWords)

&lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/179-custom-sort-a-list-of-words" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1039</id>
    <published>2007-12-08T07:13:06-08:00</published>
    <title>[Java] On Equals Method</title>
    <content type="html">&lt;p&gt;I usually implement equals methods along these lines...&lt;/p&gt;

&lt;pre&gt;
public boolean equals(Object obj)
{
    boolean isEqual = false;

    if(obj != null &amp;amp;&amp;amp; obj instanceof Bus)
    {
        Bus bus = (Bus)obj;

        isEqual = bus.getType().equals(getType())
               &amp;amp;&amp;amp; bus.getMaxRange() == getMaxRange());
    }

    return isEqual;
}

  &lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/135-equals-method/refactors/1039" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code176</id>
    <published>2007-12-08T07:03:40-08:00</published>
    <updated>2010-01-19T09:23:49-08:00</updated>
    <title>[Python] Count word occurrences in a string</title>
    <content type="html">&lt;p&gt;This code counts number of occurrences of each word in a string.&lt;/p&gt;

&lt;p&gt;How can I improve it or make it more pythonic?
&lt;/p&gt;

&lt;pre&gt;import re

def addWord(token, frequencies):
    count = 0
    word = ''.join(token)
    if word in frequencies:
        count = frequencies[word]
    frequencies[word] = count + 1

def getWordFrequencies(text):
    pattern = re.compile('\w')
    frequencies = {}
    token = []
    
    for c in text:
        if pattern.search(c):
            token.append(c)
        elif token:
            addWord(token, frequencies)
            token = []
    
    if token:
        addWord(token, frequencies)

    return frequencies



mamaMia = \
&amp;quot;Mamma mia, here I go again\
Mamma mia, here I go again\
My my, how can I resist you?\
Mamma mia, does it show again?\
My my, just how much I've missed you&amp;quot;

result = getWordFrequencies(mamaMia)

for word, freq in result.iteritems():
    print freq, &amp;quot;\t&amp;quot;, word
&lt;/pre&gt;</content>
    <author>
      <name>armandino.myopenid.com</name>
      <email>armandino@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/176-count-word-occurrences-in-a-string" rel="alternate"/>
  </entry>
</feed>

