-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_type.rb
56 lines (48 loc) · 1006 Bytes
/
sequence_type.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
def geo_sequence?(arr)
if arr.length < 1
return nil
end
i = 1
common_var = arr[i] / arr[i-1]
while i < arr.length
if (arr[i] / arr[i-1]) != common_var || (arr[i] % arr[i-1]) != 0
return false
end
i+=1
end
return true
end
def arith_sequence?(arr)
if arr.length < 1
return nil
end
i = 1
common_var = arr[i] - arr[i-1]
while i < arr.length
if (arr[i] - arr[i-1]) != common_var
return false
end
i+=1
end
return true
end
def sequence_type(arr)
geo = geo_sequence?(arr)
arith = arith_sequence?(arr)
if geo == true
return "Geometric sequence"
elsif arith == true
return "Arithematic sequence"
elsif arith == nil && geo == nil
return "Array input too short"
elsif arith == false && geo == false
return "Neither geometric or arithematic sequence detected"
else
return "Error"
end
end
puts sequence_type([1,3,5,7,9])
puts sequence_type([1,4,7,10,11])
puts sequence_type([2,4,8,16,32])
puts sequence_type([3,6,12,24,48])
puts sequence_type([2,6,5,10,48])