Skip to content

Instantly share code, notes, and snippets.

@too
Last active August 6, 2022 14:56
Show Gist options
  • Save too/0bbf52e966af85cc0b45 to your computer and use it in GitHub Desktop.
Save too/0bbf52e966af85cc0b45 to your computer and use it in GitHub Desktop.
FizzBuzzWhizz. 解决一道 ThoughtWork 的公开招聘题. 尝试应用了最近学习的敏捷开发+面向对象的概念。

具体题目:

你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是:

  1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。
  2. 让所有学生拍成一队,然后按顺序报数。
  3. 学生报数时,如果所报数字是第一个特殊数(3)的倍数,那么不能说该数字,而要说Fizz;如果所报数字是第二个特殊数(5)的倍数,那么要说Buzz;如果所报数字是第三个特殊数(7)的倍数,那么要说Whizz。
  4. 学生报数时,如果所报数字同时是两个特殊数的倍数情况下,也要特殊处理,比如第一个特殊数和第二个特殊数的倍数,那么不能说该数字,而是要说FizzBuzz, 以此类推。如果同时是三个特殊数的倍数,那么要说FizzBuzzWhizz。
  5. 学生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。

现在,我们需要你完成一个程序来模拟这个游戏,它首先接受3个特殊数,然后输出100名学生应该报数的数或单词。比如,

输入
3,5,7
输出(片段)

1
2
Fizz
4
Buzz
Fizz
Whizz
8
Fizz
Buzz
11
Fizz
Fizz
Whizz
FizzBuzz
16
17
Fizz
19
Buzz 
…
一直到100
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
For ThoughtWorks test. Run and test by Python 2.7
"""
class FizzGame(object):
three_words = ('Fizz', 'Buzz', 'Whizz')
def __init__(self, nums):
self.nums = nums
self.trans_dict = {}
self.map_words()
def passed_rule1(self):
if len(set(self.nums)) == 3:
right_nums = range(1, 10)
return all(map(lambda x:x in right_nums, self.nums))
else:
return False
def map_words(self):
if self.passed_rule1():
self.make_dict()
else:
raise Exception('Wrong numbers')
def make_dict(self):
self.trans_dict = dict(zip(self.nums, self.three_words))
def match_rule5(self, num):
return str(self.nums[0]) in str(num)
def get_match_factors(self, num):
match_factors = []
for f in self.nums:
if num % f == 0:
match_factors.append(f)
return match_factors
def get_transform(self, num):
if self.match_rule5(num):
return self.trans_dict[self.nums[0]]
else:
match_factors = self.get_match_factors(num)
if match_factors:
return ''.join([self.trans_dict[x] for x in match_factors])
else:
return num
def get_result(self):
return [self.get_transform(x) for x in range(1, 101)]
if __name__ == '__main__':
nums_str = raw_input('Please input 3 unique numbers, separate by ",", '
'eg. "3, 5, 7". (0< number < 10): ')
nums = map(int, nums_str.split(','))
game = FizzGame(nums)
for r in game.get_result():
print r
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import unittest
from fizzgame import FizzGame
class TestFizzGame(unittest.TestCase):
def test_get_transform(self):
game = FizzGame([2, 5, 7])
self.assertEqual(game.get_transform(2), 'Fizz')
self.assertEqual(game.get_transform(5), 'Buzz')
self.assertEqual(game.get_transform(7), 'Whizz')
self.assertEqual(game.get_transform(10), 'FizzBuzz')
self.assertEqual(game.get_transform(20), 'Fizz')
self.assertEqual(game.get_transform(28), 'Fizz')
self.assertEqual(game.get_transform(35), 'BuzzWhizz')
self.assertEqual(game.get_transform(70), 'FizzBuzzWhizz')
game = FizzGame([2, 3, 7])
self.assertEqual(game.get_transform(42), 'Fizz')
def test_full_result(self):
nums = (3, 5, 7)
game = FizzGame(nums)
result = game.get_result()
sample = [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 'Whizz', 8, 'Fizz', 'Buzz', 11, 'Fizz', 'Fizz',
'Whizz', 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz', 'FizzWhizz', 22, 'Fizz', 'Fizz',
'Buzz', 26, 'Fizz', 'Whizz', 29, 'Fizz', 'Fizz', 'Fizz', 'Fizz', 'Fizz', 'Fizz',
'Fizz', 'Fizz', 'Fizz', 'Fizz', 'Buzz', 41, 'FizzWhizz', 'Fizz', 44, 'FizzBuzz',
46, 47, 'Fizz', 'Whizz', 'Buzz', 'Fizz', 52, 'Fizz', 'Fizz', 'Buzz', 'Whizz',
'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz',
'BuzzWhizz', 71, 'Fizz', 'Fizz', 74, 'FizzBuzz', 76, 'Whizz', 'Fizz', 79, 'Buzz',
'Fizz', 82, 'Fizz', 'FizzWhizz', 'Buzz', 86, 'Fizz', 88, 89, 'FizzBuzz', 'Whizz',
92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 'Whizz', 'Fizz', 'Buzz']
self.assertEqual(result, sample)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment