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

Completed assignment - intro to data structures #177

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,21 @@
1. How does your data structure allow developers to access and manipulate the data?

Both the line and screen data structures allow developers to access and manipulate data through arrays. Line is a simple array
of strings which allows developers to easily add new strings to the end of the array using line.join(person), remove members
using line.leave(person), or find a specific person in line using line.search(person). The screen data structure is a two-dimensional
array which allows developers to model a screen by inserting pixels at specified x,y coordinates using screen.insert(x,y)
or find the pixel at a specified coordinate using screen.at(x,y).

2. If a developer wanted to find a specific element in your data structure, how would you search for it?

Line allows developers to find a specific person using line.search(person) which utilizes the private method line.index(person).
Line.index() returns the index of the string that was searched for and allows developers to return the string at that index in
the line.members array. Screen uses screen.at(x,y) to find the pixel at the specified x,y coordinate. If the x,y coordinate
provided is within the dimensions of the screen, screen.at() will return the pixel data.

3. What other real-world data can each structure represent?

The line data structure is analogous to a queue in online gaming. When a user sends a request to enter a game, they are placed in
a queue. The queue is first in, first out and a user can cancel the request to join (line.leave()). The screen data structure
could also represent a digital picture. A digital picture could also be modeled as a two-dimensional array with specific
pixel information at specific coordinates.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,36 @@ def initialize
end

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

def leave(person)
self.members.delete_at(index(person))
puts self.members
end

def front
self.members.first
end

def middle
index = (self.members.length/2).floor
self.members[index]
end

def back
self.members.last
end

def search(person)
person_index = index(person)
self.members[index(person)] if !person_index.nil?
end

private

def index(person)
self.members.index { |p| p == person }
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)
self.red = validate_color(red)
self.blue = validate_color(blue)
self.green = validate_color(green)
self.x = x
self.y = y
end

private

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

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

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

# Insert a Pixel at x, y
def insert(pixel, x, y)
valid_coordinate = inbounds(x,y)
self.matrix[x][y] = pixel if !valid_coordinate.nil?
end

def at(x, y)
valid_coordinate = inbounds(x,y)
self.matrix[x][y] if !valid_coordinate.nil?
end

private

def inbounds(x, y)
if (x < self.width && x >= 0) && (y < self.height && y >= 0)
return x, y
else
nil
end
end

end
end