# Code review

So, I tried the FizzBuzz challenge and i’m fairly new to the interview prep section so i’d like some feedback on my code and how to refactor it to make it faster and simpler if possible.
Here’s my code:
``` def fizzbuzz(limit): arr = for i in range(1,limit+1): is_3,is_5 = True if i%3==0 else False, True if i%5==0 else False if is_3 and is_5: arr.append(“FizzBuzz”) elif is_5: arr.append(“Buzz”) elif is_3: arr.append(“Fizz”) else:arr.append(i) return arr```

``` ```

```print(fizzbuzz(16)) ```

Can you please format your code using “</>”

ex:

``````def fizzbuzz(limit):
blah = []
for i in range(1, limit+1):

...etc
``````

Also, did you see the various responses here? Some are quite clever.

Thank you for the resources mate

Been harping on this for years so you’ll forgive me pushing the point; all the same…

``````def fizzbuzz(limit):
arr = []
for i in range(limit):
is_3, is_5 = (i + 1) % 3 == 0, (i + 1) % 5 == 0
if is_3 and is_5:
arr.append("FizzBuzz")
elif is_5:
arr.append("Buzz")
elif is_3:
arr.append("Fizz")
else:arr.append(i + 1)
return arr
``````

Taking it even further…

``````
def fizzbuzz(limit):
arr = []
for i in range(limit):
k = i + 1
f, b = not (k % 3), not (k % 5)
y = "FizzBuzz" if f and b else "Fizz" if f else "Buzz" if b else k
arr.append(y)
return arr
``````

Bear in mind, refactoring is not necessarily about making it better, only seeing it differently. It’s through that lens we see our way to a more optimized solution. The problem is more interesting than the solution. Refactoring gives us perspective, and resolution, as it were.

Aside

The signature line and the one that follow are more a quirk of mine. I would rather do arithmetic inside the loop, than in the signature line. For that reason I went with the simplest and easiest to obtain object, a default range. We do the arithmetic inside the loop, on each iteration, and it is clear to the reader how we are utilizing a default range to suit our purposes; it gives us, `k`.

The assignment line for sure gives us two booleans. Like the example earlier, there are no literals in the code. They exist in the virtual state, and get assigned quite nicely without being stated. The NOT of `k % n` is `True` when k % n == 0, else it is False.

`y` is our carrier. It gets assigned the final result of the conditional expression we wind through. This is similar to a ternary expression in JavaScript and C, Java, and a host of other languages. Python doesn’t have ternary expressions, per se, but we do have this pattern, which holds true in terms of what it gives us: An expression, or, in other words a returnable/assignable value.

If your really feel experimental, tonight or tomorrow, instead of using the carrier variable, `y`, append that expression and see what you get.

``````arr.append('FizzBuzz' if f and b else 'Buzz' if b else 'Fizz' if f else k)
``````

The reader would rather you included the carrier variable so this doesn’t get glossed over. It does illustrate the value nature of a ternary, or conditional expression and its use in an expressive code model. The proviso is that if you choose to go expressive, then go all the way, else stick with imperative. It’s what most people can read.

Personally, I love expressive code, and continually explore it. Without a career to focus upon, I’m afraid it is mostly lost to folly in the end. These forums get a sampling of my musings and one hopes they open up a few minds along the way.

1 Like