-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays_select_practice_problems.rb
More file actions
297 lines (221 loc) · 6.11 KB
/
Copy patharrays_select_practice_problems.rb
File metadata and controls
297 lines (221 loc) · 6.11 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# 1. Start with an array of numbers and create a new array with only the numbers less than 20.
# For example, [2, 32, 80, 18, 12, 3] becomes [2, 18, 12, 3].
# While Loop
numbers = [2, 32, 80, 18, 12, 3]
less_twenty = []
i = 0
while i < numbers.length
if numbers[i] < 20
less_twenty << numbers[i]
end
i += 1
end
p less_twenty
# Each Do
numbers = [2, 32, 80, 18, 12, 3]
less_twenty = []
numbers.each do |number|
if number < 20
less_twenty << number
end
end
p less_twenty
# 2. Start with an array of strings and create a new array with only the strings that start with the letter "w".
# For example, ["winner", "winner", "chicken", "dinner"] becomes ["winner", "winner"].
# While Loop
strings = ["winner", "winner", "chicken", "dinner"]
w_strings = []
i = 0
while i < strings.length
if strings[i][0] == "w"
w_strings << strings[i]
end
i += 1
end
p w_strings
# Each Do
strings = ["winner", "winner", "chicken", "dinner", "wisconsin"]
w_strings = []
strings.each do |string|
if string[0] == "w"
w_strings << string
end
end
p w_strings
# 3. Start with an array of hashes and create a new array with only the hashes with prices greater than 5 (from the :price key).
# For example, [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}] becomes [{name: "chair", price: 100}].
# While Loop
# products = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}]
# five = []
# i = 0
# while i < products.length
# product = products[i]
# if product[:price] > 5
# five << product
# end
# i += 0
# end
# p five
# Each Do
products = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}, {name: "plate", price: 8}]
expensive_products = []
products.each do |product|
if product[:price] > 5
expensive_products << product
end
end
p expensive_products
# 4. Start with an array of numbers and create a new array with only the even numbers.
# For example, [2, 4, 5, 1, 8, 9, 7] becomes [2, 4, 8].
# While Loop
numbers = [2, 4, 5, 1, 8, 9, 7]
even_numbers = []
i = 0
while i < numbers.length
if numbers[i] % 2 == 0
even_numbers << numbers[i]
end
i += 1
end
p even_numbers
# Each Do
numbers = [2, 4, 5, 1, 8, 9, 7]
even_numbers = []
numbers.each do |number|
if number % 2 == 0
even_numbers << number
end
end
p even_numbers
# 5. Start with an array of strings and create a new array with only the strings shorter than 4 letters.
# For example, ["a", "man", "a", "plan", "a", "canal", "panama"] becomes ["a", "man", "a", "a"].
# While Loop
strings = ["a", "man", "a", "plan", "a", "canal", "panama"]
small_strings = []
i = 0
while i < strings.length
if strings[i].length < 4
small_strings << strings[i]
end
i += 1
end
p small_strings
# Each Do
strings = ["a", "man", "a", "plan", "a", "canal", "panama"]
small_strings = []
strings.each do |string|
if string.length < 4
small_strings << string
end
end
p small_strings
# 6. Start with an array of hashes and create a new array with only the hashes with names shorter than 6 letters (from the :name key).
# For example, [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}] becomes [{name: "chair", price: 100}, {name: "book", price: 4}].
# While Loop
hashes = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}]
small_hashes = []
i = 0
while i < hashes.length
if hashes[i][:name].length < 6
small_hashes << hashes[i]
end
i += 1
end
p small_hashes
# Each Do
hashes = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}]
small_hashes = []
hashes.each do |hash|
if hash[:name].length < 6
small_hashes << hash
end
end
p small_hashes
# 7. Start with an array of numbers and create a new array with only the numbers less than 10.
# For example, [8, 23, 0, 44, 1980, 3] becomes [8, 0, 3].
# While Loop
numbers = [8, 23, 0, 44, 1980, 3]
small_numbers = []
i = 0
while i < numbers.length
if numbers[i] < 10
small_numbers << numbers[i]
end
i += 1
end
p small_numbers
# Each Do
numbers = [8, 23, 0, 44, 1980, 3]
small_numbers = []
numbers.each do |number|
if number < 10
small_numbers << number
end
end
p small_numbers
# 8. Start with an array of strings and create a new array with only the strings that don't start with the letter "b".
# For example, ["big", "little", "good", "bad"] becomes ["little", "good"].
# While Loop
words = ["big", "little", "good", "bad"]
b_words = []
i = 0
while i < words.length
if words[i][0] != "b"
b_words << words[i]
end
i += 1
end
p b_words
# Each Do
words = ["big", "little", "good", "bad"]
b_words = []
words.each do |word|
if word[0] != "b"
b_words << word
end
end
p b_words
# 9. Start with an array of hashes and create a new array with only the hashes with prices less than 10 (from the :price key).
# For example, [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}] becomes [{name: "pencil", price: 1}, {name: "book", price: 4}].
# While Loop
prices = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}]
low_prices = []
i = 0
while i < prices.length
if prices[i][:price] < 10
low_prices << prices[i]
end
i += 1
end
p low_prices
# Each Do
prices = [{name: "chair", price: 100}, {name: "pencil", price: 1}, {name: "book", price: 4}]
low_prices = []
prices.each do |price|
if price[:price] < 10
low_prices << price
end
end
p low_prices
# 10. Start with an array of numbers and create a new array with only the odd numbers.
# For example, [2, 4, 5, 1, 8, 9, 7] becomes [5, 1, 9, 7].
# While Loop
numbers = [2, 4, 5, 1, 8, 9, 7]
odd_numbers = []
i = 0
while i < numbers.length
if numbers[i] % 2 == 1
odd_numbers << numbers[i]
end
i += 1
end
p odd_numbers
# Each Do
numbers = [2, 4, 5, 1, 8, 9, 7]
odd_numbers = []
numbers.each do |number|
if number % 2 == 1
odd_numbers << number
end
end
p odd_numbers