














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 1
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 2
3
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 4
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 7
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 8
address data next 100 42 148 108 99 0 (null) 116 124 50 100 132 71 108 140 148 85 132 156
Starting Location of List (head) 124
Assume eachinteger and pointer requires 4 bytes.
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 9
address data next 100 42 156 108 99 0 (null) 116 124 50 100 132 71 108 140 148 85 132 156 20 148
Starting Location of List (head) 124
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 10
50 42 85 71 99 null
head
50 42 85 71 99 null
head
20 step 2 step 1
We link the new node to the list before breaking the existing link.
data = [ [ 1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]
data[0] => [1, 2, 3, 4] data[1][2] => 7 data[2][5] => nil data[4][2] => undefined method '[]' for nil
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 13
0 1 2 3 0 1 2 3 4 1 5 6 7 8 2 9 10 11 12
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 14
number of rows in the table
number of columns in the given row of the table
row col sum 0 0 1 0 1 3 0 2 6 0 3 10 1 0 15 1 1 21 1 2 28 1 3 36 2 0 45 2 1 55 2 2 66 2 3 78
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 15
0 1 2 3 0 1 2 3 4 1 5 6 7 8 2 9 10 11 12
for row in 0..table.length-1 do for col in 0..table[row].length-1 do sum = sum + table[row][col] end end
table.length = 3 table[row].length = 4 for every row
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 16
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 19
A
S
i == A.length?
Pop top 2 numbers Perform operation Push x on S Push result on S
i 0
Output Pop S
yes no
A
x A[i]
Is x a number? yes
no
i i + 1 23 – 3 = 20
23 3 - 4 6 + /
23
23 3 - 4 6 + /
3 23
23 3 - 4 6 + /
20
23 3 - 4 6 + /
4 20
23 3 - 4 6 + /
6 4 20
23 3 - 4 6 + /
20
4 + 6 = 10 10 20
23 3 - 4 6 + /
20 / 10 = 2
2
23 3 - 4 6 + /
Answer: 2
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 20
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 21
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 22
26
key key
key
Universe of keys Hash Table
h(key2)
A hash function h is used to map keys to hash-table slots
h(key3)
h(key1)
s = "hello" for i in 0..s.length-1 do print s[i], "\n" end
104 101 108 108 111
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 27
You can treat a string like an array in Ruby. If you access the ith character, you get the ASCII code for that character.
table1 = Array.new(26) => [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 28
table => ["aardvark", "beaver", nil, nil, nil, nil, nil, nil, nil, nil, "kangaroo", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "whale", nil, nil, nil] Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 31
>> insert(table1,"bunny") >> insert(table1,"bear") >> table => ["aardvark", "bear", nil, nil, nil, nil, nil, nil, nil, nil, "kangaroo", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "whale", nil, nil, nil] Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 32
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 33
**def insert(table, key)
bucket = table[h(key)]
bucket << key end**
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 34
def h(string) k = 0 for i in 0..string.length-1 do k = string[i] + k end return k end h(“hello”) => 532 h(“olleh”) => 532 Permutations still give same index (collision) and numbers are high.
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 37
def h(string) k = 0 for i in 0..string.length-1 do k = string[i] + k end return k end h(“hello”) => 448378203247 h(“olleh”) => 478560413032* Better, but numbers are still high. We probably don’t want to (or can’t) create arrays that have indices this large. Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 38
def h(string, tablesize) k = 0 for i in 0..string.length-1 do k = string[i] + k end return k % tablesize end*
We can use the modulo operator to take the large values and map them to indices for a smaller array.
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 39
**def insert(table, key)
bucket = table[h(key, table.length)]
bucket << key end**
Carnegie Mellon University - CORTINA^ 15110 Principles of Computing, 40