Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 6 tree #147

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1. How does your data structure allow developers to access and manipulate the data?
Ans: I'm using Array to represent line and matrix, simply retrieve the data via index number, and you may change it as you like.
There is other useful array method to insert/delete the element like push, delete, pop, shift, and unshift


2. If a developer wanted to find a specific element in your data structure, how would you search for it?
Ans: Going through every element of the array from the beginning to the end.

3. What other real-world data can each structure represent?
Ans: Each network packet can store in the array. Chess can be store in a matrix.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,44 @@ def initialize
end

def join(person)
members.push(person)
end

def leave(person)
members.delete(person)
end

def front
members[0];
end

def middle
members[members.length/2]
end

def back
members[members.length-1]
end

def search(person)
members.each do | member |
if member == person
return member
end
end

return nil
end

private

def index(person)
members.each_with_index do | member, index |
if member == person
return index
end
end
return nil
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
members is an array, and people is elemnet of the array.

join method is to add element into an array. Using push command can achieve that.

leave method is to remove an element. Method delete can do that.

front method is to show the first person of the array. Simply array[0] will do.

middle method is to show the middle person of the array. The length of array divid by 2 is the answer.

back method is to show the last person of the array which is length of array minus 1.

search method is to search the person. Using each method of array to search every element of array. If matches then return, if no then return nil.

I'm not sure where can I utilize the index private method, but I think it's to get the index of the person, so I use each_with_index method of array to implement it.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ class Pixel


def initialize(red, green, blue, x, y)
@red = validate_color(red)
@green = validate_color(green)
@blue = validate_color(blue)
@x = x
@y = y
end

private

def validate_color(color)
if color < 0
color = 0
elsif color > 255
color = 255
end
color
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ class Screen
attr_accessor :matrix

def initialize(width, height)
self.matrix = Array.new(width){Array.new(height){ nil }}
end

# Insert a Pixel at x, y
def insert(pixel, x, y)
pixel.x = x
pixel.y = y
matrix[x][y] = pixel
end

def at(x, y)
if x > 0 && y > 0
matrix[x][y]
else
nil
end
end

private

def inbounds(x, y)
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To initialize the pixel, before put the value into the color, need to make sure that the value is with in 0-255. Call the validate_color to modify the color value within 0-255.

To initialize a screen, we need to create a matrix. I use 2 dimension array to create a matrix with default value nil.

For the insert method, simply put the pixel into the designated matrix. Before putting in just make sure pixel's x and y coordinate are the same as given x, y.

For the at method, if given x and y are integer, then return the designated matrix.
12 changes: 10 additions & 2 deletions 01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ class MyQueue

def initialize
@queue = Array.new
@head = @queue[0]
end

def enqueue(element)
@queue[@queue.length] = element
@head = @queue[0]
@tail = @queue[-1]
end

def dequeue
item = @head
@queue = @queue[1..-1]
@head = @queue[0]
@tail = @queue[-1]
item
end

def empty?
@queue.length == 0
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Queue is first in first out, so element comes in at the end and goes out at the first of the array.

enqueue method is to add an element at the end of the queue, and set the new head to the first the new queue and new tail to the end of the new queue.

dequeue method is to get first element of the queue which is head. Then remove the first element of the array, everybody left shifts. Finally, set the new head to the first the new queue and new tail to the end of the new queue.

empty? method is to check if the array is empty via its length.
9 changes: 8 additions & 1 deletion 01-data-structures/02-stacks-and-queues/mystack/mystack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ def initialize
end

def push(item)
@stack[@stack.length] = item
self.top = item
end

def pop
item = self.top
@stack = @stack.first (@stack.length - 1)
self.top = @stack[@stack.length-1]
item
end

def empty?
@stack.length == 0
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Implemented stack using array. top is the pointer to show the top of the stack.

push method is to add one element at the end of the stack, and top pointer to the item

pop method is to release the top of the stack, so the pop's element should be the top. Then remove the last element of array, and top moves to the last element of array.

empty method is to check if array is empty via checking length of the array.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
expect(stack.empty?).to eq false
end
end
end
end
29 changes: 29 additions & 0 deletions 01-data-structures/03-linked-lists/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'benchmark'
require_relative 'linked_list'

n = 10_000_000
node = Node.new("benchmark")
llist = LinkedList.new
Benchmark.bm do |x|
x.report("Array:") { array = Array.new(n) }
x.report("LinkedList:") { for i in 1..n; llist.add_to_tail(node); end }
end

array = Array.new(n)
temp_node = llist.head

Benchmark.bm do |y|
y.report("Array:") { array[5000]}
y.report("LinkedList:") { for i in 1..5000; temp_node.next; end}
end

Benchmark.bm do |z|
z.report("Array:") { array.delete_at(5000)}
z.report("LinkedList:") {
for i in 1..4999 do
if i == 4999
temp_node.next = temp_node.next.next
end
temp_node.next
end }
end
35 changes: 35 additions & 0 deletions 01-data-structures/03-linked-lists/linked-lists-answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1.What is Spatial Locality and why does it benefit performance?
If a particular storage is used at a particular time, then it's more likely the nearby memory locations will be used later on.
If the system puts those data in nearby memory into cache, system can reference those data fast because it's in the cache.

2.Compare the performance of an Array to a Linked List using the Benchmark module.
- Compare the time it takes to create a 10,000 item Array to appending 10,000 items to a Linked List.
user system total real
Array: 0.000000 0.000000 0.000000 ( 0.000046)
LinkedList: 0.010000 0.000000 0.010000 ( 0.001606)

This is 10,000,000 item:
user system total real
Array: 0.020000 0.020000 0.040000 ( 0.048662)
LinkedList: 1.450000 0.010000 1.460000 ( 1.461256)

- Compare the time it takes to access the 5000th element of the Array and the 5000th Node in the Linked List.
user system total real
Array: 0.000000 0.000000 0.000000 ( 0.000003)
LinkedList: 0.000000 0.000000 0.000000 ( 0.000265)

This is 10,000,000 item:
user system total real
Array: 0.000000 0.000000 0.000000 ( 0.000006)
LinkedList: 0.000000 0.000000 0.000000 ( 0.000499)

- Compare the time it takes to remove the 5000th element from the Array to removing the 5000th Node in the Linked List.
- In the Array, the 5001st item becomes the 5000th, and so on.
user system total real
Array: 0.000000 0.000000 0.000000 ( 0.000006)
LinkedList: 0.000000 0.000000 0.000000 ( 0.000425)

This is 10,000,000 item:
user system total real
Array: 0.010000 0.000000 0.010000 ( 0.011722)
LinkedList: 0.000000 0.000000 0.000000 ( 0.000313)
53 changes: 52 additions & 1 deletion 01-data-structures/03-linked-lists/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,76 @@ class LinkedList

# This method creates a new `Node` using `data`, and inserts it at the end of the list.
def add_to_tail(node)
if self.tail
self.tail.next = node
end
if self.head == nil
self.head = node
end
self.tail = node
end

# This method removes the last node in the lists and must keep the rest of the list intact.
def remove_tail
temp_node = self.head

while temp_node
if self.head == self.tail
self.head = nil
self.tail = nil
elsif temp_node.next == self.tail
self.tail = temp_node
temp_node.next = nil
end
temp_node = temp_node.next
end
end

# This method prints out a representation of the list.
def print
temp_node = self.head

while temp_node
puts temp_node.data
temp_node = temp_node.next
end
end

# This method removes `node` from the list and must keep the rest of the list intact.
def delete(node)
temp_node = self.head

if self.head == node && self.tail == node
self.head = nil
self.tail = nil
elsif self.head == node
self.head = temp_node.next
temp_node = nil
end

while temp_node
if temp_node.next == node && self.tail == node
temp_node.next = node.next
self.tail = temp_node
elsif temp_node.next == node
temp_node.next = node.next
end

temp_node = temp_node.next
end
end

# This method adds `node` to the front of the list and must set the list's head to `node`.
def add_to_front(node)
node.next = self.head
self.head = node
end

# This method removes and returns the first node in the Linked List and must set Linked List's head to the second node.
def remove_front
temp_node = self.head
self.head = temp_node.next
temp_node.next = nil
temp_node
end
end
end
2 changes: 1 addition & 1 deletion 01-data-structures/03-linked-lists/linked_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
expect(llist.head).to eq nil
end
end
end
end
4 changes: 3 additions & 1 deletion 01-data-structures/03-linked-lists/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class Node
attr_accessor :data

def initialize(data)
@data = data
@next = nil
end
end
end
4 changes: 3 additions & 1 deletion 01-data-structures/04-hashes-part-1/hash_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class HashItem
attr_accessor :value

def initialize(key, value)
@key = key
@value = value
end
end
end