Created
August 8, 2015 21:48
-
-
Save paultopia/d360116128c787e22ce8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# with list comprehension | |
print '\n'.join(['Fizzbuzz' if x % 15 == 0 else 'Fizz' if x % 3 == 0 else 'Buzz' if x % 5 == 0 else str(x) for x in range(input('Start: '), input('End: ') + 1, input('Step: '))]) | |
# with lambdas and map (two versions) | |
print '\n'.join(map(lambda x: 'Fizzbuzz' if x % 15 == 0 else (lambda x: 'Fizz' if x % 3 == 0 else (lambda x: 'Buzz' if x % 5 == 0 else str(x))(x))(x), range(input('Start: '), input('End: ') + 1, input('Step: ')))) | |
print '\n'.join(map(lambda x: 'Fizzbuzz' if x % 15 == 0 else 'Fizz' if x % 3 == 0 else 'Buzz' if x % 5 == 0 else str(x), range(input('Start: '), input('End: ') + 1, input('Step: ')))) | |
# these versions sometimes work, and sometimes throw an error about being unable to convert string to int, even before user is asked for input. | |
# print '\n'.join(map(lambda x: 'Fizzbuzz' if x % 15 == 0 else (lambda x: 'Fizz' if x % 3 == 0 else (lambda x: 'Buzz' if x % 5 == 0 else str(x))(x))(x), range(int(raw_input('Start: ')), int(raw_input('End: ')) + 1, int(raw_input('Step: '))))) | |
# print '\n'.join(['Fizzbuzz' if x % 15 == 0 else 'Fizz' if x % 3 == 0 else 'Buzz' if x % 5 == 0 else str(x) for x in range(int(raw_input('Start: ')), int(raw_input('End: ')) + 1, int(raw_input('Step: ')))]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With help from the good people at SO: http://stackoverflow.com/questions/31897624/nested-if-else-lambdas-in-python-returning-function-object-rather-than-value-pa