From 74d5e6d5f2510bb8cf9257628b0f37f997c54b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crichrich51=E2=80=9D?= <346485200@qq.com> Date: Tue, 14 Jan 2020 09:20:24 +0800 Subject: [PATCH 1/4] first commit --- fizz_buzz.py | 11 +++++++++++ helloworld.py | 2 -- helloworld_test.py | 5 ----- test_module.py | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 fizz_buzz.py delete mode 100644 helloworld.py delete mode 100644 helloworld_test.py create mode 100644 test_module.py diff --git a/fizz_buzz.py b/fizz_buzz.py new file mode 100644 index 0000000..5a1af9a --- /dev/null +++ b/fizz_buzz.py @@ -0,0 +1,11 @@ +def get_fizz_buzz(n): + ext = 10 * (n % 5) + n % 3 + + if ext != 0: + if ext % 10 == 0: + return "Fizz" + elif ext < 10: + return "Buzz" + return n + else: + return "FizzBuzz" diff --git a/helloworld.py b/helloworld.py deleted file mode 100644 index 449b948..0000000 --- a/helloworld.py +++ /dev/null @@ -1,2 +0,0 @@ -def get_greetings(): - return 'Hello World!' diff --git a/helloworld_test.py b/helloworld_test.py deleted file mode 100644 index 4c27dfe..0000000 --- a/helloworld_test.py +++ /dev/null @@ -1,5 +0,0 @@ -from helloworld import get_greetings - - -def test_get_helloworld(): - assert get_greetings() == 'Hello World!' diff --git a/test_module.py b/test_module.py new file mode 100644 index 0000000..ae2463b --- /dev/null +++ b/test_module.py @@ -0,0 +1,15 @@ +import unittest +import fizz_buzz + +num = [1, 3, 5, 15] +res = [1, "Fizz", "Buzz", "FizzBuzz"] + + +class FizzBuzzTest(unittest.TestCase): + def test_case(self): + for i in range(len(num)): + self.assertEqual(res[i], fizz_buzz.get_fizz_buzz(num[i])) + + +if __name__ == '__main__': + unittest.main() From b1fb14ae50efd8caa650463410f91bf133bfb7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crichrich51=E2=80=9D?= <346485200@qq.com> Date: Tue, 14 Jan 2020 09:49:25 +0800 Subject: [PATCH 2/4] optimized --- fizz_buzz.py | 2 +- test_module.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/fizz_buzz.py b/fizz_buzz.py index 5a1af9a..cfc1e47 100644 --- a/fizz_buzz.py +++ b/fizz_buzz.py @@ -4,7 +4,7 @@ def get_fizz_buzz(n): if ext != 0: if ext % 10 == 0: return "Fizz" - elif ext < 10: + elif ext < 3: return "Buzz" return n else: diff --git a/test_module.py b/test_module.py index ae2463b..386356d 100644 --- a/test_module.py +++ b/test_module.py @@ -1,14 +1,17 @@ import unittest import fizz_buzz -num = [1, 3, 5, 15] -res = [1, "Fizz", "Buzz", "FizzBuzz"] +fb = dict() +fb[1] = 1 +fb[3] = "Fizz" +fb[5] = "Buzz" +fb[15] = "FizzBuzz" -class FizzBuzzTest(unittest.TestCase): +class TestFizzBuzz(unittest.TestCase): def test_case(self): - for i in range(len(num)): - self.assertEqual(res[i], fizz_buzz.get_fizz_buzz(num[i])) + for k, v in fb.items(): + self.assertEqual(v, fizz_buzz.get_fizz_buzz(k)) if __name__ == '__main__': From a3a2c9b0a1ce031f91df2e45369ebf8b1d7c70ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crichrich51=E2=80=9D?= <346485200@qq.com> Date: Tue, 14 Jan 2020 15:54:02 +0800 Subject: [PATCH 3/4] add hello world --- helloworld.py | 2 ++ helloworld_test.py | 5 +++++ 2 files changed, 7 insertions(+) create mode 100755 helloworld.py create mode 100755 helloworld_test.py diff --git a/helloworld.py b/helloworld.py new file mode 100755 index 0000000..449b948 --- /dev/null +++ b/helloworld.py @@ -0,0 +1,2 @@ +def get_greetings(): + return 'Hello World!' diff --git a/helloworld_test.py b/helloworld_test.py new file mode 100755 index 0000000..4c27dfe --- /dev/null +++ b/helloworld_test.py @@ -0,0 +1,5 @@ +from helloworld import get_greetings + + +def test_get_helloworld(): + assert get_greetings() == 'Hello World!' From c4b158e3c2f172afdda9b733ec96ed1103c84fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crichrich51=E2=80=9D?= <346485200@qq.com> Date: Wed, 15 Jan 2020 08:59:07 +0800 Subject: [PATCH 4/4] optimized --- fizz_buzz.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/fizz_buzz.py b/fizz_buzz.py index cfc1e47..a071f74 100644 --- a/fizz_buzz.py +++ b/fizz_buzz.py @@ -1,11 +1,8 @@ def get_fizz_buzz(n): - ext = 10 * (n % 5) + n % 3 - - if ext != 0: - if ext % 10 == 0: - return "Fizz" - elif ext < 3: - return "Buzz" - return n - else: + if n % 15 == 0: return "FizzBuzz" + if n % 3 == 0: + return "Fizz" + if n % 5 == 0: + return "Buzz" + return n