Skip to content

Instantly share code, notes, and snippets.

@kml
Created December 13, 2012 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kml/4275404 to your computer and use it in GitHub Desktop.
Save kml/4275404 to your computer and use it in GitHub Desktop.
=begin
$ uname -a
Linux jboss-updater2 3.2.21-nkt #1 SMP Wed Aug 1 20:23:57 CEST 2012 x86_64 GNU/Linux
$ java -version
java version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-2)
OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)
$ jruby --version
jruby 1.6.8 (ruby-1.9.2-p312) (2012-09-18 1772b40) (OpenJDK 64-Bit Server VM 1.7.0_03) [linux-amd64-java]
=end
require 'ostruct'
def combinations
flags = []
10.times do |i|
flags << OpenStruct.new(weight: i)
end
a = []
1.upto(flags.count).each do |n|
flags.combination(n).each { |c| a << c }
end
a.sort { |x, y| combination_weight(y) <=> combination_weight(x) }
end
def combination_weight(combination)
sum = combination.inject(0) { |sum, flag| sum + flag.weight }
x = combination.size.to_f ** 1
sum / x
end
a = [1,1]
k = (a.size.to_f ** 2)
l = a.size.to_f ** 2
m = 2.0 ** 2
n = (2.0 ** 2)
puts "BEFORE k: #{k} l: #{l} m: #{m} n: #{n}"
combinations.size
a = [1,1]
k = (a.size.to_f ** 2)
l = a.size.to_f ** 2
m = 2.0 ** 2
n = (2.0 ** 2)
puts "AFTER k: #{k} l: #{l} m: #{m} n: #{n}"
=begin
Sample outputes:
BEFORE k: 4.0 l: 4.0 m: 4.0 n: 4.0
AFTER k: 0.0 l: 1.0 m: 4.0 n: 1.0
BEFORE k: 4.0 l: 4.0 m: 4.0 n: 4.0
AFTER k: 0.0 l: 0.0 m: 4.0 n: 15.992994600801687
BEFORE k: 4.0 l: 4.0 m: 4.0 n: 4.0
AFTER k: 0.0 l: 15.992994600801687 m: 4.0 n: 1.0
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment