From 964da7f52da37744f740d07f2be3ce05ac6e7db1 Mon Sep 17 00:00:00 2001 From: nahome Date: Fri, 8 Jun 2018 23:18:18 -0700 Subject: [PATCH] Intro to data structures --- .../intro-answers.txt | 24 +++++++++++++++++++ .../line/line.rb | 9 ++++++- .../screen/pixel.rb | 12 ++++++++++ .../screen/screen.rb | 10 +++++++- 4 files changed, 53 insertions(+), 2 deletions(-) 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..c8968fd9 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,24 @@ +1. A line of people at an amusement park ride. + + The data of the people in line is structured as an array, + so developers can access and manipulate the data using Array + manipulation techniques. + + A developer can find a specific element by using the index + of the element. + + This data structure can be used to organize the orders of + customers in a restaurant in a first comes first served basis. + + 2. Pixels on a computer screen + + Pixels are created in three colors and validated so that the + color coordinate values are not out of boundary.After a pixel + is created it can be inserted to a specific coordinate on the + screen using two dimensional array. A pixels x and y coordinates + can't be negative or greater than the width and array. + + A developer can access a pixel by using its width and array coordinates. + + This data structure can be used to create a sitting arrangement for + different depending on peoples relationships, ranks or age. 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..eacbbc2d 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) + self.members.push(person) end def leave(person) + self.members.slice!(self.members.index(person)) end def front + self.members.first end def middle + self.members[()(members.length/2).ciel)] end def back + self.members.last end def search(person) + self.members.include?(person) ? person : nil; end private def index(person) + self.members.index(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..83ef51cb 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 > 255 + return color = 255 + elsif color < 0 + return color = 0 + else + return 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..7ede073a 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,26 @@ class Screen attr_accessor :matrix def initialize(width, height) + @width = width + @height = height + @matrix = Array.new(width) { Array.new(height) } end # Insert a Pixel at x, y def insert(pixel, x, y) + @matrix[x][y] = pixel end def at(x, y) + return @matrix[x][y] end private def inbounds(x, y) + if (x < 0 || x > width) || (y < 0 || y > height) + pixel = nil + end -end \ No newline at end of file +end