Last active
August 29, 2015 14:00
-
-
Save shenfeng/bd83f16e60ca16a8cbc3 to your computer and use it in GitHub Desktop.
ThoughtWorks的代码题 https://www.jinshuju.net/f/EGQL3D
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
def transform(specials, mapping, n): | |
""" | |
https://www.jinshuju.net/f/EGQL3D | |
""" | |
# rule 5 | |
if str(specials[0]) in str(n): | |
return mapping[specials[0]] | |
return ''.join(mapping[i] for i in specials if n % i == 0) or str(n) | |
def test(): | |
right_answers = """ | |
1 | |
2 | |
Fizz | |
4 | |
Buzz | |
Fizz | |
Whizz | |
8 | |
Fizz | |
Buzz | |
11 | |
Fizz | |
Fizz | |
Whizz | |
FizzBuzz | |
16 | |
17 | |
Fizz | |
19 | |
Buzz | |
""" | |
specials = [3, 5, 7] | |
mapping = {3: 'Fizz', 5: 'Buzz', 7: 'Whizz'} | |
right_answers = [line.strip() for line in right_answers.split('\n') if line.strip()] | |
for i in range(1, len(right_answers)): | |
assert transform(specials, mapping, i) == right_answers[i - 1] | |
print 'test pass' | |
def main(): | |
specials = [] | |
while len(specials) != 3: | |
raw = raw_input('input three less than 10 numbers: ') | |
specials = [int(n) for n in raw.split(' ') if n.isdigit() and int(n) < 10] | |
mapping = dict(zip(specials, ['Fizz', 'Buzz', 'Whizz'])) | |
for i in range(1, 100): | |
print transform(specials, mapping, i) | |
if __name__ == "__main__": | |
test() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment