Skip to content

Instantly share code, notes, and snippets.

@sagivmalihi
Created May 31, 2013 23:51
Show Gist options
  • Save sagivmalihi/5688701 to your computer and use it in GitHub Desktop.
Save sagivmalihi/5688701 to your computer and use it in GitHub Desktop.
Can you spot the bug?
import itertools
def expand_letters(word, times):
expanded_letters = []
for letter in word:
expanded_letters.append( (letter for _ in xrange(times)) )
return ''.join(itertools.chain.from_iterable(expanded_letters))
print expand_letters("Hello", 2)
@btv
Copy link

btv commented Jun 1, 2013

So many things that don't make sense with this... what are you trying to accomplish? Maybe I can help.

Since you're aware of the itertools module, why didn't you use repeat instead of:
(letter for _ in xrange(times))

http://docs.python.org/2/library/itertools.html#itertools.repeat

@sagivmalihi
Copy link
Author

@btv - This is just an abridged version of a bug I had (and fixed actually, now I'm just ranting...)
The output here, instead of the expected 'HHeelllloo' is actually 'oooooooooo' - Guess why ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment