-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_answers.rb
250 lines (201 loc) · 5.48 KB
/
quiz_answers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Homework
## Exercise A
### Given the following data structure:
```ruby
stops = [ "Croy", "Cumbernauld", "Falkirk High", "Linlithgow", "Livingston", "Haymarket" ]
```
### Complete these tasks:
Answers are cumulative..?
1. Add `"Edinburgh Waverley"` to the end of the array
stops << "Edinburgh Waverley"
2. Add `"Glasgow Queen St"` to the start of the array
stops.unshift("Glasgow Queen St")
3. Add `"Polmont"` at the appropriate point (between `"Falkirk High"` and `"Linlithgow"`)
stops.insert(4, "Polmont")
4. Work out the index position of `"Linlithgow"`
linlithgow_index = stops.index("Linlithgow")
5. Remove `"Livingston"` from the array using its name
stops.delete("Livingston")
6. Delete `"Cumbernauld"` from the array by index
stops.delete_at(2)
7. How many stops there are in the array?
stops.length() .size() .count()
8. How many ways can we return `"Falkirk High"` from the array?
stops[3]
lin = stops.select{ |a| a == "Falkirk High" }.join
stops.reject{ |a| a != "Falkirk High" }.join # .join removes the square brackets
stops[3...4].join()
9. Reverse the positions of the stops in the array
stops.reverse()
10. Print out all the stops using a for loop
for stop in stops
p stop
end
## Exercise B
### Given the following data structure:
# ```ruby
users = {
"Jonathan" => {
:twitter => "jonnyt",
:lottery_numbers => [6, 12, 49, 33, 45, 20],
:home_town => "Stirling",
:pets => [
{
:name => "fluffy",
:species => "cat"
},
{
:name => "fido",
:species => "dog"
},
{
:name => "spike",
:species => "dog"
}
]
},
"Erik" => {
:twitter => "eriksf",
:lottery_numbers => [18, 34, 8, 11, 24],
:home_town => "Linlithgow",
:pets => [
{
:name => "nemo",
:species => "fish"
},
{
:name => "kevin",
:species => "fish"
},
{
:name => "spike",
:species => "dog"
},
{
:name => "rupert",
:species => "parrot"
}
]
},
"Avril" => {
:twitter => "bridgpally",
:lottery_numbers => [12, 14, 33, 38, 9, 25],
:home_town => "Dunbar",
:pets => [
{
:name => "monty",
:species => "snake"
}
]
}
}
# ```
### Complete these tasks:
1. Get Jonathan's Twitter handle (i.e. the string `"jonnyt"`)
users["Jonathan"][:twitter]
# a hash must be followed by [] to access data within
2. Get Erik's hometown
users["Erik"][:home_town]
3. Get the array of Erik's lottery numbers
users["Erik"][:lottery_numbers]
4. Get the type of Avril's pet Monty
p users["Avril"][:pets][0][:species]
# watch out for bracket types / types of datastructures
# pets is an array so that it allows us to add more pets (a data group)
5. Get the smallest of Erik's lottery numbers
users ["Erik"][:lottery_numbers].min()
6. Return an array of Avril's lottery numbers that are even
users["Avril"][:lottery_numbers].select {|num| num.even?}
result = []
for number in users["Avril"][:lottery_numbers]
result << number if number.even?
end
p result
Or
def evil_even_numbers(array_of_numbers)
result = []
for number in array_of_numbers
result.push(number) if(number.even?)
end
return result
end
array = users["Avril"][:lottery_numbers]
evens = evil_even_numbers(array)
p evens
7. Erik is one lottery number short! Add the number `7` to be included in his lottery numbers
p users["Erik"][:lottery_numbers].push(7)
8. Change Erik's hometown to Edinburgh
users["Erik"][:home_town] = "Edinburgh"
9. Add a pet dog to Erik called "Fluffy"
users["Erik"][:pets].push({name: "Fluffy", species: "dog"})
dog = { # alternate method, more readable
:name => "fluffy",
:species => "dog"
}
users["Erik"][:pets] << dog
10. Add another person to the users hash
users["Alex"] = {
:twitter => (),
:lottery_numbers => [1, 2, 9, 10, 14, 28],
:home_town => "Edinburgh",
:pets => [
{
:name => (),
:species => ()
}
]
}
me = { # alternate method, more readable. better for functions?
:twitter => "tgoncalves",
:lottery_numbers => [1, 2, 9, 10, 14, 28],
:home_town => "Morningside",
:pets => [
{
:name => "tommy",
:species => "cat"
}
]
}
users["Tony"] = me # users["Tony"] accesses the user hash and checks if there is a key "Tony", if not, creates new key "Tony"
## Exercise C
### Given the following data structure:
```ruby
united_kingdom = [
{
name: "Scotland",
population: 5295000,
capital: "Edinburgh"
},
{
name: "Wales",
population: 3063000,
capital: "Swansea"
},
{
name: "England",
population: 53010000,
capital: "London"
}
]
```
### Complete these tasks:
1. Change the capital of Wales from `"Swansea"` to `"Cardiff"`.
united_kingdom[1][:capital] = "Cardiff"
2. Create a Hash for Northern Ireland and add it to the `united_kingdom` array (The capital is Belfast, and the population is 1,811,000).
united_kingdom.push({name: "Northern Ireland", population: 1811000, capital: "Belfast"})
northern_ireland = { # another method, more readable
name: "Northern Ireland",
population: 1811000,
capital: "Belfast"
}
united_kingdom.push(northern_ireland)
3. Use a loop to print the names of all the countries in the UK.
for country in united_kingdom
p country[:name]
end
4. Use a loop to find the total population of the UK.
tot_pop = 0
for country in united_kingdom
tot_pop += country[:population]
end
p tot_pop