4 ms·
That translates fairly well into Python, with its set type and list/set/dict comprehensions. def setrange(first, next, last): return set(range(firs
by Rangi42 9y ago
That translates fairly well into Python, with its set type and list/set/dict comprehensions.
def setrange(first, next, last):
return set(range(first, last+1, next-first))
fizz = setrange(3, 6, 100)
buzz = setrange(5, 10, 100)
fizzbuzz = fizz & buzz
fizz -= fizzbuzz
buzz -= fizzbuzz
num = setrange(1, 2, 100) - fizz - buzz - fizzbuzz
line_map = dict({n: "Fizz" for n in fizz}.items() +
{n: "Buzz" for n in buzz}.items() +
{n: "FizzBuzz" for n in fizzbuzz}.items() +
{n: n for n in num}.items())
for i in range(1, 100+1):
print(line_map[i])
Some SETL features Python lacks:
• No ellipsis notation for ranges; must use the "range" function and add one to the maximum value, or define a custom "setrange" function
• Can't concatenate dictionaries; must concatenate their "items" lists and convert back to a dictionary
• No consistent map-lookup notation; collections are indexed with brackets, but functions are called with parentheses
- odonnellryan 9y agoYou can concatenate dictionaries in Python: a = {* * b, * * c} (Python 3.5!) Unless this is doing something unintended? > No consistent map-lookup notation; You can use .get() in Python on a dictionary :)