Skip to content

Commit 4a53f12

Browse files
committed
7
1 parent 3041b51 commit 4a53f12

File tree

4 files changed

+344
-10
lines changed

4 files changed

+344
-10
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}

.ipynb_checkpoints/Week4_Ligang_Zhang-checkpoint.ipynb

Lines changed: 145 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@
131131
},
132132
{
133133
"cell_type": "code",
134-
"execution_count": 6,
134+
"execution_count": 7,
135135
"metadata": {},
136136
"outputs": [
137137
{
138138
"ename": "SyntaxError",
139-
"evalue": "invalid syntax (<ipython-input-6-da6edc7350c1>, line 4)",
139+
"evalue": "invalid syntax (<ipython-input-7-70de1be1fcb7>, line 4)",
140140
"output_type": "error",
141141
"traceback": [
142-
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-6-da6edc7350c1>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
142+
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-7-70de1be1fcb7>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
143143
]
144144
}
145145
],
@@ -149,17 +149,155 @@
149149
"Type = [Pickup, SUV, Sedan, Sedan, Sedan, SUV, SUV, SUV, Pickup, Pickup]\n",
150150
"Accidents each year = [25, 79, 46, 90, 29, 88, 79, 93, 20, 11]\n",
151151
"#Find the brand and model of the safest car\n",
152-
"print(\"a\", min(Accidents each year))"
152+
"min(Accidents each year)"
153+
]
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"5. Classify daily temperatures (in degrees Celsius) stored in the temperatures list belowinto four different classes:\n"
153160
]
154161
},
155162
{
156163
"cell_type": "code",
157-
"execution_count": null,
164+
"execution_count": 22,
158165
"metadata": {},
159-
"outputs": [],
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"[-5.4, -4.8, -4.4, -2.2, -3.9, -2.5, -4.6, -2.4, -3.3, -4.8, -2.8, -4.7, -5.6, -2.7, -4.6]\n",
172+
"[1.0, -1.3, 0.1, 1.9, 1.0, -0.8, -0.1, -0.4, -0.9, 1.6, 1.8, 1.6, 1.5, 1.4, 1.6, -0.0]\n",
173+
"[3.9, 4.0, 4.4, 5.1, 2.1, 2.6, 3.4, 3.1, 2.4, 4.2, 3.5, 2.6, 3.1, 2.2, 3.3, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8, 4.0, 8.6, 4.1, 8.9, 3.0, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]\n",
174+
"[]\n",
175+
"-3.913333333333333\n",
176+
"0.625\n",
177+
"5.018644067796609\n"
178+
]
179+
}
180+
],
160181
"source": [
161-
"\n"
182+
"temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4, -2.5, -4.6, \n",
183+
" 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8, -0.1, -4.7, -5.6, 2.6,\n",
184+
"-2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4, 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3,\n",
185+
"1.6, 1.5, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8,\n",
186+
"4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2,\n",
187+
"11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, -0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6,\n",
188+
"5.2]\n",
189+
"cold = [n for n in temperatures if n <= -2]\n",
190+
"slippery = [n for n in temperatures if n <= 2 and n > -2]\n",
191+
"comfortable = [n for n in temperatures if n > 2 and n <= 15]\n",
192+
"warm = [n for n in temperatures if n > 15]\n",
193+
"print(cold)\n",
194+
"print(slippery)\n",
195+
"print(comfortable)\n",
196+
"print(warm)\n",
197+
"def ave(num):\n",
198+
" nsum = 0\n",
199+
" for i in range(len(num)):\n",
200+
" nsum += num[i]\n",
201+
" return nsum / len(num)\n",
202+
"print(ave(cold))\n",
203+
"print(ave(slippery))\n",
204+
"print(ave(comfortable))\n"
162205
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"6. Given a string, find the length of the longest substring without repeating characters."
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": 36,
217+
"metadata": {
218+
"scrolled": true
219+
},
220+
"outputs": [
221+
{
222+
"ename": "IndentationError",
223+
"evalue": "unindent does not match any outer indentation level (<tokenize>, line 17)",
224+
"output_type": "error",
225+
"traceback": [
226+
"\u001b[1;36m File \u001b[1;32m\"<tokenize>\"\u001b[1;36m, line \u001b[1;32m17\u001b[0m\n\u001b[1;33m pos[string[i]] = i\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
227+
]
228+
}
229+
],
230+
"source": [
231+
"def findLongestSubstring(string): \n",
232+
" n = len(string) \n",
233+
" st = 0\n",
234+
" maxlen = 0\n",
235+
" start = 0\n",
236+
" pos = {} \n",
237+
" pos[string[0]] = 0\n",
238+
" for i in range(1, n): \n",
239+
" if string[i] not in pos: \n",
240+
" pos[string[i]] = i \n",
241+
" else: \n",
242+
" currlen = i - st \n",
243+
" if maxlen < currlen: \n",
244+
" maxlen = currlen \n",
245+
" start = st \n",
246+
" st = pos[string[i]] + 1\n",
247+
" pos[string[i]] = i \n",
248+
" if maxlen < i - st: \n",
249+
" maxlen = i - st \n",
250+
" start = st \n",
251+
" return string[start : start + maxlen] \n",
252+
"findLongestSubstring(\"hhhhhasdhhha\"){}"
253+
]
254+
},
255+
{
256+
"cell_type": "markdown",
257+
"metadata": {},
258+
"source": [
259+
"7. Given an array nums of n integers, are there elements a, b, c in nums such that a + b +\n",
260+
"c = 0? Find all unique triplets in the array which gives the sum of zero. The solution set\n",
261+
"must not contain duplicate triplets."
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": 38,
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"0 -1 1\n",
274+
"2 -3 1\n"
275+
]
276+
}
277+
],
278+
"source": [
279+
"def findTriplets(arr, n): \n",
280+
" found = True\n",
281+
" for i in range(0, n-2): \n",
282+
" for j in range(i+1, n-1): \n",
283+
" for k in range(j+1, n): \n",
284+
" if (arr[i] + arr[j] + arr[k] == 0): \n",
285+
" print(arr[i], arr[j], arr[k]) \n",
286+
" found = True\n",
287+
" if (found == False): \n",
288+
" print(\" not exist \") \n",
289+
"\n",
290+
"arr = [0, -1, 2, -3, 1] \n",
291+
"n = len(arr) \n",
292+
"findTriplets(arr, n) "
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": []
163301
}
164302
],
165303
"metadata": {

Untitled2.ipynb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 18,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"'hello$$$'"
12+
]
13+
},
14+
"execution_count": 18,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"\"{:$<8s}\".format(\"hello\")"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": []
29+
}
30+
],
31+
"metadata": {
32+
"kernelspec": {
33+
"display_name": "Python 3",
34+
"language": "python",
35+
"name": "python3"
36+
},
37+
"language_info": {
38+
"codemirror_mode": {
39+
"name": "ipython",
40+
"version": 3
41+
},
42+
"file_extension": ".py",
43+
"mimetype": "text/x-python",
44+
"name": "python",
45+
"nbconvert_exporter": "python",
46+
"pygments_lexer": "ipython3",
47+
"version": "3.7.4"
48+
}
49+
},
50+
"nbformat": 4,
51+
"nbformat_minor": 2
52+
}

Week4_Ligang_Zhang.ipynb

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,152 @@
152152
"min(Accidents each year)"
153153
]
154154
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"5. Classify daily temperatures (in degrees Celsius) stored in the temperatures list belowinto four different classes:\n"
160+
]
161+
},
155162
{
156163
"cell_type": "code",
157-
"execution_count": null,
164+
"execution_count": 22,
158165
"metadata": {},
159-
"outputs": [],
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"[-5.4, -4.8, -4.4, -2.2, -3.9, -2.5, -4.6, -2.4, -3.3, -4.8, -2.8, -4.7, -5.6, -2.7, -4.6]\n",
172+
"[1.0, -1.3, 0.1, 1.9, 1.0, -0.8, -0.1, -0.4, -0.9, 1.6, 1.8, 1.6, 1.5, 1.4, 1.6, -0.0]\n",
173+
"[3.9, 4.0, 4.4, 5.1, 2.1, 2.6, 3.4, 3.1, 2.4, 4.2, 3.5, 2.6, 3.1, 2.2, 3.3, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8, 4.0, 8.6, 4.1, 8.9, 3.0, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]\n",
174+
"[]\n",
175+
"-3.913333333333333\n",
176+
"0.625\n",
177+
"5.018644067796609\n"
178+
]
179+
}
180+
],
160181
"source": [
161-
"\n"
182+
"temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4, -2.5, -4.6, \n",
183+
" 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8, -0.1, -4.7, -5.6, 2.6,\n",
184+
"-2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4, 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3,\n",
185+
"1.6, 1.5, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8,\n",
186+
"4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2,\n",
187+
"11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6, -0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6,\n",
188+
"5.2]\n",
189+
"cold = [n for n in temperatures if n <= -2]\n",
190+
"slippery = [n for n in temperatures if n <= 2 and n > -2]\n",
191+
"comfortable = [n for n in temperatures if n > 2 and n <= 15]\n",
192+
"warm = [n for n in temperatures if n > 15]\n",
193+
"print(cold)\n",
194+
"print(slippery)\n",
195+
"print(comfortable)\n",
196+
"print(warm)\n",
197+
"def ave(num):\n",
198+
" nsum = 0\n",
199+
" for i in range(len(num)):\n",
200+
" nsum += num[i]\n",
201+
" return nsum / len(num)\n",
202+
"print(ave(cold))\n",
203+
"print(ave(slippery))\n",
204+
"print(ave(comfortable))\n"
162205
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"6. Given a string, find the length of the longest substring without repeating characters."
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": 36,
217+
"metadata": {
218+
"scrolled": true
219+
},
220+
"outputs": [
221+
{
222+
"ename": "IndentationError",
223+
"evalue": "unindent does not match any outer indentation level (<tokenize>, line 17)",
224+
"output_type": "error",
225+
"traceback": [
226+
"\u001b[1;36m File \u001b[1;32m\"<tokenize>\"\u001b[1;36m, line \u001b[1;32m17\u001b[0m\n\u001b[1;33m pos[string[i]] = i\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
227+
]
228+
}
229+
],
230+
"source": [
231+
"def findLongestSubstring(string): \n",
232+
" n = len(string) \n",
233+
" st = 0\n",
234+
" maxlen = 0\n",
235+
" start = 0\n",
236+
" pos = {} \n",
237+
" pos[string[0]] = 0\n",
238+
" for i in range(1, n): \n",
239+
" if string[i] not in pos: \n",
240+
" pos[string[i]] = i \n",
241+
" else: \n",
242+
" currlen = i - st \n",
243+
" if maxlen < currlen: \n",
244+
" maxlen = currlen \n",
245+
" start = st \n",
246+
" st = pos[string[i]] + 1\n",
247+
" pos[string[i]] = i \n",
248+
" if maxlen < i - st: \n",
249+
" maxlen = i - st \n",
250+
" start = st \n",
251+
" return string[start : start + maxlen] \n",
252+
"findLongestSubstring(\"hhhhhasdhhha\"){}"
253+
]
254+
},
255+
{
256+
"cell_type": "markdown",
257+
"metadata": {},
258+
"source": [
259+
"7. Given an array nums of n integers, are there elements a, b, c in nums such that a + b +\n",
260+
"c = 0? Find all unique triplets in the array which gives the sum of zero. The solution set\n",
261+
"must not contain duplicate triplets."
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": 38,
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"0 -1 1\n",
274+
"2 -3 1\n"
275+
]
276+
}
277+
],
278+
"source": [
279+
"def findTriplets(arr, n): \n",
280+
" found = True\n",
281+
" for i in range(0, n-2): \n",
282+
" for j in range(i+1, n-1): \n",
283+
" for k in range(j+1, n): \n",
284+
" if (arr[i] + arr[j] + arr[k] == 0): \n",
285+
" print(arr[i], arr[j], arr[k]) \n",
286+
" found = True\n",
287+
" if (found == False): \n",
288+
" print(\" not exist \") \n",
289+
"\n",
290+
"arr = [0, -1, 2, -3, 1] \n",
291+
"n = len(arr) \n",
292+
"findTriplets(arr, n) "
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": []
163301
}
164302
],
165303
"metadata": {

0 commit comments

Comments
 (0)