Suppose I have a string permutation function: >>> def permute(s): ... res = [] ... if len(s) == 1: ... res = [s] ... else: ... for i, c in enumerate(s): ... for perm in permute(s[:i] + s[i+1:]): ... res += [c + perm] ... return res ... >>> permute('abc') ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
This can trivially be reduced to one line: >>> def permute2(s): ... return [s] if len(s) == 1 else [c + perm for i, c in enumerate(s) for perm in permute2(s[:i]+s[i+1:])] ... >>> permute2('abc') ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
But suppose I want to make this a generator. Doing it with the first is easy: >>> def permute3(s): ... if len(s) == 1: ... yield s ... else: ... for i, c in enumerate(s): ... for perm in permute3(s[:i] + s[i+1:]): ... yield c + perm ... >>> permute3('abc') <generator object permute3 at 0x641c38> >>> list(permute3('abc')) ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
Here's my attempt at an analogous one-line generator: >>> def permute4(s): ... yield s if len(s) == 1 else (c + perm for i, c in enumerate(s) for perm in permute4(s[:i] + s[i+1:])) ...
This doesn't do the trick, though: >>> permute4('abc') <generator object permute4 at 0x641dc8> >>> list(permute4('abc')) [<generator object <genexpr> at 0x641e18>]
It's obvious why it doesn't do the trick (because I'm yielding a generator), but I can't figure out what the generator equivalent of the one-liner is. Any thoughts?
Edit: fixed a formatting issue, changed printing the generators to just use list().
The problem is a common exercise posed in code interviews to test your proficiency in writing simple Python code.
Problem: Print all numbers from 1-100 to the shell with three exceptions:
For each number divisible by three you print "Fizz"
For each number divisible by five you print "Buzz"
For each number divisible by three and five you print "FizzBuzz"
print('\n'.join('Fizz' * (i%3==0) + 'Buzz' * (i%5==0) or str(i) for i in range(1,101)))Love it!
This community has not been reviewed and might contain content inappropriate for certain viewers. View in the Reddit app to continue.