Skip to content

Commit eba9845

Browse files
authored
Add fizz buzz challenge (donnemartin#149)
1 parent 5acef10 commit eba9845

File tree

5 files changed

+477
-0
lines changed

5 files changed

+477
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup
123123
| Compress a string | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_challenge.ipynb)[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_solution.ipynb) |
124124
| Reverse characters in a string | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_challenge.ipynb)[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_solution.ipynb) |
125125
| Implement a hash table | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_challenge.ipynb)[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_solution.ipynb) |
126+
| Implement fizz buzz | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/fizz_buzz/fizz_buzz_challenge.ipynb)[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/fizz_buzz/fizz_buzz_solution.ipynb) |
126127
| Find the first non-repeated character in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) |
127128
| Remove specified characters in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) |
128129
| Reverse words in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) |

arrays_strings/fizz_buzz/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Challenge Notebook"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Problem: Implement Fizz Buzz.\n",
22+
"\n",
23+
"* [Constraints](#Constraints)\n",
24+
"* [Test Cases](#Test-Cases)\n",
25+
"* [Algorithm](#Algorithm)\n",
26+
"* [Code](#Code)\n",
27+
"* [Unit Test](#Unit-Test)\n",
28+
"* [Solution Notebook](#Solution-Notebook)"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"## Constraints\n",
36+
"\n",
37+
"* What is fizz buzz?\n",
38+
" * Return the string representation of numbers from 1 to n\n",
39+
" * Multiples of 3 -> 'Fizz'\n",
40+
" * Multiples of 5 -> 'Buzz'\n",
41+
" * Multiples of 3 and 5 -> 'FizzBuzz'\n",
42+
"* Can we assume the inputs are valid?\n",
43+
" * No\n",
44+
"* Can we assume this fits memory?\n",
45+
" * Yes"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"## Test Cases\n",
53+
"\n",
54+
"<pre>\n",
55+
"* None -> Exception\n",
56+
"* < 1 -> Exception\n",
57+
"* 15 ->\n",
58+
"[\n",
59+
" '1',\n",
60+
" '2',\n",
61+
" 'Fizz',\n",
62+
" '4',\n",
63+
" 'Buzz',\n",
64+
" 'Fizz',\n",
65+
" '7',\n",
66+
" '8',\n",
67+
" 'Fizz',\n",
68+
" 'Buzz',\n",
69+
" '11',\n",
70+
" 'Fizz',\n",
71+
" '13',\n",
72+
" '14',\n",
73+
" 'FizzBuzz'\n",
74+
"]\n",
75+
"</pre>"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"## Algorithm\n",
83+
"\n",
84+
"Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/fizz_buzz/fizz_buzz_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"## Code"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {
98+
"collapsed": false
99+
},
100+
"outputs": [],
101+
"source": [
102+
"class Solution(object):\n",
103+
"\n",
104+
" def fizz_buzz(self, num):\n",
105+
" # TODO: Implement me\n",
106+
" pass"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"## Unit Test"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"**The following unit test is expected to fail until you solve the challenge.**"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": null,
126+
"metadata": {
127+
"collapsed": false
128+
},
129+
"outputs": [],
130+
"source": [
131+
"# %load test_fizz_buzz.py\n",
132+
"from nose.tools import assert_equal, assert_raises\n",
133+
"\n",
134+
"\n",
135+
"class TestFizzBuzz(object):\n",
136+
"\n",
137+
" def test_fizz_buzz(self):\n",
138+
" solution = Solution()\n",
139+
" assert_raises(TypeError, solution.fizz_buzz, None)\n",
140+
" assert_raises(ValueError, solution.fizz_buzz, 0)\n",
141+
" expected = [\n",
142+
" '1',\n",
143+
" '2',\n",
144+
" 'Fizz',\n",
145+
" '4',\n",
146+
" 'Buzz',\n",
147+
" 'Fizz',\n",
148+
" '7',\n",
149+
" '8',\n",
150+
" 'Fizz',\n",
151+
" 'Buzz',\n",
152+
" '11',\n",
153+
" 'Fizz',\n",
154+
" '13',\n",
155+
" '14',\n",
156+
" 'FizzBuzz'\n",
157+
" ]\n",
158+
" assert_equal(solution.fizz_buzz(15), expected)\n",
159+
" print('Success: test_fizz_buzz')\n",
160+
"\n",
161+
"\n",
162+
"def main():\n",
163+
" test = TestFizzBuzz()\n",
164+
" test.test_fizz_buzz()\n",
165+
"\n",
166+
"\n",
167+
"if __name__ == '__main__':\n",
168+
" main()"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"metadata": {},
174+
"source": [
175+
"## Solution Notebook\n",
176+
"\n",
177+
"Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/fizz_buzz/fizz_buzz_solution.ipynb) for a discussion on algorithms and code solutions."
178+
]
179+
}
180+
],
181+
"metadata": {
182+
"kernelspec": {
183+
"display_name": "Python 3",
184+
"language": "python",
185+
"name": "python3"
186+
},
187+
"language_info": {
188+
"codemirror_mode": {
189+
"name": "ipython",
190+
"version": 3
191+
},
192+
"file_extension": ".py",
193+
"mimetype": "text/x-python",
194+
"name": "python",
195+
"nbconvert_exporter": "python",
196+
"pygments_lexer": "ipython3",
197+
"version": "3.4.3"
198+
}
199+
},
200+
"nbformat": 4,
201+
"nbformat_minor": 0
202+
}

0 commit comments

Comments
 (0)