Python completes the FizzBuzzWhizz question of a sample interview question

  • 2020-04-02 13:39:04
  • OfStack

Interview questions

1. You first name three different special Numbers that must be single digits, such as 3, 5, and 7.
2. Have all the students line up and report in order.
3. When the student reports the number, if the number is a multiple of the first special number (3), the number cannot be said, but Fizz; Say Buzz if the number reported is a multiple of the second special number (5); If the number reported is a multiple of the third special number (7), say Whizz.
4. When students report Numbers, if the number they report is multiple of two special Numbers at the same time, they also need special treatment, such as multiple of the first special number and the second special number, they can't say the number, but FizzBuzz, and so on. Say FizzBuzzWhizz if it's a multiple of three special Numbers.
5. When the student reports the number, if the number contains the first special number, then the number cannot be said, but the corresponding word. For example, the first special number in this example is 3, then the student who wants to report 13 should say Fizz. If the number contains the first special number, then rule 3 and rule 4 are ignored. For example, students who want to sign up for 35 should sign up for Fizz, not BuzzWhizz.

Now, we need you to complete a program to simulate the game, which first accepts three special Numbers and then outputs the Numbers or words that 100 students should report.


def check(a, dic, d):
    answer = ''
    if str(a) in str(d):
        return dic[a]
    for x in dic:
        if not (d % x):
            answer = answer + dic[x]
    if not answer:
        return d
    return answer
if __name__ == '__main__':
    a = int(raw_input('input u a: '))
    b = int(raw_input('input u b: '))
    c = int(raw_input('input u c: '))
    dic = {a: 'Fizz', b: 'Buzz', c: 'Whizz'}
    for x in xrange(1, 101):
        print check(a, dic, x)


['Fizz'[(str(3)not in str(i))*4:]or 'Fizz'[i % 3 * 5 : ] + 'Buzz'[i % 5 * 5 : ] + 'Whizz'[i % 7 * 5 : ] or i for i in range(1,101)]


Related articles: