{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%load_ext watermark" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "last updated: 2016-06-09 \n", "\n", "CPython 3.5.1\n", "IPython 4.2.0\n" ] } ], "source": [ "%watermark -a 'Sebastian Raschka' -u -d -v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fizz Buzz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *Fizz Buzz* test is probably the simplest coding challenge I could think of -- maybe except for things like \"iterate through a range of integers from 1 to 10 in your favoritie programming language.\" As originally described by [Imran Ghory](https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/), the Fizz Buzz test goes as follows:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> \"Write a program that prints the numbers from 1 to 100. 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”.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*What's the point of this?* you may ask ... As annoying as it is for the experienced programmer, the Fizz Buzz test has become a popular question to assess whether interview candidates applying for a sofware engineering position can code at all. Surprisingly, a large [proportion of candidates](https://blog.codinghorror.com/why-cant-programmers-program/) this most basic \"test.\" Ironically, this makes Fizz Buzz one of the most important algorithms in our modern age then? Well, I hope not, but maybe it's an important step for your personal career. If the output wouldn't take up so much space, I could see it becoming the new \"Hello World\" at least ;)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "Fizz\n", "4\n", "Buzz\n", "Fizz\n", "7\n", "8\n", "Fizz\n", "Buzz\n", "11\n", "Fizz\n", "13\n", "14\n", "FizzBuzz\n", "16\n", "17\n", "Fizz\n", "19\n", "Buzz\n", "Fizz\n", "22\n", "23\n", "Fizz\n", "Buzz\n", "26\n", "Fizz\n", "28\n", "29\n", "FizzBuzz\n", "31\n", "32\n", "Fizz\n", "34\n", "Buzz\n", "Fizz\n", "37\n", "38\n", "Fizz\n", "Buzz\n", "41\n", "Fizz\n", "43\n", "44\n", "FizzBuzz\n", "46\n", "47\n", "Fizz\n", "49\n", "Buzz\n", "Fizz\n", "52\n", "53\n", "Fizz\n", "Buzz\n", "56\n", "Fizz\n", "58\n", "59\n", "FizzBuzz\n", "61\n", "62\n", "Fizz\n", "64\n", "Buzz\n", "Fizz\n", "67\n", "68\n", "Fizz\n", "Buzz\n", "71\n", "Fizz\n", "73\n", "74\n", "FizzBuzz\n", "76\n", "77\n", "Fizz\n", "79\n", "Buzz\n", "Fizz\n", "82\n", "83\n", "Fizz\n", "Buzz\n", "86\n", "Fizz\n", "88\n", "89\n", "FizzBuzz\n", "91\n", "92\n", "Fizz\n", "94\n", "Buzz\n", "Fizz\n", "97\n", "98\n", "Fizz\n", "Buzz\n" ] } ], "source": [ "def fizzbuzz():\n", " for i in range(1, 101):\n", " f, b = not i % 3, not i % 5\n", " if f and b:\n", " print('FizzBuzz')\n", " elif f:\n", " print('Fizz')\n", " elif b:\n", " print('Buzz')\n", " else:\n", " print(i)\n", " \n", "fizzbuzz()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }