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..27ada4ac 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,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. 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..6fa6616e 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,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 \ 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..7fe4bb90 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) + 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 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..cbbd3a65 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,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 \ No newline at end of file +end