-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays_hashes_ex1.rb
68 lines (56 loc) · 963 Bytes
/
arrays_hashes_ex1.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
# Exercise 1
stops = [ "Croy", "Cumbernauld", "Falkirk High", "Linlithgow",
"Livingston", "Haymarket" ]
puts "","Answer 1:"
# 1.1
stops.push ("Edinburgh Waverley")
p stops
puts "","Answer 2:"
# 1.2
stops.unshift("Glasgow Queen St")
p stops
puts "","Answer 3:"
# 1.3
stops.insert(4, "Polmont")
p stops
puts "","Answer 4:"
# 1.4
stop_index = stops.index("Linlithgow")
p stop_index
puts "","Answer 5:"
# 1.5
stops.delete("Livingston")
p stops
puts "","Answer 6:"
# 1.6
# At this point "Cumbernauld" element is at index 2
stops.delete_at(2)
p stops
puts "","Answer 7:"
# 1.7
num_stops = stops.length()
p num_stops
=begin
# from the homework solution
stops.size()
stops.count()
=end
puts "","Answer 8:"
# 1.8
p stops[2]
p stops.at(2)
p stops.fetch(2)
p stops.include?("Falkirk High")
=begin
# from the homework solution
stops[-5]
stops.at(-5)
=end
puts "","Answer 9:"
# 1.9
p stops.reverse()
puts "","Answer 10:"
# 1.10
for stop in stops
puts stop
end