[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

how autistic is this fizzbuzz /g/? in python: def fizzBuzz(n):

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 13
Thread images: 1

File: 1463745051736.png (13KB, 266x220px) Image search: [Google]
1463745051736.png
13KB, 266x220px
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))
>>
>>58363335
Python 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
Thread posts: 13
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.