From 2df6da02726c30e373b32419df09ef4d29178822 Mon Sep 17 00:00:00 2001 From: Conor Souhrada Date: Thu, 4 Oct 2018 10:17:38 -0600 Subject: [PATCH 1/4] assignment completion --- .DS_Store | Bin 0 -> 6148 bytes 01-data-structures/.DS_Store | Bin 0 -> 6148 bytes .../line/line.rb | 9 ++++++- .../line/line_answers.txt | 23 ++++++++++++++++++ .../line/line_spec.rb | 4 +-- .../screen/pixel.rb | 12 +++++++++ .../screen/pixel_spec.rb | 12 ++++----- .../screen/screen.rb | 13 +++++++++- .../screen/screen_answers.txt | 12 +++++++++ .../screen/screen_spec.rb | 2 +- 10 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 .DS_Store create mode 100644 01-data-structures/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..61190987751e14f9ebda5129f8aa0770b7024463 GIT binary patch literal 6148 zcmeHK&59H;5U$LQni&xX5!Pcaf`<`ib{F>`LNl(1y$LOPP}%L?9^8huv-FRHjwAgT z`bNHikE35DnH5=uJq;>VQ2DBpstWlMk}e`rvo@O&jfhA?8G8dXzY!j1y&%2NvjtQx zM?wuHbf0RV$iKh<@7*n$&=k9tVefwKUqvmQ;Jc-Y=2RaR_9QKA3V%K<525$IJArk{ z9O zl0MJpi=)x)$F^*eyqs4qIM3#AdG#VMGrO4Cw#;gmC-wtc5A@(@w7p#J?2OIs-gGrK z%l-Yy*zD~-Sgi(n`|kA7;qlw|AC@0ipFX?Qk;3_Qax>r>zQDK?fA8X|ENuA({_9-p z9D>4tFdz(Unt^Z+>Fb-GSmqN3gn_@F0lps$lraj}d309?8dm}U`{;H8n|}{E#srK4 zb{;VTQ9cytLscj-ln+P034T$)&Z7?}6^aiPW>%p>QEqnRH_n|@eGU@a3oY4%7+y X4I98HVCNA%5cv^sG)N;1Y?Of?am#5d literal 0 HcmV?d00001 diff --git a/01-data-structures/.DS_Store b/01-data-structures/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..83496f7f59f6effe3e90933589641dc2da0e98e6 GIT binary patch literal 6148 zcmeHK&2G~`5T0!VI6B635i1@<&XA&kfKlyy&)OF0Z?n#7O`sV)IUUR2+DLe7y;K^dhKP`NhRG?)mO2>fdi;In&&I@F~$MYMc=k8qYFoaF>P1+a}%?ZNV3 zl7bI6d;?)W>}MF?j&J_e~0{OY-4Q^NIHH*`LHwUUWKN#Kvar+VvZj z)vy}38n4`OI&}-LDD#n*z2MptuNeE4ZCdp9{V#v$q zQ4*xnQCcQJF6VJohh^2R`hne?&3e6-(;sx_EoZj7+i5w2{?2?}x0-jm5BCq>oSx0z z&EKEP)sw*YQOcIaAMg>zrNsZOn8d9!1ysd=P0pHP@ABZf!(I=>aGzVF5ut) z4Zm06f35H>_}>AaZ>5mEua1|y^n7WTf_{ZW?@8M!z@J5czYh+Saj0>j zP<}d4$R_}>hGr?S`TA#$qj9Klp%6U~p{PJb75a%G6dmo39yipuP^jo6^y5Ql&qBXY zg!GR29XXuDP@ySJ1WW{0Be1EeHsAk`fBpVnC7GIufQi6=6#-G(bN5DAlD=El7RPt3 u13iSY@VJFSNkO5nV`=bJd>bkSeGVJIp~i(mv_Q;{fDD5vOa%UF1il03fXKH1 literal 0 HcmV?d00001 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..22d5a316 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,26 +9,33 @@ def initialize end def join(person) + members.push(person) end def leave(person) + members.delete(person) end def front + members.first end def middle + members[members.length / 2] end def back + members.last end def search(person) + members.detect {|e| e === person} end private def index(person) + members.find_index(person) end -end \ No newline at end of file +end diff --git a/01-data-structures/01-introduction-to-data-structures/line/line_answers.txt b/01-data-structures/01-introduction-to-data-structures/line/line_answers.txt index e69de29b..46398e69 100644 --- a/01-data-structures/01-introduction-to-data-structures/line/line_answers.txt +++ b/01-data-structures/01-introduction-to-data-structures/line/line_answers.txt @@ -0,0 +1,23 @@ + For this challenge regarding the line at an amusement park I opted to use an +array as the data structure. I came to this decision as an array is already much +like a line. + + You can easily add "people" to the end of the "line" using the array.push +method, similar to people joining a real world line at the end. An array of +is also linear like a line, and using some basic ruby method you can modify the +array to show people leaving and entering the line in addition to seeing who is +first, last and in the middle. + + Developers would be able to access and manipulate this data by using Ruby array +method in addition to searching by index. Developers could also search by name +using the detect method Ruby provides. + + I believe that an array was the right solution. I can also envision arrays +being a useful data structure for other situations such as a to-do list of items, +or the midi values and their related notes on a piano. Some other examples that +come to mind would be a repair queue at a phone shop, storing high scores on +sequential levels of an arcade game, etc. The list could go on and on as there +is much real-world data that an array could represent. + + While arrays are a more straight forward and simple data structure their use +and ability to represent data is very valuable to developers. diff --git a/01-data-structures/01-introduction-to-data-structures/line/line_spec.rb b/01-data-structures/01-introduction-to-data-structures/line/line_spec.rb index b755dabb..13435d11 100644 --- a/01-data-structures/01-introduction-to-data-structures/line/line_spec.rb +++ b/01-data-structures/01-introduction-to-data-structures/line/line_spec.rb @@ -1,6 +1,6 @@ include RSpec -require_relative 'line' +require_relative 'line' RSpec.describe Line, type: Class do let(:line) { Line.new } @@ -59,4 +59,4 @@ end end -end \ No newline at end of file +end 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..5e087197 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 @@ -12,11 +12,23 @@ 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 + 0 + elsif color > 255 + 255 + else + color + end end end diff --git a/01-data-structures/01-introduction-to-data-structures/screen/pixel_spec.rb b/01-data-structures/01-introduction-to-data-structures/screen/pixel_spec.rb index b3ab426e..3136cbed 100644 --- a/01-data-structures/01-introduction-to-data-structures/screen/pixel_spec.rb +++ b/01-data-structures/01-introduction-to-data-structures/screen/pixel_spec.rb @@ -4,24 +4,24 @@ describe "#initialize" do it "creates a pixel with the specified parameters" do pixel = Pixel.new(255, 200, 160, 5, 7) - + expect(pixel.red).to eq 255 expect(pixel.green).to eq 200 expect(pixel.blue).to eq 160 expect(pixel.x).to eq 5 expect(pixel.y).to eq 7 end - + it "corrects a red value if it's less than 0" do pixel = Pixel.new(-7, 100, 100, 5, 5) expect(pixel.red).to eq 0 end - + it "corrects a blue value if it's less than 0" do pixel = Pixel.new(100, -10, 100, 5, 5) expect(pixel.green).to eq 0 end - + it "corrects a green value if it's less than 0" do pixel = Pixel.new(100, 100, -12, 5, 5) expect(pixel.blue).to eq 0 @@ -31,12 +31,12 @@ pixel = Pixel.new(256, 100, 100, 5, 5) expect(pixel.red).to eq 255 end - + it "corrects a green value if it's greater than 255" do pixel = Pixel.new(100, 258, 100, 5, 5) expect(pixel.green).to eq 255 end - + it "corrects a blue value if it's greater than 255" do pixel = Pixel.new(100, 100, 300, 5, 5) expect(pixel.blue).to eq 255 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..32b0acf1 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 @@ -6,18 +6,29 @@ class Screen attr_accessor :matrix def initialize(width, height) + @matrix = Array.new(width) { Array.new(height, 0) } + @height = height + @width = width end # Insert a Pixel at x, y def insert(pixel, x, y) + @matrix[x][y] = pixel end def at(x, y) + inbounds(x, y) end private def inbounds(x, y) + if x > @width || y > @height || x < 0 || y < 0 + nil + else + @matrix[x][y] + end + end -end \ No newline at end of file +end diff --git a/01-data-structures/01-introduction-to-data-structures/screen/screen_answers.txt b/01-data-structures/01-introduction-to-data-structures/screen/screen_answers.txt index e69de29b..ca244897 100644 --- a/01-data-structures/01-introduction-to-data-structures/screen/screen_answers.txt +++ b/01-data-structures/01-introduction-to-data-structures/screen/screen_answers.txt @@ -0,0 +1,12 @@ + For this challenge regarding the pixel and screen I opted to use a matrix made +up of an array containing arrays. This allowed me to represent the height and +width of the screen that will then contain the pixels. + + This data structure will allow for easy access to the data in the matrix. By +using matrix[coordinate-x][coordinate-y] you can easily manipulate or retrieve the data in the structure. Developers +would be able to easily search for pixels by coordinates. + + This data structure could be useful for other real world data representations +such as any game that utilizes a grid, for example battleship, connect 4 or +tick tack toe. In addition previous checkpoints showed that a matrix can be used +to represent graph data such as vertices and edges. 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..45a2e008 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 @@ -9,7 +9,7 @@ screen.insert(pixel, 1, 1) expect(screen.at(1, 1)).to eq pixel - end + end it "retains color information upon insertion" do pixel = Pixel.new(255, 200, 175, 1, 1) From 70ea231014b860cc07bccdc756d0fd2e4fabdd9a Mon Sep 17 00:00:00 2001 From: Conor Souhrada Date: Thu, 4 Oct 2018 15:53:41 -0600 Subject: [PATCH 2/4] assignment completion --- .../02-stacks-and-queues/myqueue/myqueue.rb | 15 ++++++++++++++- .../myqueue/myqueue_answers.txt | 14 ++++++++++++++ .../02-stacks-and-queues/mystack/mystack.rb | 13 ++++++++++++- .../mystack/mystack_answers.txt | 12 ++++++++++++ .../02-stacks-and-queues/mystack/mystack_spec.rb | 8 +++++++- 5 files changed, 59 insertions(+), 3 deletions(-) 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..8f0817ef 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,24 @@ def initialize end def enqueue(element) + @queue << element + @head = @queue[0] + @tail = @queue[-1] end def dequeue + temp = @head + @queue.delete_at(0) + @head = @queue[0] + @tail = @queue[-1] + temp end def empty? + if(@queue.length === 0) + true + else + false + end end -end \ No newline at end of file +end diff --git a/01-data-structures/02-stacks-and-queues/myqueue/myqueue_answers.txt b/01-data-structures/02-stacks-and-queues/myqueue/myqueue_answers.txt index e69de29b..f72c15d4 100644 --- a/01-data-structures/02-stacks-and-queues/myqueue/myqueue_answers.txt +++ b/01-data-structures/02-stacks-and-queues/myqueue/myqueue_answers.txt @@ -0,0 +1,14 @@ + I have created a queue and based on the tests, in addition to my understanding +I believe it has what is necessary to function properly as a queue. + + My enqueue method appends the element to the end of the array. It will then +set the new head as the element at queue[0] and the tail as element at queue[-1]. + + My dequeue method allows for FIFO as it will remove the first item in the queue +after storing it temporarily(so that it can be returned). Following the removal of +the first element it will set the new head to the new item at queue[0] and the new +tail to queue[-1]. It will then return the previous head. + + My empty method will check to see if the queue length is equal to zero and if +it is we know the queue is empty and the method will return true. If not equal +to zero then the method will return false as it is not empty 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..61a96e0c 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,22 @@ def initialize end def push(item) + self.top = item + @stack << item end def pop + temp = self.top + @stack.delete_at(-1) + self.top = @stack[-1] + temp end def empty? + if(@stack.length === 0) + true + else + false + end end -end \ No newline at end of file +end diff --git a/01-data-structures/02-stacks-and-queues/mystack/mystack_answers.txt b/01-data-structures/02-stacks-and-queues/mystack/mystack_answers.txt index e69de29b..c0e80d9e 100644 --- a/01-data-structures/02-stacks-and-queues/mystack/mystack_answers.txt +++ b/01-data-structures/02-stacks-and-queues/mystack/mystack_answers.txt @@ -0,0 +1,12 @@ + With my current understanding of a stack I believe my solution meets the necessary +requirements. + + For my push method I appended the item to the end of the stack. From there my +pop method will temporarily store the current top then delete the last element +in the array. It will then reset the top to the new last element and return +the old top. + + The empty? method will check to see if the array length is equal to zero and +if it is it means the stack is empty and will return true. If the length is not +equal to zero then that will mean there is at least one element in the array +and will return false. diff --git a/01-data-structures/02-stacks-and-queues/mystack/mystack_spec.rb b/01-data-structures/02-stacks-and-queues/mystack/mystack_spec.rb index c0d1af80..37f2a39b 100644 --- a/01-data-structures/02-stacks-and-queues/mystack/mystack_spec.rb +++ b/01-data-structures/02-stacks-and-queues/mystack/mystack_spec.rb @@ -40,9 +40,15 @@ expect(stack.empty?).to eq true end + it "returns true when the stack is empty" do + stack.push("Rob") + stack.pop + expect(stack.empty?).to eq true + end + it "returns false when the stack is not empty" do stack.push("Rob") expect(stack.empty?).to eq false end end -end \ No newline at end of file +end From 76e7b01fdc929a7d5dfa6bbaf35729b360d535e8 Mon Sep 17 00:00:00 2001 From: Conor Souhrada Date: Mon, 8 Oct 2018 16:06:13 -0600 Subject: [PATCH 3/4] linked list started --- .../03-linked-lists/linked-lists-answers.txt | 4 ++ .../03-linked-lists/linked_list.rb | 66 ++++++++++++++++++- .../03-linked-lists/linked_list_spec.rb | 2 +- 01-data-structures/03-linked-lists/node.rb | 4 +- 4 files changed, 73 insertions(+), 3 deletions(-) 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..ba89be7f 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,4 @@ +spacial Locality involves accessing items that are close together in memory, as +in an array. This benefits performance as it limits the number of RAM lookups which +are more time consuming. + diff --git a/01-data-structures/03-linked-lists/linked_list.rb b/01-data-structures/03-linked-lists/linked_list.rb index 5ee2a533..3dfea23c 100644 --- a/01-data-structures/03-linked-lists/linked_list.rb +++ b/01-data-structures/03-linked-lists/linked_list.rb @@ -4,27 +4,91 @@ class LinkedList attr_accessor :head attr_accessor :tail + def initialize + @head = nil + @tail = @head + end + # This method creates a new `Node` using `data`, and inserts it at the end of the list. def add_to_tail(node) + if !@head + @head = node + @tail = node + else + currentNode = @head + + while(currentNode.next != nil) + currentNode = currentNode.next + end + + currentNode.next = node + @tail = node + end end # This method removes the last node in the lists and must keep the rest of the list intact. def remove_tail + currentNode = @head + + while currentNode.next + previousNode = currentNode + currentNode = currentNode.next + end + + if @head.next + currentNode = nil + previousNode.next = nil + @tail = previousNode + else + @head = nil + @tail = nil + end + end # This method prints out a representation of the list. def print + currentNode = @head + + while currentNode + puts currentNode.data + currentNode = currentNode.next + end end # This method removes `node` from the list and must keep the rest of the list intact. def delete(node) + currentNode = @head + + if(node == @head) + remove_front + + elsif(node == @tail) + remove_tail + + else + + while currentNode != node + previousNode = currentNode + currentNode = currentNode.next + end + + previousNode.next = currentNode.next + currentNode = 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) + old_head = @head + @head = node + @head.next = old_head 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 + front = @head + @head = @head.next + front end -end \ No newline at end of file +end diff --git a/01-data-structures/03-linked-lists/linked_list_spec.rb b/01-data-structures/03-linked-lists/linked_list_spec.rb index cabef225..bf236ace 100644 --- a/01-data-structures/03-linked-lists/linked_list_spec.rb +++ b/01-data-structures/03-linked-lists/linked_list_spec.rb @@ -89,4 +89,4 @@ expect(llist.head).to eq nil end end -end \ No newline at end of file +end diff --git a/01-data-structures/03-linked-lists/node.rb b/01-data-structures/03-linked-lists/node.rb index 016acb90..326bdcc0 100644 --- a/01-data-structures/03-linked-lists/node.rb +++ b/01-data-structures/03-linked-lists/node.rb @@ -3,5 +3,7 @@ class Node attr_accessor :data def initialize(data) + @data = data + @next = nil end -end \ No newline at end of file +end From 8d60abe511f835d05d788f290da3f653f8b642c0 Mon Sep 17 00:00:00 2001 From: Conor Souhrada Date: Tue, 9 Oct 2018 12:53:46 -0600 Subject: [PATCH 4/4] assignment completion --- .../03-linked-lists/linked-lists-answers.txt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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 ba89be7f..28d717d9 100644 --- a/01-data-structures/03-linked-lists/linked-lists-answers.txt +++ b/01-data-structures/03-linked-lists/linked-lists-answers.txt @@ -1,4 +1,15 @@ -spacial Locality involves accessing items that are close together in memory, as + Spacial Locality involves accessing items that are close together in memory, as in an array. This benefits performance as it limits the number of RAM lookups which are more time consuming. - + + I have created the benchmark tests for the array and the linked list. I found +array out performed the linked list in all 3 tests. When creating the 10,000 item +array and appending 10,000 items to the linked list the array completed slightly +more then 4 seconds before the linked list. The list had to move through all previous +nodes to add to the tail node, explaining why it took so much longer. Accessing +the 5,000th element and deleting the 5,000 element were much closer as far as time +but the array was still faster in both tests. + + I have attached links to my benchmark tests on repl.it. +Array Benchmarks: https://repl.it/@ConSou/Array-Benchmark +Linked List Benchmarks: https://repl.it/@ConSou/Linked-List-Benchmark