Fizz Buzz in Python 3
TechnologyI got home today from my day job slinging SQL code. Thought I would take a crack at Fizz Buzz in Python 3 after coming across an article by John Sonmez at Simple Programmer.
Though I am pretty sure I originally saw it at Jeff Atwood’s blog Coding Horror via Hacker News or something.
Fizz Buzz is a simple way for an employer to see if a programmer can, well, program. The task is for every number from 1 to 100:
- If the number is a multiple of 3 print the word ‘Fizz’,
- If the number is a multiple of 5 print the word ‘Buzz’,
- If the number is a multiple of both 3 and 5 then print ‘FizzBuzz’.
First, I admit I totally blanked on using
range(101)
to count from 1 to 100. I kept wanting to do
for 1 to 100
Must have been a long day with SQL Server bringing back up old muscle memory from my BASIC days. Anyways once I got that reboot for my brain the next thing was getting print function to not insert the new line when printing ‘Fizz’ and ‘Buzz’.
Here is what I came up with:
for i in range(101):
print(str(i) + ': ', end = "")
if i % 3 == 0:
print('fizz', end = "")
if i % 5 == 0:
print('buzz', end = ""),
print('\n')
The main reason I went through this exercise? I want to start writing and publishing to this blog regularly, again! Momentum is key! First step is write something.
Since I am no longer using WordPress I also wanted to experiment with my workflow and get used to writing in Markdown, including using code blocks, with Emacs. I think there is a lot of potential with being able to use text files to compose on the fly. Eventually, I am going to see about using org mode and code blocks for creating Hugo ready blog posts.
Share with Friends!