Skip to content

Instantly share code, notes, and snippets.

@laurybueno
Last active June 18, 2020 16:45
Show Gist options
  • Save laurybueno/4929836020e0cef4b2bc92cfb64285c7 to your computer and use it in GitHub Desktop.
Save laurybueno/4929836020e0cef4b2bc92cfb64285c7 to your computer and use it in GitHub Desktop.
Fizzbuzz - a Pyhton solution with timings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fizzbuzz\n",
"\n",
"The problem: print all numbers from 1 to a given limit, however if the number is:\n",
"- a multiple of 3, prinf \"Fizz\"\n",
"- a multiple of 5, print \"Buzz\"\n",
"- a multiple of 3 and 5, print \"FizzBuzz\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A brute solution\n",
"For each number in the sequence, check if it is a multiple of 3 and 5"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def brute_fizzbuzz_aux(num):\n",
" multiple_3 = bool(num % 3 == 0)\n",
" multiple_5 = bool(num % 5 == 0)\n",
" \n",
" output = ''\n",
" if multiple_3:\n",
" output += 'Fizz'\n",
" if multiple_5:\n",
" output += 'Buzz'\n",
" \n",
" return output or num\n",
"\n",
"def brute_fizzbuzz(limit):\n",
" for n in range(0, limit):\n",
" print(brute_fizzbuzz_aux(n))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A slightly smarter solution\n",
"The solution for each number cycles at every 15 numbers (since \"3*5 == 15\"). So, we can calculate and store the results for the first 15 numbers and just retrieve them when needed."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def soft_fizzbuzz_aux(num):\n",
" multiple_3 = bool(num % 3 == 0)\n",
" multiple_5 = bool(num % 5 == 0)\n",
" \n",
" output = ''\n",
" if multiple_3:\n",
" output += 'Fizz'\n",
" if multiple_5:\n",
" output += 'Buzz'\n",
" \n",
" return output or False\n",
"\n",
"def soft_fizzbuzz(limit):\n",
" current = 1\n",
" cycle = []\n",
" while current <= 15:\n",
" result = soft_fizzbuzz_aux(current)\n",
" cycle.append(result)\n",
" print(result or current)\n",
" current += 1\n",
" \n",
" while current < limit:\n",
" print(cycle[((current-1) % 15)] or current)\n",
" current += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time it\n",
"When the limit is low (below 100), the optimization is barely noticiable."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"903 µs ± 219 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"%%capture\n",
"brute_fizzbuzz(100)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"784 µs ± 147 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"%%capture\n",
"soft_fizzbuzz(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, with greater values, we can see the improvement that the \"cycle\" approach has."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.73 s ± 995 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"%%capture\n",
"brute_fizzbuzz(10000000)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.16 s ± 175 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"%%capture\n",
"soft_fizzbuzz(10000000)"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment