Skip to content

Instantly share code, notes, and snippets.

@noahd1
Created October 10, 2012 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahd1/3863064 to your computer and use it in GitHub Desktop.
Save noahd1/3863064 to your computer and use it in GitHub Desktop.
Homework 1, #6
#!/usr/bin/env ruby
class Array
def agreement(other)
raise ArgumentError, "Cannot compare arrays of different sizes" if size != other.size
other.each_with_index.map do |elem, i|
self[i] == elem ? 1 : 0
end
end
end
data_set = [
[1,0,1],
[1,1,0],
[1,1,1]
]
target_functions = [
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 1, 0],
[1, 0, 1],
[0, 1, 1],
[1, 1, 1]
]
def score
running_tally = 0
[3,2,1,0].each do |weight|
running_tally += (yield weight) * weight
end
running_tally
end
def xor(point)
point.count(1) % 2 == 1 ? 1 : 0
end
def xor_all(points)
points.map { |point| xor(point) }
end
def nxor(point)
point.count(1) % 2 == 1 ? 0 : 1
end
def nxor_all(points)
points.map { |point| nxor(point) }
end
total_a = score do |i|
target_functions.select { |target_function| target_function.count(1) == i }.size
end
total_b = score do |i|
target_functions.select { |target_function| target_function.count(0) == i }.size
end
xor_data = xor_all(data_set)
nxor_data = nxor_all(data_set)
total_c = score do |i|
target_functions.select do |target_function|
target_function.agreement(xor_data).count(1) == i
end.size
end
total_d = score do |i|
target_functions.select do |target_function|
target_function.agreement(nxor_data).count(1) == i
end.size
end
puts total_a
puts total_b
puts total_c
puts total_d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment