Last active
December 15, 2015 06:39
-
-
Save krzysiekherod/5218227 to your computer and use it in GitHub Desktop.
Script for calculating your happinass from www.trackyourhappiness.org reports
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
# Script for calculating sumarized happiness | |
# from raports from www.trackyourhappiness.org | |
# Reports from surveys have very detailed results, | |
# but don't have sumarized estimation of your happiness | |
# during the survey. You can use this script to calculate it | |
# from data stored in the page of the report | |
# | |
# How to use it: | |
# Fill in SURVEYS array and run "ruby trackyourhappiness.rb" | |
# | |
# You have to have ruby installed, check out http://www.ruby-lang.org | |
# Tested on ruby 1.9.3 | |
class TrackYourHappiness | |
# using Firebug in Firefox, or any other your favorite development tool | |
# of your favorite browser, check the URL of the image of the "Weekday" chart | |
# it starts like this: | |
# https://chart.googleapis.com/chart?chs=310x200&cht=bvg... | |
# at the end of this URL there is: | |
# ...&chd=s%3ACLNcln7&chbh=30 | |
# so "CLNcln7" (last 7 characters before last "&") is your data. | |
# Put this data from all the surveys you filled in this array | |
SURVEYS = ["AJRajr5", "CLNcln7"] | |
def print | |
SURVEYS.each_with_index do |values_string, chart| | |
puts "survey #{chart + 1}:" | |
values = map_to_values(values_string, chart_dictionary) | |
normalized = normalize(values, chart_dictionary.length) | |
average = (normalized.inject(:+) / normalized.length).round | |
puts "#{average}%, days: #{normalized.inspect} " | |
end | |
end | |
def map_to_values(string, dictionary) | |
result = [] | |
string.each_char { |ch| result << dictionary.index(ch) } | |
result | |
end | |
def chart_dictionary | |
return @chart_values if defined?(@chart_values) | |
@chart_values = [] | |
@chart_values += characters_from_range('A'..'Z') | |
@chart_values += characters_from_range('a'..'z') | |
@chart_values += characters_from_range('0'..'9') | |
@chart_values | |
end | |
def normalize(values, dictionary_size) | |
values.map { |value| (100 * Float(value) / (dictionary_size - 1)).round } | |
end | |
def characters_from_range(range) | |
result = [] | |
range.each { |ch| result << ch } | |
result | |
end | |
end | |
TrackYourHappiness.new.print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment