Зиг-заг

Краен срок
16.10.2013 12:00

Срокът за предаване на решения е отминал

Дефинирайте функция zig_zag(n), която приема като единствен параметър цяло число n > 0 и връща като резултат квадратна матрица n × n, представена като списък от списъци, която съдържа числата от 1 до n², които я изпълват зигзагообразно отгоре надолу, ето така:

────────┐
┌───────┘
└────────

Нечетните редове на матрицата съдържат числа, нарастващи отляво надясно, а четните редове съдържат числа, нарастващи отдясно наляво. Пример за n = 3 и n = 4:

zig_zag(3)
> [[1, 2, 3],
   [6, 5, 4],
   [7, 8, 9]]

zig_zag(4)
> [[1,  2,  3,  4 ],
   [8,  7,  6,  5 ],
   [9,  10, 11, 12],
   [16, 15, 14, 13]]

Няма да получите некоректни данни, така че не се притеснявайте дали n е отрицателно число.

P.S. Видно от sample_spec-a zig_zag(1) следва да връща [[1]]

Решения

Методи Димитров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Методи Димитров
def zig_zag(n)
matrix_array = 1.upto(n ** 2).group_by { |number| number.pred / n }.values
matrix_array.each_with_index.map do |row, index|
index.odd? ? row.reverse : row
end
end
...

Finished in 0.00479 seconds
3 examples, 0 failures
Любомир Георгиев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Любомир Георгиев
#feeling a little bit weird with these naming conventions and "no abbreviations" rule : /
def zig_zag(n)
return_array = []
number_to_start_with = 1
(1..n).each do |i|
inner_array = (number_to_start_with..(number_to_start_with+n-1)).to_a
inner_array.reverse! if i.even?
return_array.push(inner_array)
number_to_start_with = number_to_start_with + n
end
return_array
end
...

Finished in 0.00574 seconds
3 examples, 0 failures
Георги Гърдев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Гърдев
def zig_zag(n)
1.upto(n**2).each_slice(n).map.each_with_index do |slice, index|
index.odd? ? slice.reverse : slice
end
end
...

Finished in 0.01962 seconds
3 examples, 0 failures
Георги Ангелов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Ангелов
def zig_zag(n)
1.upto(n**2).each_slice(n).map.with_index do |row, rowIndex|
if rowIndex.odd?
row.reverse
else
row
end
end
end
...

Finished in 0.00461 seconds
3 examples, 0 failures
Никола Ненков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Никола Ненков
def zig_zag(n)
zig_zagged_array = (1..n**2).each_slice(n).to_a
(0..n.pred).each { |i| zig_zagged_array[i].reverse! if i.odd? }
zig_zagged_array
end
...

Finished in 0.00447 seconds
3 examples, 0 failures
Александър Тахчиев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Тахчиев
public
def zig_zag(n)
slf = (1..(n * n)).to_a.each_slice(n).to_a
slf.each_index do |i|
if i % 2 == 1
slf[i] = slf[i].reverse
end
end
end
...

Finished in 0.00379 seconds
3 examples, 0 failures
Иван Проданов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Проданов
def zig_zag (n)
return nil if n<=0
snake_reverse = ->(line, shouldReverse) { shouldReverse ? line.reverse : line}
(0...n).map{|line| snake_reverse.((line*n+1..line*n+n).to_a, line.odd?)}
end
...

Finished in 0.00457 seconds
3 examples, 0 failures
Красимира Божанова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Красимира Божанова
def zig_zag(n)
1.upto(n*n).each_slice(n).map { |row| (row.last / n).odd? ? row : row.reverse }
end
...

Finished in 0.0046 seconds
3 examples, 0 failures
Цветан Иванов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Цветан Иванов
def zig_zag(n)
matrix = []
limit = (1..n * n).to_a
(0..n - 1).each do |row|
current_row = limit[row * n..row * n + n - 1]
matrix << (row.even? ? current_row : current_row.reverse)
end
matrix
end
...

Finished in 0.00472 seconds
3 examples, 0 failures
Емануела Моллова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Емануела Моллова
def zig_zag(n)
matrix = 1.upto(n*n).each_slice(n).to_a
0.upto(n.pred).select { |index| index.odd? }. map { |index| matrix[index].reverse! }
matrix
end
...

Finished in 0.03352 seconds
3 examples, 0 failures
Деян Хаджиев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Деян Хаджиев
def zig_zag(n)
zig_zag_table = Array.new(n) do |row|
Array.new(n) { |column| row * n + column + 1 }
end
0.upto(n - 1).each { |row| zig_zag_table[row].reverse! if row.odd? }
zig_zag_table
end
...

Finished in 0.00441 seconds
3 examples, 0 failures
Георги Пурнаров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Пурнаров
def zig_zag(n)
list=[]
i=1
flag=1
while i<=n*n
j=0
help_list=[]
while j<n
if flag>0
help_list.push i
else
help_list.unshift i
end
i+=1
j+=1
end
flag=-flag
list.push help_list
end
return list
end
...

Finished in 0.0045 seconds
3 examples, 0 failures
Николай Генов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Генов
def zig_zag(n)
(1..n**2).each_slice(n).each_with_index.map { |row,i| i.odd? ? row.reverse : row }
end
...

Finished in 0.03875 seconds
3 examples, 0 failures
Илиян Танев
  • Некоректно
  • 0 успешни тест(а)
  • 3 неуспешни тест(а)
Илиян Танев
def zig_zag(number)
matrix = []
1.upto(number) do |row|
current_row = []
upper_limit = row * number
lower_limit = upper_limit - number + 1
lower_limit.upto(upper_limit) do |i|
row % 2 == 0 ? current_row.unshift(i) : current_row << i
end
matrix << current_row
end
return matrix.to_s
end
FFF

Failures:

  1) zig_zag works for n = 1
     Failure/Error: zig_zag(1).should eq [[1]]
       
       expected: [[1]]
            got: "[[1]]"
       
       (compared using ==)
     # /tmp/d20131016-4165-2tadkl/spec.rb:3: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) zig_zag works for n = 2
     Failure/Error: zig_zag(2).should eq [[1, 2], [4, 3]]
       
       expected: [[1, 2], [4, 3]]
            got: "[[1, 2], [4, 3]]"
       
       (compared using ==)
     # /tmp/d20131016-4165-2tadkl/spec.rb:7: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) zig_zag works for n = 10
     Failure/Error: zig_zag(10).should eq [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
       
       expected: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [20, 19, 18, 17, 16, 15, 14, 13, 12, 11], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [40, 39, 38, 37, 36, 35, 34, 33, 32, 31], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [60, 59, 58, 57, 56, 55, 54, 53, 52, 51], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [80, 79, 78, 77, 76, 75, 74, 73, 72, 71], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [100, 99, 98, 97, 96, 95, 94, 93, 92, 91]]
            got: "[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [20, 19, 18, 17, 16, 15, 14, 13, 12, 11], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [40, 39, 38, 37, 36, 35, 34, 33, 32, 31], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [60, 59, 58, 57, 56, 55, 54, 53, 52, 51], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [80, 79, 78, 77, 76, 75, 74, 73, 72, 71], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [100, 99, 98, 97, 96, 95, 94, 93, 92, 91]]"
       
       (compared using ==)
     # /tmp/d20131016-4165-2tadkl/spec.rb:11: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.00516 seconds
3 examples, 3 failures

Failed examples:

rspec /tmp/d20131016-4165-2tadkl/spec.rb:2 # zig_zag works for n = 1
rspec /tmp/d20131016-4165-2tadkl/spec.rb:6 # zig_zag works for n = 2
rspec /tmp/d20131016-4165-2tadkl/spec.rb:10 # zig_zag works for n = 10
Милен Мавров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Милен Мавров
def zig_zag(n)
zig_zag_matrix = []
one_through_n_array = (1..n**2).to_a
n.times do
zig_zag_matrix << one_through_n_array.take(n)
one_through_n_array.slice!(0...n)
end
zig_zag_matrix.each { |row| row.reverse! if zig_zag_matrix.rindex(row).odd? }
end
...

Finished in 0.00459 seconds
3 examples, 0 failures
Наталия Пацовска
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Наталия Пацовска
def zig_zag(n)
(1..n**2).each_slice(n).map.each_with_index { |array, index| index % 2 == 1 ? array.reverse : array }
end
...

Finished in 0.00458 seconds
3 examples, 0 failures
Михаил Господинов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Михаил Господинов
def next_n_numbers (start, n)
(start * n - n + 1 .. start * n).to_a
end
def zig_zag n
result , alternate = [] , false
(1 .. n).each { |startNumber|
if alternate then result << next_n_numbers(startNumber,n).reverse
else result << next_n_numbers(startNumber,n) end
alternate = (not alternate)
}
return result
end
...

Finished in 0.08221 seconds
3 examples, 0 failures
Мария Митева
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Мария Митева
def zig_zag(n)
result_array = []
sliced_array = 1.upto(n**2).to_a.each_slice(n).each_with_index do |item, index|
if index % 2 == 1
result_array << item.reverse
else
result_array << item
end
end
return result_array
end
...

Finished in 0.00573 seconds
3 examples, 0 failures
Христо Хърсев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Христо Хърсев
def zig_zag(n)
_ = (1..n**2).each_slice(n).to_a
_.each_with_index { |subarray, index| subarray.reverse! if index.odd? }
end
...

Finished in 0.00457 seconds
3 examples, 0 failures
Владимир Кузмов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Владимир Кузмов
def zig_zag n
(1..n*n).each_slice(n).inject([]) { |total, e| total << e }.each_with_index.map { |e, i| i.odd? ? e.reverse : e }
end
...

Finished in 0.00458 seconds
3 examples, 0 failures
Марио Даскалов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Марио Даскалов
def zig_zag(n)
(1..n * n).each_slice(n).map do |i|
i.last.div(n).odd? ? i : i.reverse
end
end
...

Finished in 0.00439 seconds
3 examples, 0 failures
Станимир Килявков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Станимир Килявков
def zig_zag(n)
zigzag=Array.new{Array.new}
(1..n**2).each_slice(n){|i| zigzag.push(i)}
zigzag.each_index{|x| zigzag[x]=x%2!=0? zigzag[x].reverse : zigzag[x]}
return zigzag
end
...

Finished in 0.00453 seconds
3 examples, 0 failures
Йордан Дикманов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Йордан Дикманов
def zig_zag(n)
Array arr= Array.new(n) { Array.new(n) }
arr_value= 1
1.upto (n*n) do |i|
arr_row= (i-1) / n
arr_cow=( i-1) % n
if (arr_row) % 2 == 0 then arr [arr_row][arr_cow] = arr_value
else
arr [arr_row][n-arr_cow-1] = arr_value
end
arr_value+=1
end
return arr
end
...

Finished in 0.00457 seconds
3 examples, 0 failures
Александър Попов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Попов
require 'matrix'
def zig_zag(n)
matrix = Matrix.build(n) { |row, col| row.even? ? col + n * row + 1 : n * (row + 1) - col }.to_a
end
...

Finished in 0.00483 seconds
3 examples, 0 failures
Давид Петров
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Давид Петров
def zig_zag(n)
matrix=Array.new(n) {Array.new(n)}
matrix.each_index {|x| matrix[x].each_index {|y| matrix[x][y]= x.even? ? x*n+y+1 : (x+1)*n-y} }
end
...

Finished in 0.00455 seconds
3 examples, 0 failures
Иван Капукаранов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Капукаранов
def zig_zag(n)
matrix = []
index = 0
(1..n**2).map.with_index do |number, i|
if i % n == 0
matrix[index] = [number]
elsif i % n == n - 1
matrix[index] << number
index += 1
else
matrix[index] << number
end
end
matrix.map.with_index { |line, i| if i.even? then line else line.reverse end }
end
...

Finished in 0.05262 seconds
3 examples, 0 failures
Стефан Василев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Стефан Василев
def zig_zag(n)
result_array, partial_array, reverse_every = [], [], 2
(1..n.abs2).each do |i|
partial_array << i
if i.remainder(n).zero?
result_array << partial_array
partial_array = []
end
end
result_array.each_index { |i| result_array[i] = result_array[i].reverse if (i + 1).remainder(reverse_every).zero? }
result_array
end
...

Finished in 0.00406 seconds
3 examples, 0 failures
Кристиан Ташков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиан Ташков
def zig_zag(n)
(1..n**2).each_slice(n).with_index.map do |line, index|
(index+1).remainder(2).zero? ? line.reverse : line
end
end
...

Finished in 0.00451 seconds
3 examples, 0 failures
Росен Рачев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Росен Рачев
def zig_zag(n)
matrix, i = build(n), 0
matrix.map do |element|
element = element.map do |item|
item += i
i += 1
end
element = (i / n).even? ? element.reverse : element
end
end
private
def build(n)
matrix, row = [], []
0.upto(n - 1).each do
row.push 1
matrix.push row
end
matrix
end
...

Finished in 0.00465 seconds
3 examples, 0 failures
Николай Колев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Колев
def zig_zag(n)
Array.new(n) do |row|
row_content = Array.new(n){ |col| row * n + col + 1 }
row.odd? ? row_content.reverse : row_content
end
end
...

Finished in 0.00447 seconds
3 examples, 0 failures
Петър Мазълов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Мазълов
def zig_zag(number)
matrix = []
rows = []
(1..number * number).each do |x|
if x % number != 0
rows << x
else
rows << x
matrix << rows
rows = []
end
end
(0..number-1).each do |i|
if i.odd?
matrix[i].replace(matrix[i].reverse)
end
end
matrix
end
...

Finished in 0.00456 seconds
3 examples, 0 failures
Ангел Цанев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Цанев
def zig_zag(order = 1) #square matrix of order N
matrix = []
matrix.initialise_square_matrix(order)
0.upto(order ** 2 - 1).each do |i|
row = i.quo(order).floor
(row.even?) ? matrix[row].insert(-1, i + 1) : matrix[row].insert(0, i + 1)
end
return matrix
end
class Array
def initialise_square_matrix(rows)
0.upto(rows - 1).each { |i| self[i] = [] }
end
end
...

Finished in 0.00477 seconds
3 examples, 0 failures
Владимир Конушлиев
  • Некоректно
  • 0 успешни тест(а)
  • 3 неуспешни тест(а)
Владимир Конушлиев
class FmiRubyChallenges
def self.zig_zag(n)
all = (1..n**2).to_a
result = []
n.times do |i|
line = all.slice(i * n, n)
line.reverse! if i.odd?
result << line
end
result
end
end
FFF

Failures:

  1) zig_zag works for n = 1
     Failure/Error: zig_zag(1).should eq [[1]]
     NoMethodError:
       undefined method `zig_zag' for #<RSpec::Core::ExampleGroup::Nested_1:0xba41c4bc>
     # /tmp/d20131016-4165-1i5vrob/spec.rb:3: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) zig_zag works for n = 2
     Failure/Error: zig_zag(2).should eq [[1, 2], [4, 3]]
     NoMethodError:
       undefined method `zig_zag' for #<RSpec::Core::ExampleGroup::Nested_1:0xba437924>
     # /tmp/d20131016-4165-1i5vrob/spec.rb:7: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) zig_zag works for n = 10
     Failure/Error: zig_zag(10).should eq [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     NoMethodError:
       undefined method `zig_zag' for #<RSpec::Core::ExampleGroup::Nested_1:0xba436ee8>
     # /tmp/d20131016-4165-1i5vrob/spec.rb:11: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.00354 seconds
3 examples, 3 failures

Failed examples:

rspec /tmp/d20131016-4165-1i5vrob/spec.rb:2 # zig_zag works for n = 1
rspec /tmp/d20131016-4165-1i5vrob/spec.rb:6 # zig_zag works for n = 2
rspec /tmp/d20131016-4165-1i5vrob/spec.rb:10 # zig_zag works for n = 10
Слав Керемидчиев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Слав Керемидчиев
def zig_zag(n)
zig_zag_array = Array.new(n){ |i| Array.new(n){ |j| (n * i) + j + 1 } }
zig_zag_array.each_with_index do |element, index|
element.reverse! if (index + 1) % 2 == 0
end
end
...

Finished in 0.00453 seconds
3 examples, 0 failures
Димитър Керанов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Димитър Керанов
def zig_zag(n)
matrix = (1..n**2).each_slice(n).to_a
matrix.each_with_index.map do |item, index|
if index % 2 != 0
item.reverse
else
item
end
end
end
...

Finished in 0.00464 seconds
3 examples, 0 failures
Антонио Николов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Антонио Николов
def zig_zag(n)
matrix, line_array = [], []
(0..n - 1).each do |line|
(line*n + 1..line*n + n).each do |line_element|
line_array << line_element
end
if (line + 1).remainder(2).zero?
matrix << line_array.reverse
else
matrix << line_array
end
line_array = []
end
matrix
end
...

Finished in 0.00462 seconds
3 examples, 0 failures
Кристиян Кисимов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиян Кисимов
def zig_zag(n)
result_array, array_container = [], []
1.upto(n * n) do |item|
if item % n == 0
array_container << item
result_array << (result_array.length.even? ? array_container : array_container.reverse)
array_container = []
else
array_container << item
end
end
result_array
end
...

Finished in 0.06796 seconds
3 examples, 0 failures
Георги Шопов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Шопов
def zig_zag(n)
(1..n).collect do |i|
next_line = (((i - 1)*n + 1)..i*n).to_a.sort
i % 2 == 0 ? next_line.reverse : next_line
end
end
...

Finished in 0.00454 seconds
3 examples, 0 failures
Диан Николов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Диан Николов
:left
:right
def zig_zag(n)
finalArray = []
iteratorArray = []
direction = :right
1.upto(n*n).each do |i|
iteratorArray << i
if i % n == 0
if direction == :left
iteratorArray = iteratorArray.reverse
direction = :right
else
direction = :left
end
finalArray << iteratorArray
iteratorArray = []
end
end
finalArray
end
...

Finished in 0.00461 seconds
3 examples, 0 failures
Радослав Даскалов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Радослав Даскалов
def zig_zag(n)
1.upto(n**2).each_slice(n).collect.with_index {|a, i| i % 2 == 0 ? a : a.reverse}
end
...

Finished in 0.00452 seconds
3 examples, 0 failures
Калоян Калудов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Калоян Калудов
def zig_zag(n)
matrix = []
(1..n * n).each_slice(n) { |slice| matrix << slice }
(0...n).each { |i| matrix[i].reverse! if i.odd? }
matrix
end
...

Finished in 0.00448 seconds
3 examples, 0 failures
Мария Терзиева
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Мария Терзиева
def zig_zag(n)
matrix_elements = 1.upto(n**2).to_a
matrix, counter = [], 0
until matrix_elements.empty?
row = matrix_elements.take(n)
counter += 1
row = row.reverse if counter.even?
matrix << row
matrix_elements = matrix_elements.drop(n)
end
matrix
end
...

Finished in 0.00452 seconds
3 examples, 0 failures
Венцислав Велков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Венцислав Велков
def zig_zag(n)
array = []
(1..n * n).each_slice(n) { |i| array << i }
(1..(n - 1)).each { |i| array[i] = array[i].reverse if i.odd? }
array
end
...

Finished in 0.00454 seconds
3 examples, 0 failures
Сашо Михайлов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Сашо Михайлов
def zig_zag(n)
list=[]
i=0 #брояч за първия цикъл
k=0 #брояч за първия цикъл
while i<n*n and k<n
j=0 #брояч за втория цикъл
list_help=[]
while j<n
list_help[j]=i+j+1
j+=1
end
list_help.reverse! if k%2!=0
list[k]=list_help
k+=1
i+=j
end
return list
end
...

Finished in 0.00451 seconds
3 examples, 0 failures
Ангел Венчев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Венчев
def zig_zag(n)
1.upto(n**2).each_slice(n).map.with_index do |element, i|
i.odd? ? element.reverse : element
end
end
...

Finished in 0.00471 seconds
3 examples, 0 failures
Кристиян Азманов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Кристиян Азманов
def zig_zag(n)
level = 1
result_array = []
(1..n*n).each do |member| if(member % n == 0) then
result_array<<current_level_array(n,level)
level += 1
end
end
return result_array
end
def current_level_array(initial_param,level)
current_level_array = []
(initial_param * level).downto(initial_param * (level - 1) + 1) do |member|
current_level_array<<member
end
return level % 2 == 0 ? current_level_array : current_level_array.reverse
end
...

Finished in 0.00459 seconds
3 examples, 0 failures
Моника Димитрова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Моника Димитрова
def zig_zag(n)
result = []
(1..n ** 2).each_slice(n) { |i| result << i }
(0...n).each { |i| i.odd? and result[i].reverse! }
result
end
...

Finished in 0.00449 seconds
3 examples, 0 failures
Илия Тобов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Илия Тобов
def zig_zag(n)
zig_zagged = (1..n**2).each_slice(n).to_a
zig_zagged.map { |list| zig_zagged.index(list).odd? ? list.reverse : list}
end
...

Finished in 0.08002 seconds
3 examples, 0 failures
Веселин Генадиев
  • Некоректно
  • 1 успешни тест(а)
  • 2 неуспешни тест(а)
Веселин Генадиев
def zig_zag(n)
n_sqr = n * n
list = Array.new(n)
0.upto(n-1) {|i| list[i] = Array.new(n)}
(1..n_sqr).each{|i| list[(i-1)/n][get_column_index(i-1, n)] = i}
list
end
def get_column_index(i, n)
if i / n % 2 == 0
i % n
else
n - i % n
end
end
.FF

Failures:

  1) zig_zag works for n = 2
     Failure/Error: zig_zag(2).should eq [[1, 2], [4, 3]]
       
       expected: [[1, 2], [4, 3]]
            got: [[1, 2], [nil, 4, 3]]
       
       (compared using ==)
     # /tmp/d20131016-4165-l34spf/spec.rb:7: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) zig_zag works for n = 10
     Failure/Error: zig_zag(10).should eq [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
       
       expected: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [20, 19, 18, 17, 16, 15, 14, 13, 12, 11], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [40, 39, 38, 37, 36, 35, 34, 33, 32, 31], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [60, 59, 58, 57, 56, 55, 54, 53, 52, 51], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [80, 79, 78, 77, 76, 75, 74, 73, 72, 71], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [100, 99, 98, 97, 96, 95, 94, 93, 92, 91]]
            got: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [nil, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [nil, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [nil, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [nil, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [nil, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91]]
       
       (compared using ==)
     # /tmp/d20131016-4165-l34spf/spec.rb:11: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.00546 seconds
3 examples, 2 failures

Failed examples:

rspec /tmp/d20131016-4165-l34spf/spec.rb:6 # zig_zag works for n = 2
rspec /tmp/d20131016-4165-l34spf/spec.rb:10 # zig_zag works for n = 10
Ясен Трифонов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ясен Трифонов
#!/usr/local/bin/ruby -w
def zig_zag(n)
(0...n).to_a.map do |i|
array = (i * n + 1..i * n + n).to_a
i.even? ? array : array.reverse
end
end
...

Finished in 0.00461 seconds
3 examples, 0 failures
Георги Урумов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Урумов
def zig_zag(n)
matrix = []
(1..n**2).each_slice(n) {|row| matrix.size % 2 == 0 ?
matrix << row : matrix << row.reverse}
matrix
end
...

Finished in 0.00569 seconds
3 examples, 0 failures
Елена Орешарова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Елена Орешарова
def zig_zag(n)
returnArray = Array.new(n)
(0..n-1).each { |i|
returnArray[i] = []
(i * n + 1..n * (i + 1)).each { |j| returnArray[i] << j }
if i.odd?
returnArray[i].reverse!
end
}
returnArray
end
...

Finished in 0.00452 seconds
3 examples, 0 failures
Иван Латунов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Латунов
def zig_zag(n)
main_array = (1..n**2).to_a
return_array = main_array.each_slice(n).to_a
return_array.each_index {|x| return_array[x].reverse! if x % 2 == 1}
return_array
end
...

Finished in 0.00462 seconds
3 examples, 0 failures
Петя Делчева
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Петя Делчева
def zig_zag(row)
matrix,p=Array.new{Array.new},0
(1..row**2).each_slice(row){|i| matrix[p]=i and p+=1}
matrix.each_index do |x|
if x%2!=0 then matrix[x]=matrix[x].reverse end
end
matrix
end
...

Finished in 0.00643 seconds
3 examples, 0 failures
Петко Борджуков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Петко Борджуков
def zig_zag(n)
1.upto(n ** 2).each_slice(n).map.with_index { |row, index| index.odd? ? row.reverse : row }
end
...

Finished in 0.0045 seconds
3 examples, 0 failures
Илиян Бобев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Бобев
def zig_zag(n)
result = []
direction = true
1.upto(n) do |i|
row = []
if direction
((i-1)*n +1).upto(i*n) { |j| row << j }
else
(i*n).downto((i-1)*n +1) { |j| row << j }
end
direction = !direction
result << row
end
result
end
...

Finished in 0.0047 seconds
3 examples, 0 failures
Симеон Цветков
  • Некоректно
  • 0 успешни тест(а)
  • 3 неуспешни тест(а)
Симеон Цветков
def zig_zag(n)
zig_zag = []
(0..n).each do|i|
if i%2 == 1
zig_zag[i] = (n*2..n+1).each do |j| zig_zag[i][n-j] = j end
else
zig_zag[i] = (n+1..n*2).each do |j| zig_zag[i][n-j] = j end
end
end
end
FFF

Failures:

  1) zig_zag works for n = 1
     Failure/Error: zig_zag(1).should eq [[1]]
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block (2 levels) in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `zig_zag'
     # /tmp/d20131016-4165-194fk7x/spec.rb:3: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) zig_zag works for n = 2
     Failure/Error: zig_zag(2).should eq [[1, 2], [4, 3]]
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block (2 levels) in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `zig_zag'
     # /tmp/d20131016-4165-194fk7x/spec.rb:7: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) zig_zag works for n = 10
     Failure/Error: zig_zag(10).should eq [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block (2 levels) in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:7:in `block in zig_zag'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `each'
     # /tmp/d20131016-4165-194fk7x/solution.rb:3:in `zig_zag'
     # /tmp/d20131016-4165-194fk7x/spec.rb:11: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.00359 seconds
3 examples, 3 failures

Failed examples:

rspec /tmp/d20131016-4165-194fk7x/spec.rb:2 # zig_zag works for n = 1
rspec /tmp/d20131016-4165-194fk7x/spec.rb:6 # zig_zag works for n = 2
rspec /tmp/d20131016-4165-194fk7x/spec.rb:10 # zig_zag works for n = 10