Skip to content

Commit d19983e

Browse files
committed
done chapter 03 > lecture 07
1 parent 2e04013 commit d19983e

File tree

4 files changed

+448
-12
lines changed

4 files changed

+448
-12
lines changed
Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Functions and Methods Homework \n",
8+
"\n",
9+
"Complete the following questions:\n",
10+
"____\n",
11+
"**Write a function that computes the volume of a sphere given its radius.**\n",
12+
"<p>The volume of a sphere is given as $$\\frac{4}{3} πr^3$$</p>"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"def vol(rad):\n",
22+
" pass"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"data": {
32+
"text/plain": [
33+
"33.49333333333333"
34+
]
35+
},
36+
"execution_count": 2,
37+
"metadata": {},
38+
"output_type": "execute_result"
39+
}
40+
],
41+
"source": [
42+
"# Check\n",
43+
"vol(2)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"___\n",
51+
"**Write a function that checks whether a number is in a given range (inclusive of high and low)**"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"def ran_check(num,low,high):\n",
61+
" pass"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 4,
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": [
73+
"5 is in the range between 2 and 7\n"
74+
]
75+
}
76+
],
77+
"source": [
78+
"# Check\n",
79+
"ran_check(5,2,7)"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"If you only wanted to return a boolean:"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 5,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"def ran_bool(num,low,high):\n",
96+
" pass"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 6,
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"data": {
106+
"text/plain": [
107+
"True"
108+
]
109+
},
110+
"execution_count": 6,
111+
"metadata": {},
112+
"output_type": "execute_result"
113+
}
114+
],
115+
"source": [
116+
"ran_bool(3,1,10)"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"____\n",
124+
"**Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**\n",
125+
"\n",
126+
" Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n",
127+
" Expected Output : \n",
128+
" No. of Upper case characters : 4\n",
129+
" No. of Lower case Characters : 33\n",
130+
"\n",
131+
"HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n",
132+
"\n",
133+
"If you feel ambitious, explore the Collections module to solve this problem!"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 7,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"def up_low(s):\n",
143+
" pass"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 8,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"Original String : Hello Mr. Rogers, how are you this fine Tuesday?\n",
156+
"No. of Upper case characters : 4\n",
157+
"No. of Lower case Characters : 33\n"
158+
]
159+
}
160+
],
161+
"source": [
162+
"s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n",
163+
"up_low(s)"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"____\n",
171+
"**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n",
172+
"\n",
173+
" Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n",
174+
" Unique List : [1, 2, 3, 4, 5]"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": 9,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"def unique_list(lst):\n",
184+
" pass"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 10,
190+
"metadata": {},
191+
"outputs": [
192+
{
193+
"data": {
194+
"text/plain": [
195+
"[1, 2, 3, 4, 5]"
196+
]
197+
},
198+
"execution_count": 10,
199+
"metadata": {},
200+
"output_type": "execute_result"
201+
}
202+
],
203+
"source": [
204+
"unique_list([1,1,1,1,2,2,3,3,3,3,4,5])"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"____\n",
212+
"**Write a Python function to multiply all the numbers in a list.**\n",
213+
"\n",
214+
" Sample List : [1, 2, 3, -4]\n",
215+
" Expected Output : -24"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 11,
221+
"metadata": {},
222+
"outputs": [],
223+
"source": [
224+
"def multiply(numbers): \n",
225+
" pass"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 12,
231+
"metadata": {},
232+
"outputs": [
233+
{
234+
"data": {
235+
"text/plain": [
236+
"-24"
237+
]
238+
},
239+
"execution_count": 12,
240+
"metadata": {},
241+
"output_type": "execute_result"
242+
}
243+
],
244+
"source": [
245+
"multiply([1,2,3,-4])"
246+
]
247+
},
248+
{
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"____\n",
253+
"**Write a Python function that checks whether a passed in string is palindrome or not.**\n",
254+
"\n",
255+
"Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run."
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": 13,
261+
"metadata": {},
262+
"outputs": [],
263+
"source": [
264+
"def palindrome(s):\n",
265+
" pass"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 14,
271+
"metadata": {},
272+
"outputs": [
273+
{
274+
"data": {
275+
"text/plain": [
276+
"True"
277+
]
278+
},
279+
"execution_count": 14,
280+
"metadata": {},
281+
"output_type": "execute_result"
282+
}
283+
],
284+
"source": [
285+
"palindrome('helleh')"
286+
]
287+
},
288+
{
289+
"cell_type": "markdown",
290+
"metadata": {},
291+
"source": [
292+
"____\n",
293+
"#### Hard:\n",
294+
"\n",
295+
"**Write a Python function to check whether a string is pangram or not.**\n",
296+
"\n",
297+
" Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n",
298+
" For example : \"The quick brown fox jumps over the lazy dog\"\n",
299+
"\n",
300+
"Hint: Look at the string module"
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": 15,
306+
"metadata": {},
307+
"outputs": [],
308+
"source": [
309+
"import string\n",
310+
"\n",
311+
"def ispangram(str1, alphabet=string.ascii_lowercase):\n",
312+
" pass"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": 16,
318+
"metadata": {},
319+
"outputs": [
320+
{
321+
"data": {
322+
"text/plain": [
323+
"True"
324+
]
325+
},
326+
"execution_count": 16,
327+
"metadata": {},
328+
"output_type": "execute_result"
329+
}
330+
],
331+
"source": [
332+
"ispangram(\"The quick brown fox jumps over the lazy dog\")"
333+
]
334+
},
335+
{
336+
"cell_type": "code",
337+
"execution_count": 17,
338+
"metadata": {},
339+
"outputs": [
340+
{
341+
"data": {
342+
"text/plain": [
343+
"'abcdefghijklmnopqrstuvwxyz'"
344+
]
345+
},
346+
"execution_count": 17,
347+
"metadata": {},
348+
"output_type": "execute_result"
349+
}
350+
],
351+
"source": [
352+
"string.ascii_lowercase"
353+
]
354+
},
355+
{
356+
"cell_type": "markdown",
357+
"metadata": {
358+
"collapsed": true
359+
},
360+
"source": [
361+
"#### Great Job!"
362+
]
363+
}
364+
],
365+
"metadata": {
366+
"kernelspec": {
367+
"display_name": "Python 3",
368+
"language": "python",
369+
"name": "python3"
370+
},
371+
"language_info": {
372+
"codemirror_mode": {
373+
"name": "ipython",
374+
"version": 3
375+
},
376+
"file_extension": ".py",
377+
"mimetype": "text/x-python",
378+
"name": "python",
379+
"nbconvert_exporter": "python",
380+
"pygments_lexer": "ipython3",
381+
"version": "3.6.3"
382+
}
383+
},
384+
"nbformat": 4,
385+
"nbformat_minor": 1
386+
}

0 commit comments

Comments
 (0)