Skip to content

Commit 3368e92

Browse files
hash maps
1 parent 3034b38 commit 3368e92

File tree

2 files changed

+744
-0
lines changed

2 files changed

+744
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# List or Array data structure - O(n)"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"stock_prices = []\n",
17+
"with open(\"stock_prices.csv\", \"r\") as f:\n",
18+
" for line in f:\n",
19+
" tokens = line.split(',')\n",
20+
" day = tokens[0]\n",
21+
" price = float(tokens[1])\n",
22+
" stock_prices.append([day, price])"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"data": {
32+
"text/plain": [
33+
"[['march 6', 310.0],\n",
34+
" ['march 7', 340.0],\n",
35+
" ['march 8', 380.0],\n",
36+
" ['march 9', 302.0],\n",
37+
" ['march 10', 297.0],\n",
38+
" ['march 11', 323.0]]"
39+
]
40+
},
41+
"execution_count": 2,
42+
"metadata": {},
43+
"output_type": "execute_result"
44+
}
45+
],
46+
"source": [
47+
"stock_prices"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 3,
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"302.0\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"# this is O(n)\n",
65+
"\n",
66+
"for element in stock_prices:\n",
67+
" if element[0] == \"march 9\":\n",
68+
" print(element[1])"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {},
74+
"source": [
75+
"# Dictionary- Python's use of hash map/tables - O(1)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 7,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"stock_prices = {}\n",
85+
"with open(\"stock_prices.csv\", \"r\") as f:\n",
86+
" for line in f:\n",
87+
" tokens = line.split(',')\n",
88+
" day = tokens[0]\n",
89+
" price = float(tokens[1])\n",
90+
" stock_prices[day] = price"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 8,
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"data": {
100+
"text/plain": [
101+
"{'march 6': 310.0,\n",
102+
" 'march 7': 340.0,\n",
103+
" 'march 8': 380.0,\n",
104+
" 'march 9': 302.0,\n",
105+
" 'march 10': 297.0,\n",
106+
" 'march 11': 323.0}"
107+
]
108+
},
109+
"execution_count": 8,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"stock_prices"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 11,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"data": {
125+
"text/plain": [
126+
"302.0"
127+
]
128+
},
129+
"execution_count": 11,
130+
"metadata": {},
131+
"output_type": "execute_result"
132+
}
133+
],
134+
"source": [
135+
"# this is O(1)\n",
136+
" \n",
137+
"stock_prices['march 9']"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": []
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": []
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": []
160+
}
161+
],
162+
"metadata": {
163+
"kernelspec": {
164+
"display_name": "Python 3",
165+
"language": "python",
166+
"name": "python3"
167+
},
168+
"language_info": {
169+
"codemirror_mode": {
170+
"name": "ipython",
171+
"version": 3
172+
},
173+
"file_extension": ".py",
174+
"mimetype": "text/x-python",
175+
"name": "python",
176+
"nbconvert_exporter": "python",
177+
"pygments_lexer": "ipython3",
178+
"version": "3.8.5"
179+
}
180+
},
181+
"nbformat": 4,
182+
"nbformat_minor": 4
183+
}

0 commit comments

Comments
 (0)