{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fifty Fizzbuzzes\n",
"#### An Extreme Programming Exercise by Vi Hart\n",
"\n",
"One of the many things I learned from Evelyn Eastmond in my time working with her is this technique for deeply exploring, releasing yourself from preconceptions, thinking in new ways.\n",
"\n",
"You make 50 of something. In one big marathon. (Maybe spaced over a few days, but it's important to exhaust your possibilities to break and illuminate habits, rather than making habits as with making a thing a week.) \n",
"\n",
"50 is a LOT, and it takes you on this weird journey of ups and downs and complexifying and simplifying and combining and taking apart. It can be oddly emotional and intense.\n",
"\n",
"This technique was meant for artists, but my research group has used it for VR stuff and programming language design too. So now to learn python, over the weekend I made 50 implementations of fizzbuzz.\n",
"\n",
"Fizzbuzz is a classic simple programming exercise and common interview question based on a classic kids game, where you take turns counting, but if the number is divisible by 3 you say \"fizz\", and if it's divisible for 5 you say \"buzz\", and if it's divisible by both you say \"fizzbuzz\".\n",
"\n",
"* Ok, so this is a jupyter notebook, so if you want to run stuff you'll need to download this and open it, for which you might need https://www.anaconda.com/download/ \n",
"* To run the examples, click in the box and press ctrl+enter. \n",
"* Each numbered section is independently runnable unless otherwise noted.(For sections with multiple boxes you might have to run the parts above it for it to work.) \n",
"* If things break or get weird, try kernel -> restart & clear output\n",
"* ...but things are going to get weird no matter what.\n",
"* Feel free to skip around. Some have visuals, some have sound. 48 is a complete text adventure.\n",
"\n",
"# 1: The Obvious\n",
"\n",
"The first way that came to mind would be to do it in order like you play the game: for each number, check if it's divisible by 3 or 5:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101):\n",
" if i%3!=0 and i%5!=0: print(i)\n",
" if i%3==0 and i%5!=0: print(\"fizz\")\n",
" if i%3!=0 and i%5==0: print(\"buzz\")\n",
" if i%3==0 and i%5==0: print(\"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2: Simple elif\n",
"\n",
"Let's elif it up and take out redundancies:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101):\n",
" if i%3!=0 and i%5!=0: print(i)\n",
" elif i%3==0 and i%5!=0: print(\"fizz\")\n",
" elif i%3!=0: print(\"buzz\")\n",
" else: print(\"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3: Conserving Modulus\n",
"\n",
"There's something I don't like about testing the divisibility more than once. There's only two variables here. \n",
"\n",
"Also, I don't like how there's a separate state for fizzbuzz, we should be able to combine the fizz and the buzz."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101):\n",
" div3 = bool(i%3)\n",
" div5 = bool(i%5)\n",
" print(div3*div5*str(i)+(not div3)*\"fizz\"+(not div5)*\"buzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4: One Line Fizzbuzz\n",
"\n",
"It's hard not to want to make a one line version."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101): print(bool(i%3)*bool(i%5)*str(i)+(not bool(i%3))*\"fizz\"+(not bool(i%5))*\"buzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The line is a bit long though. \n",
"\n",
"# 5: Least Common Multiple\n",
"\n",
"The fizz/buzz pattern is only 15 things long. Why should we do all this tricky modulusing when the computer could save some trouble and just loop through the pattern?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range (1,100,15):\n",
" print(i, i+1, \"fizz\", i+3, \"buzz\", \n",
" \"fizz\", i+6, i+7, \"fizz\", \"buzz\", \n",
" i+10, \"fizz\", i+12, i+13, \"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sure it doesn't stop right at 100, but it's good enough for me.\n",
"\n",
"# 6: Just Print It\n",
"\n",
"Actually, if you really want the fastest running code I don't see why you wouldn't just do this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just because you can do algorithms to it, doesn't mean you always should. Maybe for our application speed is more important than short code.\n",
"\n",
"# 7: Use Fizzbuzz File\n",
"\n",
"Actually, let's go ahead and save our fizzbuzz output, now that we know we'll probably be reusing it a lot:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"fizzbuzzresult.txt\", \"w\") as fizzfile:\n",
" fizzfile.write(\"1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Great! Now we're ready to do fizzbuzz the dependent way."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"fizzbuzzresult.txt\", \"r\") as fizzfile: \n",
" fizztext = fizzfile.read()\n",
" print(fizztext)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 8: Convolutedly Use File Dependencies\n",
"(dependent on above example)\n",
"\n",
"Seems a shame not to loop over anything though. Maybe we can make it into a list by splitting it on the spaces. Then we can loop over the list and print the values one by one instead of just printing the list, for no reason other than to look like we did something.\n",
"\n",
"Note the added benefit that unlike our earlier for loops, we can range this one from 0 to 100 instead of 1 to 101!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"fizzbuzzresult.txt\", \"r\") as fizzfile:\n",
" fizzlist = fizzfile.read().split(\" \")\n",
"\n",
"for i in range(0,100):\n",
" print(fizzlist[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 9: Multiple Refinements of a List\n",
"\n",
"Well, if we're going to do a list, I've got a better way. Let's start from scratch this time.\n",
"\n",
"Rather than thinking about getting each value correct in order, like you would when playing the kid's game, let's do some iterations over the entire list.\n",
"\n",
"We'll start by listing the numbers, then overwrite the multiples of 3 to be \"fizz\", and so on.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"listbuzz= []\n",
"\n",
"for i in range(1,101): listbuzz.append(i)\n",
"for i in range(2,101,3): listbuzz[i] = \"fizz\"\n",
"for i in range(4,101,5): listbuzz[i] = \"buzz\"\n",
"for i in range(14,101,15): listbuzz[i] = \"fizzbuzz\"\n",
" \n",
"listbuzz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, the order of operations matters. But what if we wanted to start with fizzbuzzes?\n",
"\n",
"# 10: Conservation of Fizzbuzz\n",
"\n",
"We'll start with a list of \"fizzbuzz\", and we want to conserve the letters.\n",
"\n",
"First we slice the buzz off of fizzbuzzes that are multiples of three (but not of 15).\n",
"Then we slice the fizz off of fizzbuzzes that are multiples of five (but not of 15).\n",
"Finally, we replace all the fizzbuzzes that aren't multiples of five or three."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"listzz= []\n",
"\n",
"for i in range(1,101): listzz.append(\"fizzbuzz\")\n",
" \n",
"for i in range(2,101,3): \n",
" if (i+1)%15 != 0: listzz[i] = listzz[i][:4]\n",
"for i in range(4,101,5): \n",
" if (i+1)%15 != 0: listzz[i] = listzz[i][4:]\n",
"\n",
"for i in range(0,100):\n",
" if ((i+1)%3 != 0) and ((i+1)%5 != 0):\n",
" listzz[i] = i+1\n",
" \n",
"listzz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's something fun about starting with all fizzbuzzes, but all the mods and checking for divisibility is confusing, especially what with the i+1s and zero indexing. \n",
"\n",
"# 11: Iterate and Protect\n",
"\n",
"What if we started with a list of all fizzbuzzes, and then can't add any more, so we delete the parts we don't need, and we make little shields to protect the good parts from deletion?\n",
"\n",
"(Note that \"None\" is the shield. It's a shield of nothingness.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lizz = []\n",
"shield = []\n",
"\n",
"# Make everything fizzbuzz:\n",
"\n",
"for i in range(0,100): lizz.append(\"fizzbuzz\"), shield.append(4)\n",
"\n",
"# add shields to fizz, buzz, and fizzbuzzes we want to keep:\n",
"\n",
"for i in range(14,100,15): shield[i] = None\n",
"for i in range(2,101,3): \n",
" lizz[i] = lizz[i][:shield[i]]\n",
" shield[i]= None\n",
"for i in range(4,101,5): \n",
" lizz[i] = lizz[i][shield[i]:]\n",
" shield[i]= None\n",
"\n",
"# delete unprotected fizzbuzzes:\n",
" \n",
"for i in range(0,100):\n",
" lizz[i] = lizz[i][shield[i]:shield[i]]\n",
" if lizz[i] == \"\": lizz[i] = i+1\n",
" \n",
"lizz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's fun to try avoiding certain functions or syntax. \n",
"\n",
"# 12: No Mods Allowed\n",
"\n",
"When I first heard this problem (which is apparently common in coding interviews? I dunno I've never done one), I knew I'd want to use % if it were in the languages I know, but I was just learning python and could only guess as to whether % is mod like I'm used to.\n",
"\n",
"So I want to check for divisibility, without using mod. But the very first lesson in intro python was about types and casting. So I can divide, and check if the float result is the same as the integer result.\n",
"\n",
"Also, let's use an if/else tree."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range (1,101):\n",
" if i/3 == int(i/3): \n",
" if i/5 == int(i/5): print(\"fizzbuzz\")\n",
" else: print(\"fizz\")\n",
" else: \n",
" if i/5 == int(i/5): print(\"buzz\")\n",
" else: print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 13: No For Loops\n",
"\n",
"Actually, I don't think the first python class I took even taught for loops. It did teach while True loops with break, though!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 0\n",
"count = 100\n",
"\n",
"while True:\n",
" if n == count: break\n",
" n += 1\n",
" if n/3 == int(n/3): \n",
" if n/5 == int(n/5): print(\"fizzbuzz\")\n",
" else: print(\"fizz\")\n",
" else: \n",
" if n/5 == int(n/5): print(\"buzz\")\n",
" else: print(n) \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 14: Functions and Math\n",
"\n",
"What about functions? Let's use functions. Cool programmers use functions, right?\n",
"\n",
"At the same time, I'm going to try and math my way out of repeating the exact same comparison tests every time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fizzify(n):\n",
" if n%3+n%5 == 0: return \"fizzbuzz\"\n",
" elif n%3==0: return \"fizz\"\n",
" elif n%5==0: return \"buzz\"\n",
" else: return n\n",
" \n",
"for i in range(1,100): print(fizzify(i))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yes, I do like that better! \n",
"\n",
"# 15: Too Many Functions\n",
"\n",
"Let's add more functions! Let's get weird!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def num(a):\n",
" if a%3 and a%5 != 0: return str(a)\n",
" else: return \"\"\n",
"\n",
"def fizz(a):\n",
" if a%3 == 0: return \"fizz\"\n",
" else: return \"\"\n",
" \n",
"def buzz(a):\n",
" if a%5 == 0: return \"buzz\"\n",
" else: return \"\"\n",
"\n",
"def fizzy(n):\n",
" return num(n) + fizz(n) + buzz(n)\n",
" \n",
"def fizzbuzz():\n",
" for i in range(1,101): print(fizzy(i))\n",
" \n",
"fizzbuzz()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We've now gone through a fizzbuzz amount of fizzbuzzes!\n",
"\n",
"Only 35 more to go.\n",
"\n",
"Let's relax the constraints.\n",
"\n",
"# 16: Leverage User Knowledge\n",
"\n",
"What if instead of calculating the answer ourself, we crowdsourced? Let's leverage the knowledge of the user!\n",
"\n",
"We'll just ask them for the answers, over and over."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fizzle = []\n",
"for i in range(0,100): fizzle.append(i+1)\n",
"\n",
"print(\"Hello! \\n\\nLet's play a game. It's called fizzbuzz. \\n\\n It works like this:\")\n",
"print(\"If the number is divisible by 3, say \\'fizz\\'. If it's divisible by 5, say \\'buzz\\'.\")\n",
"print(\"If it's divisible by both, say \\'fizzbuzz\\', and neither, then just say the number.\")\n",
"print(\"\\nready?\\n\")\n",
"\n",
"\n",
"for i in range(1,101): \n",
" print(\"how about\",i,\"?\")\n",
" fizzle[i+1] = input()\n",
" print(\"good work! let's do another!\")\n",
"\n",
" \n",
"print(\"You Win!\")\n",
"print(fizzle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ok, no one is going to play that to the end, and they're going to get some wrong.\n",
"\n",
"# 17: Fizzbuzz Game \n",
"\n",
"How about you can just do some random ones until you're tired? And let's make it a bit more polished, with an intro and a quit function, and motivation so that our crowdsourcing has a better chance of getting us our data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"encouragements = [\"great!\", \"amazing!\", \"that's the ticket!\", \"wow!\", \"You're wonderful!\"]\n",
"\n",
"def encourage():\n",
" print(encouragements[random.randint(0, len(encouragements)-1)],\"\\n\")\n",
"\n",
"fuzzle = []\n",
"for i in range(0,100): fuzzle.append(i+1)\n",
"\n",
"ask = input(\"Hello! Would you like to play a game? Y/N: \").lower()\n",
"\n",
"while ask == \"n\": ask = input(\"oh come on it'll be fun! Play a game y/n? \")\n",
" \n",
"if ask == (\"y\" or \"yes\"): \n",
" print(\"Hooray! Let's play!\")\n",
"else: \n",
" print(\"I'll take that as a yes.\")\n",
" \n",
"print(\"You can quit any time by typing q.\\n\")\n",
"\n",
"print(\"Here's the game: \\n\")\n",
"print(\"If the number is divisible by 3, say \\'fizz\\'. If it's divisible by 5, say \\'buzz\\'.\")\n",
"print(\"If it's divisible by both, say \\'fizzbuzz\\', and neither, then just say the number.\\n\\n\")\n",
"\n",
"input(\"ready? \")\n",
"\n",
"while ask != \"q\":\n",
" a = random.randint(1,101)\n",
" print(\"How about\",a,\"?\")\n",
" ask = input()\n",
" if ask.lower() == \"q\": break\n",
" fuzzle[a-1] = ask\n",
" encourage()\n",
"\n",
"print(\"Thank you for playing!!!\")\n",
"print(fuzzle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first time I tested this game I got the following fizzbuzz result:\n",
"\n",
"[1, '', 3, 4, 5, 6, 7, 8, 9, 10, 11, 'fizz', 13, 14, 15, 16, '', 18, 'nineteen', 20, 21, 22, 23, 24, 25, 26, 'fizz', 28, 29, 'fizzbuzz', 31, 32, 'fizz', 'thertyfour', 35, 36, 37, 'thirtyeight', 39, 40, 41, 42, 'fizz', 44, 45, 46, 'fourtyseven', 48, 49, 50, 'fizz', 52, 53, 54, 55, 56, 'fizz', 'fiftyeight', 'fizz', 60, 61, 62, 63, 64, 65, 66, 'sixtyseven', '', 69, 70, 71, 'fizz', 73, 'buzz', 'fizzbuss', 76, 77, 78, 79, 80, 81, 'eightyto', '83', 84, 85, 'fizz', 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n",
"\n",
"Not bad, not bad.\n",
"\n",
"# 18: Fizzbuzz Game with Judginess\n",
"\n",
"Now, maybe we could make the game more fun while also increasing accuracy if we compare the new fizzbuzz list we're creating to a known good list. Does it count? Sure."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"correctBuzz = [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16, 17, 'fizz', 19, 'buzz', 'fizz', 22, 23, 'fizz', 'buzz', 26, 'fizz', 28, 29, 'fizzbuzz', 31, 32, 'fizz', 34, 'buzz', 'fizz', 37, 38, 'fizz', 'buzz', 41, 'fizz', 43, 44, 'fizzbuzz', 46, 47, 'fizz', 49, 'buzz', 'fizz', 52, 53, 'fizz', 'buzz', 56, 'fizz', 58, 59, 'fizzbuzz', 61, 62, 'fizz', 64, 'buzz', 'fizz', 67, 68, 'fizz', 'buzz', 71, 'fizz', 73, 74, 'fizzbuzz', 76, 77, 'fizz', 79, 'buzz', 'fizz', 82, 83, 'fizz', 'buzz', 86, 'fizz', 88, 89, 'fizzbuzz', 91, 92, 'fizz', 94, 'buzz', 'fizz', 97, 98, 'fizz', 'buzz']\n",
"\n",
"encouragements = [\"great!\", \"amazing!\", \"that's the ticket!\", \"wow!\", \"You're wonderful!\"]\n",
"boos = [\"nope\", \"oh come on\", \"wrong!!!\", \"try again.\"]\n",
"\n",
"def encourage():\n",
" print(encouragements[random.randint(0, len(encouragements)-1)],\"\\n\")\n",
"\n",
"def boo():\n",
" print(boos[random.randint(0, len(boos)-1)],\"\\n\")\n",
"\n",
"buzzle = []\n",
"for i in range(0,100): buzzle.append(i+1)\n",
"\n",
"ask = input(\"Hello! Would you like to play a game? Y/N: \").lower()\n",
"\n",
"while ask == \"n\": ask = input(\"oh come on it'll be fun! Play a game y/n? \")\n",
" \n",
"if ask == (\"y\" or \"yes\"): \n",
" print(\"Hooray! Let's play!\")\n",
"else: \n",
" print(\"I'll take that as a yes.\")\n",
" \n",
"print(\"You can quit any time by typing q.\\n\")\n",
"\n",
"print(\"Here's the game: \\n\")\n",
"print(\"If the number is divisible by 3, say \\'fizz\\'. If it's divisible by 5, say \\'buzz\\'.\")\n",
"print(\"If it's divisible by both, say \\'fizzbuzz\\', and neither, then just repeat the number.\\n\\n\")\n",
"\n",
"input(\"ready? \")\n",
"\n",
"while ask != \"q\":\n",
" a = random.randint(1,101)\n",
" print(\"How about\",a,\"?\")\n",
" ask = input()\n",
" if ask.lower() == \"q\": break\n",
" if ask == str(correctBuzz[a-1]):\n",
" buzzle[a-1] = ask\n",
" encourage()\n",
" else:\n",
" boo()\n",
"\n",
"print(\"Thank you for playing!!!\")\n",
"print(buzzle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 19: Fizzbuzz Game with Point Multipliers\n",
"\n",
"Let's try one more game version, this time allowing faster answering and with a points system.\n",
"\n",
"We'll even have point multipliers and an end state!\n",
"\n",
"(And we'll struggle with what exactly and how Python does the whole function global local variable thing and how to get around it!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"bizlet = []\n",
"for i in range(0,100): bizlet.append(0)\n",
"\n",
"points = 0\n",
"streak = 0\n",
"streakMult = 1\n",
"encouragements = [\"great!\", \"amazing!\", \"that's the ticket!\", \"wow!\", \"You're wonderful!\"]\n",
"boos = [\"nope\", \"oh come on\", \"wrong!!!\", \"try again.\"]\n",
"\n",
"def right(points, streak, streakMult):\n",
" bizlet[a-1] = ask\n",
" print(encouragements[random.randint(0, len(encouragements)-1)],\"\\n\")\n",
" streak += 1\n",
" if streak == 3: \n",
" streakMult = 2\n",
" print(\"THREE IN A ROW!\\nPOINTS MULTIPLIER x2\")\n",
" elif streak == 5: \n",
" streakMult = 3\n",
" print(\"!!!FIVE IN A ROW!!!!\\n!!!POINTS MULTIPLIER x3!!!\")\n",
" elif streak == 10: \n",
" streakMult = 5\n",
" print(\"!!!!!!!!!!!!!!!\\n!!!TEN IN A ROW!!!!\\n!!!POINTS MULTIPLIER x5!!!\\n!!!!!!!!!!!!!!!\")\n",
" elif streak == 15: \n",
" streakMult = 15\n",
" print(\"FIZZBUZZ STREAK BONUS\"*15)\n",
" points += 5*streakMult\n",
" print(\"score:\", points)\n",
" return points, streak, streakMult\n",
"\n",
"def wrong(points, streak, streakMult):\n",
" print(boos[random.randint(0, len(boos)-1)],\"\\n\")\n",
" streak = 0\n",
" print(\"score:\", points)\n",
" return points, streak, streakMult\n",
" \n",
"def win():\n",
" print(\"\\n*******\\n*YOU WIN!*\\n*******\\n\\nYour final score is: \",score)\n",
"\n",
"ask = input(\"Hello! Would you like to play a game? Y/N: \").lower()\n",
"\n",
"while ask == \"n\": ask = input(\"oh come on it'll be fun! Play a game y/n? \")\n",
" \n",
"if ask == (\"y\" or \"yes\"): \n",
" print(\"Hooray! Let's play!\")\n",
"else: \n",
" print(\"I'll take that as a yes.\")\n",
" \n",
"print(\"You can quit any time by typing q.\\n\")\n",
"\n",
"print(\"Here's the game: \\n\")\n",
"print(\"If the number is divisible by 3, enter \\'a\\'. \\nIf it's divisible by 5, enter \\'s\\'.\")\n",
"print(\"If it's divisible by both, enter \\'d\\', \\nand if it's neither, then just hit enter.\\n\\n\")\n",
"\n",
"input(\"ready? \")\n",
"\n",
"while ask != \"q\":\n",
" if not(0 in bizlet): win()\n",
" a = random.randint(1,101)\n",
" print(\"How about\",a,\"?\")\n",
" ask = input()\n",
" if ask.lower() == \"q\": break\n",
" if ask == \"\":\n",
" if a%3!=0 and a%5!=0: \n",
" points, streak, streakMult = right(points, streak, streakMult)\n",
" else: \n",
" points, streak, streakMult = wrong(points, streak, streakMult)\n",
" elif ask == \"a\":\n",
" if a%3==0 and a%5!=0: \n",
" points, streak, streakMult = right(points, streak, streakMult)\n",
" else: \n",
" points, streak, streakMult = wrong(points, streak, streakMult)\n",
" elif ask == \"s\":\n",
" if a%3!=0 and a%5==0: \n",
" points, streak, streakMult = right(points, streak, streakMult)\n",
" else: \n",
" points, streak, streakMult = wrong(points, streak, streakMult)\n",
" elif ask == \"d\":\n",
" if a%3+a%5 == 0: \n",
" points, streak, streakMult = right(points, streak, streakMult)\n",
" else: \n",
" points, streak, streakMult = wrong(points, streak, streakMult)\n",
" else:\n",
" print(\"enter for normal, a for divisible by 3, s for divisible by 5, d for both, q to quit.\")\n",
"\n",
"print(\"Thank you for playing!!!\")\n",
"print(bizlet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That was a long one!\n",
"\n",
"# 20. Minimalist\n",
"\n",
"How about something more minimal:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"fizzbuzz\\n\"*100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So it's not 100% accurate. So sue me.\n",
"\n",
"# 21: Random Guesses\n",
"\n",
"Let's just take a random guess for each. Who will be able to tell the difference, really?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"for i in range(1,100):\n",
" r = random.randint(1,10)\n",
" if r < 6: print(i)\n",
" elif r < 8: print(\"fizz\")\n",
" elif r < 10: print(\"buzz\")\n",
" else: print(\"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Maybe we can predict the correct answer using a more analytical approach.\n",
"\n",
"# 22: Linear Regression\n",
"\n",
"Let's start with simple linear regression. This means we'll need a data set, say, the first 20 correct answers.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"datafizz = [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16, 17, 'fizz', 19, 'buzz']\n",
"numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n",
"fizzframe = pd.DataFrame({\"number\": numbers, \"answer\": datafizz})\n",
"fizzframe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hmm, the data set has all these non-number values. Well, we'll just throw those out of course, they are clearly garbage data.\n",
"\n",
"I'm sure there's a better way to do this, but let's try:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ff = fizzframe\n",
"j = 0\n",
"for i in range(0,20):\n",
" if type(ff.iloc[j,1]) == str: \n",
" ff = ff.drop(ff.index[j])\n",
" else: j+=1\n",
"ff"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ah, those look like datums that we can do data analysis to! \n",
"\n",
"I wonder if we can find a corellation between the number and anwser that will allow the creation of a predictive model?\n",
"\n",
"Let's take a visual look at our data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"plt.scatter(ff.number, ff.answer, color='red')\n",
"plt.xlabel(\"number\")\n",
"plt.ylabel(\"correct fizzification\")\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Looks pretty promising! Let's see if linear regression can help us fill in those gaps."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn import linear_model\n",
"regr = linear_model.LinearRegression()\n",
"x = ff[[\"number\"]]\n",
"y = ff[[\"answer\"]]\n",
"regr.fit (x, y)\n",
"\n",
"print ('Coefficients: ', regr.coef_)\n",
"print ('Intercept: ',regr.intercept_)\n",
"\n",
"plt.scatter(ff.number, ff.answer, color='red')\n",
"plt.plot(x, regr.coef_[0][0]*x + regr.intercept_[0], '-r', color='pink')\n",
"plt.xlabel(\"number\")\n",
"plt.ylabel(\"correct fizzbuzzification\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wow, that's a remarkable fit! It's things like this that let us know machine learning is magic that humans will never understand and that will develop consciousness all by itself. \n",
"\n",
"Now let's use it to predict the value of fizzbuzzification for the full range of 1 through 100."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(0,100): \n",
" prediction = regr.coef_[0][0]*i + regr.intercept_[0]\n",
" print(prediction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because our machines are so smart because they learned so much, we know that just because our puny human minds think \"that doesn't look like fizzbuzz\" doesn't mean we're right. We can be sure that the above is the correct fizzbuzz, better than what humans can predict.\n",
"\n",
"# 23: Linear Regression with Veneer\n",
"\n",
"...but let's throw some fizzes and buzzes in there just to be sure it looks real fizzbuzzy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#dependent on running 22\n",
"\n",
"for i in range(0,51,3): \n",
" prediction = regr.coef_[0][0]*i + regr.intercept_[0]\n",
" print(prediction)\n",
" print(\"fizz\")\n",
" print(\"buzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yesss, yes, there we go.\n",
"\n",
"# 24: Simple Answer\n",
"\n",
"Let's step back a moment. Really fizzbuzz should be this nice simple answer, regardless of whether it's this strange concept called \"correct\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101): print(i,i%3*\"FIZZ\",i%5*\"BUZZ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 25: BUZZY FIZZY BUZZ BUZZ FIZZ BUZZ"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101): print(i%2*\"fizz\",i%3*\"FIZZ\",i%4*\"buzz\",i%5*\"BUZZ\",i%6*\"fizzbuzz\",i%7*\"FIZZBUZZ\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 26: Fizz Fuzz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range (1,51):\n",
" print(i*\"f\"+\"izz\"*(not bool(i%3))+\"uzz\"*(not bool(i%5)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 27: Mod Mod Mod Mod Mod\n",
"\n",
"This one's actually correct:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range (1,101):\n",
" print(str(i)*(not(i+1)%3%2)*(not(i+1)%5%4%3%2) + \n",
" \"fizz\"*((i+1)%3%2) + \n",
" \"buzz\"*((i+1)%5%4%3%2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 28: Backspace\n",
"\n",
"For every line, print number, fi(zz), and bu(zz)! Then backspace over the parts you don't want.\n",
"\n",
"I haven't used \\b before but it seems to also be able to delete itself? So you can't stack them? Not exactly sure what's up but that's why it's fibu and not all four letters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101):\n",
" print(\n",
" str(i) + \n",
" (i+1)%3%2*(int((i+10)/10)+2)*\"\\b\" + \n",
" (i+1)%5%4%3%2*(int((i+10)/10)+2)*(i%3)*\"\\b\" + \n",
" \"fi\" +\n",
" i%3*\"\\b\"*2 +\n",
" \"bu\" +\n",
" i%5*\"\\b\"*2\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So there's some mistakes. Mistakes are part of life. We must embrace them and move on!\n",
"\n",
"# 29: Beep Buzz\n",
"\n",
"I heard that you can use an escape sequence to sound the computer bell. Let's test out this \\a bell function, for an auralization of where the fizzes and buzzes are in the sequence!\n",
"\n",
"Note the logic we can use if we aren't differentiating between fizzez, buzzez, and fizzbuzzez."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101): \n",
" if (i%3 and i%5)==0: print(i,\"bing!\\a\")\n",
" else: print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is not making the noises though, so I don't know if I'm doing it wrong or if this computer or jupyter notebooks doesn't do it? Hmm! Also I imagine to the human ear it is all happening at once.\n",
"\n",
"# 30: Auralization\n",
"\n",
"Let's look up how to play stuff with timing, and making different tones!\n",
"\n",
"And by \"look up\", I mean this is mostly going to be a copy+paste job. I have no idea how to do audio in a python jupyter notebook, but I think I can modify someone else's thing to fizzbuzz.\n",
"\n",
"Then we can make fizz, buzz, and fizzbuzz sound different!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#mostly from https://ipython-books.github.io/117-creating-a-sound-synthesizer-in-the-notebook/\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import (\n",
" Audio, display, clear_output)\n",
"from ipywidgets import widgets\n",
"from functools import partial\n",
"%matplotlib inline\n",
"\n",
"rate = 16000.\n",
"duration = 2\n",
"t = np.linspace(\n",
" 0., duration, int(rate * duration))\n",
"\n",
"#ok now just gotta modify their synth function to play more than one tone\n",
"\n",
"audioFizz = []\n",
"\n",
"def synth(f):\n",
" for i in range(0,len(f)):\n",
" x = np.sin(f[i] * 2. * np.pi * t)\n",
" display(Audio(x, rate=rate, autoplay=True))\n",
"\n",
"for i in range(1,21): \n",
" if i%3+i%5 == 0: audioFizz.append(800)\n",
" elif i%3==0: audioFizz.append(600)\n",
" elif i%5==0: audioFizz.append(700)\n",
" else: audioFizz.append(220)\n",
"\n",
"synth(audioFizz) \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sooooooo... I guess it plays them all at once, eh. Or tries to, and then loads out of order. I spared your ears and computer by limiting it to 20 instead of 100.\n",
"\n",
"I'll have to figure out how the audio thingy actually works, but in the mean time:\n",
"\n",
"# 31: Vizualization\n",
"\n",
"Let's go back to our datavis graphs, since I understand the tools better.\n",
"\n",
"We'll plot the numbers, but give fizz a fizzy boost, and buzz a dip down. For both, it'll even out to a small dip down."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"fizzbee = []\n",
"\n",
"for i in range(0,100): \n",
" fizzbee.append(i+1)\n",
" if (i+1)%3 == 0: fizzbee[i] += 10\n",
" if (i+1)%5 == 0: fizzbee[i] -= 20\n",
"\n",
"plt.plot(range(1,101), fizzbee)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Maybe it would be more accurate to make all fizzes and buzzes the same value, rather than relative to the numbers?\n",
"\n",
"But which value should we choose? How about something negative."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"fuzzbee = []\n",
"\n",
"for i in range(0,100): \n",
" fuzzbee.append(i+1)\n",
" if (i+1)%3 == 0: fuzzbee[i] = -10\n",
" if (i+1)%5 == 0: fuzzbee[i] = -20\n",
" if (i+1)%15 == 0: fuzzbee[i] = -30\n",
"\n",
"plt.plot(range(1,101), fuzzbee)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ah, yes, I like that better.\n",
"\n",
"Let's go ahead and bar graph the stats, too.\n",
"\n",
"Might as well vary the logic on the fizzbuzz part, too."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"n, f, b, fb = 0,0,0,0\n",
"\n",
"for i in range(1,101):\n",
" if (i%3 and i%5) != 0: n+=1\n",
" elif i%5 != 0: f+=1\n",
" elif i%3 != 0: b+=1\n",
" else: fb+=1\n",
" \n",
"labels = ('number','fizz','buzz','fizzbuzz')\n",
"datums = (n,f,b,fb)\n",
" \n",
"plt.bar(labels,datums)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 32 Auralization Attempt 2\n",
"\n",
"Let's try this again. I'm going to take a closer look at this audio display function to see what kind of input it actually wants, and see if we can concatenate all the audiobits."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from IPython.display import (\n",
" Audio, display, clear_output)\n",
"\n",
"rate = 16000.\n",
"duration = 0.25\n",
"t = np.linspace(\n",
" 0., duration, int(rate * duration))\n",
"\n",
"audioFuzz = []\n",
"\n",
"def synth(f):\n",
" x = np.sin(f[0] * 2. * np.pi * t)\n",
" for i in range(0,len(f)):\n",
" x = np.append(x, np.sin(f[i] * 2. * np.pi * t))\n",
" print(len(x))\n",
" print(x)\n",
" display(Audio(x, rate=rate, autoplay=True))\n",
"\n",
"for i in range(1,101): \n",
" if i%3+i%5 == 0: audioFuzz.append(800)\n",
" elif i%3==0: audioFuzz.append(600)\n",
" elif i%5==0: audioFuzz.append(700)\n",
" else: audioFuzz.append(220+i)\n",
"\n",
"synth(audioFuzz) \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yes, that's more like it!\n",
"\n",
"# 33: RGBuzz\n",
"\n",
"Well, I don't know how to do much in the way of graphics, but I know how to plot graphs. So let's use a bar graph to represent fizzbuzz in color."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"rgcount = []\n",
"rg1 = []\n",
"for i in range(0,100):\n",
" rgcount.append(i)\n",
" rg1.append(1)\n",
"\n",
"rgbuzz = plt.bar(rgcount,rg1)\n",
"\n",
"for i in range(1,100):\n",
" if i%3+i%5 == 0: rgbuzz[i].set_color(\"yellow\")\n",
" elif i%3==0: rgbuzz[i].set_color(\"red\")\n",
" elif i%5==0: rgbuzz[i].set_color(\"orange\")\n",
" else: rgbuzz[i].set_color(\"grey\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 34: Text Color\n",
"\n",
"Now that I think of it, let's learn how to format text, and do classic fizzbuzz but with formatting.\n",
"\n",
"Uhh these codes are ridiculous but ok?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"R = '\\033[31m' # red\n",
"G = '\\033[32m' # green\n",
"O = '\\033[33m' # orange\n",
"B = '\\033[34m' # blue\n",
"\n",
"for i in range(1,101):\n",
" if i%3!=0 and i%5!=0: print(B+str(i))\n",
" elif i%3==0 and i%5!=0: print(R+\"fizz\")\n",
" elif i%3!=0: print(O+\"buzz\")\n",
" else: print(G+\"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not bad. I'd like that in a block please.\n",
"\n",
"# 35: Color Fuzz Block\n",
"\n",
"Let's add some more and put them all close together for color time. I'm also going to have two shades of blue for numbers so that when they're together they don't blend together."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"R = '\\033[31m' # red\n",
"G = '\\033[32m' # green\n",
"O = '\\033[33m' # orange\n",
"B = '\\033[34m' # blue\n",
"D = '\\033[36m' # lighter blueish\n",
"\n",
"fizzString = \"\"\n",
"\n",
"for i in range(1,501):\n",
" if i%3!=0 and i%5!=0 and i%2!=0: fizzString += (B+str(i))\n",
" elif i%3!=0 and i%5!=0: fizzString += (D+str(i))\n",
" elif i%3==0 and i%5!=0: fizzString += (R+\"fizz\")\n",
" elif i%3!=0: fizzString += (O+\"buzz\")\n",
" else: fizzString += (G+\"fizzbuzz\")\n",
" \n",
"print(fizzString)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 35: Time to learn to draw Rectangles?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib.patches as mpatches\n",
"%matplotlib inline\n",
"\n",
"rect = mpatches.Rectangle((1,1),.5,1.5)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nope, can't figure it out. Why is searching for simple things the hardest?\n",
"\n",
"\n",
"But I did find a 2d histogram thing that has rectangles, let's try that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"histfizz = []\n",
"\n",
"for i in range(1,100):\n",
" if i%3+i%5 == 0: histfizz.append(0.2)\n",
" elif i%3==0: histfizz.append(0.4)\n",
" elif i%5==0: histfizz.append(0.7)\n",
" else: histfizz.append(0.9)\n",
"\n",
"plt.hist2d(histfizz,histfizz)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yep, it's rectangles, we win.\n",
"\n",
"# 36: User Set Fizz Fuzz\n",
"\n",
"Why always 5 and 3? Why fizz and buzz? Let's let the user decide what numbers are special and what we call them.\n",
"\n",
"Notice the checks to make sure their selected numbers are in a useful range and are not mutual multiplicabits."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"input(\"hello, would you like to play fizzbuzz?\")\n",
"input(\"Great. I don't know how to play fizzbuzz, so how about you make up a new game?\")\n",
"\n",
"gameName=\"\"\n",
"num1=\"\"\n",
"num2=\"\"\n",
"say1=\"\"\n",
"say2=\"\"\n",
"\n",
"while gameName == \"\":\n",
" gameName = input(\"what do you want to call the new game? \")\n",
" while len(gameName)<6:\n",
" gameName = input(\"Maybe a longer name?\")\n",
" \n",
"say1=gameName[:len(gameName)//2]\n",
"say2=gameName[len(gameName)//2:]\n",
"\n",
"print(\"\\ngreat! let's play\", gameName,\"\\nNow all we need are rules.\")\n",
"\n",
"while True:\n",
" num1 = input(\"pick a smallish integer: \")\n",
" if num1.isdigit() == True:\n",
" if int(num1) < 2:\n",
" print(\"not that small!\")\n",
" elif int(num1) > 40:\n",
" print(\"that's a little big, don't you think?\")\n",
" else: break\n",
" else: print(\"counting numbers only, please\")\n",
" \n",
"num1=int(num1)\n",
"\n",
"print(\"\\nok,\", num1, \"will do. \\nNow,\")\n",
"\n",
"while True:\n",
" num2 = input(\"pick another: \")\n",
" if str.isdigit(num2) == True:\n",
" num2=int(num2)\n",
" if num2 < 2:\n",
" print(\"\\nnot that small!\\n\")\n",
" elif num2 > 100/num1:\n",
" print(\"\\nthat's a little big, don't you think?\\n\")\n",
" elif (num1%num2) * (num2%num1) == 0:\n",
" print(\"\\nno no no, now one is a factor of the other.\\n\")\n",
" else: break\n",
" else: print(\"counting numbers only please!!!\")\n",
" \n",
"print(\"\\nsure,\", num2, \"it is.\")\n",
"print(\"\\n\\nReady to play\",gameName,\"?\\n\\n\")\n",
"\n",
"for i in range(1,101):\n",
" if i%num1!=0 and i%num2!=0: print(i)\n",
" elif i%num1==0 and i%num2!=0: print(say1)\n",
" elif i%num1!=0: print(say2)\n",
" else: print(gameName)\n",
" \n",
"print(\"\\n\\nI WIN!!!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 38: Classy Fizzbuzz\n",
"\n",
"I gotta learn how to write a class! Let's do fizzbuzz the classy way!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class fizzclass(object):\n",
" def __init__(self,num1=3,num2=5,text1=\"fizz\",text2=\"buzz\"):\n",
" self.num1 = num1\n",
" self.num2 = num2\n",
" self.text1 = text1\n",
" self.text2 = text2\n",
" \n",
" def rules(self):\n",
" print(\"multiples of\",self.num1,\"are\",self.text1,\n",
" \"\\nmultiples of\",self.num2,\"are\",self.text2,\n",
" \"\\nmultiples of both are\",self.text1+self.text2)\n",
" \n",
" def run(self, n=100):\n",
" for i in range(1, n+1):\n",
" if i%self.num1 + i%self.num2 == 0: print(self.text1+self.text2)\n",
" elif i%self.num2==0: print(self.text1)\n",
" elif i%self.num1==0: print(self.text2)\n",
" else: print(i)\n",
" \n",
"fizzobject = fizzclass()\n",
"fizzobject.rules()\n",
"fizzobject.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the fizzclass class, we can create anything in the class of fizzbuzz-type games."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"blipblop = fizzclass(4,7,\"blip\",\"blop\")\n",
"blipblop.run(30)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ohwhy = fizzclass(1,2,\"oh \",\"why!\")\n",
"ohwhy.run(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ah, so classy!\n",
"\n",
"# 39: Fizz Bizz Wuzz\n",
"\n",
"Why just two numbers? Let's allow any number of special multiples."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class fuzzclass(object):\n",
" def __init__(self,numbers = [3,5],text = [\"fizz\",\"buzz\"]):\n",
" self.numbers = numbers\n",
" self.text = text\n",
" \n",
" def rules(self):\n",
" i=0\n",
" while i < len(self.numbers):\n",
" print(\"multiples of\",self.numbers[i],\"are:\",self.text[i])\n",
" i+=1\n",
" print(\"multiples of both\",self.numbers[0],\"and\", self.numbers[1], \n",
" \"are:\", self.text[0]+self.text[1], \"\\nand so on.\\n\")\n",
" \n",
" def run(self, n=100):\n",
" for i in range(1, n+1):\n",
" fuzzstring = \"\"\n",
" for j in range(len(self.numbers)):\n",
" if i%self.numbers[j] == 0:\n",
" fuzzstring += self.text[j]\n",
" if fuzzstring != \"\": print(fuzzstring.capitalize())\n",
" else: print(i)\n",
" \n",
"wuzz = fuzzclass()\n",
"wuzz.numbers = [3,5,7]\n",
"wuzz.text = [\"fizz\", \"bizz\", \"wuzz\"]\n",
"\n",
"wuzz.rules()\n",
"wuzz.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try a more extreme example!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wooz=fuzzclass([2,3,4,5,6,7,8,9],[\"tu\",\"re\",\"fo\",\"fiv\",\"ix\",\"eve\",\"ate\",\"ine\"])\n",
"wooz.run(50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or an artsier one:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"poemnums=[1,2,3,4,5,6,7,8,9,10]\n",
"poemwords=[\"i\",\" don't\",\" think\",\" know\",\" have\",\" that\",\" wish\",\" why\",\" or\",\" anything\"]\n",
"poem = fuzzclass(poemnums,poemwords)\n",
"poem.run(30)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take this poem thing to the next level.\n",
"\n",
"# 40: Fizz Poem Friend\n",
"\n",
"Let's make a poem bot that bases its fizz-style poem off of user input."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#first I'll borrow the fuzzclass, with some modifications\n",
"\n",
"class foezzclass(object):\n",
" def __init__(self,numbers = [3,5],text = [\"fizz\",\"buzz\"]):\n",
" self.numbers = numbers\n",
" self.text = text\n",
" \n",
" def run(self, n=100):\n",
" for i in range(1, n+1):\n",
" fuzzstring = \"\"\n",
" for j in range(len(self.numbers)):\n",
" if i%self.numbers[j] == 0:\n",
" fuzzstring += self.text[j]+\" \"\n",
" if fuzzstring != \"\": print(fuzzstring.capitalize())\n",
" else: print(\"\")\n",
"\n",
"#I wonder how many lines our poem should be?\n",
"\n",
"poemLength = 50\n",
"maxPoem = 50\n",
" \n",
"#Now for some user interaction!\n",
"\n",
"instring = \"\"\n",
"while instring == \"\": instring = input(\"Hi! How are you? \")\n",
"instring += \" \"+input(\"Ah! I see! Tell me more. \")\n",
"instring += \" \"+input(\"Well, I am very good at keeping secrets.\")\n",
"\n",
"poemWords = instring.split(\" \")\n",
"\n",
"if len(poemWords)>maxPoem: poemWords = poemWords[:maxPoem]\n",
"\n",
"poemNums = []\n",
"for i in range(len(poemWords)): poemNums.append(i+2)\n",
"\n",
"if len(poemWords)<50: poemLength = len(poemWords) + 3\n",
" \n",
"input(\"I feel like I'm starting to understand you.\")\n",
"print(\"\\nI wrote you this poem: \\n\")\n",
" \n",
"botpoem = foezzclass(poemNums,poemWords)\n",
"botpoem.run(poemLength)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I like this game. But let's step back again:\n",
"\n",
"# 41: Factors\n",
"\n",
"maybe the word for every number should be its factors, something like the second example in section 39, but accommodating for the fact that we run out of words for primes.\n",
"\n",
"The tricky part is making sure we keep track of how much we've converted into words and how much might be a multiplicative remainder."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#we've got to modify fuzzclass so that 9 is \"fizzfizzfizz\" and such\n",
"\n",
"class factorname(object):\n",
" def __init__(self,numbers = [3,5],text = [\"fizz\",\"buzz\"]):\n",
" self.numbers = numbers\n",
" self.text = text\n",
" \n",
" def run(self, n=100):\n",
" for i in range(1, n+1):\n",
" fuzzstring = \"\"\n",
" accountedFor=1\n",
" for j in range(len(self.numbers)):\n",
" k = i\n",
" while k%self.numbers[j] == 0:\n",
" fuzzstring += self.text[j]\n",
" accountedFor *= self.numbers[j]\n",
" k = k/self.numbers[j]\n",
" if fuzzstring != \"\": \n",
" if i-accountedFor != 0: fuzzstring += str(int(i/accountedFor))\n",
" print(fuzzstring.capitalize())\n",
" else: print(i)\n",
" \n",
"primeWords = [\"two\",\"three\",\"five\",\"seven\",\"eleven\",\"thirteen\",\"seventeen\",\"nineteen\"]\n",
"primeNums = [2,3,5,7,11,13,17,19]\n",
"\n",
"primeFizz = factorname(primeNums,primeWords)\n",
"primeFizz.run(200)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Note that the above does not find prime numbers, and the single numbers aren't necessarily prime, they just don't have named factors.)\n",
"\n",
"# 42: Froodbeeb\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,51):\n",
" if i==42: print(\"\\033[31m *****DON'T PANIC*****\"'\\033[30m')\n",
" elif i%3!=0 and i%5!=0: print(i)\n",
" elif i%5!=0: print(\"frood\")\n",
" elif i%3!=0: print(\"beeb\")\n",
" else: print(\"hoopy frood beeblebrox\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 43: Text Adventure Dungeon"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"monsters = [\"bats\",\"bears\",\"dungeon mice\",\"fizz snakes\", \"buzzbees\", \"growlumps\", \"wolfizzes\", \"robots\", \"buzzdemons\"]\n",
"adj = [\"evil\",\"demonic\",\"angry\",\"slimy\",\"buzzing\",\"fizzing\",\"hairy\",\"giant\"]\n",
"acc = [\"\", \"q\", \"f\", \"b\", \"fb\"]\n",
"win = [0]\n",
"for i in range(1,101):\n",
" if i%3+i%5 == 0: win.append(\"fb\")\n",
" elif i%5==0: win.append(\"b\")\n",
" elif i%3==0: win.append(\"f\")\n",
" else: win.append(\"\")\n",
"\n",
"def fight(a,r,m):\n",
" while a not in acc: input(\"enter to walk through, f for Fizz, b for Buzz, fb to dual wield: \")\n",
" if a == win[r]:\n",
" if a == \"\": print(\"\\nYou walk through the illusory\",m)\n",
" elif a == \"f\": print(\"\\nYou sword the\", m,\"to death!\")\n",
" elif a == \"b\": print(\"\\nYou stab every single one of the\",m)\n",
" else: print(\"\\nYou dual wield to victory over the\",m)\n",
" return True\n",
" else:\n",
" if a == \"\": print(\"\\nOH NO, turns out the\",m,\"aren't illusions!\")\n",
" else: print(\"\\nYou try to defeat the\", m,\"but are wielding the wrong weapon!\")\n",
" print(\"\\n\\nYOU DIE\\n\")\n",
" return False\n",
"\n",
"#Start game\n",
"\n",
"ask = input(\"Dare you descend into Fizz Dungeon? Y/N: \").lower()\n",
"\n",
"while ask == \"n\": ask = input(\"Too scared? Come on. Play a game y/n? \")\n",
" \n",
"if ask == (\"y\" or \"yes\"): \n",
" print(\"\\nBEWARE! YOU ARE ENTERING FIZZ DUNGEON!\\n\")\n",
"else: \n",
" print(\"\\nFizz dungeon is full of mysteries. \\nYou cannot resist, despite the danger.\\n\")\n",
"\n",
"print(\"Traveler, it is unlikely you will survive FIZZ DUNGEON.\")\n",
"\n",
"a = input(\"ready? \")\n",
"\n",
"while True:\n",
"\n",
" print(\"\\n\\n***FIZZ DUNGEON***\\n\\n\")\n",
"\n",
"\n",
" print(\"You descend into fizz dungeon with naught but your wits and your trusty sword Fizz. \")\n",
" print(\"\\nYou see a small dagger on the ground.\")\n",
"\n",
" input(\"\\nThe dagger seems to have an inscription on it.\")\n",
"\n",
" print(\"\\nIt says \\'Buzz\\' in elven script. You decide to keep it. You can wield it by typing \\'b\\'.\")\n",
"\n",
" a = input(\"\\nDescend to the first floor?\")\n",
" while a == \"b\": a=input(\"you flail around wildly with the dagger.\")\n",
"\n",
" print(\"\\nYou cannot resist the call of the dungeon, and descend into the first floor.\\n\\n1\\n There is a large face suspended in the middle of the room, that seems to mirror your own. You say to yourself: this can't be real!\")\n",
"\n",
" a=input(\"\\nPress enter to walk safely through the illusion: \")\n",
" while a not in acc: a=input(\"just enter, not anything else.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou swing your dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break\n",
" if a == \"f\":\n",
" print(\"\\nYou swing your sword wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break\n",
" \n",
" print(\"\\nAs you step through the illusion, it disperses in a cold fog that makes you shiver to your core.\")\n",
" \n",
" print(\"\\n2\\nYou enter the second room, and two giant eyes stare back at you. They blink when you blink. You realize they are another illusion, of your own eyes.\")\n",
"\n",
" a=input(\"Press enter to walk safely through the illusion: \")\n",
" while a not in acc: a=input(\"just enter, not anything else.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou swing your dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break\n",
" if a == \"f\":\n",
" print(\"\\nYou swing your sword wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break \n",
" if a == \"fb\":\n",
" print(\"\\nYou swing your sword and dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break\n",
" \n",
" print(\"\\nYou made it to room 3!\")\n",
" print(\"You see three skeletons. You can smell their rotting flesh, hear the scrape of their bones.\")\n",
" print(\"Your trusty sword Fizz might be up to the task!\")\n",
" a=input(\"type \\'f\\' to use your sword Fizz: \")\n",
" \n",
" while a not in acc: a=input(\"use enter, f, b, or fb.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou try to sneak up and dagger a skeleton with Buzz, but fail. Another skeleton grabs you and you get eated. \\n\\nYOU DIE\")\n",
" break\n",
" elif a == \"\": \n",
" print(\"\\nYou attempt to walk through the skeletons and they find you a convenient, defenseless snack.\\n\\n YOU DIE\")\n",
" break \n",
" elif a == \"fb\":\n",
" print(\"\\nYou swing your sword and dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" break\n",
" elif a == \"f\":\n",
" print(\"Bones crunch and fly as you gallantly slice apart the three skeletons.\")\n",
" \n",
" room = 4\n",
" while room<101:\n",
" print(room)\n",
" print(\"You made it to room\", room,\"!\")\n",
" mon = adj[random.randint(0, len(adj)-1)] + \" \" + monsters[random.randint(0, len(monsters)-1)]\n",
" print(\"You see\",room,mon,\". How will you get past?\")\n",
" a = input()\n",
" success = fight(a,room,mon)\n",
" if not success: break\n",
" room += 1\n",
" if success: print(\"You have escaped Fizz Dungeon!!!\")\n",
"\n",
"print(\"Thank you for playing!!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 44: Inverse Fizzbuzz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1,101):\n",
" if i%3 == 0 or i%5 == 0: print(i)\n",
" else: print(\"fizzbuzz\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 45: Sets\n",
"\n",
"Let's use some set operations! I want to ampersand something!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numberSet = set()\n",
"fizzSet = set()\n",
"buzzSet = set()\n",
" \n",
"for i in range(1,101): \n",
" numberSet.add(i)\n",
" if i%3 == 0: fizzSet.add(i)\n",
" if i%5 == 0: buzzSet.add(i)\n",
" \n",
"fizzbuzzSet = fizzSet & buzzSet # <---- AMPERSAND SET INTERSECTION! :D\n",
"numberSet -= (fizzSet.union(buzzSet))\n",
"fizzSet -= fizzbuzzSet\n",
"buzzSet -= fizzbuzzSet\n",
"\n",
"setSet = [numberSet,fizzSet,buzzSet,fizzbuzzSet]\n",
"setDict = [0,'fizz','buzz','fizzbuzz']\n",
"\n",
"for i in range(0,101):\n",
" for j in range(4):\n",
" if i in setSet[j]: \n",
" if setDict[j] != 0: print(setDict[j])\n",
" else: print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 46: Venn Fizz Buzz\n",
"\n",
"I do like venndiagrams, and apparently matplotlib has them?\n",
"\n",
"MUST VENN MY FIZZBUZZ\n",
"\n",
"ok I guess this version doesn't come with venns?\n",
"I dunno, can't get itto work. we'll just MAKE OUR OWN AND IT WILL BE BEAUTIFUL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numberSet = set()\n",
"fizzSet = set()\n",
"buzzSet = set()\n",
" \n",
"for i in range(1,101): \n",
" numberSet.add(i)\n",
" if i%3 == 0: fizzSet.add(i)\n",
" if i%5 == 0: buzzSet.add(i)\n",
" \n",
"fizzbuzzSet = fizzSet & buzzSet\n",
"numberSet -= (fizzSet.union(buzzSet))\n",
"fizzSet -= fizzbuzzSet\n",
"buzzSet -= fizzbuzzSet\n",
"\n",
"print(\" \")\n",
"print(\" fizz buzz \")\n",
"print(\" ---------------- ------------------- \")\n",
"print(' / \\\\ / \\\\ ')\n",
"print(' | ',fizzSet)\n",
"print(' | | | |')\n",
"print(' | | | |')\n",
"print(' | |',fizzbuzzSet)\n",
"print(' | | / |')\n",
"print(\" \\\\ /\\\\ \",buzzSet)\n",
"print(\" \\\\ / \\\\ /\")\n",
"print(\" ----------------- -----------------\")\n",
"print(\"\")\n",
"print(numberSet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yes, so beautiful.\n",
"\n",
"Also I clearly don't know a lot about how to python sets because I have no idea how to get anything out of them.\n",
"\n",
"# 47: Do I remember how to dataframe?\n",
"\n",
"apparently not. But this is why we do things 50 times, and also search the same things on stack overflow over and over.\n",
"\n",
"also logic!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"numberData = []\n",
"fizzData = []\n",
"buzzData = []\n",
"fizzbuzzData = []\n",
"\n",
"for i in range(0,101):\n",
" div3 = not bool(i%3)\n",
" div5 = not bool(i%5)\n",
" numberData.append(not div3 or div5)\n",
" fizzData.append(div3 and not div5)\n",
" buzzData.append(not div3 and div5)\n",
" fizzbuzzData.append(div3 and div5) \n",
" \n",
"setFrame = pd.DataFrame({'number':numberData,'fizz':fizzData,'buzz':buzzData,'fizzbuzz':fizzbuzzData})\n",
"setFrame\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 48: Text Adventure with Sound"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's create some sound effects!\n",
"\n",
"I'm going to modify our old synth function and play with new noises:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import numpy as np\n",
"from IPython.display import (\n",
" Audio, display, clear_output)\n",
"\n",
"# create synth stuff:\n",
"\n",
"def play(freqs):\n",
" rate = 16000.\n",
" duration = freqs[0]\n",
" t = np.linspace(\n",
" 0., duration, int(rate * duration))\n",
" x = np.sin(freqs[0] * 2. * np.pi * t)\n",
" for i in range(1,len(freqs)):\n",
" x = np.append(x, np.sin(freqs[i] * 2. * np.pi * t))\n",
" display(Audio(x, rate=rate, autoplay=True))\n",
" \n",
"# Make new sounds!!!\n",
"bling = [0.1,350,450,530,700]\n",
"bloo = [1,200,190,180]\n",
"\n",
"footsteps = [0.006]\n",
"for i in range(5): #number of footsteps\n",
" for i in range(1,31): footsteps.append(random.randint(int(8+100*(1/i)), int(200+ 500*(1/i))))\n",
" for i in range(1,21): footsteps.append(0)\n",
"\n",
"shiny = [0.1,960,1450]\n",
"\n",
"buzzfx = [0.05]\n",
"for i in range(10):\n",
" buzzfx.append(random.randint(100,2000))\n",
" \n",
"fizzfx = [0.05]\n",
"for i in range(5):\n",
" fizzfx.append(200 + 40*(i%2))\n",
" \n",
"enterfx = [0.05]\n",
"for i in range(10):\n",
" enterfx.append(i*100)\n",
"\n",
"play(enterfx)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's integrate some of our new sounds into our dungeon fizz adventure!\n",
"\n",
"# Dungeon Fizz Adventure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import random\n",
"import numpy as np\n",
"from IPython.display import (\n",
" Audio, display, clear_output)\n",
"\n",
"# create synth stuff:\n",
"\n",
"def play(freqs):\n",
" rate = 16000.\n",
" duration = freqs[0]\n",
" t = np.linspace(\n",
" 0., duration, int(rate * duration))\n",
" x = np.sin(freqs[0] * 2. * np.pi * t)\n",
" for i in range(1,len(freqs)):\n",
" x = np.append(x, np.sin(freqs[i] * 2. * np.pi * t))\n",
" display(Audio(x, rate=rate, autoplay=True))\n",
" \n",
"# Create sounds:\n",
"\n",
"bling = [0.1,350,450,530,700]\n",
"bloo = [1,200,190,180]\n",
"shiny = [0.1,960,1450]\n",
"\n",
"footsteps = [0.006]\n",
"for i in range(5): #number of footsteps\n",
" for i in range(1,31): footsteps.append(random.randint(int(8+100*(1/i)), int(200+ 500*(1/i))))\n",
" for i in range(1,21): footsteps.append(0)\n",
" \n",
"audioFuzz = [0.20]\n",
"for i in range(1,75): \n",
" if i%3+i%5 == 0: audioFuzz.append(800)\n",
" elif i%3==0: audioFuzz.append(600)\n",
" elif i%5==0: audioFuzz.append(700)\n",
" else: audioFuzz.append(220+i)\n",
" \n",
"buzzfx = [0.05]\n",
"for i in range(10):\n",
" buzzfx.append(random.randint(100,2000))\n",
"\n",
"# Set up monster types\n",
"\n",
"monsters = [\"bats\",\"bears\",\"dungeon mice\",\"fizz snakes\", \"buzzbees\", \"growlumps\", \"wolfizzes\", \"robots\", \"buzzdemons\", \"fizzfoxes\", \"owls\", \"ligers\", \"spiders\", \"buglets\", \"fleas\", \"brains\"]\n",
"adj = [\"\", \"\", \"evil\",\"sticky\",\"demonic\",\"angry\",\"slimy\",\"wobbly\",\"buzzing\",\"fizzing\",\"hairy\",\"giant\", \"horrifying\", \"growling\", \"drooling\", \"scary\", \"floating\", \"flying\", \"crawling\", \"red\", \"black\", \"purple\"]\n",
"verb = [\"growl\", \"stare\", \"ooze\", \"buzz\", \"fizz\"]\n",
"adv = [\"\", \"\", \"angrily\", \"demonically\", \"agressively\", \"spookily\", \"with contempt\", \"sneakily\"]\n",
"\n",
"#acceptable inputs\n",
"acc = [\"\", \"f\", \"b\", \"fb\", \"q\"]\n",
"\n",
"#fizzbuzz for winning strategy for each level\n",
"win = [0]\n",
"for i in range(1,101):\n",
" if i%3+i%5 == 0: win.append(\"fb\")\n",
" elif i%5==0: win.append(\"b\")\n",
" elif i%3==0: win.append(\"f\")\n",
" else: win.append(\"\")\n",
"\n",
"#Fight on each level. (input, room, monster)\n",
"def fight(a,r,m):\n",
" while a not in acc: \n",
" a = input(\"enter to walk through, f for Fizz, b for Buzz, fb to dual wield, q to quit: \")\n",
" print(\"The\", m, verb[random.randint(0, len(verb)-1)], adv[random.randint(0, len(adv)-1)]) \n",
" if a == \"q\":\n",
" return False\n",
" if a == win[r]:\n",
" play(bling)\n",
" if a == \"\": \n",
" print(\"\\nYou walk through the illusory\",m)\n",
" else:\n",
" if a == \"f\": \n",
" print(\"\\nYou sword the\", m,\"to death!\")\n",
" elif a == \"b\": \n",
" print(\"\\nYou stab every single one of the\",m)\n",
" else: \n",
" print(\"\\nYou dual wield to victory over the\",m)\n",
" return True\n",
" else:\n",
" play(bloo)\n",
" if a == \"\": print(\"\\nOH NO, turns out the\",m,\"aren't illusions!\")\n",
" else: print(\"\\nYou try to defeat the\", m,\"but are wielding the wrong weapon!\")\n",
" return False\n",
" \n",
"# death sequence and ask to play again\n",
"\n",
"def death():\n",
" print(\"\\n\\nYOU DIE\\n\")\n",
" play(bloo)\n",
" a = input(\"Dare you attempt the dungeon again? y/n: \")\n",
" if a.lower() == (\"y\" or \"yes\"): \n",
" descend()\n",
" else:\n",
" return\n",
"\n",
"\n",
"# oh, let's make the meat of the game a function?\n",
"\n",
"def descend(): \n",
"\n",
" print(\"\\n\\n***FIZZ DUNGEON***\\n\\n\")\n",
" \n",
" # The first three rooms are done by hand as a tutorial:\n",
"\n",
" print(\"\\nYou cannot resist the call of the dungeon, and descend into the first floor with your trusty sword Fizz and your new dagger Buzz.\\n\")\n",
" play(footsteps)\n",
" print(\"\\n1\\n There is a large face suspended in the middle of the room, that seems to mirror your own. You say to yourself: this can't be real!\")\n",
"\n",
" a=input(\"\\nPress enter to walk safely through the illusion: \")\n",
" while a not in acc: a=input(\"just enter, not anything else.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou swing your dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"f\":\n",
" print(\"\\nYou swing your sword wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death() \n",
" return\n",
" elif a == \"fb\":\n",
" print(\"\\nYou swing your sword and dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"q\":\n",
" return\n",
" \n",
" play(bling) \n",
" print(\"\\nAs you step through the illusion, it disperses in a cold fog that makes you shiver to your core.\")\n",
" \n",
" print(\"\\n2\\nYou enter the second room, and two giant eyes stare back at you. They blink when you blink. \\n\\nYou realize they are another illusion, of your own eyes.\")\n",
"\n",
" a=input(\"Press enter to walk safely through the illusion: \")\n",
" while a not in acc: a=input(\"just enter, not anything else.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou swing your dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"f\":\n",
" print(\"\\nYou swing your sword wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death() \n",
" return\n",
" elif a == \"fb\":\n",
" print(\"\\nYou swing your sword and dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"q\":\n",
" return\n",
" \n",
" play(bling) \n",
" print(\"\\nYou made it to room 3!\")\n",
" print(\"You see three skeletons. You can smell their rotting flesh, hear the scrape of their bones.\")\n",
" print(\"Your trusty sword Fizz might be up to the task!\")\n",
" a=input(\"type \\'f\\' to use your sword Fizz: \")\n",
" \n",
" while a not in acc: a=input(\"use enter, f, b, or fb.\")\n",
" if a == \"b\":\n",
" print(\"\\nYou try to sneak up and dagger a skeleton with Buzz, but fail. Another skeleton grabs you and you get eated. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"\": \n",
" print(\"\\nYou attempt to walk through the skeletons and they find you a convenient, defenseless snack.\\n\\n YOU DIE\")\n",
" death() \n",
" return\n",
" elif a == \"fb\":\n",
" print(\"\\nYou swing your sword and dagger wildly at the illusion and accidentally stab yourself. \\n\\nYOU DIE\")\n",
" death()\n",
" return\n",
" elif a == \"q\":\n",
" return\n",
" elif a == \"f\":\n",
" print(\"Bones crunch and fly as you gallantly slice apart the three skeletons.\")\n",
" play(bling)\n",
"\n",
" # Here's where we loop over the rooms 4-100 with random monsters\n",
" \n",
" room = 4\n",
" while room<101:\n",
" print(room)\n",
" print(\"You made it to room\", room,\"!\")\n",
" m = adj[random.randint(0, len(adj)-1)] + \" \" + monsters[random.randint(0, len(monsters)-1)]\n",
" print(\"You see\",room,m,\". How will you get past?\")\n",
" print(\"The\", m, verb[random.randint(0, len(verb)-1)], adv[random.randint(0, len(adv)-1)]+\".\") \n",
" a = input()\n",
" success = fight(a,room,m)\n",
" if not success: \n",
" death()\n",
" return\n",
" room += 1\n",
" if success: \n",
" print(\"You have escaped Fizz Dungeon!!!\")\n",
" play(audioFuzz) \n",
" return\n",
" \n",
"#Start game\n",
"\n",
"play(audioFuzz) \n",
"\n",
"ask = input(\"Dare you descend into Fizz Dungeon? Y/N: \").lower()\n",
"\n",
"while ask == \"n\": ask = input(\"Too scared? Come on. Play a game y/n? \")\n",
" \n",
"if ask == (\"y\" or \"yes\"): \n",
" print(\"\\nBEWARE! YOU ARE ENTERING FIZZ DUNGEON!\\n\")\n",
"else: \n",
" print(\"\\nFizz dungeon is full of mysteries. \\nYou cannot resist, despite the danger.\\n\")\n",
"\n",
"print(\"Traveler, it is unlikely you will survive FIZZ DUNGEON.\")\n",
"\n",
"play(footsteps)\n",
"\n",
"a = input(\"ready? \")\n",
"\n",
"while a == (\"n\" or \"no\"): print(\"Just let me know when you're ready. How bout now? \")\n",
"\n",
"play(footsteps)\n",
"print(\"You descend into fizz dungeon with naught but your wits and your trusty sword Fizz. \")\n",
"print(\"\\nYou see a small dagger on the ground.\")\n",
"\n",
"input(\"\\nThe dagger seems to have an inscription on it.\")\n",
"play(shiny)\n",
"print(\"\\nIt says \\'Buzz\\' in elven script. You decide to keep it. You can wield it by typing \\'b\\'.\")\n",
"\n",
"a = input(\"\\nIT IS TIME TO DESCEND TO THE FIRST FLOOR.\")\n",
"while a == \"b\": \n",
" play(buzzfx)\n",
" a=input(\"you flail around wildly with the dagger.\")\n",
"\n",
"descend()\n",
"\n",
"print(\"Thank you for playing!!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A classic for the ages.\n",
"\n",
"Let's scale back a bit though, I feel like our fizzbuzzes are starting to suffer from feature bloat.\n",
"\n",
"# 49: Data Analysis Predictor\n",
"\n",
"Let's forget games and get computer science serious by analyzing the data again. \n",
"\n",
"We'll start with just the first 20 correct outputs as a dataframe, and do dataframe analysises to it!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"fizz20 =pd.DataFrame([1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16, 17, 'fizz', 19, 'buzz'])\n",
"\n",
"fizz20.describe()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, according to data analysis techniques fizzbuzz trends towards 'fizz', and so we can more accurately represent fizzbuzz by leveraging this statistical inevitability:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(100): print(fizz20.mode())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Are those weird extra zeroes because I don't remember how to get just that cell of the dataframe? Or because I don't know the best method to just get the mode without a dataframe around it?\n",
"\n",
"No!\n",
"\n",
"Those are not zeroes, they are fizzy bubbles rising to victory because WE PREDICTED FIZZBUZZ USING DATASCIENCE!!!! \n",
"\n",
"(Innaccuracies you say? Well of course statistical methods are never 100% accurate, they're STATISTICAL. That's what that means. But in the long run we've got to trust the computer, which isn't plagued by biased opinions like \"where's buzz, there should be more buzz\" or \"this just doesn't look right to me\". Your perception is only human, it's not your fault!)\n",
"\n",
"Ok, it is finally time for:\n",
"\n",
"# 50: The Fizzbuzz To End All Fizzbuzzes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lamentationPre = [\"Goodbye \", 'I cry at the thought this could be the last ', \"Alas! \", \"We weep for \", 'Ah, what a beautiful ',\"Rest in peace, dear \", \"Like a butterfly emerged too early in spring appears \", \"God have mercy on \"]\n",
"lamentationSuf = [\", farewell.\", \", life is so fragile!\", \", alas! alas!\", \". How could the universe allow this to happen?\", \", you were loved.\", \". Why? Why?\", \", such is the human condition.\", \", a bubble in the stream.\", \", we will try to carry on nonetheless.\"]\n",
"for i in range(1,101):\n",
" print(\n",
" lamentationPre[i%len(lamentationPre)] +\n",
" str(i)*bool(i%3*i%5) +\n",
" 'Fizz'*bool(not i%3) +\n",
" 'Buzz'*bool(not i%5) +\n",
" lamentationSuf[i%len(lamentationSuf)] +\n",
" \"\\n\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}