forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayExchange.rb
More file actions
36 lines (29 loc) · 719 Bytes
/
ArrayExchange.rb
File metadata and controls
36 lines (29 loc) · 719 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
25
26
27
28
29
30
31
32
33
34
35
36
=begin
Array Exchange and Reversing
It's time for some array exchange! The objective is simple: To exchange between
two arrays but with one catch; the content of the exchanged array must be
reversed.
my_array = ['a', 'b', 'c']
other_array= [1, 2, 3]
my_array.exchange_with!(other_array)
The expected output:
my_array = [3, 2, 1]
other_array = ['c', 'b', 'a']
=end
# My Solution
class Array
def exchange_with!(other_array)
a = []
self.each{|x| a << x}
self.replace(other_array.reverse)
other_array.replace(a.reverse)
end
end
# Better Solution
class Array
def exchange_with!(other_array)
temp = self.dup
self.replace other_array.reverse
other_array.replace temp.reverse
end
end