title: Algorithm implementation in python
body: <p>I have certain algorithm need to implement. Basically the rules are: </p>
  
  <ol>
  <li>The first whitespace-separated token on a line will be the word being defined. </li>
  <li>The later tokens will be the definition. If the definition is “.”, that word is a primitive, i.e., a word with no definition.</li>
  <li>The output is a single comma-separated line of text containing each word in the dictionary exactly once. Each word is only to be printed after all of the words in its definition. Note that for certain input sets, there may be multiple valid outputs.</li>
  </ol>
  
  <p>For example the input:</p>
  
  <pre><code>Civic        Honda Car
  Honda        Manufacturer
  VW           Manufacturer
  Manufacturer .
  Car          .
  Beetle       VW Car
  </code></pre>
  
  <p>Some Possible Outputs:</p>
  
  <pre><code>Car, Manufactor, Honda, VW, Beetle, Civic
  Manufacturer, VW, Car, Beetle, Honda, Civic
  Manufacturer, Honda, VW, Car, Beetle, Civic
  </code></pre>
  
  <p>My implementation:</p>
  
  <pre><code>def defIt(pre, cur):
      # If previous and current strings are the same, no action take
      if pre == cur:
          return pre
  
      # Split two strings to list
      l_pre = pre.split()
      l_cur = cur.split()
  
      # If previous string length is shorter than the current string     length, then swap two lists
      if len(l_pre) &lt; len(l_cur):
          l_pre, l_cur = l_cur, l_pre
  
      found = False
  
  
      for j, w_cur in enumerate(l_cur):
          for i, w_pre in enumerate(l_pre):
              if w_pre == w_cur:
                  found = True
                  return ' '.join(l_cur[j:] + l_cur[:j] + l_pre[:i] + l_pre[(i + 1):])
  
  
      if not found:
          return ' '.join(l_cur[1:] + [l_cur[0]] + l_pre)
  </code></pre>
  
  <p>Just cannot get it right. What do I missing? Thanks a lot.</p>