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

Intro to data structures assignment #152

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@ 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

def index(person)
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
end
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down