Skip to content

Instantly share code, notes, and snippets.

@nmische
Last active December 16, 2015 23:19
Show Gist options
  • Save nmische/5512786 to your computer and use it in GitHub Desktop.
Save nmische/5512786 to your computer and use it in GitHub Desktop.
Given a string list all the two letter pairs. Also, give the probability of a third letter following a given pair.
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.each_pair do |k,v|
p k.to_s + ": " + ( v.empty? ? "1" : v.values.reduce(:+).to_s )
end
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}%"
"Pairs:"
"if: 4"
"f : 2"
" y: 8"
"yo: 8"
"ou: 9"
"u : 1"
" a: 9"
"ar: 6"
"re: 7"
"e : 13"
" s: 6"
"se: 3"
"el: 2"
"lf: 2"
"f-: 2"
"-m: 2"
"mo: 3"
"ot: 5"
"ti: 3"
"iv: 2"
"va: 2"
"at: 5"
"te: 5"
"ed: 5"
"d,: 2"
", : 5"
" w: 13"
"wo: 4"
"ow: 2"
"w,: 1"
" t: 9"
"th: 7"
"hi: 2"
"is: 6"
"s : 14"
"or: 10"
"rl: 3"
"ld: 3"
"d : 5"
" i: 7"
"ta: 1"
"ai: 1"
"il: 8"
"lo: 2"
" f: 8"
"fo: 4"
"r : 8"
"u.: 4"
". : 10"
"he: 4"
" b: 7"
"bo: 1"
"un: 2"
"nd: 2"
"da: 1"
"ri: 4"
"ie: 3"
"es: 6"
"al: 5"
"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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment