Решение на Шеста задача от Никола Терзиев

Обратно към всички решения

Към профила на Никола Терзиев

Резултати

  • 6 точки от тестове
  • 1 отнета точка
  • 5 точки общо
  • 14 успешни тест(а)
  • 0 неуспешни тест(а)

Код

module TurtleGraphics
class Turtle
DIRECTIONS = {
'left': [0, -1],
'right': [0, 1],
'up': [-1, 0],
'down': [1, 0]
}
DIRECTIONS_SEQUENCE = { 'left': 0, 'up': 1, 'right': 2, 'down': 3 }
def initialize(rows, cols)
@rows = rows
@cols = cols
@travel_matrix = Array.new(rows) { Array.new(cols) { |_| 0 } }
@travel_matrix[0][0] = 1
@location = [0, 0]
@direction = :right
end
def draw(draw_function = nil, &block)
instance_eval &block if block_given?
if draw_function.nil?
@travel_matrix
else
draw_function.draw @travel_matrix, max_steps_in_cell
end
end
def max_steps_in_cell
@travel_matrix.map do |row|
row.max
end.max
end
def spawn_at(row, col)
# Should only be called after initialize
@travel_matrix[@location[0]][@location[1]] = 0
@location = [row, col]
@travel_matrix[row][col] = 1
end
def move
direction = DIRECTIONS[@direction]
row = _translate(@location[0] + direction[0], @rows)
col = _translate(@location[1] + direction[1], @cols)
@location[0] = row
@location[1] = col
@travel_matrix[row][col] += 1
end
def _translate(current, total)
if current >= total
current = 0
elsif current < 0
current = total - 1
end
current
end
def look(orientation)
@direction = orientation
end
def turn_left
_turn(:left)
end
def turn_right
_turn(:right)
end
def _turn(direction)
if direction == :right
step = 1
elsif direction == :left
step = -1
end
next_orientation = DIRECTIONS_SEQUENCE[@direction.to_sym] + step
if next_orientation < 0
next_orientation = DIRECTIONS_SEQUENCE.length - 1
elsif next_orientation >= DIRECTIONS_SEQUENCE.length
next_orientation = 0
end
@direction = DIRECTIONS_SEQUENCE.key(next_orientation)
end
end
module Canvas
class BaseDraw
def get_intensity_ratio(cell, max_steps)
cell == 0 ? 0 : cell / max_steps.to_f
end
end
class ASCII < BaseDraw
def initialize(intensity_array)
@intensity_array = intensity_array
intensity_period = 1 / (intensity_array.length.to_f - 1)
@intensity_periods = [0]
@intensity_array[1..-1].each_with_index do |_, index|
@intensity_periods << intensity_period * (index + 1)
end
end
def draw(travel_matrix, max_steps)
travel_matrix.map do |row|
row.map do |cell|
get_cell_char cell, max_steps
end.join
end.join("\n")
end
def get_cell_char(cell, max_steps)
intensity_ratio = get_intensity_ratio cell, max_steps
index = get_intensity_index intensity_ratio
@intensity_array[index]
end
def get_intensity_index(intensity_ratio)
@intensity_periods.find_index { |period| intensity_ratio <= period }
end
end
class HTML < BaseDraw
def initialize(size)
@size = size
@table = ''
end
def html_template
'<!DOCTYPE html><html><head><title>Turtle graphics</title>' +\
'<style> table {border-spacing: 0;} tr {padding: 0;} ' +\
"td {width: #{@size}px; height: #{@size}px; " +\
'background-color: black; padding: 0; }' +\
"</style></head><body><table>#{@table}</table></body></html>"
end
def draw(travel_matrix, max_steps)
generate_table travel_matrix, max_steps
html_template
end
def generate_table(travel_matrix, max_steps)
@table = travel_matrix.map do |row|
generate_tr row, max_steps
end.join
end
def generate_tr(row, max_steps)
table_row = row.map do |cell|
generate_td cell, max_steps
end.join
"<tr>#{table_row}</tr>"
end
def generate_td(cell, max_steps)
intensity = get_intensity_ratio cell, max_steps
"<td style=\"opacity: #{format('%.2f', intensity)}\"></td>"
end
end
end
end

Лог от изпълнението

..............

Finished in 0.01205 seconds
14 examples, 0 failures

История (4 версии и 2 коментара)

Никола обнови решението на 29.11.2015 03:42 (преди над 8 години)

+require 'bigdecimal'
+
+module TurtleGraphics
+ class Turtle
+ DIRECTIONS = {
+ 'left': [0, -1],
+ 'right': [0, 1],
+ 'up': [-1, 0],
+ 'down': [1, 0]
+ }
+ DIRECTIONS_SEQUENCE = { 'left': 0, 'up': 1, 'right': 2, 'down': 3 }
+ def initialize(rows, cols)
+ @rows = rows
+ @cols = cols
+ @travel_matrix = Array.new(rows) { Array.new(cols) { |_| 0 } }
+ @travel_matrix[0][0] = 1
+ @location = [0, 0]
+ @direction = :right
+ end
+
+ def draw(draw_function = nil, &block)
+ instance_eval &block if block_given?
+ if draw_function.nil?
+ @travel_matrix
+ else
+ draw_function.draw @travel_matrix, max_steps_in_cell
+ end
+ end
+
+ def max_steps_in_cell
+ @travel_matrix.map do |row|
+ row.max
+ end.max
+ end
+
+ def spawn_at(row, col)
+ # Should only be called after initialize
+ @travel_matrix[0][0] = 0
+ @location = [row, col]
+ @travel_matrix[row][col] = 1
+ end
+
+ def move
+ direction = DIRECTIONS[@direction]
+ row = _translate(@location[0] + direction[0], @rows)
+ col = _translate(@location[1] + direction[1], @cols)
+ @location[0] = row
+ @location[1] = col
+ @travel_matrix[row][col] += 1
+ end
+
+ def _translate(current, total)
+ if current >= total
+ current = 0
+ elsif current < 0
+ current = total - 1
+ end
+ current
+ end
+
+ def look(orientation)
+ @direction = orientation.to_s
+ end
+
+ def turn_left
+ _turn(:left)
+ end
+
+ def turn_right
+ _turn(:right)
+ end
+
+ def _turn(direction)
+ if direction == :right
+ step = 1
+ elsif direction == :left
+ step = -1
+ end
+ next_orientation = DIRECTIONS_SEQUENCE[@direction.to_sym] + step
+ if next_orientation < 0
+ next_orientation = DIRECTIONS_SEQUENCE.length - 1
+ elsif next_orientation >= DIRECTIONS_SEQUENCE.length
+ next_orientation = 0
+ end
+ @direction = DIRECTIONS_SEQUENCE.key(next_orientation)
+ end
+ end
+
+ module Canvas
+ class BaseDraw
+ def get_intensity_ratio(cell, max_steps)
+ decimal_cell = BigDecimal.new(cell.to_s)
+ decimal_max_steps = BigDecimal.new(max_steps.to_s)
+ cell == 0 ? 0 : decimal_cell / decimal_max_steps
+ end
+ end
+
+ class ASCII < BaseDraw
+ def initialize(intensity_array)
+ @intensity_array = intensity_array
+ end
+
+ def draw(travel_matrix, max_steps)
+ travel_matrix.map do |row|
+ row.map do |cell|
+ get_cell_char cell, max_steps
+ end.join
+ end.join('\n')
+ end
+
+ def get_cell_char(cell, max_steps)
+ intensity_ratio = get_intensity_ratio cell, max_steps
+ get_intensity_index intensity_ratio.floor(1)
+ end
+
+ def get_intensity_index(intensity_ratio)
+ # Should have edge case checks
+ if intensity_ratio == 0
+ @intensity_array[0]
+ elsif intensity_ratio <= BigDecimal.new('0.3')
+ @intensity_array[1]
+ elsif intensity_ratio <= BigDecimal.new('0.6')
+ @intensity_array[2]
+ elsif intensity_ratio <= 1
+ @intensity_array[3]
+ end
+ end
+ end
+
+ class HTML < BaseDraw
+ def initialize(size)
+ @size = size
+ @table = ''
+ end
+
+ def html_template
+ '<!DOCTYPE html><html><head><title>Turtle graphics</title>' +\
+ '<style> table {border-spacing: 0;} tr {padding: 0;} ' +\
+ "td {width: #{@size}px; height: #{@size}px; " +\
+ 'background-color: black; padding: 0; }' +\
+ "</style></head><body><table>#{@table}</table></body></html>"
+ end
+
+ def draw(travel_matrix, max_steps)
+ generate_table travel_matrix, max_steps
+ html_template
+ end
+
+ def generate_table(travel_matrix, max_steps)
+ @table = travel_matrix.map do |row|
+ generate_tr row, max_steps
+ end.join
+ end
+
+ def generate_tr(row, max_steps)
+ table_row = row.map do |cell|
+ generate_td cell, max_steps
+ end.join
+ "<tr>#{table_row}</tr>"
+ end
+
+ def generate_td(cell, max_steps)
+ intensity = get_intensity_ratio cell, max_steps
+ "<td style=\"opacity: #{format('%.2f', intensity)}\"></td>"
+ end
+ end
+ end
+end

Никола обнови решението на 29.11.2015 15:30 (преди над 8 години)

-require 'bigdecimal'
-
module TurtleGraphics
class Turtle
DIRECTIONS = {
'left': [0, -1],
'right': [0, 1],
'up': [-1, 0],
'down': [1, 0]
}
DIRECTIONS_SEQUENCE = { 'left': 0, 'up': 1, 'right': 2, 'down': 3 }
def initialize(rows, cols)
@rows = rows
@cols = cols
@travel_matrix = Array.new(rows) { Array.new(cols) { |_| 0 } }
@travel_matrix[0][0] = 1
@location = [0, 0]
@direction = :right
end
def draw(draw_function = nil, &block)
instance_eval &block if block_given?
if draw_function.nil?
@travel_matrix
else
draw_function.draw @travel_matrix, max_steps_in_cell
end
end
def max_steps_in_cell
@travel_matrix.map do |row|
row.max
end.max
end
def spawn_at(row, col)
# Should only be called after initialize
@travel_matrix[0][0] = 0
@location = [row, col]
@travel_matrix[row][col] = 1
end
def move
direction = DIRECTIONS[@direction]
row = _translate(@location[0] + direction[0], @rows)
col = _translate(@location[1] + direction[1], @cols)
@location[0] = row
@location[1] = col
@travel_matrix[row][col] += 1
end
def _translate(current, total)
if current >= total
current = 0
elsif current < 0
current = total - 1
end
current
end
def look(orientation)
@direction = orientation.to_s
end
def turn_left
_turn(:left)
end
def turn_right
_turn(:right)
end
def _turn(direction)
if direction == :right
step = 1
elsif direction == :left
step = -1
end
next_orientation = DIRECTIONS_SEQUENCE[@direction.to_sym] + step
if next_orientation < 0
next_orientation = DIRECTIONS_SEQUENCE.length - 1
elsif next_orientation >= DIRECTIONS_SEQUENCE.length
next_orientation = 0
end
@direction = DIRECTIONS_SEQUENCE.key(next_orientation)
end
end
module Canvas
class BaseDraw
def get_intensity_ratio(cell, max_steps)
- decimal_cell = BigDecimal.new(cell.to_s)
- decimal_max_steps = BigDecimal.new(max_steps.to_s)
- cell == 0 ? 0 : decimal_cell / decimal_max_steps
+ cell == 0 ? 0 : cell / max_steps.to_f
end
end
class ASCII < BaseDraw
def initialize(intensity_array)
@intensity_array = intensity_array
+ intensity_period = 1 / intensity_array.length.to_f
+ @intensity_periods = []
+ @intensity_array.each_with_index do |_, index|
+ @intensity_periods << intensity_period * (index + 1)
+ end
end
def draw(travel_matrix, max_steps)
travel_matrix.map do |row|
row.map do |cell|
get_cell_char cell, max_steps
end.join
end.join('\n')
end
def get_cell_char(cell, max_steps)
intensity_ratio = get_intensity_ratio cell, max_steps
- get_intensity_index intensity_ratio.floor(1)
+ index = get_intensity_index intensity_ratio
+ @intensity_array[index]
end
def get_intensity_index(intensity_ratio)
- # Should have edge case checks
- if intensity_ratio == 0
- @intensity_array[0]
- elsif intensity_ratio <= BigDecimal.new('0.3')
- @intensity_array[1]
- elsif intensity_ratio <= BigDecimal.new('0.6')
- @intensity_array[2]
- elsif intensity_ratio <= 1
- @intensity_array[3]
- end
+ @intensity_periods.find_index { |period| intensity_ratio <= period }
end
end
class HTML < BaseDraw
def initialize(size)
@size = size
@table = ''
end
def html_template
'<!DOCTYPE html><html><head><title>Turtle graphics</title>' +\
'<style> table {border-spacing: 0;} tr {padding: 0;} ' +\
"td {width: #{@size}px; height: #{@size}px; " +\
'background-color: black; padding: 0; }' +\
"</style></head><body><table>#{@table}</table></body></html>"
end
def draw(travel_matrix, max_steps)
generate_table travel_matrix, max_steps
html_template
end
def generate_table(travel_matrix, max_steps)
@table = travel_matrix.map do |row|
generate_tr row, max_steps
end.join
end
def generate_tr(row, max_steps)
table_row = row.map do |cell|
generate_td cell, max_steps
end.join
"<tr>#{table_row}</tr>"
end
def generate_td(cell, max_steps)
intensity = get_intensity_ratio cell, max_steps
"<td style=\"opacity: #{format('%.2f', intensity)}\"></td>"
end
end
end
end

Никола обнови решението на 29.11.2015 22:54 (преди над 8 години)

module TurtleGraphics
class Turtle
DIRECTIONS = {
'left': [0, -1],
'right': [0, 1],
'up': [-1, 0],
'down': [1, 0]
}
DIRECTIONS_SEQUENCE = { 'left': 0, 'up': 1, 'right': 2, 'down': 3 }
def initialize(rows, cols)
@rows = rows
@cols = cols
@travel_matrix = Array.new(rows) { Array.new(cols) { |_| 0 } }
@travel_matrix[0][0] = 1
@location = [0, 0]
@direction = :right
end
def draw(draw_function = nil, &block)
instance_eval &block if block_given?
if draw_function.nil?
@travel_matrix
else
draw_function.draw @travel_matrix, max_steps_in_cell
end
end
def max_steps_in_cell
@travel_matrix.map do |row|
row.max
end.max
end
def spawn_at(row, col)
# Should only be called after initialize
- @travel_matrix[0][0] = 0
+ @travel_matrix[@location[0]][@location[1]] = 0
@location = [row, col]
@travel_matrix[row][col] = 1
end
def move
+ p @direction
direction = DIRECTIONS[@direction]
row = _translate(@location[0] + direction[0], @rows)
col = _translate(@location[1] + direction[1], @cols)
@location[0] = row
@location[1] = col
@travel_matrix[row][col] += 1
end
def _translate(current, total)
if current >= total
current = 0
elsif current < 0
current = total - 1
end
current
end
def look(orientation)
- @direction = orientation.to_s
+ @direction = orientation
end
def turn_left
_turn(:left)
end
def turn_right
_turn(:right)
end
def _turn(direction)
if direction == :right
step = 1
elsif direction == :left
step = -1
end
next_orientation = DIRECTIONS_SEQUENCE[@direction.to_sym] + step
if next_orientation < 0
next_orientation = DIRECTIONS_SEQUENCE.length - 1
elsif next_orientation >= DIRECTIONS_SEQUENCE.length
next_orientation = 0
end
@direction = DIRECTIONS_SEQUENCE.key(next_orientation)
end
end
module Canvas
class BaseDraw
def get_intensity_ratio(cell, max_steps)
cell == 0 ? 0 : cell / max_steps.to_f
end
end
class ASCII < BaseDraw
def initialize(intensity_array)
@intensity_array = intensity_array
intensity_period = 1 / intensity_array.length.to_f
@intensity_periods = []
@intensity_array.each_with_index do |_, index|
@intensity_periods << intensity_period * (index + 1)
end
end
def draw(travel_matrix, max_steps)
travel_matrix.map do |row|
row.map do |cell|
get_cell_char cell, max_steps
end.join
end.join('\n')
end
def get_cell_char(cell, max_steps)
intensity_ratio = get_intensity_ratio cell, max_steps
index = get_intensity_index intensity_ratio
@intensity_array[index]
end
def get_intensity_index(intensity_ratio)
@intensity_periods.find_index { |period| intensity_ratio <= period }
end
end
class HTML < BaseDraw
def initialize(size)
@size = size
@table = ''
end
def html_template
'<!DOCTYPE html><html><head><title>Turtle graphics</title>' +\
'<style> table {border-spacing: 0;} tr {padding: 0;} ' +\
"td {width: #{@size}px; height: #{@size}px; " +\
'background-color: black; padding: 0; }' +\
"</style></head><body><table>#{@table}</table></body></html>"
end
def draw(travel_matrix, max_steps)
generate_table travel_matrix, max_steps
html_template
end
def generate_table(travel_matrix, max_steps)
@table = travel_matrix.map do |row|
generate_tr row, max_steps
end.join
end
def generate_tr(row, max_steps)
table_row = row.map do |cell|
generate_td cell, max_steps
end.join
"<tr>#{table_row}</tr>"
end
def generate_td(cell, max_steps)
intensity = get_intensity_ratio cell, max_steps
"<td style=\"opacity: #{format('%.2f', intensity)}\"></td>"
end
end
end
end

Здравей!

Малко коментари:

  • Излишно е да предаваш max_steps_in_cell на draw. Нищо не ти гарантира, че подаденият canvas ще го използва. Така ако има canvas, който не го използва ще трябва да го игнорира. Достатъчно лесно е да се сметне в самия canvas, ако му трябва.
  • Усещам духа на Python в кода. :) В Ruby има private методи - използвай ги, вместо да ги префиксваш с долни черти.
  • Забравил си едно принтиране - махни го.
  • turn може да се опрости, ако използваш масив вместо хеш за посоките. Можеш ли да се сетиш как? Също, тази проверка за :left и :right е ненужна, тъй като можеш просто да подаваш 1 или -1 на метода.
  • intensity_periods е ненужно и може да се замени с много проста математическа формула. Помисли как да превърнеш интензитет в индекс.
  • +\ не е добър начин за правене на многоредов низ. Само + или \ биха свършили работа. Но по-добрият вариант е с heredoc или като темплейт (за оператора за форматиране на низове - % или format)

Никола обнови решението на 01.12.2015 23:43 (преди над 8 години)

module TurtleGraphics
class Turtle
DIRECTIONS = {
'left': [0, -1],
'right': [0, 1],
'up': [-1, 0],
'down': [1, 0]
}
DIRECTIONS_SEQUENCE = { 'left': 0, 'up': 1, 'right': 2, 'down': 3 }
def initialize(rows, cols)
@rows = rows
@cols = cols
@travel_matrix = Array.new(rows) { Array.new(cols) { |_| 0 } }
@travel_matrix[0][0] = 1
@location = [0, 0]
@direction = :right
end
def draw(draw_function = nil, &block)
instance_eval &block if block_given?
if draw_function.nil?
@travel_matrix
else
draw_function.draw @travel_matrix, max_steps_in_cell
end
end
def max_steps_in_cell
@travel_matrix.map do |row|
row.max
end.max
end
def spawn_at(row, col)
# Should only be called after initialize
@travel_matrix[@location[0]][@location[1]] = 0
@location = [row, col]
@travel_matrix[row][col] = 1
end
def move
- p @direction
direction = DIRECTIONS[@direction]
row = _translate(@location[0] + direction[0], @rows)
col = _translate(@location[1] + direction[1], @cols)
@location[0] = row
@location[1] = col
@travel_matrix[row][col] += 1
end
def _translate(current, total)
if current >= total
current = 0
elsif current < 0
current = total - 1
end
current
end
def look(orientation)
@direction = orientation
end
def turn_left
_turn(:left)
end
def turn_right
_turn(:right)
end
def _turn(direction)
if direction == :right
step = 1
elsif direction == :left
step = -1
end
next_orientation = DIRECTIONS_SEQUENCE[@direction.to_sym] + step
if next_orientation < 0
next_orientation = DIRECTIONS_SEQUENCE.length - 1
elsif next_orientation >= DIRECTIONS_SEQUENCE.length
next_orientation = 0
end
@direction = DIRECTIONS_SEQUENCE.key(next_orientation)
end
end
module Canvas
class BaseDraw
def get_intensity_ratio(cell, max_steps)
cell == 0 ? 0 : cell / max_steps.to_f
end
end
class ASCII < BaseDraw
def initialize(intensity_array)
@intensity_array = intensity_array
- intensity_period = 1 / intensity_array.length.to_f
- @intensity_periods = []
- @intensity_array.each_with_index do |_, index|
+ intensity_period = 1 / (intensity_array.length.to_f - 1)
+ @intensity_periods = [0]
+ @intensity_array[1..-1].each_with_index do |_, index|
@intensity_periods << intensity_period * (index + 1)
end
end
def draw(travel_matrix, max_steps)
travel_matrix.map do |row|
row.map do |cell|
get_cell_char cell, max_steps
end.join
- end.join('\n')
+ end.join("\n")
end
def get_cell_char(cell, max_steps)
intensity_ratio = get_intensity_ratio cell, max_steps
index = get_intensity_index intensity_ratio
@intensity_array[index]
end
def get_intensity_index(intensity_ratio)
@intensity_periods.find_index { |period| intensity_ratio <= period }
end
end
class HTML < BaseDraw
def initialize(size)
@size = size
@table = ''
end
def html_template
'<!DOCTYPE html><html><head><title>Turtle graphics</title>' +\
'<style> table {border-spacing: 0;} tr {padding: 0;} ' +\
"td {width: #{@size}px; height: #{@size}px; " +\
'background-color: black; padding: 0; }' +\
"</style></head><body><table>#{@table}</table></body></html>"
end
def draw(travel_matrix, max_steps)
generate_table travel_matrix, max_steps
html_template
end
def generate_table(travel_matrix, max_steps)
@table = travel_matrix.map do |row|
generate_tr row, max_steps
end.join
end
def generate_tr(row, max_steps)
table_row = row.map do |cell|
generate_td cell, max_steps
end.join
"<tr>#{table_row}</tr>"
end
def generate_td(cell, max_steps)
intensity = get_intensity_ratio cell, max_steps
"<td style=\"opacity: #{format('%.2f', intensity)}\"></td>"
end
end
end
end