17
\$\begingroup\$

Summary

Implement FizzBuzz in Python, with the fewest possible tokens.

Challenge

Write a program that prints the decimal numbers from 1 to 100 inclusive. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Programs must be written in some version of Python.

For more details, see 1, 2, Fizz, 4, Buzz

Scoring

Your score will be equal to the number of nodes in the abstract syntax tree of your code, as reported by this program for Python 3, or this program for Python 2. To run the programs, provide the filename of your code as command line argument to the program. For instance:

python simple_counter.py fizzbuzz.py

These programs are based off of Python's ast module. If you have any difficulty, let me know.

To prevent trivial solutions, such as executing a long string with the actual program, or hardcoding the output, there are some additional restrictions:

  • No token in your code may be longer than 15 characters. The above programs will check this requirement for you. Note that for ease of implementation, the above programs count comments as tokens.

  • Code execution/evaluation is banned.

If you have questions as to whether something is allowed, ask me.

Scoring Heuristics

The following rules are typically enough to calculate the score of your program:

  • Block statements are 1 points: if, for ... in ..., while, else, etc.

  • Standalone statements are 1 point: print in Python 2, break, pass, etc.

  • Variables are 2 points

  • Single-token literals are 1 point: 2131, "Hello, world!", True

  • Functions are 3 points (2 for using a variable, 1 extra): print in Python 3, range, etc.

  • Operators are 2 points: +, *, %, and, not, etc.

  • = is 1 point

  • Augmented assignment is 2 points: +=, |=, etc.

  • Parentheses, indentation, etc. are 0 points.

  • A line containing an expression, as opposed to an assignment or an expression, is + 1 point.

  • Having code at all is 1 point.

Challenge:

The lowest score wins. Good luck!

caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
asked Nov 23, 2016 at 1:08
\$\endgroup\$
3
  • 3
    \$\begingroup\$ I like this style. You have to go for simple programs rather than just short ones. \$\endgroup\$ Commented Nov 24, 2016 at 7:40
  • 3
    \$\begingroup\$ Do we really need code-challenge ast-golf for this? Isn't this just a specific form of atomic-code-golf? \$\endgroup\$ Commented Nov 24, 2016 at 8:58
  • \$\begingroup\$ @MartinEnder The reason I did it this way is because the tag text for atomic-code-golf says "Atomic code golf is scored by the number of tokens of your program," and that's not the same as this, though perhaps only a pretty small amount. \$\endgroup\$ Commented Nov 24, 2016 at 17:49

5 Answers 5

11
\$\begingroup\$

33

Python 2

for i in range(1,101):print[i,'Fizz','Buzz','FizzBuzz'][int(`300102100120100`[i%15])]
answered Nov 23, 2016 at 13:39
\$\endgroup\$
4
\$\begingroup\$

46

for x in range(100):print('Fizz'*(x%3>1)+'Buzz'*(x%5>3)or str(x+1))
answered Nov 23, 2016 at 2:35
\$\endgroup\$
3
  • \$\begingroup\$ Can't x+1 not be stringified in python 3? \$\endgroup\$ Commented Nov 23, 2016 at 8:41
  • 1
    \$\begingroup\$ @DestructibleWatermelon Python 3 doesn't have backticks for repr as far as I remember. \$\endgroup\$ Commented Nov 23, 2016 at 13:20
  • 3
    \$\begingroup\$ Print can take an integer as its argument, was my point \$\endgroup\$ Commented Nov 24, 2016 at 3:11
4
\$\begingroup\$

(削除) 39 (削除ここまで) 34

for i in range(1,101):print [i,'Fizz','Buzz','FizzBuzz'][19142723>>2*(i%15)&3]
answered Nov 23, 2016 at 10:57
\$\endgroup\$
4
\$\begingroup\$

Python 3, 35

for x in range(1,101): print(('' if x%3 else 'Fizz') + ('' if x%5 else 'Buzz') or x)
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 20, 2021 at 19:33
\$\endgroup\$
2
\$\begingroup\$

Python 2, 36

for i in range(1, 101):
 print (not i % 3) * "Fizz" + (not i % 5) * "Buzz" or i

I think this is the shortest of the approaches that don't use large numbers / strings.

answered Nov 24, 2016 at 7:37
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.