how autistic is this fizzbuzz /g/?
in python:def fizzBuzz(n):
if n%3 == 0 and n%5 == 0:
return "FizzBuzz"
if n%3 == 0:
return "Fizz"
if n%5 == 0:
return "Buzz"
return n
for i in range(1,101):
print(fizzBuzz(i))
>>58362221
now try https://github.com/h5bp/Front-end-Developer-Interview-Questions
>>58362221
>python
Absolutely normal.
Now wrie it in INTERCAL.
>>58362271
>front end
It's fucking web-development
>>58362221
Only complaints would be that;
- the function returns different types depending on input
- the function is not needed
- no spaces between modulo operators
threes = {*range(3,101,3)}
fives = {*range(5,101,5)}
all_ = {*range(1,101)}
fizzbuzz = threes & fives
fizz = threes - fizzbuzz
buzz = fives - fizzbuzz
rest = all_ - threes - fives
concat = [[i, str(i)] for i in rest] + [[i, "fizz"] for i in fizz] + [[i, "buzz"] for i in buzz] + [[i, "fizzbuzz"] for i in fizzbuzz]
concat.sort(key=lambda x: x[0])
print("\n".join((i[1] for i in concat)))
There, No if statements of modulus operations.
>>58363088
>Also you need an else so that it doesn't always print the number regardless.
you don't need an else because it will only print the number if it doesnt return on any of the conditionals
>>58362221
If something is divisible by both 3 and 5 then by definition it is divisible by 15.def fizzBuzz(n):
if n % 15 == 0:
return "FizzBuzz"
if n % 3 == 0:
return "Fizz"
if n % 5 == 0:
return "Buzz"
return n
for i in range(1, 101):
print(fizzBuzz(i))
>>58362970
Not only would I reject your application, I'd have security sodomize you with a nightstick for being such a fucking retard.
>>58363123
I missed that, thank you.
hmmm
>>58363138
You can also save a little bit of computation with larger numbers by using bools.def fizzBuzz(n):
string = ''
isDivisible = False
if n % 3 == 0:
string = string + 'Fizz'
isDivisible = True
if n % 5 == 0:
string = string + 'Buzz'
isDivisible = True
if not isDivisible:
string = n
return string
for i in range(1, 101):
print(fizzBuzz(i))
>>58363335Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import timeit
>>> first = '''def fizzBuzz(n):
if n % 15 == 0:
return "FizzBuzz"
if n%3 == 0:
return "Fizz"
if n%5 == 0:
return "Buzz"
else:
return n
for i in range(1, 101):
fizzBuzz(i)'''
>>> second = '''def fizzBuzz(n):
string = ''
isDivisible = False
if n % 3 == 0:
string = string + 'Fizz'
isDivisible = True
if n % 5 == 0:
string = string + 'Buzz'
isDivisible = True
if not isDivisible:
string = n
return string
for i in range(1, 101):
fizzBuzz(i)'''
>>> timeit.timeit(first)
23.958802697751487
>>> timeit.timeit(second)
29.626801612583286
>>>
String concatenation is more expensive than an extra elseif