Решение на Втора задача от Кристиан Цветков

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

Към профила на Кристиан Цветков

Резултати

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

Код

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
occupied_positions = food + snake
xs = (((0...dimensions[:width]).to_a) * dimensions[:height]).sort
ys = ((0...dimensions[:height]).to_a) * dimensions[:width]
all_positions = xs.zip ys
return (all_positions - occupied_positions).first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(position, dimensions)
x = position[0]
y = position[1]
return x < 0 || x >= dimensions[:width] ||
y < 0 || y >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end

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

.F.F.F...........F..

Failures:

  1) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151026-15631-1i8zd29/solution.rb:38:in `next_position'
     # /tmp/d20151026-15631-1i8zd29/solution.rb:3:in `move'
     # /tmp/d20151026-15631-1i8zd29/spec.rb:12:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) #grow grows snake up/right/left/down
     Failure/Error: expect(grow(snake, [1, 0])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-1i8zd29/spec.rb:27:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) #grow does not mutate the given arguments
     Failure/Error: expect { grow(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-1i8zd29/spec.rb:38:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) #danger? returns true if obstacle in two turns
     Failure/Error: expect(danger?([[6, 6], [7, 6], [8, 6]], [1, 0], dimensions)).to eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-1i8zd29/spec.rb:113:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01499 seconds
20 examples, 4 failures

Failed examples:

rspec /tmp/d20151026-15631-1i8zd29/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-1i8zd29/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-1i8zd29/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-1i8zd29/spec.rb:112 # #danger? returns true if obstacle in two turns

История (8 версии и 0 коментара)

Кристиан обнови решението на 18.10.2015 10:15 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.drop(1)
+ new_snake << next_position(new_snake.last, direction)
+end
+
+def grow(snake, direction)
+ snake << next_position(snake.last, direction)
+end
+
+def new_food(food, snake, dimensions)
+=begin
+ unavailable_coordinates = food + snake
+ (0...dimensions[:width]).each do |x|
+ (0...dimensions[:height]).each do |y|
+ return [x, y] unless unavailable_coordinates.include? [x, y]
+ end
+ end
+=end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = next_position(snake.last, direction)
+ snake.include?(next_position) ||
+ breaking_the_wall?(next_position, dimensions)
+end
+
+def danger?(snake, direction, dimensions)
+ snake_next_turn = snake + next_position(snake.last, direction)
+ obstacle_ahead?(snake, direction, dimensions) ||
+ obstacle_ahead?(snake_next_turn, direction, dimensions)
+end
+
+def breaking_the_wall?(coordinates, dimensions)
+ x_axis = coordinates[0]
+ y_axis = coordinates[1]
+ return x_axis < 0 || x_axis >= dimensions[:width] ||
+ y_axis < 0 || y_axis >= dimensions[:height]
+end
+
+def next_position(head, direction)
+ [head[0] + direction[0], head[1] + direction[1]]
+end

Кристиан обнови решението на 18.10.2015 17:59 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
-=begin
unavailable_coordinates = food + snake
- (0...dimensions[:width]).each do |x|
- (0...dimensions[:height]).each do |y|
- return [x, y] unless unavailable_coordinates.include? [x, y]
- end
- end
-=end
+ taken_xs = unavailable_coordinates.map { |coordinate| coordinate.first }
+ xs = get_not_taken_coordinate(taken_xs, dimensions[:width])
+ taken_ys = unavailable_coordinates.map { |coordinate| coordinate.last }
+ ys = get_not_taken_coordinate(taken_ys, dimensions[:height])
+ [xs, ys]
+end
+
+def get_not_taken_coordinate(occurrences, max_occurrence)
+ # create a hash with number occurence
+ occurrences_count =
+ occurrences.each_with_object(Hash.new(0)) { |obj, h| h[obj] += 1 }
+ first_not_taken = occurrences_count.select { |k, v| v < max_occurrence }
+ .keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(coordinates, dimensions)
x_axis = coordinates[0]
y_axis = coordinates[1]
return x_axis < 0 || x_axis >= dimensions[:width] ||
y_axis < 0 || y_axis >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end

Кристиан обнови решението на 18.10.2015 18:01 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
unavailable_coordinates = food + snake
taken_xs = unavailable_coordinates.map { |coordinate| coordinate.first }
xs = get_not_taken_coordinate(taken_xs, dimensions[:width])
taken_ys = unavailable_coordinates.map { |coordinate| coordinate.last }
ys = get_not_taken_coordinate(taken_ys, dimensions[:height])
[xs, ys]
end
def get_not_taken_coordinate(occurrences, max_occurrence)
- # create a hash with number occurence
occurrences_count =
occurrences.each_with_object(Hash.new(0)) { |obj, h| h[obj] += 1 }
first_not_taken = occurrences_count.select { |k, v| v < max_occurrence }
.keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(coordinates, dimensions)
x_axis = coordinates[0]
y_axis = coordinates[1]
return x_axis < 0 || x_axis >= dimensions[:width] ||
y_axis < 0 || y_axis >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end

Кристиан обнови решението на 18.10.2015 18:18 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
- unavailable_coordinates = food + snake
- taken_xs = unavailable_coordinates.map { |coordinate| coordinate.first }
- xs = get_not_taken_coordinate(taken_xs, dimensions[:width])
- taken_ys = unavailable_coordinates.map { |coordinate| coordinate.last }
- ys = get_not_taken_coordinate(taken_ys, dimensions[:height])
+ taken_coordinates = food + snake
+ xs_occurrences = taken_coordinates.map { |coordinate| coordinate.first }
+ xs = get_not_taken_coordinate(xs_occurrences, dimensions[:width])
+ ys_occurrences = taken_coordinates.map { |coordinate| coordinate.last }
+ ys = get_not_taken_coordinate(ys_occurrences, dimensions[:height])
[xs, ys]
end
def get_not_taken_coordinate(occurrences, max_occurrence)
occurrences_count =
occurrences.each_with_object(Hash.new(0)) { |obj, h| h[obj] += 1 }
first_not_taken = occurrences_count.select { |k, v| v < max_occurrence }
.keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(coordinates, dimensions)
- x_axis = coordinates[0]
- y_axis = coordinates[1]
+ x_axis = coordinates.first
+ y_axis = coordinates.last
return x_axis < 0 || x_axis >= dimensions[:width] ||
y_axis < 0 || y_axis >= dimensions[:height]
end
def next_position(head, direction)
- [head[0] + direction[0], head[1] + direction[1]]
+ [head.first + direction.first, head.last + direction.last]
end

Кристиан обнови решението на 18.10.2015 19:02 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
- taken_coordinates = food + snake
- xs_occurrences = taken_coordinates.map { |coordinate| coordinate.first }
- xs = get_not_taken_coordinate(xs_occurrences, dimensions[:width])
- ys_occurrences = taken_coordinates.map { |coordinate| coordinate.last }
- ys = get_not_taken_coordinate(ys_occurrences, dimensions[:height])
- [xs, ys]
+ occupied_positions = food + snake
+
+ xs = occupied_positions.map { |position| position[0] }
+ x = get_unassigned_coordinate(xs, dimensions[:width])
+
+ ys = occupied_positions.map { |position| position[1] }
+ y = get_unassigned_coordinate(ys, dimensions[:height])
+
+ [x, y]
end
-def get_not_taken_coordinate(occurrences, max_occurrence)
- occurrences_count =
- occurrences.each_with_object(Hash.new(0)) { |obj, h| h[obj] += 1 }
- first_not_taken = occurrences_count.select { |k, v| v < max_occurrence }
- .keys.first
+def get_unassigned_coordinate(assigned_coordinates, max_occurrence)
+ coordinates_with_occurrence =
+ assigned_coordinates.each_with_object(Hash.new(0)) { |c, h| h[c] += 1 }
+ first_not_taken = coordinates_with_occurrence
+ .select { |k, v| v < max_occurrence }.keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
-def breaking_the_wall?(coordinates, dimensions)
- x_axis = coordinates.first
- y_axis = coordinates.last
- return x_axis < 0 || x_axis >= dimensions[:width] ||
- y_axis < 0 || y_axis >= dimensions[:height]
+def breaking_the_wall?(position, dimensions)
+ x = position[0]
+ y = position[1]
+ return x < 0 || x >= dimensions[:width] ||
+ y < 0 || y >= dimensions[:height]
end
def next_position(head, direction)
- [head.first + direction.first, head.last + direction.last]
+ [head[0] + direction[0], head[1] + direction[1]]
end

Кристиан обнови решението на 18.10.2015 19:08 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
occupied_positions = food + snake
xs = occupied_positions.map { |position| position[0] }
x = get_unassigned_coordinate(xs, dimensions[:width])
ys = occupied_positions.map { |position| position[1] }
y = get_unassigned_coordinate(ys, dimensions[:height])
[x, y]
end
def get_unassigned_coordinate(assigned_coordinates, max_occurrence)
coordinates_with_occurrence =
assigned_coordinates.each_with_object(Hash.new(0)) { |c, h| h[c] += 1 }
- first_not_taken = coordinates_with_occurrence
+ first_unassigned = coordinates_with_occurrence
.select { |k, v| v < max_occurrence }.keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(position, dimensions)
x = position[0]
y = position[1]
return x < 0 || x >= dimensions[:width] ||
y < 0 || y >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end

Кристиан обнови решението на 19.10.2015 09:17 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
occupied_positions = food + snake
-
- xs = occupied_positions.map { |position| position[0] }
- x = get_unassigned_coordinate(xs, dimensions[:width])
-
- ys = occupied_positions.map { |position| position[1] }
- y = get_unassigned_coordinate(ys, dimensions[:height])
-
- [x, y]
+ xs = (((0...dimensions[:width]).to_a) * dimensions[:height]).sort
+ ys = ((0...dimensions[:height]).to_a) * dimensions[:width]
+ all_positions = xs.zip ys
+ return (all_positions - occupied_positions).first
end
def get_unassigned_coordinate(assigned_coordinates, max_occurrence)
coordinates_with_occurrence =
assigned_coordinates.each_with_object(Hash.new(0)) { |c, h| h[c] += 1 }
first_unassigned = coordinates_with_occurrence
.select { |k, v| v < max_occurrence }.keys.first
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(position, dimensions)
x = position[0]
y = position[1]
return x < 0 || x >= dimensions[:width] ||
y < 0 || y >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end

Кристиан обнови решението на 19.10.2015 09:18 (преди над 9 години)

def move(snake, direction)
new_snake = snake.drop(1)
new_snake << next_position(new_snake.last, direction)
end
def grow(snake, direction)
snake << next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
occupied_positions = food + snake
xs = (((0...dimensions[:width]).to_a) * dimensions[:height]).sort
ys = ((0...dimensions[:height]).to_a) * dimensions[:width]
all_positions = xs.zip ys
return (all_positions - occupied_positions).first
end
-def get_unassigned_coordinate(assigned_coordinates, max_occurrence)
- coordinates_with_occurrence =
- assigned_coordinates.each_with_object(Hash.new(0)) { |c, h| h[c] += 1 }
- first_unassigned = coordinates_with_occurrence
- .select { |k, v| v < max_occurrence }.keys.first
-end
-
def obstacle_ahead?(snake, direction, dimensions)
next_position = next_position(snake.last, direction)
snake.include?(next_position) ||
breaking_the_wall?(next_position, dimensions)
end
def danger?(snake, direction, dimensions)
snake_next_turn = snake + next_position(snake.last, direction)
obstacle_ahead?(snake, direction, dimensions) ||
obstacle_ahead?(snake_next_turn, direction, dimensions)
end
def breaking_the_wall?(position, dimensions)
x = position[0]
y = position[1]
return x < 0 || x >= dimensions[:width] ||
y < 0 || y >= dimensions[:height]
end
def next_position(head, direction)
[head[0] + direction[0], head[1] + direction[1]]
end