Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6925362

Browse files
committedDec 26, 2018
maps and hashing begun
1 parent 0d5635b commit 6925362

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed
 

‎3-search-sort/QuickSort-mine.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ def quicksort(array):
55
if len(array) < 2:
66
return array
77
stack_of_index = []
8+
# index stores beginning and the end boundaries
89
index = (0, len(array) - 1)
910
stack_of_index.append(index)
1011
for index in stack_of_index:
12+
# first value is the element to compare
1113
e_index = index[0]
14+
# second value is the pivot
1215
pivot_index = index[1]
16+
# until they cross paths, keep executing the following:
1317
while pivot_index > e_index:
1418
pivot = array[pivot_index]
1519
e = array[e_index]
@@ -18,7 +22,7 @@ def quicksort(array):
1822
array[e_index] = array[pivot_index - 1]
1923
array[pivot_index - 1] = pivot
2024
pivot_index -= 1
21-
else:
25+
else: # it's in the correct side of the pivot, so move on
2226
e_index += 1
2327
low = index[0]
2428
high = index[1]

‎4-maps-hashing/DictQuiz-mine.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Time to play with Python dictionaries!
2+
You're going to work on a dictionary that
3+
stores cities by country and continent.
4+
One is done for you - the city of Mountain
5+
View is in the USA, which is in North America.
6+
7+
You need to add the cities listed below by
8+
modifying the structure.
9+
Then, you should print out the values specified
10+
by looking them up in the structure.
11+
12+
Cities to add:
13+
Bangalore (India, Asia)
14+
Atlanta (USA, North America)
15+
Cairo (Egypt, Africa)
16+
Shanghai (China, Asia)"""
17+
18+
locations = {
19+
'North America': {'USA': ['Mountain View', 'Atlanta']},
20+
'Asia': {'India': ['Bangalore'], 'China': ['Shanghai']},
21+
'Africa': {'Egypt': ['Cairo']},
22+
23+
}
24+
25+
"""Print the following (using "print").
26+
1. A list of all cities in the USA in
27+
alphabetic order.
28+
2. All cities in Asia, in alphabetic
29+
order, next to the name of the country.
30+
In your output, label each answer with a number
31+
so it looks like this:
32+
1
33+
American City
34+
American City
35+
2
36+
Asian City - Country
37+
Asian City - Country"""
38+
39+
print('1')
40+
array = sorted(locations['North America']['USA'])
41+
for i in array:
42+
print(i)
43+
print('2')
44+
asian = locations['Asia'].items()
45+
asian = sorted(asian, key=lambda item:item[1])
46+
for i in asian:
47+
print(i[1][0] + " - " + i[0])

‎4-maps-hashing/DictQuiz.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
locations = {'North America': {'USA': ['Mountain View']}}
2+
locations['North America']['USA'].append('Atlanta')
3+
locations['Asia'] = {'India': ['Bangalore']}
4+
locations['Asia']['China'] = ['Shanghai']
5+
locations['Africa'] = {'Egypt': ['Cairo']}
6+
7+
print 1
8+
usa_sorted = sorted(locations['North America']['USA'])
9+
for city in usa_sorted:
10+
print city
11+
12+
print 2
13+
asia_cities = []
14+
for countries, cities in locations['Asia'].iteritems():
15+
city_country = cities[0] + " - " + countries
16+
asia_cities.append(city_country)
17+
asia_sorted = sorted(asia_cities)
18+
for city in asia_sorted:
19+
print city

‎4-maps-hashing/Hashing-notes.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### Hash Functions
2+
3+
Using a hash function, we can perform lookups in constant time. This contrasts with the use of lists and sets, which take linear time for lookups.
4+
5+
The way it works is that we have a value that we pass through some function to get a hash value, and use the hash value as a location in the array we store to. That way we can immediately know where a value is stored since we know the hash value.
6+
7+
For example, if we had numbers as values, we could take the remainder of that number when divided by another number, and use the remainder as the location in an array.
8+
9+
Ex:
10+
11+
4979 as our number, and 109 as the hash function division. 4979 % 109 is 74, so we store that number in our array[74] position.
12+
13+
### Collisions
14+
15+
What if we have two numbers that end up at the same position when we run them through our hash function?
16+
17+
1. We can change our hash and make it bigger, so that each colliding value gets its own location in the array.
18+
19+
The downsides with the above approach are that this increases the space complexity, and if this is being performed reactively, then changing the hash function, recalculating, and copying the old values into a new array also increases some time complexity.
20+
21+
2. Instead of storing one value in each array location, we can make a "bucket" of values by creating a list in each array location.
22+
23+
The downsides with this approach are that searching through each bucket now takes linear time complexity according to the size of each bucket. In the worst case, if all values end up stored in the same bucket, we now have O(n) time like we did with a list.
24+
25+
Other methods include perhaps making a second hash function in each bucket to further divide up the elements.
26+
27+
### Load Factor
28+
29+
Load factor is the # of entries divided by the # of buckets.
30+
31+
### Quiz Answer:
32+
33+
Coworker has a hash function that divides a group of values by 100 and uses remainder as key. Values are 100 numbers, all divisible by 5.
34+
35+
What is the load factor?
36+
37+
It is 1, because there are 100 numbers, and the hash function has 100 unique spots.
38+
39+
What number would you recommend his function to divide by to speed it up?
40+
41+
87
42+
107 <----
43+
125
44+
1001
45+
46+
87 creates collisions.
47+
125 is divisible by 5 and hence also creates collisions.
48+
1001 is wasted space.

0 commit comments

Comments
 (0)
Please sign in to comment.