码迷,mamicode.com
首页 > 其他好文 > 详细

FizzBuzzWhizz

时间:2019-11-03 13:03:48      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:ide   into   from   seq   wan   rom   mes   str   div   

Problem

You are a physical education teacher, in five minutes a class from the class, you decide to get into a game. There are 100 students in our class at. The rules of the game are:

1 your first name three different special number, must be a number, such as 3, 5, 7.
2 let all the students into a team, and then according to the sequence number.

3 of the studentsReport, if the number is the first special numbers (3) times, so can‘t say the number, say Fizz; if the reported figures are second special numbers (5) times, so to say Buzz; if the reported figures are third special numbers (7) times, so to say Whizz.

4 of the students reported the number, if the reported figures also is a multiple of two special number, also want special treatment, such as the first special numbers and second special integer multiples, so can not say this, but to say FizzBuzz, and so on. If it is a multiple of three special numbers, so to say FizzBuzzWhizz.

5 of the students reported the number, if the reported figures include the first special number, so can not say this, but to say the word, such as in the case of the first special number is 3, then to 13 of the students said that Fizz should be. If the number is included in the special number first, then ignore the rules 3 and 4, for example, to report 35 of the students reported that Fizz, not BuzzWhizz.

Rule Base Solution

def atom_rule(match, to):
    return lambda N: to(N) if match(N) else ''


def anyof(*rules):
    def f(N):
        rule = filter(lambda r: r(N), rules)
        return rule[0](N) if rule else ''
    return f


def allof(*rules):
    return lambda N: ''.join(map(lambda r: r(N), rules))


def r_contains_3(N):
    m, r = divmod(N, 10)
    return 'Fizz' if m == 3 or r == 3 else ''


r1_3 = atom_rule(lambda x: x % 3 == 0, lambda _: 'Fizz')
r1_5 = atom_rule(lambda x: x % 5 == 0, lambda _: 'Buzz')
r1_7 = atom_rule(lambda x: x % 7 == 0, lambda _: 'Whizz')

r_contains_3 = atom_rule(lambda x: '3' in str(x), lambda _: 'Fizz')
r1 = anyof(r1_3, r1_5, r1_7)
r2 = allof(r1_3, r1_5, r1_7)
d_r = atom_rule(lambda _: True, lambda N: str(N))
spec = anyof(r_contains_3, r2, r1, d_r)

for i in range(100):
    print spec(i)
   

FizzBuzzWhizz

标签:ide   into   from   seq   wan   rom   mes   str   div   

原文地址:https://www.cnblogs.com/zhaodonglin/p/11785492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!