/
ch-2.rb
executable file
·52 lines (46 loc) · 919 Bytes
/
ch-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/ruby
def matrixscore(matrix0)
matrix = matrix0
0.upto(matrix.length-1) do |i|
if matrix[i][0] == 0 then
0.upto(matrix[i].length-1) do |j|
matrix[i][j] = 1 - matrix[i][j]
end
end
end
t = matrix.length.div(2)
1.upto(matrix[0].length-1) do |i|
c = 0
0.upto(matrix.length-1) do |j|
if matrix[j][i] == 0 then
c += 1
end
end
if c > t then
0.upto(matrix.length-1) do |j|
matrix[j][i] = 1 - matrix[j][i]
end
end
end
tot = 0
matrix.each do |m|
p = 0
m.each do |n|
p *= 2
if n == 1 then
p += 1
end
end
tot += p
end
return tot
end
require 'test/unit'
class TestMatrixscore < Test::Unit::TestCase
def test_ex1
assert_equal(39, matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]]))
end
def test_ex2
assert_equal(1, matrixscore([[0]]))
end
end