Skip to content

Instantly share code, notes, and snippets.

@aandr
Created March 9, 2010 10:03
Show Gist options
  • Save aandr/326452 to your computer and use it in GitHub Desktop.
Save aandr/326452 to your computer and use it in GitHub Desktop.
# Начин на употреба:
#
# За да бенчмаркнете всички решения
# $ python3 fbbench.py fizzbuzz_*.py
#
# За да бенчмаркнете едно решение:
# $ python3 fbbench.py fizzbuzz_string.py
#
# Забележка: fizzbuzzrecursive.py не може да се бенчмарква,
# защото работи при n < 999, а fbbenchmark.py ползва n = 1000000
import sys
from timeit import Timer
for name in sys.argv[1:]:
print("Time for %s" % name, end = " ")
time = Timer('mod.freebeer(1000000)', 'mod = __import__("%s")' % name[:-3]).timeit(number = 3)
print(time)
def freebeer(n):
"""Form a string of numbers from 1 to n, sipping beer in the process."""
items = []
for i in range(1, n + 1):
if i % 15 == 0:
items.append('freebeer')
elif i % 3 == 0:
items.append('free')
elif i % 5 == 0:
items.append('beer')
else:
items.append(str(i))
return ' '.join(items)
def freebeer(end):
return " ".join((map(lambda x : str(x) * (not not (x % 3 and x % 5)) + "free" * (not x % 3) + "beer" * (not x % 5), range(1, end + 1))))
def freebeer(n):
def value(x):
if x % 15 == 0: return 'freebeer'
elif x % 3 == 0: return 'free'
elif x % 5 == 0: return 'beer'
else: return str(x)
return " ".join([value(x) for x in range(1, n + 1)])
def freebeer(n):
return " ".join((value(x) for x in range(1, n + 1)))
def value(x):
if x % 15 == 0: return 'freebeer'
elif x % 3 == 0: return 'free'
elif x % 5 == 0: return 'beer'
else: return str(x)
def compose(*args):
def composition(n):
accum = n
for func in args:
accum = func(accum)
return accum
return composition
def carbonize(modulus, message):
def beer(n):
if type(n) == str: return n
return message if n % modulus == 0 else n
return beer
def freebeer(n):
f = compose(carbonize(15, 'freebeer'),
carbonize(5, 'beer'),
carbonize(3, 'free'), str)
return ' '.join(map(f, range(1, n + 1)))
def freebeer(n):
if n == 1: return '1'
if n % 15 == 0: return freebeer(n-1) + ' freebeer'
if n % 3 == 0: return freebeer(n-1) + ' free'
if n % 5 == 0: return freebeer(n-1) + ' beer'
else: return freebeer(n-1) + ' ' + str(n)
def freebeer(n):
res = ""
for x in range(1, n+1):
if x % 15 == 0:
res += 'freebeer '
elif x % 3 == 0:
res += 'free '
elif x % 5 == 0:
res += 'beer '
else:
res += str(x) + ' '
return res.rstrip()
def freebeer(n):
return " ".join(["freebeer"[i*i%3*4:8--i**4%5]or str(i) for i in range(0,n+1)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment