Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I prefer the Gordian Knot approach:</p> <pre><code>print '\n'.join(['Jack', 'Kack', 'Lack', 'Mack', 'Nack', 'Ouack', 'Pack', 'Quack']) </code></pre> <p>Strictly speaking, that's just a (somewhat radical) modification.</p> <p>Stuff that, Perl guy.</p> <p>All kidding aside, you could do it as the others have mentioned by special casing the "O" and "Q". A (very) slightly more general approach is to think of the extra vowels not as a special case but rather as the only "visible" members of a general case:</p> <pre><code>suffixes = ['ack'] * 8 prefixes = 'JKLMNOPQ' infixes = ',,,,,u,,u'.split(',') </code></pre> <p>The split of the infixes string returns empty strings surrounding the consecutive commas, so the 'u's are the only non-empty elements and they fall in the right places for the final result.</p> <p>This makes three iterables (think lists, but strings count too) where all of the parts are lined up in the correct slots. This is enough of a shift that you can trade the old loop for a zip, which is probably not the intent of the exercise but is still a pythonic technique. Now you just need to run a zipper down the three, which is what the zip function is for, of course.</p> <p>The only wrinkle is that zip gives you a out a list of string pairs and we want whole strings, which is what a ''.join is for. They are processed by a list comprehension, which you will definitely want to learn if you don't already know:</p> <pre><code>result = [''.join(pair) for pair in zip(infixes, suffixes)] result = [''.join(pair) for pair in zip(prefixes, result)] </code></pre> <p>All that is left is to print:</p> <pre><code>print '\n'.join(result) </code></pre> <p>To paraphrase a great man, my disclaimer: In theory, I can prove the correctness of this code, but I have not run it, so there may be bugs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload