-
-
Save mydiemho/9c7a7c6c139040fa8b3c15071e153568 to your computer and use it in GitHub Desktop.
Python FizzBuzz with lookup sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import pstats | |
import StringIO | |
pr = cProfile.Profile() | |
pr.enable() | |
threes = {num for num in range(3, 100, 3)} | |
fives = {num for num in range(5, 100, 5)} | |
not_in = {num for num in range(1, 100) if num not in threes and num not in fives} | |
both = threes.intersection(fives) | |
iNot = set() | |
iFizz = set() | |
iBuzz = set() | |
iFizzBuzz = set() | |
span = range(1, 101) | |
def calc(): | |
for num in span: | |
if num in not_in: | |
iNot.add(num) | |
elif num in both: | |
iFizzBuzz.add(num) | |
elif num in threes: | |
iFizz.add(num) | |
else: | |
iBuzz.add(num) | |
for i in xrange(0, 1000000): | |
iNot.clear() | |
iFizz.clear() | |
iBuzz.clear() | |
iFizzBuzz.clear() | |
calc() | |
pr.disable() | |
print "3's: {}".format(len(iFizz)) | |
print "5's: {}".format(len(iBuzz)) | |
print "both: {}".format(len(iFizzBuzz)) | |
print "neither: {}".format(len(iNot)) | |
s = StringIO.StringIO() | |
sortby = 'cumulative' | |
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) | |
ps.print_stats() | |
print s.getvalue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment