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

Checkpoint 2 stacks and queues #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added 01-data-structures/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include RSpec

require_relative 'line'
require_relative 'line'

RSpec.describe Line, type: Class do
let(:line) { Line.new }
Expand Down Expand Up @@ -59,4 +59,4 @@
end
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion 01-data-structures/02-stacks-and-queues/myqueue/myqueue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 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,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
end
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
end