Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Also show SD in fitness.

  • Loading branch information...
commit 9136935c77e33927129c223ef80499b87036ceb3 1 parent f37fd4c
@threedaymonk authored
Showing with 32 additions and 4 deletions.
  1. +32 −4 bin/fitness.rb
View
36 bin/fitness.rb
@@ -10,7 +10,32 @@
SIZE = 10
FLEET = [5, 4, 3, 3, 2]
-load ARGV[0]
+module Stats
+ def sum(&blk)
+ map(&blk).inject { |sum, element| sum + element }
+ end
+
+ def mean
+ (sum.to_f / length.to_f)
+ end
+
+ def median
+ sort[length / 2]
+ end
+
+ def variance
+ m = mean
+ sum { |i| ( i - m )**2 } / length
+ end
+
+ def std_dev
+ Math.sqrt(variance)
+ end
+end
+
+path = ARGV[0]
+$:.unshift File.join(File.dirname(path), "lib")
+load path
player_class = Battleship::Util.find_player_classes.first
@@ -26,7 +51,10 @@
shots
}
-mean = results.inject(&:+) / results.length
-median = results.sort[results.length / 2]
+results.extend(Stats)
-puts "mean: %d median: %d min: %d max: %d" % [mean, median, results.min, results.max]
+puts "mean: %.1f" % results.mean
+puts "median: %d" % results.median
+puts "min: %d" % results.min
+puts "max: %d" % results.max
+puts "sd: %.1f" % results.std_dev
Please sign in to comment.
Something went wrong with that request. Please try again.