-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.rb
More file actions
executable file
·24 lines (24 loc) · 832 Bytes
/
task1.rb
File metadata and controls
executable file
·24 lines (24 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env ruby
def mymethod(str_arr)
return [] if str_arr.empty?
# first, sort array by element length, shortest first
new_arr = Array.new(str_arr.sort_by(&:size))
# second, seek through array and put subarus to beginning
ind = 0
foresters = 0
subarus = 0
until new_arr[ind].nil?
# if found, pull it out, add a comment and insert to the beginning
if 'Subaru Forester' == new_arr[ind]
sub = new_arr.slice!(ind) << ' - this guy knows what to do very well'
new_arr.insert(foresters, sub)
foresters += 1
elsif /^Subaru/ =~ new_arr[ind]
sub = new_arr.slice!(ind) << ' - this guy knows what to do'
new_arr.insert(foresters + subarus, sub)
subarus += 1
end
ind += 1
end
new_arr
end