Магически квадрат

Краен срок
10.11.2012 14:00

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

Магически квадрат

Магическият квадрат е квадратна матрица с числа, такава че сумата на числата във всеки ред, стълб и в двата диагонала да е еднаква.

Напишете функция magic_square?, която приема като аргумент матрица, представена като списък от списъци (списък от редовете на матрицата) и проверява дали матрицата е магически квадрат:

magic_square? [[1, 1], [1, 1]] # true
magic_square? [[1, 2], [1, 1]] # false

Решения

Стефан Кънев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Стефан Кънев
module Magic
def magic_square?(square)
# meh
end
end
Александър Иванов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Иванов
module Magic
private
def self.magic_rows?(matrix, expected)
matrix.all? { |row| row.inject(:+) == expected }
end
def self.magic_columns?(matrix, expected)
matrix.transpose.all? { |column| column.inject(:+) == expected }
end
def self.magic_diagonals?(matrix, expected)
matrix.flatten.each_slice(matrix.length + 1).
inject(0) { |sum, slice| sum + slice[0] } == expected and
matrix[1..-1].flatten.each_slice(matrix.length - 1).
inject(0) { |sum, slice| sum + slice[0] } == expected
end
public
def self.magic_square?(matrix)
return true if matrix[0].nil?
expected = matrix[0].inject :+
result = self.magic_rows?(matrix, expected) and
self.magic_columns?(matrix, expected) and
self.magic_diagonals?(matrix, expected)
result ? true : false
end
end
Кирчо Кирев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Кирчо Кирев
module Magic
def self.magic_square?(matrix)
rows_sums = matrix.map { |row| row.inject(&:+) }
matrix_transposed = matrix.transpose
cols_sums = matrix_transposed.map { |row| row.inject(&:+) }
diagonals = [0, 0]
0.upto(matrix.length - 1) do |i|
diagonals[0] += matrix[i][i]
diagonals[1] += matrix[i][matrix.length - 1 - i]
end
union = rows_sums | cols_sums | diagonals
union.length == 1
end
end
Милан Миланов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Милан Миланов
class Magic
def self.magic_square?(rows)
return false if rows.size.zero?
magic_sum = rows.first.inject(:+)
rows.map { |row| return false if row.inject(:+) != magic_sum } #rows
rows.transpose.map { |row| return false if row.inject(:+) != magic_sum } #cols
main_diagonal_sum, second_diagonal_sum, matrix_size = 0, 0, rows.size - 1
0.upto(matrix_size) do |i|
0.upto(matrix_size) do |j|
main_diagonal_sum += rows[i][j] if i == j #main diagonal
second_diagonal_sum += rows[j][i] if i + j == matrix_size #secondary diagonal
end
end
return main_diagonal_sum == magic_sum && second_diagonal_sum == magic_sum
end
end
Николай Димитров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Димитров
class Magic
def self.magic_square?(matrix)
sums = [sum_of_main_diagonal(matrix), sum_of_antidiagonal(matrix)]
sums |= row_sums(matrix) | row_sums(transpose(matrix))
sums.size == 1
end
def self.transpose(matrix)
(0.upto matrix.length-1).map { |index| matrix.map { |row| row[index] } }
end
def self.row_sums(matrix)
matrix.map do |row|
row.inject { |sum, elem| sum + elem }
end
end
def self.sum_of_main_diagonal(matrix)
(0.upto matrix.length-1).map { |index| matrix[index][index] }.inject { |sum, elem| sum + elem }
end
def self.sum_of_antidiagonal(matrix)
(0.upto matrix.length-1).map { |index| matrix[index][matrix.length-1-index] }.inject { |sum, elem| sum + elem }
end
end
Красимир Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Красимир Георгиев
class Matrix < Array
def row_sums
map { |row| row.reduce(:+) }
end
def diagonal_sum
each_with_index.map { |row, i| row[i] }.reduce(:+)
end
def upper_sums
row_sums + [diagonal_sum]
end
end
class Magic
def self.magic_square?(square)
sums = [square, square.transpose].flat_map do |matrix|
Matrix.new(matrix).upper_sums
end
sums.uniq.length == 1
end
end
Георги Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Георгиев
class Magic
def self.calculate_diagonal(matrix)
result = 0
matrix.each_with_index { |row, index| result += row[index] }
result
end
def self.calculate_antidiagonal(matrix)
result = 0
matrix.each_with_index { |row, index| result += row[row.length - 1 - index] }
result
end
def self.magic_square?(matrix)
magic_number = calculate_diagonal(matrix) if calculate_diagonal(matrix) == calculate_antidiagonal(matrix)
matrix.all? { |row| row.inject(:+) == magic_number } and
matrix.transpose.all? { |row| row.inject(:+) == magic_number }
end
end
Йордан Петров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Йордан Петров
class Magic
def self.magic_square?(matrix)
rows = matrix.map { |row| row.inject(& :+) }
columns = matrix.transpose.map { |column| column.inject(& :+) }
main_diagonal = (0...matrix.size).map { |index| matrix[index][index] }.inject(& :+)
secondary_diagonal = (0...matrix.size).map { |index| matrix.reverse[index][index] }.inject(& :+)
(rows + columns + [main_diagonal] + [secondary_diagonal]).uniq.size.eql? 1
end
end
Мартин Попов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Попов
module Magic
def self.transpose(matrix)
(1..matrix.size).map do |iterator|
matrix.map { |row| row.fetch(iterator - 1) }
end
end
def self.sum_rows(matrix)
matrix.map do |row|
row.inject { |a,b| a + b }
end
end
def self.sum_columns(matrix)
transposed = self.transpose(matrix)
Magic.sum_rows(transposed)
end
def self.major_diagonal_sum(matrix)
diagonal = (1..matrix.size).map{ |iterator| matrix.fetch(iterator - 1).fetch(iterator - 1) }
diagonal.inject { |a,b| a + b }
end
def self.secondary_diagonal_sum(matrix)
diagonal = (1..matrix.size).map{ |iterator| matrix.fetch(iterator - 1).fetch(matrix.size - iterator) }
diagonal.inject { |a,b| a + b }
end
def self.magic_square?(matrix)
helper = self.sum_rows(matrix) + self.sum_columns(matrix)
helper << self.major_diagonal_sum(matrix)
helper << self.secondary_diagonal_sum(matrix)
if helper.uniq.count == 1 then true
else false
end
end
end
Орлин Христов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Орлин Христов
class Magic
class << self
def magic_square?(matrix)
magic_lines = [*matrix, *matrix.transpose, main_diagonal(matrix), secondary_diagonal(matrix)]
all_same? magic_lines.map { |line| line.inject &:+ }
end
private
def all_same?(list)
list.min == list.max
end
def main_diagonal(matrix)
matrix.each_index.map { |index| matrix[index][index] }
end
def secondary_diagonal(matrix)
main_diagonal matrix.transpose
end
end
end
Валентин Ейткен
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Валентин Ейткен
class Magic
def self.magic_square?(list)
matrix = Matrix.new(list)
matrix.is_magic?
end
end
class Matrix
def initialize(list)
@matrix = list
@magic_sum = @matrix.first.inject(0) { |sum, x| sum + x }
end
def is_magic?
magic_rows?(@matrix) and magic_rows?(@matrix.transpose) and
magic_diagonal? and magic_diagonal2?
end
private
def magic_diagonal?
@magic_sum == (0.. @matrix.length - 1).inject(0) do |sum, i|
sum + @matrix[i][i]
end
end
def magic_diagonal2?
@magic_sum == (0.. @matrix.length - 1).inject(0) do |sum, i|
sum + @matrix[i][@matrix.length - 1 - i]
end
end
def magic_rows?(matrix)
matrix.each.inject(true) do |magic, row|
magic and @magic_sum == (row.inject(0) { |sum, x| sum + x })
end
end
end
Гергана Петрова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Гергана Петрова
class Matrix
def initialize(list)
@matrix = list
end
def sum_of_rows
@matrix.map { |element| element.inject(:+) }
end
def sum_of_columns
@matrix.transpose.map { |element| element.inject(:+) }
end
def sum_of_main_diagonal
(0...@matrix.size).map { |index| @matrix[index][index] }.inject(:+)
end
def sum_of_secondary_diagonal
(0...@matrix.size).map { |index| @matrix.transpose[index][index] }.inject(:+)
end
def row_sum
sum_of_rows.uniq.size > 1 ? nil : sum_of_rows.first
end
def column_sum
sum_of_columns.uniq.size > 1 ? nil : sum_of_columns.first
end
def diagonal_sum
sum_of_main_diagonal == sum_of_secondary_diagonal ? sum_of_main_diagonal : nil
end
end
module Magic
def self.magic_square?(list)
matrix = Matrix.new list
matrix.row_sum == matrix.column_sum and matrix.column_sum == matrix.diagonal_sum
end
end
Никола Таушанов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Никола Таушанов
class Magic
def self.magic_square?(matrix)
sums = Matrix.rows_sums(matrix) | Matrix.rows_sums(matrix.transpose)
sums.uniq.size == 1 and
sums.first == Matrix.diagonal_sum(matrix) and
sums.first == Matrix.diagonal_sum(matrix.map(&:reverse))
end
end
class Matrix
def self.rows_sums(matrix)
matrix.map { |row| row.inject(:+) }
end
def self.diagonal_sum(matrix)
matrix.each_with_index.inject(0) do |result, (row, index)|
result + row[index]
end
end
end
Илиян Величков
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Величков
module Magic
def self.magic_square?(square)
#return false if square.any? { |row| row.size != square.size }
sum = square.first.reduce :+
return false if square.drop(1).any? { |row| sum != row.reduce(:+) }
return false if (0...square.size).any? do |n|
sum != square.map { |row| row[n] }.reduce(:+)
end
return false if sum != (0...square.size).map { |n| square[n][n] }.reduce(:+)
return false if sum != (0...square.size).map do |n|
square[n][square.size - 1 - n]
end.reduce(:+)
true
end
end
Йосиф Йосифов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Йосиф Йосифов
class Magic
def self.magic_square? array
colsSum = sum_cols array
rowsSum = sum_rows array
diagonalsSum = sum_diagonals array
return (colsSum != -1 && rowsSum != -1 && diagonalsSum != -1 &&
colsSum = rowsSum && colsSum == diagonalsSum)
end
def self.sum_rows array
maxValue = (0..array.length-1).map{|i| sum_single_array array[i]}.max
minValue = (0..array.length-1).map{|i| sum_single_array array[i]}.min
return (minValue == maxValue) ? minValue : -1
end
def self.sum_cols array
maxValue = (0..array.length-1).map{|i| sum_single_array (0..array.length-1)
.map{|rowIndex| array[rowIndex][i]}}.max
minValue = (0..array.length-1).map{|i| sum_single_array (0..array.length-1)
.map{|rowIndex| array[rowIndex][i]}}.min
return (minValue == maxValue) ? minValue : -1
end
def self.sum_diagonals array
mainDiagonalSum = sum_single_array (0..array.length-1).map{|i| array[i][i]}
subDiagonalSum = sum_single_array (0..array.length-1).map{|i| array[i][array.length-i-1]}
return (mainDiagonalSum == subDiagonalSum) ? mainDiagonalSum : -1
end
def self.sum_single_array array
array.inject(:+)
end
end
Станислав Гатев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Станислав Гатев
class Square
def initialize(square_matrix)
@matrix = square_matrix
end
def size
@matrix.length
end
def rows
@matrix
end
def cols
@matrix.transpose
end
def primary_diagonal
(0...size).collect { |i| @matrix[i][i] }
end
def secondary_diagonal
(0...size).collect { |i| @matrix[i][@matrix.length - 1 - i] }
end
def diagonals
[primary_diagonal, secondary_diagonal]
end
end
class Magic
def self.magic_square?(matrix)
square = Square.new matrix
sum_each = lambda { |element| element.reduce :+ }
row_values = square.rows.map(&sum_each)
col_values = square.cols.map(&sum_each)
diagonal_values = square.diagonals.map(&sum_each)
magic_line? [*row_values, *col_values, *diagonal_values]
end
def self.magic_line?(line)
line.max == line.min
end
end
Ивайло Христов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Ивайло Христов
class Magic
def self.magic_square?(square)
if square.is_a? Array
equal_rows?(square) && equal_columns?(square) && equal_diagonals?(square)
end
end
def self.equal_rows?(square)
square.inject(0) do |memo,row|
if memo == 0
memo = row.reduce(:+)
else
if memo != row.reduce(:+)
return false
else
memo = memo
end
end
end
true
end
def self.equal_columns?(square)
equal_rows? square.transpose
end
def self.equal_diagonals?(square)
diagonals = [0,0]
square.each_with_index do |row,index|
diagonals[0] += row[index]
diagonals[1] += row.reverse[index]
end
diagonals[0] == diagonals[1]
end
end
Ангел Николов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Николов
class Magic
def self.magic_square?(matrix)
n = matrix.size
rows = matrix
columns = matrix.transpose
main_diagonal = n.times.map { |i| matrix[i][i] }
antidiagonal = n.times.map { |i| matrix[i][n - i - 1] }
all = (rows + columns) << main_diagonal << antidiagonal
all.map { |sequence| sequence.inject(&:+) }.uniq.size <= 1
end
end
Георги Гърдев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Георги Гърдев
class Array # Sorry, I just HAD to do this once I thought of it
def all_equal?
min == max
end
end
class Magic
def self.magic_square?(matrix)
MagicSquare.new(matrix).magic?
end
end
class MagicSquare < Magic # Because a Magic Square is a kind of Magic
def initialize(matrix)
@matrix = matrix
end
def magic?
return true if @matrix == [[]]
return false if not square?
conditions ||= [] << row_sum << row_sum(@matrix.transpose) << diagonal_sum
conditions.all? and conditions.all_equal?
end
private
def square?
row_sizes = @matrix.map(&:count)
row_sizes.all_equal? and row_sizes.first == row_sizes.count
end
def row_sum(matrix = @matrix)
row_sums = matrix.map { |row| row.inject(:+) }
row_sums.all_equal? ? row_sums.first : false
end
def diagonal_sum
main_diagonal = @matrix.map.with_index{ |row, i| row[i] }.inject(:+)
second_diagonal = @matrix.map.with_index{ |row, i| row[row.size-i-1] }.inject(:+)
second_diagonal == main_diagonal ? main_diagonal : false
end
end
Иван Георгиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Георгиев
module Magic
def self.magic_square?(matrix)
list_of_sums = sum_of_the_rows(matrix) + sum_of_the_rows(matrix.transpose)
list_of_sums << sum_of_the_first_diagonal(matrix)
list_of_sums << sum_of_the_second_diagonal(matrix)
if list_of_sums.uniq.size == 1 then true
else false
end
end
def self.sum_of_the_rows(matrix)
array_of_sums = matrix.map { |row| row.inject { |a, b| a + b } }
end
def self.sum_of_the_first_diagonal(matrix)
sum_of_the_elements, index = 0, 0
matrix.each do |row|
sum_of_the_elements += row[index]
index += 1
end
sum_of_the_elements
end
def self.sum_of_the_second_diagonal(matrix)
sum_of_the_elements, index = 0, 1
matrix.each do |row|
sum_of_the_elements += row[row.size - index]
index += 1
end
sum_of_the_elements
end
end
Виктория Христова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Виктория Христова
module Magic
def self.magic_square?(square)
return true if square == [[]]
array = self.rows(square) + self.columns(square) + self.diagonals(square)
sums = array.map { |elem| elem.inject(0) { |a,b| a + b } }
sums.uniq.size == 1
end
def self.rows(square)
square
end
def self.columns(square)
square.transpose
end
def self.diagonals(square)
main = [square.map { |row| row.slice(square.index(row)) }]
secondary = [square.map { |row| row.slice(square.size - square.index(row) - 1) }]
main + secondary
end
end
Свилен Андонов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Свилен Андонов
class Magic
def Magic.magic_square? matrix
size = matrix.size
return true if size == 0
0.upto(size-1).each do |i|
return false if size != matrix[i].size
end
sum = matrix[0].inject(:+)
0.upto(size-1).each do |i|
return false if sum != 0.upto(size-1).inject(0) { |s,j| s + matrix[i][j] }
return false if sum != 0.upto(size-1).inject(0) { |s,j| s + matrix[j][i] }
end
return false if sum != 0.upto(size-1).inject(0) { |s,i| s + matrix[i][i] }
return false if sum != 0.upto(size-1).inject(0) { |s,i| s + matrix[i][size-1-i] }
true
end
end
Елена Денева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Елена Денева
class Magic
def self.magic_square? square
arr = square.map {|x| 0} << 0 << 0
square.each_index do |i|
arr << square[i].inject(0) { |sum,n| sum + n }
arr[square.length] += square[i][i]
arr[square.length + 1] += square[i][square.length-1-i]
square[i].each_index { |index| arr[index] += square[i][index] }
end
arr.all? {|item| item == arr[0]}
end
end
Ивайло Христов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Ивайло Христов
class Magic
def self.magic_square?(matrix)
return true if matrix.length <= 1
sum = matrix[0].inject(:+)
length = matrix.length - 1
lengthRange = 0..length
checkRowsSum = Proc.new { |row| return false if row.inject(:+) != sum }
matrix .each(&checkRowsSum);
matrix.transpose .each(&checkRowsSum);
[lengthRange.map { |i| matrix[i][i] }] .each(&checkRowsSum);
[lengthRange.map { |i| matrix[length - i][i] }].each(&checkRowsSum);
true
end
end
Филарета Йорданова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Филарета Йорданова
module Magic
def self.magic_square?(matrix)
self.by_columns? matrix and
self.by_rows? matrix and
self.by_diagonals? matrix
end
def self.by_rows?(matrix)
self.match_sum? matrix
end
def self.by_columns?(matrix)
self.match_sum? matrix.transpose
end
def self.match_sum?(matrix)
sum = matrix[0].inject(:+)
matrix.map{ |(*a)| a.inject(:+) }.all?{ |s| s == sum }
end
def self.by_diagonals?(matrix)
c,d = [],[]
matrix.each_with_index do |array,i|
array.each_with_index do |number,j|
if i == j then c << number end
if i + j == matrix.length - 1 then d << number end
end
end
c.inject(:+) == matrix[0].inject(:+) and
d.inject(:+) == matrix[0].inject(:+)
end
end
Чанита Иванова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Чанита Иванова
class Magic
def Magic.lines(array,last)
array.map{ |x|
current = 0
x.map{|y| current += y }
last = current if(!last)
return false if(last != current) }
last
end
def Magic.diagonal(array,last)
current = 0
array.each_index{ |x| current += array[x][x] }
return false if(last != current)
last
end
def Magic.magic_square?(array)
result = nil
return false if(!(result = lines(array,result)))
array = array.transpose
return false if(!(result = lines(array,result)))
return false if(!(result = diagonal(array,result)))
array = array.map{ |x| x.reverse! }
return false if(!(result = diagonal(array,result)))
true
end
end
Даяна Беркова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Даяна Беркова
class Magic
attr_accessor :is_it_magical, :sum, :matrix
def initialize(matrix)
@sum = 0
@is_it_magical = true
@matrix = matrix
diagonal
rows
columns
secondary_diagonal
end
def self.magic_square?(matrix)
return true if (matrix.length == 1)
self.new(matrix).is_it_magical
end
def diagonal
@matrix.each_index do |index|
@sum += @matrix[index][index]
end
end
def rows
@matrix.map do |rows|
sum_row = 0
rows.map { |x| sum_row += x }
@is_it_magical = false if sum_row != @sum
end
end
def columns
(0..@matrix.length-1).map do |index|
sum_column = 0
@matrix.map { |item| sum_column += item[index] }
@is_it_magical = false if sum_column != @sum
end
end
def secondary_diagonal
sum_secondary_diagonal = 0
@matrix.each_with_index { |item, index| sum_secondary_diagonal += item[@matrix.length - 1 - index] }
@is_it_magical = false if sum_secondary_diagonal != @sum
end
end
Нели Хатева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Нели Хатева
class Magic
def self.magic_square?(square)
self.sum_of_rows(square) == self.sum_of_columns(square) and
self.sum_of_rows(square) == self.sum_of_major_diagonal(square) and
self.sum_of_rows(square) == self.sum_of_minor_diagonal(square)
end
def self.sum_of_rows(square)
sums_of_rows = square.map { |row| row.reduce(:+) }
if sums_of_rows.all? { |sum| sum == sums_of_rows[0] }
sums_of_rows[0]
end
end
def self.sum_of_columns(square)
self.sum_of_rows(square.transpose)
end
def self.sum_of_major_diagonal(square)
major_diagonal = []
(0...square.length).each do |index|
major_diagonal << square[index][index]
end
major_diagonal.reduce(:+)
end
def self.sum_of_minor_diagonal(square)
minor_diagonal = []
(0...square.length).each do |index|
minor_diagonal << square[index][square.length - 1 - index]
end
minor_diagonal.reduce(:+)
end
end
Камелия Пандаклиева
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Камелия Пандаклиева
class Magic
def self.magic_square?(matrix)
magic_number = main_diagonal_sum matrix
return false if magic_number != (main_diagonal_sum matrix.transpose)
(row_and_column_sums matrix).all? { |sum| magic_number == sum }
end
def self.row_and_column_sums(matrix)
(matrix + matrix.transpose).map { |row| row.inject(:+) }
end
def self.main_diagonal_sum(matrix)
(0..matrix.length - 1).collect { |index| matrix[index][index] }.inject(:+)
end
end
Цвета Гергичанова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Цвета Гергичанова
class Magic
def Magic.magic_square?(matrix)
columns = matrix.transpose.map { |array| array.inject { |a, b| a + b } }
rows = matrix.map { |array| array.inject { |a, b| a + b } }
last = matrix.size() - 1
diagonal1 = 0.upto(last).map { |i| matrix[i][i] }.inject { |a, b| a + b }
diagonal2 = 0.upto(last).map { |i| matrix[i][last-i] }.inject { |a, b| a + b }
flag = true
flag = false unless(diagonal1 == diagonal2)
1.upto(last).each { |i| flag = false unless (columns[0] == columns[i] and rows[0] == rows[i]) }
flag
end
end
Николай Колев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Николай Колев
class Array
def sum
items = map { |element| yield element }
items.reduce(:+)
end
end
class Magic
def Magic.magic_square? matrix
first_sum = matrix[0].reduce(:+)
last_index = matrix[0].length - 1
indices = 0.upto(last_index).to_a
indices.all? { |i| matrix[i].reduce(:+) == first_sum } and
indices.all? { |i| matrix.sum { |element| element[i] } == first_sum } and
indices.sum { |i, j| matrix[i][i] } == first_sum and
indices.sum { |i, j| matrix[i][last_index - i] } == first_sum
end
end
Тихомир Янев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Тихомир Янев
class Magic
def initialize(rows)
@sum, @size, @rows = rows[0].inject(0, :+), rows[0].size, rows
end
def self.magic_square?(rows)
square = Magic.new(rows)
square.magic?
end
def magic?
magic_rows? and magic_cols? and magic_diagonals?
end
private
def magic_rows?
@rows.each { |row| return false if row.inject(0, :+) != @sum }
true
end
def magic_cols?
(0...@size).each do |n|
return false if @rows.map { |row| row[n] } .inject(0, :+) != @sum
end
true
end
def magic_diagonals?
# I wish I could think of a better way to check this out
main_diag_sum, sec_diag_sum = 0, 0
(0...@size).each do |n|
main_diag_sum += @rows[n][n]
sec_diag_sum += @rows[@size - n - 1][n]
end
main_diag_sum == sec_diag_sum and sec_diag_sum == @sum
end
end
Петър Костов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Петър Костов
class Magic
def self.magic_square? square
return true if square[0].empty?
constant = (0...square.size).map { |i| square[i][i] }.inject(&:+)
magic = constant == (0...square.size).map { |i| square.transpose[i][i] }.inject(&:+)
magic &= square.all? { |row| row.inject(&:+) == constant }
magic &= square.transpose.all? { |column| column.inject(&:+) == constant }
end
end
Атанас Пройчев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Атанас Пройчев
class Magic
def self.magic_square?(matrix)
array = []
matrix.each { |row| array << row.inject(:+) }
matrix.transpose.each { |row| array << row.inject(:+) }
array << (0...matrix.size).map { |index| matrix[index][index] }.inject(:+)
array << (0...matrix.size).map { |index| matrix[index][matrix.size - index - 1] }.inject(:+)
if array.uniq.size == 1
return true
else
return false
end
end
end
Иван Арабаджиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Арабаджиев
module Magic
def self.magic_square?(matrix)
(matrix + columns(matrix) + diagonals(matrix)).map { |vector| vector.reduce :+ }.uniq.length == 1
end
private
def self.diagonals(matrix)
n = matrix.length - 1
half_done = (0..n).map { |index| [matrix[index][index], matrix[index][n-index]] }
half_done.shift.zip *half_done
end
def self.columns(matrix)
matrix.first.zip *(1...matrix.length).map { |index| matrix[index] }
end
end
Йоана Тодорова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Йоана Тодорова
class Magic
def self.magic_square?(matrix)
sum = matrix.first.reduce(:+)
rows,columns = true, true
0.upto(matrix.size-1) do |i|
rows = (rows and matrix[i].reduce(:+) == sum)
columns = (columns and matrix.reduce(0) { |total,row| total + row[i] } == sum)
break if ( !rows or !columns)
end
primary_diagonal = matrix.inject(0) { |total,row| total+row[matrix.index(row)] } == sum
secondary_diagonal = matrix.inject(0) { |total,row| total+row[-(matrix.index(row)+1)] } == sum
rows and columns and primary_diagonal and secondary_diagonal
end
end
Методи Димитров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Методи Димитров
module Magic
include Enumerable
def magic_square?(input_square)
if input_square.length() <= 1 then
return true
end
magic_sum = input_square[0].inject(0, :+)
magic_rows = input_square.map { |x| x.inject(:+) == magic_sum }.all?
magic_columns = input_square.transpose.map do |x|
x.inject(:+) == magic_sum
end.all?
main_diagonal_array = input_square.map.with_index do |value, index|
value[index]
end
magic_main_diagonal = main_diagonal_array.inject(0, :+) == magic_sum
second_diagonal = input_square.map.with_index do |value, index|
value.reverse[index]
end
magic_second_diagonal = second_diagonal.inject(0, :+) == magic_sum
magic_sum and magic_rows and magic_main_diagonal and magic_second_diagonal
end
end
Кирил Владимиров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Кирил Владимиров
class Magic
def self.magic_square?(square)
row_sum = square.map { |number| number.inject(:+) }
column_sum = square.transpose.map { |number| number.inject(:+) }
diagonal_sum = [square.map.with_index { |number, i| number[i] }.inject(:+)]
(row_sum + column_sum + diagonal_sum).all? { |sum| sum == row_sum[0] }
end
end
Теодор Илиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Теодор Илиев
class Magic
def self.magic_square?(matrix_array)
matrix = SquareMatrix.new matrix_array
all_sums = []
all_sums += matrix.rows.map { |row| row.reduce(:+) }
all_sums += matrix.columns.map { |column| column.reduce(:+) }
all_sums << matrix.major_diagonal.reduce(:+)
all_sums << matrix.minor_diagonal.reduce(:+)
all_sums.uniq.length == 1
end
end
class SquareMatrix
def initialize(matrix_array)
@matrix_container = matrix_array
end
def major_diagonal
matrix_diagonal @matrix_container
end
def minor_diagonal
matrix_diagonal(@matrix_container.map(&:reverse))
end
def rows
@matrix_container
end
def columns
@matrix_container.transpose
end
private
def matrix_diagonal(square_matrix_array)
square_matrix_array.map.with_index { |row, index| row[index] }
end
end
Светлана Величкова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Светлана Величкова
class Magic
def self.magic_square?(array)
sum = array[0].inject(:+)
sums = [0]
size = array.size
array.each do |x|
if sum != x.inject(:+)
return false
end
end
current_sum = 0
main_diagonal = 0
off_diagonal = 0
0.upto(size-1) do |i|
0.upto(size-1) do |j|
current_sum+=array[j][i]
end
main_diagonal += array[i][i]
off_diagonal += array[i][size-i-1]
if current_sum != sum
return false
end
current_sum = 0
end
if main_diagonal != sum
return false
end
if off_diagonal != sum
return false
end
return true
end
end
Деян Гюрджеклиев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Деян Гюрджеклиев
class Magic
def self.magic_square?(matrix)
magic_const = matrix[0].inject(:+)
rows_are_magic? matrix, magic_const and
columns_are_magic? matrix, magic_const and
diagonals_are_magic? matrix, magic_const
end
private
def self.rows_are_magic?(matrix,magic_const)
matrix.all? { |row| magic_const == row.inject(:+)}
end
def self.columns_are_magic?(matrix,magic_const)
matrix.transpose.all? { |column| magic_const == column.inject(:+)}
end
def self.diagonals_are_magic?(matrix,magic_const)
matrix.map.with_index { |element,index| element[index] }.inject(:+) == magic_const and
matrix.map.with_index { |element,index| element[matrix.size-index-1]}.inject(:+) == magic_const
end
end
Илиян Бобев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Илиян Бобев
class Magic
def self.magic_square?(square)
sum = square[0].inject(&:+)
return false if square.any? { |line| sum!=line.inject(&:+) }
return false if square.transpose.any? { |line| sum!=line.inject(&:+) }
return false if sum!=square.collect { |e| square[square.index(e)][square.index(e)] }.inject(&:+)
return false if sum!=square.collect { |e| square[square.index(e)][-square.index(e)-1] }.inject(&:+)
true
end
end
Валентин Гелински
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Валентин Гелински
class Magic
def Magic.magic_square?(matrix)
uniq_sums = all_sums(matrix).uniq
uniq_sums.size == 1
end
def Magic.rows_sums(matrix)
matrix.map{ |row| row.inject(:+) }
end
def Magic.columns_sums(matrix)
result = Array.new matrix.size, 0
matrix.each() do |row|
result = result.zip(row).map{ |pair| pair.inject(:+)}
end
result
end
def Magic.diagonals_sums(matrix)
main_diagonal_sum = 0
secondary_diagonal_sum = 0
last_index = matrix.size - 1
matrix.each_with_index() do |row, index|
main_diagonal_sum += row[index]
secondary_diagonal_sum += row[last_index - index]
end
[main_diagonal_sum, secondary_diagonal_sum]
end
def Magic.all_sums(matrix)
all_sums = rows_sums(matrix) + columns_sums(matrix) + diagonals_sums(matrix)
end
end
Стоян Найденов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Стоян Найденов
class Magic
def Magic.magic_square?(matrix)
return true if matrix == [[]]
sum = matrix.first.inject(:+)
matrix.each_index do |i|
return false unless matrix[i].inject(:+) == sum
return false unless matrix.inject(0) { |sum, row| sum += row[i] } == sum
end
magic_diagonals? matrix, sum
end
def Magic.magic_diagonals?(matrix, sum)
first_diagonal_sum, second_diagonal_sum = 0, 0
matrix.each_index do |row|
first_diagonal_sum += matrix[row][row]
second_diagonal_sum += matrix[row][matrix.size() - 1 - row]
end
first_diagonal_sum == sum and
second_diagonal_sum == sum
end
end
Радослав Върбанов
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Радослав Върбанов
class Magic
def self.magic_square?(rows)
current_sum = rows[0].reduce(:+)
is_magic = true
rows.map{ |row| row.reduce(:+) }.each do |row_sum|
is_magic &= (row_sum == current_sum)
end
(0..rows.size - 1).each do |i|
is_magic &= ((0..rows.size - 1).map{ |j| rows[i][j] }.reduce(:+) == current_sum)
end
is_magic &= ((0..rows.size - 1).map{ |i| rows[i][i] }.reduce(:+) == current_sum)
is_magic &= ((0..rows.size - 1).map{ |i| rows[i][rows.size - 1 - i] }.reduce(:+) == current_sum)
is_magic
end
end
Емил Гоцев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Емил Гоцев
class Magic
def self.magic_square?(square)
magic_const = (0..square.size - 1).map { |i| square[i][i] }.inject(:+)
return false if magic_const != (1..square.size).map { |i| square[-i][i-1] }.inject(:+)
return false if square.any? { |row| row.inject(:+) != magic_const }
!square.transpose.any? { |row| row.inject(:+) != magic_const }
end
end
Мартина Величкова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Мартина Величкова
class Magic
def self.magic_square? square
sums_horizontal = square.map { |line| line.inject(:+) }
sums_veritical = square.transpose.map { |column| column.inject(:+) }
sum_main_diagonal , index = 0, 0
square.each do |line|
sum_main_diagonal += line[index]
index += 1
end
sum_antidiagonal, reverce_index = 0, square.size-1
square.each do |line|
sum_antidiagonal += line[reverce_index]
reverce_index -= 1
end
sum_main_diagonal == sum_antidiagonal and
sums_horizontal.all? { |sum| sum == sum_main_diagonal } and
sums_veritical.all? { |sum| sum == sum_main_diagonal }
end
end
Ростислав Градинаров
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Ростислав Градинаров
module Magic
def self.magic_square?(matrix)
(sums_of_rows(matrix) == sums_of_columns(matrix) &&
primary_diagonal_sum(matrix) == secondary_diagonal_sum(matrix) &&
sums_of_rows(matrix).uniq.length == 1 &&
sums_of_rows(matrix)[0] == primary_diagonal_sum(matrix)) ||
matrix[0].empty?
end
def self.sum_of_row(row)
row.inject(0) { |sum, element| sum += element }
end
def self.sums_of_rows(matrix)
matrix.map { |row| sum_of_row(row) }
end
def self.sums_of_columns(matrix)
sums_of_rows(matrix.transpose)
end
def self.primary_diagonal_sum(matrix)
sum = 0
matrix.each_with_index { |row, index| sum += row[index] }
sum
end
def self.secondary_diagonal_sum(matrix)
sum = 0
#help = matrix.size - 1
matrix.each_with_index { |row, index| sum += row[matrix.size - 1 - index] }
sum
end
end
Христо Хърсев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Христо Хърсев
class Magic
attr_accessor :square, :elements, :transposed
def initialize(square)
@elements = []
@square = square
@transposed = square.transpose
square.each do |row|
row.each { |element| elements << element }
end
end
def self.magic_square? (square)
matrix = Magic.new(square)
return true if matrix.elements.length == 0 or matrix.elements.length == 1
return true if matrix.rows_equal_to == matrix.columns_equal_to and matrix.rows_equal_to == matrix.diagonals_equal_to
false
end
def rows_equal_to
rows_equal = []
square.each { |row| rows_equal << row.inject(:+) }
return rows_equal.uniq if rows_equal.uniq.size == 1
"rows"
end
def columns_equal_to
columns_equal = []
transposed.each { |row| columns_equal << row.inject(:+) }
return columns_equal.uniq if columns_equal.uniq.size == 1
"columns"
end
def diagonals_equal_to
first_diagonal = 0
second_diagonal = 0
result = []
(0...elements.length).step(square.length+1).map { |index| first_diagonal += elements[index] }
((square.length-1)...elements.length-(square.length-1)).step(square.length-1).map { |index| second_diagonal += elements[index] }
result << first_diagonal if first_diagonal == second_diagonal
return result
end
end
Мирослав Крамолински
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Мирослав Крамолински
module Magic
def Magic.sum(matrix_side)
sum = 0
matrix_side.map { |number| sum += number }
sum
end
def Magic.horizontal_match?(matrix, magic_sum)
matrix.map { |side| return false if magic_sum != sum(side) }
true
end
def Magic.magic_square_candidate?(square, size_of_side)
calculate_magic_sum(square) % size_of_side == 0
end
def Magic.calculate_magic_sum(square)
magic_sum = 0
square.map { |number| magic_sum += number }
magic_sum
end
def Magic.magic_square?(square)
return false if !magic_square_candidate?(square.flatten, square.length)
magic_sum, length = calculate_magic_sum(square.flatten) / square.length, square.length
square[0] == [] ||
horizontal_match?(square, magic_sum) &&
horizontal_match?(square.transpose, magic_sum) &&
diagonal_match?(square, magic_sum) &&
diagonal_match?(square.transpose, magic_sum)
end
def Magic.diagonal_match?(square, magical_sum)
diagonal_sum = 0
square.each_index { |index| diagonal_sum += square[index][index] }
diagonal_sum == magical_sum
end
end
Петко Борджуков
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Петко Борджуков
class Magic
def self.magic_square?(square)
sums = sum_rows(square) + sum_columns(square) + sum_diagonals(square)
sums.all? { |sum| sum == sums.first }
end
private
def self.sum_rows(matrix)
matrix.map { |row| row.reduce(:+) }
end
def self.sum_columns(matrix)
sum_rows matrix.transpose
end
def self.sum_diagonals(square)
sum_rows [diagonal(square), off_diagonal(square)]
end
def self.diagonal(square)
square.map.with_index { |_, index| square[index][index] }
end
def self.off_diagonal(square)
diagonal square.reverse
end
end
Илия Ячев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Илия Ячев
class Magic
def Magic.magic_square?(array)
matrix = Matrix.new array
matrix.magic_sums.uniq.size == 1
end
end
class Matrix
def initialize(matrix)
@matrix = matrix
end
def rows
@matrix
end
def columns
@matrix.transpose
end
def primary_diagonal
@matrix.each_index.map { |i| @matrix[i][i] }
end
def secondary_diagonal
Matrix.new(@matrix.reverse).primary_diagonal
end
def diagonals
[primary_diagonal, secondary_diagonal]
end
def magic_lines
rows + columns + diagonals
end
def magic_sums
magic_lines.map { |line| line.inject(:+) }
end
end
Изабела Недялкова
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Изабела Недялкова
class Magic
def self.magic_square?(matrix)
first = (0..matrix.length-1).map { |index| matrix[index][index] }.inject { |a,b| a + b }
second = (0..matrix.length-1).map { |index| matrix[index][matrix.length-1-index] }.inject { |a,b| a + b }
third = matrix.map { |line| line.inject { |a,b| a + b } }
fourth = matrix.transpose.map { |line| line.inject { |a,b| a + b } }
*magic = first, second, *third, *fourth
magic.uniq.length == 1
end
end
Христо Владев
  • Непроверено
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)
Христо Владев
module Magic
def self.magic_square?(matrix)
return false if equal_sums? sums_of_rows(matrix)
return false if equal_sums? sums_of_columns(matrix)
sums_of_rows(matrix)[0] == sums_of_columns(matrix)[0] and
sums_of_rows(matrix)[0] == calculate_main_diagonal(matrix) and
sums_of_rows(matrix)[0] == calculate_secondary_diagonal(matrix)
end
def self.equal_sums?(sums)
sums.uniq.length > 1
end
def self.sums_of_rows(matrix)
matrix.map { |row| row.inject(:+) }
end
def self.sums_of_columns(matrix)
sums_of_rows matrix.transpose
end
def self.calculate_main_diagonal(matrix)
matrix.inject(0) do |sum, row|
sum += row[matrix.index(row)]
end
end
def self.calculate_secondary_diagonal(matrix)
matrix.inject(0) do |sum, row|
sum += row[matrix.length - matrix.index(row) - 1]
end
end
end