-
Notifications
You must be signed in to change notification settings - Fork 68
/
MakeMaths.rb
37 lines (26 loc) · 859 Bytes
/
MakeMaths.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
=begin
Given two numbers and an arithmetic operator (the name of it, as a string),
return the result of the two numbers having that operator used on them.
a and b will both be positive integers, and a will always be the first number
in the operation, and b always the second.
The four operators are "add", "subtract", "divide", "multiply".
A few examples:
arithmetic(5, 2, "add") should return 7
arithmetic(5, 2, "subtract") should return 3
arithmetic(5, 2, "multiply") should return 10
arithmetic(5, 2, "divide") should return 2.5
=end
# My Solution
def arithmetic(a, b, operator)
case operator
when "add" ; a + b
when "subtract" ; a - b
when "multiply" ; a * b
when "divide" ; a / b
end
end
# Better Solution
def arithmetic(a, b, operator)
ar={"add"=>a+b,"subtract"=>a-b,"multiply"=>a*b,"divide"=>a.div(b)}
ar[operator]
end