Last active
December 14, 2015 22:28
-
-
Save granolocks/5158551 to your computer and use it in GitHub Desktop.
proc_tracker.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| class Tracker | |
| def procs | |
| [ | |
| Proc.new { |i| i ** -3 }, | |
| Proc.new { |i| i ** -2 }, | |
| Proc.new { |i| i ** -1 }, | |
| Proc.new { |i| i ** 0 }, | |
| Proc.new { |i| i ** 1 }, | |
| Proc.new { |i| i ** 2 }, | |
| Proc.new { |i| i ** 3 }, | |
| ] | |
| end | |
| def results(data) | |
| result = procs.inject({}) do |output,proc| | |
| source,line = proc.source_location[0], proc.source_location[1] | |
| output[File.read(source).split("\n")[line-1].strip] = proc.call data | |
| output | |
| end | |
| puts "Results for #{data}" | |
| puts JSON.pretty_generate(result) | |
| result | |
| end | |
| def compare(a,b) | |
| a_results = results(a) | |
| b_results = results(b) | |
| same_answer_count = a_results.inject(0) do |count, key_value| | |
| b_results[key_value[0]] == key_value[1] ? count += 1 : nil | |
| count | |
| end | |
| values = a_results.values + b_results.values | |
| result_types = values.inject({}) do |counter, v| | |
| v = v.class | |
| counter[v] == nil ? counter[v] = 1 : counter[v] += 1 | |
| counter | |
| end | |
| differences = a_results.inject({}) do |tracker, kv| | |
| key = kv[0] | |
| a_val = kv[1] | |
| begin | |
| tracker[key] = a_val - b_results[key] | |
| rescue | |
| tracker[key] = "CAN NOT SUBTRACT BVAL from AVAL" | |
| end | |
| tracker | |
| end | |
| puts "Similarities: " | |
| puts "Perfect Match: #{((same_answer_count.to_f / procs.length.to_f) * 100).round(3)}%" | |
| puts "Result Types: " | |
| result_types.each { |type, count| puts "\t#{type.name}: \t#{count}" } | |
| puts "Differences:" | |
| differences.each { |proc_str, diff| puts "\t#{proc_str}\tDelta:\t#{diff}" } | |
| end | |
| end | |
| Tracker.new.compare(5,6) | |
| Tracker.new.compare(99,95) | |
| # Results for: 5 | |
| # { | |
| # "Proc.new { |i| i ** -3 },": "1/125", | |
| # "Proc.new { |i| i ** -2 },": "1/25", | |
| # "Proc.new { |i| i ** -1 },": "1/5", | |
| # "Proc.new { |i| i ** 0 },": 1, | |
| # "Proc.new { |i| i ** 1 },": 5, | |
| # "Proc.new { |i| i ** 2 },": 25, | |
| # "Proc.new { |i| i ** 3 },": 125 | |
| # } | |
| # Results for: 6 | |
| # { | |
| # "Proc.new { |i| i ** -3 },": "1/216", | |
| # "Proc.new { |i| i ** -2 },": "1/36", | |
| # "Proc.new { |i| i ** -1 },": "1/6", | |
| # "Proc.new { |i| i ** 0 },": 1, | |
| # "Proc.new { |i| i ** 1 },": 6, | |
| # "Proc.new { |i| i ** 2 },": 36, | |
| # "Proc.new { |i| i ** 3 },": 216 | |
| # } | |
| # Similarities: | |
| # Perfect Match: 14.286% | |
| # Result Types: | |
| # Rational: 6 | |
| # Fixnum: 8 | |
| # Differences: | |
| # Proc.new { |i| i ** -3 }, Delta: 91/27000 | |
| # Proc.new { |i| i ** -2 }, Delta: 11/900 | |
| # Proc.new { |i| i ** -1 }, Delta: 1/30 | |
| # Proc.new { |i| i ** 0 }, Delta: 0 | |
| # Proc.new { |i| i ** 1 }, Delta: -1 | |
| # Proc.new { |i| i ** 2 }, Delta: -11 | |
| # Proc.new { |i| i ** 3 }, Delta: -91 | |
| # Results for: 99 | |
| # { | |
| # "Proc.new { |i| i ** -3 },": "1/970299", | |
| # "Proc.new { |i| i ** -2 },": "1/9801", | |
| # "Proc.new { |i| i ** -1 },": "1/99", | |
| # "Proc.new { |i| i ** 0 },": 1, | |
| # "Proc.new { |i| i ** 1 },": 99, | |
| # "Proc.new { |i| i ** 2 },": 9801, | |
| # "Proc.new { |i| i ** 3 },": 970299 | |
| # } | |
| # Results for: 95 | |
| # { | |
| # "Proc.new { |i| i ** -3 },": "1/857375", | |
| # "Proc.new { |i| i ** -2 },": "1/9025", | |
| # "Proc.new { |i| i ** -1 },": "1/95", | |
| # "Proc.new { |i| i ** 0 },": 1, | |
| # "Proc.new { |i| i ** 1 },": 95, | |
| # "Proc.new { |i| i ** 2 },": 9025, | |
| # "Proc.new { |i| i ** 3 },": 857375 | |
| # } | |
| # Similarities: | |
| # Perfect Match: 14.286% | |
| # Result Types: | |
| # Rational: 6 | |
| # Fixnum: 8 | |
| # Differences: | |
| # Proc.new { |i| i ** -3 }, Delta: -112924/831910105125 | |
| # Proc.new { |i| i ** -2 }, Delta: -776/88454025 | |
| # Proc.new { |i| i ** -1 }, Delta: -4/9405 | |
| # Proc.new { |i| i ** 0 }, Delta: 0 | |
| # Proc.new { |i| i ** 1 }, Delta: 4 | |
| # Proc.new { |i| i ** 2 }, Delta: 776 | |
| # Proc.new { |i| i ** 3 }, Delta: 112924 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment