Skip to content

Instantly share code, notes, and snippets.

@MostAwesomeDude
Last active July 14, 2022 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MostAwesomeDude/5040218cdc72bfdecb62b680addbb48b to your computer and use it in GitHub Desktop.
Save MostAwesomeDude/5040218cdc72bfdecb62b680addbb48b to your computer and use it in GitHub Desktop.
DIVSPL interpreter in Python 3
#!/usr/bin/env nix-shell
#! nix-shell -i python3 -p python3
import sys
# This is a basic interpreter for DIVSPL, as described at
# https://www.promptworks.com/blog/the-fastest-fizzbuzz-in-the-west
def parseAssignment(line):
word, number = [w.strip() for w in line.rsplit("=", 1)]
return word, int(number)
def parse(lines):
# The first line must be the range.
start, stop = [int(n.strip()) for n in next(lines).split("...")]
assignments = [parseAssignment(line) for line in lines]
return start, stop, assignments
def run(start, stop, assignments):
for i in range(start, stop + 1):
s = [w for w, n in assignments if not i % n]
yield "".join(s) if s else str(i)
# NB: programs should already be split into lines
def main(program):
start, stop, assignments = parse(program)
for line in run(start, stop, assignments):
print(line)
return 0
if __name__ == "__main__":
sys.exit(main(sys.stdin))
1...15
fizz=3
buzz=5
1...105
fizz=3
buzz=5
fuzz=7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment