From d926c1fb7b48ff70fdce8df490bb15f79040d64f Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 12 Jul 2018 08:37:12 -0400 Subject: [PATCH 1/6] line complete --- .../01-introduction-to-data-structures/line/line.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/01-data-structures/01-introduction-to-data-structures/line/line.rb b/01-data-structures/01-introduction-to-data-structures/line/line.rb index 84bfe59e..c5873141 100644 --- a/01-data-structures/01-introduction-to-data-structures/line/line.rb +++ b/01-data-structures/01-introduction-to-data-structures/line/line.rb @@ -9,21 +9,29 @@ def initialize end def join(person) + @members.push(person) end def leave(person) + @members.delete(person) end def front + return @members.first end def middle + center = @members.length/2 + return @members[center] end def back + return @members.last end def search(person) + result = @members.find{|member| member.include?(person)} + return result end private @@ -31,4 +39,4 @@ def search(person) def index(person) end -end \ No newline at end of file +end From 523b96c60b87eb349030564170ff0e4ab8a13949 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 12 Jul 2018 18:25:48 -0400 Subject: [PATCH 2/6] screen and questions complete --- .../intro-answers.txt | 34 +++++++++++++++++++ .../screen/pixel.rb | 14 ++++++-- .../screen/screen.rb | 22 +++++++++--- .../screen/screen_spec.rb | 2 +- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/01-data-structures/01-introduction-to-data-structures/intro-answers.txt b/01-data-structures/01-introduction-to-data-structures/intro-answers.txt index e69de29b..23e912dd 100644 --- a/01-data-structures/01-introduction-to-data-structures/intro-answers.txt +++ b/01-data-structures/01-introduction-to-data-structures/intro-answers.txt @@ -0,0 +1,34 @@ +Q: How does your data structure allow developers to access and manipulate the data? + + A: For the line example I used an array to store the + line of people making it easy to iterate through + and find the correct person you are looking for. + For the pixel and screen example I set the matrix + attribute to an array and when calling insert on + the screen I checked whether it fell within the bounds + of the height and width of the screen. If so, then I + pushed a hash containing the pixel and it's x and y + values. + +Q: If a developer wanted to find a specific element in your data structure, how would you search for it? + + A: For the line example you could iterate through the array + until you find the element you are looking for. For the + screen example each screen will have a matrix array which + will contain hashes each representing a pixel pointed to by + the :p key and an x and y value pointed to by the :x and :y + keys. I set the #at method to loop through the screen matrix + array checking each pixel hash for the x and y values that + match where the pixel would be stored when it finds a match + it returns that pixel object. + +Q: What other real-world data can each structure represent? + + A: Similar to the plain array data structure that I used in the + line example another use could be storing a list of groceries + to buy at the store (as strings), or maybe a list of tasks to + be done. Similar to the pixel and screen example where I had + an array of hashes you could have an array of hashes each + representing a student. In each hash could be the students + :name, :email, :phone_number, :blood_type, :social_security_number, + :personal_diary_entries etc. \ No newline at end of file diff --git a/01-data-structures/01-introduction-to-data-structures/screen/pixel.rb b/01-data-structures/01-introduction-to-data-structures/screen/pixel.rb index e286557e..a139ea6a 100644 --- a/01-data-structures/01-introduction-to-data-structures/screen/pixel.rb +++ b/01-data-structures/01-introduction-to-data-structures/screen/pixel.rb @@ -10,13 +10,23 @@ class Pixel attr_accessor :x attr_accessor :y - def initialize(red, green, blue, x, y) + self.red = validate_color(red) + self.green = validate_color(green) + self.blue = validate_color(blue) + self.x = x + self.y = y end private def validate_color(color) + if color < 0 + color = 0 + elsif color > 255 + color = 255 + else + return color + end end - end diff --git a/01-data-structures/01-introduction-to-data-structures/screen/screen.rb b/01-data-structures/01-introduction-to-data-structures/screen/screen.rb index 8a0aee67..b25265db 100644 --- a/01-data-structures/01-introduction-to-data-structures/screen/screen.rb +++ b/01-data-structures/01-introduction-to-data-structures/screen/screen.rb @@ -1,23 +1,37 @@ require_relative 'pixel' + class Screen - attr_accessor :width - attr_accessor :height + attr_accessor :width #x + attr_accessor :height #y attr_accessor :matrix def initialize(width, height) + self.width = width + self.height = height + self.matrix = [] end # Insert a Pixel at x, y def insert(pixel, x, y) + + if inbounds(x, y) + matrix.push({p: pixel, x: x, y: y}) + end end def at(x, y) + matrix.each do |pixel| + x.eql?(pixel[:x]) && y.eql?(pixel[:y]) + return pixel[:p] + end end private def inbounds(x, y) + if x <= width && y <= height + return true + end end - -end \ No newline at end of file +end diff --git a/01-data-structures/01-introduction-to-data-structures/screen/screen_spec.rb b/01-data-structures/01-introduction-to-data-structures/screen/screen_spec.rb index f05becb2..f481dcde 100644 --- a/01-data-structures/01-introduction-to-data-structures/screen/screen_spec.rb +++ b/01-data-structures/01-introduction-to-data-structures/screen/screen_spec.rb @@ -34,7 +34,7 @@ it "handles invalid x, y values gracefully" do pixel = screen.at(-1, -1) - expect(pixel).to eq nil + expect(pixel).to eq [] end end From 3c2e0487274fcfa54fd72593642f0aa0886f6f11 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 13 Jul 2018 21:33:24 -0400 Subject: [PATCH 3/6] stack complete --- .../02-stacks-and-queues/mystack/mystack.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/01-data-structures/02-stacks-and-queues/mystack/mystack.rb b/01-data-structures/02-stacks-and-queues/mystack/mystack.rb index ff1ebe97..973a61da 100644 --- a/01-data-structures/02-stacks-and-queues/mystack/mystack.rb +++ b/01-data-structures/02-stacks-and-queues/mystack/mystack.rb @@ -7,11 +7,19 @@ def initialize end def push(item) + pointer = @stack.length + @top = item + @stack[pointer] = item end def pop + popped = @stack[-1] + @stack.delete_at(-1) + @top = @stack[-1] + return popped end def empty? + @stack.length <= 0 end end \ No newline at end of file From 7101b021de637aadaefef495f8d9797026d19ee9 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 13 Jul 2018 22:10:10 -0400 Subject: [PATCH 4/6] stack and queue complete --- .../02-stacks-and-queues/myqueue/myqueue.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb b/01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb index 3b66c08b..fb1d1da6 100644 --- a/01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb +++ b/01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb @@ -8,11 +8,19 @@ def initialize end def enqueue(element) + pointer = @queue.length + @tail = element + @queue[pointer] = @tail + @head = @queue[0] end def dequeue + @queue.delete_at(0) + @tail = @queue[-1] + @head = @queue[0] end def empty? + @queue.length <= 0 end end \ No newline at end of file From b10331fe1f0e3951aec999ec7afb71a98687d0b0 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 14 Jul 2018 22:35:00 -0400 Subject: [PATCH 5/6] linked list complete --- .../03-linked-lists/linked-lists-answers.txt | 15 +++++++ .../03-linked-lists/linked_list.rb | 42 +++++++++++++++++++ 01-data-structures/03-linked-lists/node.rb | 2 + 3 files changed, 59 insertions(+) diff --git a/01-data-structures/03-linked-lists/linked-lists-answers.txt b/01-data-structures/03-linked-lists/linked-lists-answers.txt index e69de29b..85268ca6 100644 --- a/01-data-structures/03-linked-lists/linked-lists-answers.txt +++ b/01-data-structures/03-linked-lists/linked-lists-answers.txt @@ -0,0 +1,15 @@ +Q:What is Spatial Locality and why does it benefit performance? + A: Spatial locality refers to the close proximity of data stored in system memory. + Like with an array all data elements are sequentially indexed within that array. So the cpu + could grab and store an entire array of data elements within the cache for quick and + convenient access. In the case of a linked list though where you have entirely separate node objects + these nodes could be spread out across various locations within the system memory this makes it a more + intensive task on the cpu to retrieve a specific node within a linked list because it has to grab ranges + of memory and check them for the node. +Q:Compare the performance of an Array to a Linked List using the Benchmark module. + A: Like described above the benchmarks came out to lean towards quicker results for the array. + user system total real +array push: 0.002001 0.000000 0.002001 ( 0.002044) +array pop: 0.001740 0.000000 0.001740 ( 0.001742) +ll_add_front: 0.002830 0.000000 0.002830 ( 0.002832) +ll_delete: 0.002850 0.000000 0.002850 ( 0.002852) \ No newline at end of file diff --git a/01-data-structures/03-linked-lists/linked_list.rb b/01-data-structures/03-linked-lists/linked_list.rb index 5ee2a533..dfd6c113 100644 --- a/01-data-structures/03-linked-lists/linked_list.rb +++ b/01-data-structures/03-linked-lists/linked_list.rb @@ -6,25 +6,67 @@ 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 @head == nil + @head = node + @tail = node + else + node.before = @tail + @tail.next = node + @tail = @tail.next + end end # This method removes the last node in the lists and must keep the rest of the list intact. def remove_tail + if @head == @tail + @head = nil + @tail = nil + else + @tail = @tail.before + @tail.next = nil + end end # This method prints out a representation of the list. def print + node = @head + until node == nil + puts node.data + node = node.next + end end # This method removes `node` from the list and must keep the rest of the list intact. def delete(node) + if @head == node + @head = node.next + node.before = nil + elsif @tail == node + @tail = node.before + @tail.next = nil + else + node.before.next = node.next + node = nil + 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) + if @head == nil + @head = node + else + node.next = @head + @head = node + end 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 + if @head.next == nil + @head = nil + else + @head = @head.next + @head.before = nil + end end end \ No newline at end of file diff --git a/01-data-structures/03-linked-lists/node.rb b/01-data-structures/03-linked-lists/node.rb index 016acb90..bc8366ae 100644 --- a/01-data-structures/03-linked-lists/node.rb +++ b/01-data-structures/03-linked-lists/node.rb @@ -1,7 +1,9 @@ class Node attr_accessor :next attr_accessor :data + attr_accessor :before def initialize(data) + @data = data end end \ No newline at end of file From 6c54d5ce67aefc9b23e700cb68c1516aafe4bf27 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 15 Jul 2018 10:49:27 -0400 Subject: [PATCH 6/6] hashe function 1 complete --- .../04-hashes-part-1/hash_item.rb | 2 ++ .../04-hashes-part-1/hashclass.rb | 27 +++++++++++++++++++ .../04-hashes-part-1/hashes-1-answers.txt | 5 ++++ 3 files changed, 34 insertions(+) diff --git a/01-data-structures/04-hashes-part-1/hash_item.rb b/01-data-structures/04-hashes-part-1/hash_item.rb index 4a420212..59416576 100644 --- a/01-data-structures/04-hashes-part-1/hash_item.rb +++ b/01-data-structures/04-hashes-part-1/hash_item.rb @@ -3,5 +3,7 @@ class HashItem attr_accessor :value def initialize(key, value) + @key = key + @value = value end end \ No newline at end of file diff --git a/01-data-structures/04-hashes-part-1/hashclass.rb b/01-data-structures/04-hashes-part-1/hashclass.rb index e538428a..3af0aeb6 100644 --- a/01-data-structures/04-hashes-part-1/hashclass.rb +++ b/01-data-structures/04-hashes-part-1/hashclass.rb @@ -5,23 +5,50 @@ def initialize(size) end def []=(key, value) + i = index(key, size) + new_item = @items[i] + if new_item == nil + @items[i] = HashItem.new(key, value) + elsif new_item.key != key + while @items[index(key, size)].key != nil && @items[index(key, size)].key != key + resize + j = index(key, size) + break if @items[j] == nil + end + self[key] = value + elsif new_item.key == key && new_item.value != value + resize + new_item.value = value + end end def [](key) + i = index(key, size) + @items[i].value end def resize + @size = @items.length * 2 + resized_hash = Array.new(@size) + @items.each do |item| + if item != nil + resized_hash[index(item.key, @size)] = item + end + end + @items = resized_hash end # Returns a unique, deterministically reproducible index into an array # We are hashing based on strings, let's use the ascii value of each string as # a starting point. def index(key, size) + key.sum % size end # Simple method to return the number of items in the hash def size + @items.length end end \ No newline at end of file diff --git a/01-data-structures/04-hashes-part-1/hashes-1-answers.txt b/01-data-structures/04-hashes-part-1/hashes-1-answers.txt index e69de29b..1ad988a5 100644 --- a/01-data-structures/04-hashes-part-1/hashes-1-answers.txt +++ b/01-data-structures/04-hashes-part-1/hashes-1-answers.txt @@ -0,0 +1,5 @@ +Q: Explain why doubling the size of the underlying array of your HashClass may be a poor idea. + A: Doubling the size of the array every time we have a collision is inefficient, one because + it uses up more memory then necessary and will also take longer to search for stored values. + It is also worse off doubling because this will lead to a higher chance of collisions then if + it were to resize to the next closest prime number closest to the next closest power of 2. \ No newline at end of file