-
Notifications
You must be signed in to change notification settings - Fork 68
/
CalcFunc.rb
100 lines (93 loc) · 2.18 KB
/
CalcFunc.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
=begin
This time we want to write calculations using functions and get the results.
Let's have a look at some examples:
seven(times(five)) # must return 35
four(plus(nine)) # must return 13
eight(minus(three)) # must return 5
six(divided_by(two)) # must return 3
Requirements:
There must be a function for each number from 0 ("zero") to 9 ("nine")
There must be a function for each of the following mathematical operations:
plus, minus, times, dividedBy (divided_by in Ruby)
Each calculation consist of exactly one operation and two numbers
The most outer function represents the left operand, the most inner function
represents the right operand
=end
# My Solution
def zero(op="")
return 0 if op == ""
check(0,op[1].to_i,op[0])
end
def one(op="")
return 1 if op == ""
check(1,op[1].to_i,op[0])
end
def two(op="")
return 2 if op == ""
check(2,op[1].to_i,op[0])
end
def three(op="")
return 3 if op == ""
check(3,op[1].to_i,op[0])
end
def four(op="")
return 4 if op == ""
check(4,op[1].to_i,op[0])
end
def five(op="")
return 5 if op == ""
check(5,op[1].to_i,op[0])
end
def six(op="")
return 6 if op == ""
check(6,op[1].to_i,op[0])
end
def seven(op="")
return 7 if op == ""
check(7,op[1].to_i,op[0])
end
def eight(op="")
return 8 if op == ""
check(8,op[1].to_i,op[0])
end
def nine(op="")
return 9 if op == ""
check(9,op[1].to_i,op[0])
end
def plus(num,num2="")
return "+#{num}" if num2 == ""
return num + num2
end
def minus(num,num2="")
return "-#{num}" if num2 == ""
return num - num2
end
def times(num,num2="")
return "*#{num}" if num2 == ""
return num * num2
end
def divided_by(num,num2="")
return "/#{num}" if num2 == ""
return num.to_f / num2.to_f
end
def check (num1, num2, op)
case op
when "+" ; plus(num1,num2)
when "-" ; minus(num1,num2)
when "*" ; times(num1,num2)
when "/" ; divided_by(num1,num2)
end
end
# Better Solution
class Object
%w[zero one two three four five six seven eight nine].each_with_index do |name, n|
define_method(name) do |args = nil|
args ? n.send(*args) : n.to_f
end
end
%w[plus + minus - times * divided_by /].each_slice(2) do |name, symbol|
define_method(name) do |n|
[symbol, n]
end
end
end