forked from AdaGold/recursion-tracing
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmystery-methods.rb
67 lines (61 loc) · 923 Bytes
/
mystery-methods.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
def mystery1(n)
puts n
if n == 1
return n
else
return n + mystery1(n-1)
end
end
def mystery2(n)
if n < 10
return n
else
return (n%10) + mystery2(n/10)
end
end
def mystery3(n)
if n == 0
return 100
elsif n == -1
return 200
end
if n%2 == 0
return mystery3(n/2)
else
return mystery3(n-1)
end
end
def mystery4(b,e)
if e == 0
return 1
else
return b * mystery4(b,e-1)
end
end
def mystery5(s)
if s.length == 0
return ""
else
return "*" + mystery5(s[1..-1])
end
end
def mystery6(s)
if s == nil || s.length == 0
return ""
else
space = 0
until space >= s.length || s[space] == " "
space += 1
end
return mystery6(s[(space+1)..-1]) + " " + s[0...space]
end
end
def mystery7(word)
if word.length < 2
return true
elsif word[0] != word[-1]
return false
else
return mystery7(word[1..-2])
end
end