{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\"\"\"\n", "Birthday problem:\n", "\n", "You have a baby. The doctor tells you your baby will live to be exactly 128 years old. \n", "Because you are a kind parent, you intend to buy your child enough candals to last every\n", "birthday of their lifetime. Because you are a thrifty parent, you intend to buy the \n", "fewest number of candals possible to represent every birthday your child will have.\n", "\n", "How many candals do you need to represent every birthday number between 1 and 128 in base \n", "10? In base 9? 8? Which base system requires you to buy the fewest candals? \n", "\"\"\"\n", "\n", "DIGITS = \"0123456789ABCDEFG\"\n", "\n", "def toString(num: int, base: int) -> str:\n", " \"\"\"\n", " Convert |num| to a string base |base|\n", " \"\"\"\n", " ret = \"\"\n", " while num:\n", " ret = DIGITS[num % base]+ ret\n", " num = num // base\n", " return ret\n", "\n", "assert(toString(7, 2) == \"111\")\n", "assert(toString(8, 2) == \"1000\")\n", "assert(toString(7, 10) == \"7\")\n", "\n", "def countMaxOfDigit(ageMax: int, base: int, digit: str) -> int:\n", " \"\"\"\n", " Given all the birthday cake needs from 0 to |ageMax|, find\n", " the most candals of type |digit| you need at any given point\n", " \"\"\"\n", " return max([toString(age, base).count(digit) for age in range(ageMax)])\n", "\n", "assert(countMaxOfDigit(150, 10, '0') == 2)\n", "\n", "def countTotalCandals(ageMax: int, base: int) -> int:\n", " \"\"\"\n", " Count the total number of candals needed to represent all ages\n", " from 0 to |ageMax| in base |base|\n", " \"\"\"\n", " return sum([countMaxOfDigit(ageMax, base, digit) for digit in DIGITS])\n", "\n", "assert(countTotalCandals(128, 10) == 21)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
base 2base 3base 4base 5base 6base 7base 8base 9base 10base 11base 12base 13base 14
age 54444444444444
age 106656789999999
age 156767889101112131414
age 2087888910101112131415
age 25879991010111213131415
age 3089910101011111213141515
age 35109911101111121313141516
age 40109911121112121314151516
age 4510101011131212131414151616
age 5010101011131413131415151617
age 5510101011131414141415161617
age 6010101011131514141515161717
age 6512101212131516151516161718
age 7012101212131516151616171718
age 7512101212131517161617171818
age 8012101212131517161717181819
age 8512121212131517181718181919
age 9012121312141517181818181919
age 9512121313141517191818191920
age 10012121313141517191919192020
age 10512121313141517192019202020
age 11012121313141517192020202021
age 11512121313141617192120202121
age 12012121313141617192120212121
age 12512131314141617192122212122
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import HTML, display\n", "\n", "def renderBirthdayMatrix(maxAgeMax: int, maxBase: int):\n", " rows = []\n", " \n", " firstRow = [\"\"]\n", " for base in range(2, maxBase):\n", " firstRow.append(\"base %s\" % (base,))\n", " rows.append(''.join(firstRow))\n", " \n", " for ageMax in range(5, maxAgeMax, 5):\n", " row = [\"age %s\" % (ageMax,)]\n", " for base in range(2, maxBase):\n", " row.append(\"%d\" % (countTotalCandals(ageMax, base),))\n", " rows.append(''.join(row))\n", " \n", " rows = [\"%s\" % (row) for row in rows]\n", " \n", " \n", " return HTML(\"%s
\" % (''.join(rows)))\n", "\n", "display(renderBirthdayMatrix(128, 15))" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }