065 with CodeBlock('fizzbuzz definition'):
066 fizzbuzz = lambda n: zip(range(0, n), ['fizzbuzz' if ((v % 3 == 0) and (v % 5 == 0)) else 'fizz' if (v % 3 == 0) else 'buzz' if (v % 5 == 0) else None for v in range(0, n)])
067 for i in range(0, 1):
068 pass
069
070 with CodeBlock('fizzbuzz for 0...16'):
071 print([(i, s) for (i, s) in fizzbuzz(17) if not (s is None)])
072
[(0, 'fizzbuzz'), (3, 'fizz'), (5, 'buzz'), (6, 'fizz'), (9, 'fizz'), (10, 'buzz'), (12, 'fizz'), (15, 'fizzbuzz')]
073 with CodeBlock('fizzbuzz binned for 0...500'):
074 data = [(i, s) for (i, s) in fizzbuzz(500) if not (s is None)]
075 strings = set([s for (i, s) in data])
076 indices = [[i for (i, s) in data if s == string] for string in strings]
077 binned = sorted(zip(strings, indices), key = lambda x: x[0])
078 for (s, i) in binned:
079 print('{} : {}{}'.format(s, i, chr(60) + 'br' + chr(62)))
080
buzz : [5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100, 110, 115, 125, 130, 140, 145, 155, 160, 170, 175, 185, 190, 200, 205, 215, 220, 230, 235, 245, 250, 260, 265, 275, 280, 290, 295, 305, 310, 320, 325, 335, 340, 350, 355, 365, 370, 380, 385, 395, 400, 410, 415, 425, 430, 440, 445, 455, 460, 470, 475, 485, 490]
fizz : [3, 6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78, 81, 84, 87, 93, 96, 99, 102, 108, 111, 114, 117, 123, 126, 129, 132, 138, 141, 144, 147, 153, 156, 159, 162, 168, 171, 174, 177, 183, 186, 189, 192, 198, 201, 204, 207, 213, 216, 219, 222, 228, 231, 234, 237, 243, 246, 249, 252, 258, 261, 264, 267, 273, 276, 279, 282, 288, 291, 294, 297, 303, 306, 309, 312, 318, 321, 324, 327, 333, 336, 339, 342, 348, 351, 354, 357, 363, 366, 369, 372, 378, 381, 384, 387, 393, 396, 399, 402, 408, 411, 414, 417, 423, 426, 429, 432, 438, 441, 444, 447, 453, 456, 459, 462, 468, 471, 474, 477, 483, 486, 489, 492, 498]
fizzbuzz : [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495]
081 with CodeBlock('inspect'):
082 import inspect
083 from typing import List
084 def call_by_name(value: int = 1) -> List[int]:
085 frame = inspect.currentframe()
086 namespace = inspect.currentframe().f_back.f_locals
087 self = namespace[inspect.getframeinfo(frame).function]
088 print('I am a {} with signature {}'.format(self, inspect.signature(self)))
089 return [0] * value
090 locals()['call_by_name']()
091
I am a with signature (value: int = 1) -> List[int]
092 with CodeBlock('overloading'):
093 from functools import singledispatch
094 from math import factorial as ifactorial
095 @singledispatch
096 def factorial(arg):
097 return 'undefined'
098 @factorial.register(None.__class__) # using only None doesn't work
099 def _(arg):
100 return None
101 @factorial.register(int)
102 def _(arg):
103 return ifactorial(arg)
104 @factorial.register(float)
105 def _(arg):
106 return 'scipy.special.zeta({})'.format(arg)
107 values = [(i, factorial(i)) for i in [5, 5.3, None, "string"]]
108 for (v, f) in values:
109 print('{}! = {}'.format(v, f))
110
5! = 120
5.3! = scipy.special.zeta(5.3)
None! = None
string! = undefined
111 with CodeBlock('the end'):
112 pass