Last active
December 3, 2019 23:41
-
-
Save kellyjandrews/b4dd66042cfe7e38a93dad46e0630269 to your computer and use it in GitHub Desktop.
Bootcamp Challenges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image | |
def initialize(image) | |
@image = image | |
end | |
def displayImage | |
@image.each{|row| puts row.join} | |
end | |
def blur(dist = 1) | |
coords = [] | |
@image.each_with_index do |row_data, row| | |
row_data.each_with_index do |col_data, col| | |
coords << [row, col] if col_data == 1 | |
end | |
end | |
coords.each do |coord| | |
row,col = coord | |
1.upto(dist) do |d| | |
0.upto(dist-d) do |i| | |
# top-left quadrant | |
@image[row - i][col - d] = 1 unless row - i < 0 || col + d >= @image.length | |
# top-right quadrant | |
@image[row - d][col + i] = 1 unless row - d < 0 || col + i >= @image.length | |
# bottom-right quadrant | |
@image[row + i][col + d] = 1 unless row + i >= @image.length || col + d >= @image.length | |
# bottom-left quadrant | |
@image[row + d][col - i] = 1 unless row + d >= @image.length || col - i < 0 | |
end | |
end | |
end | |
self.displayImage | |
end | |
end | |
image = Image.new([ | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0] | |
]) | |
image.blur(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image | |
def initialize(image) | |
@image = image | |
end | |
def displayImage | |
@image.each{|row| puts row.join} | |
end | |
def blur(dist = 1) | |
coords = [] | |
@image.each_with_index do |row_data, row| | |
row_data.each_with_index do |col_data, col| | |
coords << [row, col] if col_data == 1 | |
end | |
end | |
coords.each do |coord| | |
row,col = coord | |
dist.downto(-dist) do |d| | |
# center column | |
unless row - d < 0 || row + d >= @image.length | |
@image[row - d][col] = 1 | |
1.upto(dist-d.abs) do |i| | |
#right side | |
@image[row - d][col + i] = 1 unless col + i >= @image.length | |
#left side | |
@image[row - d][col - i] = 1 unless col - i < 0 | |
end | |
end | |
end | |
end | |
self.displayImage | |
end | |
end | |
image = Image.new([ | |
[1,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0] | |
]) | |
image.blur(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedListNode | |
attr_accessor :value, :next_node | |
def initialize(value, next_node=nil) | |
@value = value | |
@next_node = next_node | |
end | |
end | |
class Stack | |
attr_reader :data | |
def initialize | |
@data = nil | |
end | |
def push(value) | |
@data = LinkedListNode.new(value, @data) | |
end | |
def pop | |
if @data then | |
r = @data.value; | |
@data = @data.next_node | |
else | |
r = nil | |
end | |
return r | |
end | |
end | |
def print_list(stack) | |
data = stack.data | |
begin | |
print "#{data.value} --> " | |
data = data.next_node | |
end while data | |
print "nil\n" | |
return | |
end | |
def reverse_list(stack) | |
reverse_stack = Stack.new | |
while stack.data | |
reverse_stack.push(stack.pop); | |
end | |
return print_list(reverse_stack) | |
end | |
myStack = Stack.new | |
myStack.push(37) | |
myStack.push(99) | |
myStack.push(12) | |
myStack.push(544) | |
myStack.push(34) | |
myStack.push(87) | |
myStack.push(2) | |
print_list(myStack) | |
puts "-------" | |
reverse_list(myStack) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment