Skip to content

Commit 903b8e0

Browse files
committed
Chapter programs, snippets and solutions to Challenges
1 parent ed34054 commit 903b8e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6861
-0
lines changed

chapter1/Chapter1.html

Lines changed: 620 additions & 0 deletions
Large diffs are not rendered by default.

chapter1/Chapter1.ipynb

Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"##Snippets and Programs from Chapter 1: Numbers\n",
8+
"\n"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 15,
14+
"metadata": {
15+
"collapsed": false
16+
},
17+
"outputs": [
18+
{
19+
"data": {
20+
"text/plain": [
21+
"Fraction(3, 4)"
22+
]
23+
},
24+
"execution_count": 15,
25+
"metadata": {},
26+
"output_type": "execute_result"
27+
}
28+
],
29+
"source": [
30+
"# P5: fractions demo\n",
31+
">>> from fractions import Fraction\n",
32+
">>> f = Fraction(3, 4)\n",
33+
"f"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 16,
39+
"metadata": {
40+
"collapsed": false
41+
},
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"Enter a complex number: 2+3j\n"
48+
]
49+
},
50+
{
51+
"data": {
52+
"text/plain": [
53+
"(2+3j)"
54+
]
55+
},
56+
"execution_count": 16,
57+
"metadata": {},
58+
"output_type": "execute_result"
59+
}
60+
],
61+
"source": [
62+
"#P12: Complex number input\n",
63+
">>> z = complex(input('Enter a complex number: ')) #2+3j is valid, 2 + 3j is invalid\n",
64+
"z"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 19,
70+
"metadata": {
71+
"collapsed": false
72+
},
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"True\n",
79+
"False\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"#P12: Checking if a number is a factor of another\n",
85+
"def is_factor(a, b):\n",
86+
" if b % a == 0:\n",
87+
" return True\n",
88+
" else:\n",
89+
" return False\n",
90+
"\n",
91+
"# try it out\n",
92+
"print(is_factor(4, 1024))\n",
93+
"print(is_factor(4, 21))"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 1,
99+
"metadata": {
100+
"collapsed": false
101+
},
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"Your Number Please: 25\n",
108+
"1\n",
109+
"5\n",
110+
"25\n"
111+
]
112+
}
113+
],
114+
"source": [
115+
"#P14: Find the factors of an integer\n",
116+
"'''\n",
117+
"Find the factors of an integer\n",
118+
"'''\n",
119+
"def factors(a):\n",
120+
" for i in range(1, a+1):\n",
121+
" if a % i == 0:\n",
122+
" print(i)\n",
123+
" \n",
124+
"if __name__ == '__main__':\n",
125+
" a = input('Your Number Please: ')\n",
126+
" a = float(a)\n",
127+
" if a > 0 and a.is_integer():\n",
128+
" factors(int(a))\n",
129+
" else:\n",
130+
" print('Please enter a positive integer')\n",
131+
" "
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 14,
137+
"metadata": {
138+
"collapsed": false
139+
},
140+
"outputs": [
141+
{
142+
"name": "stdout",
143+
"output_type": "stream",
144+
"text": [
145+
"At the grocery store, I bought some apples and bananas and grapes\n"
146+
]
147+
}
148+
],
149+
"source": [
150+
"#P15: Format example\n",
151+
"\n",
152+
">>> item1 = 'apples'\n",
153+
">>> item2 = 'bananas'\n",
154+
">>> item3 = 'grapes'\n",
155+
">>> print('At the grocery store, I bought some {0} and {1} and {2}'.format(item1, item2, item3))\n"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 20,
161+
"metadata": {
162+
"collapsed": false
163+
},
164+
"outputs": [
165+
{
166+
"name": "stdout",
167+
"output_type": "stream",
168+
"text": [
169+
"Enter a number: 5\n",
170+
"5.0 x 1 = 5.0\n",
171+
"5.0 x 2 = 10.0\n",
172+
"5.0 x 3 = 15.0\n",
173+
"5.0 x 4 = 20.0\n",
174+
"5.0 x 5 = 25.0\n",
175+
"5.0 x 6 = 30.0\n",
176+
"5.0 x 7 = 35.0\n",
177+
"5.0 x 8 = 40.0\n",
178+
"5.0 x 9 = 45.0\n",
179+
"5.0 x 10 = 50.0\n"
180+
]
181+
}
182+
],
183+
"source": [
184+
"#P16: Multiplication table printer\n",
185+
"\n",
186+
"'''\n",
187+
"Multiplication table printer\n",
188+
"'''\n",
189+
"def multi_table(a):\n",
190+
" for i in range(1, 11):\n",
191+
" print('{0} x {1} = {2}'.format(a, i, a*i))\n",
192+
"\n",
193+
"if __name__ == '__main__':\n",
194+
" a = input('Enter a number: ')\n",
195+
" multi_table(float(a))"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 29,
201+
"metadata": {
202+
"collapsed": false
203+
},
204+
"outputs": [
205+
{
206+
"data": {
207+
"text/plain": [
208+
"37.0"
209+
]
210+
},
211+
"execution_count": 29,
212+
"metadata": {},
213+
"output_type": "execute_result"
214+
}
215+
],
216+
"source": [
217+
"#P18: Fahrenheit to Celsius conversion\n",
218+
">>> F = 98.6\n",
219+
">>> (F - 32) * (5 / 9)\n"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 32,
225+
"metadata": {
226+
"collapsed": false
227+
},
228+
"outputs": [
229+
{
230+
"data": {
231+
"text/plain": [
232+
"98.60000000000001"
233+
]
234+
},
235+
"execution_count": 32,
236+
"metadata": {},
237+
"output_type": "execute_result"
238+
}
239+
],
240+
"source": [
241+
"#P18: Celsius to Fahrenheit\n",
242+
">>> C = 37\n",
243+
">>> C * (9 / 5) + 32\n"
244+
]
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": 35,
249+
"metadata": {
250+
"collapsed": false
251+
},
252+
"outputs": [
253+
{
254+
"name": "stdout",
255+
"output_type": "stream",
256+
"text": [
257+
"1. Kilometers to Miles\n",
258+
"2. Miles to Kilometers\n",
259+
"Which conversion would you like to do?: 2\n",
260+
"Enter distance in miles: 100\n",
261+
"Distance in kilometers: 160.9\n"
262+
]
263+
}
264+
],
265+
"source": [
266+
"#P19: Unit conversion\n",
267+
"\n",
268+
"'''\n",
269+
"Unit converter: Miles and Kilometers\n",
270+
"'''\n",
271+
"def print_menu():\n",
272+
" print('1. Kilometers to Miles')\n",
273+
" print('2. Miles to Kilometers')\n",
274+
" \n",
275+
"def km_miles():\n",
276+
" km = float(input('Enter distance in kilometers: '))\n",
277+
" miles = km / 1.609\n",
278+
" print('Distance in miles: {0}'.format(miles))\n",
279+
"\n",
280+
"def miles_km():\n",
281+
" miles = float(input('Enter distance in miles: '))\n",
282+
" km = miles * 1.609\n",
283+
" print('Distance in kilometers: {0}'.format(km))\n",
284+
"\n",
285+
"if __name__ == '__main__':\n",
286+
" print_menu()\n",
287+
" choice = input('Which conversion would you like to do?: ')\n",
288+
" if choice == '1':\n",
289+
" km_miles()\n",
290+
" if choice == '2':\n",
291+
" miles_km()\n"
292+
]
293+
},
294+
{
295+
"cell_type": "code",
296+
"execution_count": 38,
297+
"metadata": {
298+
"collapsed": false
299+
},
300+
"outputs": [
301+
{
302+
"name": "stdout",
303+
"output_type": "stream",
304+
"text": [
305+
"-1.0\n",
306+
"-1.0\n"
307+
]
308+
}
309+
],
310+
"source": [
311+
"#P21: Roots of a quadratic equation example\n",
312+
">>> a = 1\n",
313+
">>> b = 2\n",
314+
">>> c = 1\n",
315+
">>> D = (b**2 - 4*a*c)**0.5\n",
316+
">>> x_1 = (-b + D)/(2*a)\n",
317+
">>> print(x_1)\n",
318+
">>> x_2 = (-b - D)/(2*a)\n",
319+
">>> print(x_2)\n"
320+
]
321+
},
322+
{
323+
"cell_type": "code",
324+
"execution_count": 40,
325+
"metadata": {
326+
"collapsed": false
327+
},
328+
"outputs": [
329+
{
330+
"name": "stdout",
331+
"output_type": "stream",
332+
"text": [
333+
"Enter a: 1\n",
334+
"Enter b: 2\n",
335+
"Enter c: 1\n",
336+
"x1: -1.0\n",
337+
"x2: -1.0\n"
338+
]
339+
}
340+
],
341+
"source": [
342+
"#P21: Quadratic Equation Root calculator\n",
343+
"'''\n",
344+
"Quadratic Equation root calculator\n",
345+
"'''\n",
346+
"def roots(a, b, c):\n",
347+
" D = (b*b - 4*a*c)**0.5\n",
348+
" x_1 = (-b + D)/(2*a)\n",
349+
" x_2 = (-b - D)/(2*a)\n",
350+
" print('x1: {0}'.format(x_1))\n",
351+
" print('x2: {0}'.format(x_2))\n",
352+
"\n",
353+
"if __name__ == '__main__':\n",
354+
" a = input('Enter a: ')\n",
355+
" b = input('Enter b: ')\n",
356+
" c = input('Enter c: ')\n",
357+
" roots(float(a), float(b), float(c))\n"
358+
]
359+
}
360+
],
361+
"metadata": {
362+
"kernelspec": {
363+
"display_name": "Python 3",
364+
"language": "python",
365+
"name": "python3"
366+
},
367+
"language_info": {
368+
"codemirror_mode": {
369+
"name": "ipython",
370+
"version": 3
371+
},
372+
"file_extension": ".py",
373+
"mimetype": "text/x-python",
374+
"name": "python",
375+
"nbconvert_exporter": "python",
376+
"pygments_lexer": "ipython3",
377+
"version": "3.4.3"
378+
}
379+
},
380+
"nbformat": 4,
381+
"nbformat_minor": 0
382+
}

0 commit comments

Comments
 (0)