-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuby Array - Index, Part 1
More file actions
91 lines (75 loc) · 2.08 KB
/
Ruby Array - Index, Part 1
File metadata and controls
91 lines (75 loc) · 2.08 KB
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
def init_array()
# create and return array with 10 elements ( integer ) in it
# DO NOT EDIT THIS FUNCTION
x = [9, 7, 6, 5, 4, 6, 7, 1, 2, 3]
x
end
def element_at(arr, index)
arr[index]
end
def inclusive_range(arr, start_pos, end_pos)
arr[start_pos..end_pos]
end
def non_inclusive_range(arr, start_pos, end_pos)
arr[start_pos...end_pos]
end
def start_and_length(arr, start_pos, length)
arr[start_pos,length]
end
arr = init_array()
unless arr.is_a? Array
puts "The variable returned from `init_array` is not an array"
exit(0)
end
unless 10 == arr.length
puts "Elements of the Array variable has to be exactly 10"
exit(0)
else
puts "Correct! Elements of the Array variable are 10 in number!"
end
unless arr.all? {|element| element.is_a? Integer}
puts "All the elements of the Array initialized has to be integers"
exit(0)
else
puts "Correct! All the elements of the Array are Integers!"
end
ind = 8
val = element_at(arr, ind)
unless arr[ind] == element_at(arr, ind)
x = val.to_s
x = "nil" if val.nil?
puts "Element at #{ind} returns #{x} is not #{arr[ind]}"
exit(0)
else
puts "Correct! Element at #{ind} is #{arr[ind]}!"
end
st = 4
en = 9
val = inclusive_range(arr, st, en)
unless arr[st..en] == val
val = "nil" if val.nil?
puts "The elements between the index #{st} and #{en} is #{arr[st..en]} and not #{val}"
exit(0)
else
puts "Correct! The elements between the index #{st} and #{en} is #{val}!"
end
st = 3
en = 8
val = non_inclusive_range(arr, st, en)
unless arr[st...en] == val
val = "nil" if val.nil?
puts "The elements between the index #{st} and #{en} (not included) is #{arr[st...en]} and not #{val}"
exit(0)
else
puts "Correct! The elements between the index #{st} and #{en} (not inclusive) is #{val}!"
end
st = 3
len = 6
val = start_and_length(arr, st, len)
unless arr[st, len] == val
val = "nil" if val.nil?
puts "The #{len} elements starting from #{st} are #{arr[st, len]} and not #{val}"
exit(0)
else
puts "Correct! The #{len} elements starting from #{st} are #{val}!"
end