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 #143

Open
wants to merge 1 commit 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,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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 > 255
return color = 255
elsif color < 0
return color = 0
else
return color
end
end

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