From 6c30c95311f2c47dce1d3f1d62965f275a9f092c Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Mon, 25 May 2020 08:10:04 +0000 Subject: [PATCH 1/2] refactored to make it more generic for any range --- recipes/Python/576612_FizzBuzz/recipe-576612.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/recipes/Python/576612_FizzBuzz/recipe-576612.py b/recipes/Python/576612_FizzBuzz/recipe-576612.py index 33ca9d33b..bfb995ab2 100644 --- a/recipes/Python/576612_FizzBuzz/recipe-576612.py +++ b/recipes/Python/576612_FizzBuzz/recipe-576612.py @@ -1,5 +1,10 @@ -n=map(str, range(101)) -n[::3]=['Fizz']*34 -n[::5]=['Buzz']*21 -n[::15]=['FizzBuzz']*7 -print '\n'.join(n[1:]) +from __future__ import division +from math import ceil + +N = 101 +n = map(str, range(N)) +n[::3]=['Fizz']*int(ceil(N/3)) +n[::5]=['Buzz']*int(ceil(N/5)) +n[::15]=['FizzBuzz']*int(ceil(N/15)) + +print '\n'.join(str(n) for n in n[1:]) From 899c249f2a1510aafe26cacb15ffe7f82784eba3 Mon Sep 17 00:00:00 2001 From: Ashok Bakthavathsalam Date: Mon, 25 May 2020 08:13:38 +0000 Subject: [PATCH 2/2] refactored to eliminate third slicing operation --- recipes/Python/576612_FizzBuzz/recipe-576612.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/recipes/Python/576612_FizzBuzz/recipe-576612.py b/recipes/Python/576612_FizzBuzz/recipe-576612.py index bfb995ab2..ef0ba3fc3 100644 --- a/recipes/Python/576612_FizzBuzz/recipe-576612.py +++ b/recipes/Python/576612_FizzBuzz/recipe-576612.py @@ -2,9 +2,8 @@ from math import ceil N = 101 -n = map(str, range(N)) +n = range(N) n[::3]=['Fizz']*int(ceil(N/3)) -n[::5]=['Buzz']*int(ceil(N/5)) -n[::15]=['FizzBuzz']*int(ceil(N/15)) +n[::5]= [ t + "Buzz" if type(t) == str else "Buzz" for t in n[::5] ] print '\n'.join(str(n) for n in n[1:])