-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcodewords_from_generating_matrics.rb
75 lines (63 loc) · 1.46 KB
/
codewords_from_generating_matrics.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/ruby
# https://youtu.be/iQsKdtjwtYI?t=181
# STDIN.each_line do |line|
# nums = line.scan(/\d+/).map(&:to_i)
# a = nums[0]
# b = nums[1]
# res = (a - b).abs
# puts res
# end
# STDIN.each_line do |line|
# for i in 1...line.length
# if line[i] == 's' and line[i - 1] == 's'
# puts 'hiss'
# exit
# end
# end
# puts 'no hiss'
# end
def product(matrix, input, k)
matrix.map do |row|
sum = 0
0.upto(k - 1) do |i|
sum += row[i] * input[i]
end
sum % 2
end
end
def process(matrix, n, k)
# puts "\nMatrix: "
# matrix.each { |row| puts row.to_s }
# puts
min_distance = k
[0,1].repeated_permutation(k).each do |bin_input|
# print bin_input.to_s
codeword = product(matrix, bin_input, k)
distance = codeword.count(1)
# puts " - #{distance} -- #{codeword}"
if distance > 0 and distance < min_distance
min_distance = distance
end
end
puts min_distance == k ? 0 : min_distance
end
gets.chomp.to_i.times do
n, k = gets.chomp.split(' ').map(&:to_i)
# puts
matrix = []
n.times do
matrix << gets.chomp.split(' ').map(&:to_i)
end
process(matrix, n, k)
end
# STDIN.each_line do |line|
# while true
# line = gets.chomp
# arr = line.split(' ')
# exit if arr.length != 2
# s, l = arr
# llen = l.length
# slen = s.length
# puts "#{get_type1(l, s, llen, slen)} #{get_type2(l, s, llen, slen)} #{get_type3(l, s, llen, slen)}"
# end
# STDIN.read / STDOUT.write