Skip to content

Instantly share code, notes, and snippets.

@davekonopka
Forked from jamiely/probability.rb
Last active December 16, 2015 23:19
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 davekonopka/5513456 to your computer and use it in GitHub Desktop.
Save davekonopka/5513456 to your computer and use it in GitHub Desktop.
input = "If you are self-motivated, wow, this world is tailored for you. The boundaries are all gone. But if you're not self-motivated, this world will be a challenge because the walls, ceilings and floors that protected people are also disappearing. That is what I mean when I say \"it is a 401(k) world.\" Government will do less for you. Companies will do less for you. Unions can do less for you. There will be fewer limits, but also fewer guarantees. Your specific contribution will define your specific benefits much more. Just showing up will not cut it. - T. Friedman".downcase
pair = "ou"
third = "r"
pairs = (0..input.length-2).inject({}) do |h,i|
key, val = input[i,2], input[i + 2]
h[key] = {} unless h[key]
h[key][val] = (h[key][val] || 0) + 1 if val
h
end
p "Pairs:"
pairs.keys.sort.each { |k| p k }
prob = if pairs[pair] && pairs[pair][third]
( pairs[pair][third].to_f / pairs[pair].values.reduce(:+).to_f ) * 100
else
0
end
p "Probability:"
p "#{prob}%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment