File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+
3+
4+
5+ FizzBuzz Mini
6+ Given an integer, return a string based on the following rules:
7+
8+ If the number is divisible by 3, return "Fizz".
9+ If the number is divisible by 5, return "Buzz".
10+ If the number is divisible by both 3 and 5, return "FizzBuzz".
11+ Otherwise, return the given number as a string.
12+ """
13+
14+
15+ import unittest
16+
17+
18+ class FizzBuzzMiniTest (unittest .TestCase ):
19+
20+ def test1 (self ):
21+ self .assertEqual (fizz_buzz_mini (3 ), "Fizz" )
22+
23+ def test2 (self ):
24+ self .assertEqual (fizz_buzz_mini (4 ), "4" )
25+
26+ def test3 (self ):
27+ self .assertEqual (fizz_buzz_mini (35 ), "Buzz" )
28+
29+ def test4 (self ):
30+ self .assertEqual (fizz_buzz_mini (75 ), "FizzBuzz" )
31+
32+ def test5 (self ):
33+ self .assertEqual (fizz_buzz_mini (98 ), "98" )
34+
35+
36+
37+
38+
39+ def fizz_buzz_mini (n ):
40+
41+ if n % 3 == 0 and n % 5 == 0 :
42+ return "FizzBuzz"
43+ elif n % 3 == 0 :
44+ return "Fizz"
45+ elif n % 5 == 0 :
46+ return "Buzz"
47+ else :
48+ return str (n )
49+
50+ def fizz_buzz_mini_oneliner (n ):
51+
52+ return "FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n % 5 == 0 else str (n )
53+
54+ def fizz_buzz_mini_dict_approach (n ):
55+
56+ return "" .join (word for divisor , word in {3 :"Fizz" , 5 :"Buzz" }.items () if n % divisor == 0 ) or str (n )
57+
58+ if __name__ == "__main__" :
59+ unittest .main ()
You can’t perform that action at this time.
0 commit comments