<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users472</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/472" rel="self"/>
  <title>orip</title>
  <updated>Fri Jan 18 18:55:07 +0000 2008</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1633</id>
    <published>2008-01-18T18:55:07+00:00</published>
    <title>[C#] On Code to Simplify</title>
    <content type="html">&lt;p&gt;I liked Jonathan Starr's approach, here's my variation.&lt;/p&gt;

&lt;pre&gt;## [cs]
        private int TableIndex(char c)
        {
            if (Char.IsLetter(c))
            {
                return (int)Char.ToUpper(c) - (int)'A';
            }
            else if (Char.IsNumber(c))
            {
                return 26 + int.Parse(c.ToString());
            }
            throw new ArgumentException(&amp;quot;Invalid character&amp;quot;);
        }

        private string CodiceControllo2(string codice_fiscale)
        {
            int[] table_even = new int[] {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23,1,0,5,7,9,13,15,17,19,21};
            int[] table_odd  = new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9};

            int somma = 0;

            for (int i = 0; i &amp;lt; 15; i++)
            {
                char c = codice_fiscale[i];

                if (i % 2 == 0)
                {
                    somma += table_even[TableIndex(c)];
                }
                else
                {
                    somma += table_odd[TableIndex(c)];
                }
            }

            int resto = somma % 26;
            return char.ConvertFromUtf32(resto + 65);
        }&lt;/pre&gt;</content>
    <author>
      <name>orip</name>
      <email>oripel@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/188-code-to-simplify/refactors/1633" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1632</id>
    <published>2008-01-18T18:51:49+00:00</published>
    <title>[C#] On Code to Simplify</title>
    <content type="html">&lt;p&gt;nibbles&amp;amp;bits: I also think a dictionary captures the key-&amp;gt;value mapping better than a switch or an array, but the verbose syntax for initializing it takes some readability away.&lt;/p&gt;

&lt;p&gt;C# 3.0 syntax is nice, though:&lt;/p&gt;

&lt;pre&gt;## using dictionary initializers [cs]
Dictionary&amp;lt;char,int&amp;gt; dict = new Dictionary&amp;lt;char,int&amp;gt; {
  {'A', 1},
  {'B', 0},
  {'C', 5},
  //...
  {'8', 19},
  {'9', 21}
};&lt;/pre&gt;</content>
    <author>
      <name>orip</name>
      <email>oripel@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/188-code-to-simplify/refactors/1632" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1614</id>
    <published>2008-01-18T01:12:55+00:00</published>
    <title>[Python] On Custom sort  a list of words</title>
    <content type="html">&lt;p&gt;armandino - I liked how you subtracted the two indexes to get the cmp, cool!&lt;/p&gt;

&lt;p&gt;How about this version? (append to your original code)&lt;/p&gt;

&lt;pre&gt;## alphabet_cmp [python]
from itertools import izip
def alphabet_cmp(a, b):
    # Make sure no letters are missing. For performance in final code, comment out or remove with 'python -O'
    assert set(a).issubset( set(alphabet) ) and set(b).issubset( set(alphabet) )

    # find the first non-equal character
    for c_a, c_b in izip(a,b):
        if c_a != c_b:
            return alphabet.index(c_a) - alphabet.index(c_b)
    
    # one word is a prefix of another, sort by length
    return len(a) - len(b)

print sorted(inputWords, alphabet_cmp)&lt;/pre&gt;</content>
    <author>
      <name>orip</name>
      <email>oripel@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/179-custom-sort-a-list-of-words/refactors/1614" rel="alternate"/>
  </entry>
</feed>
