Skip to content

Commit

Permalink
Merge pull request #193 from escallic/patch-1
Browse files Browse the repository at this point in the history
calculate n choose k
  • Loading branch information
Joshua Horwitz committed Feb 20, 2018
2 parents a212e9f + 5bd0ed4 commit 0a7e017
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/calc.rb
Expand Up @@ -87,7 +87,23 @@ def self.interpret(command)
return responses
end


matches = command.match(/^what\s+is\s+(.+)\s+choose\s+(.)$/)
if matches

if (matches[1].numeric? && matches[2].numeric?)
arg1 = matches[1]
arg2 = matches[2]
else
return []
end

responses << {
command: "echo 'define f (x) { if (x <= 1) return (1); return (f(x-1) * x); } f(#{arg1})/(f(#{arg2})*f(#{arg1}-#{arg2}))' | bc",
explanation: "Calculates #{arg1} choose #{arg2}"
}
return responses
end

matches = command.match(/^what\s+is\s+(.+)\s+(.+)\s(.+)$/)
if matches

Expand Down

0 comments on commit 0a7e017

Please sign in to comment.