-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_and_search.rb
66 lines (56 loc) · 1.21 KB
/
sort_and_search.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
#11.1
def merge_arrays(a, b)
al = a.length - 1
bl = b.length - 1
tl = al + bl + 1
while bl >= 0
if al >= 0 && a[al] > b[bl]
a[tl] = a[al]
al -= 1
else
a[tl] = b[bl]
bl -= 1
end
tl -= 1
end
a
end
def merge_arrays_from_beginning(a,b)
c = Array.new
loop do
break if a.empty? || b.empty?
c << (a.first < b.first ? a.shift : b.shift)
end
c = c + a + b
end
describe 'merge two sorted array' do
it 'merge_arrays' do
a = [1, 2, 4, 6, 7]
b = [3, 5, 8, 9, 10]
c = merge_arrays_from_beginning(a, b)
c.should eql([1,2,3,4,5,6,7,8,9,10])
end
end
# 11.2
def sort_array_of_strings_for_anagrams(array)
# table = {}
# array.map do |element|
# s = element.chars.sort.join
# table[s] ||= []
# table[s].push(element)
# end
table = array.inject({}) do |memo, element|
s = element.chars.sort.join
memo[s] ||= []
memo[s].push(element)
memo
end
table.values.flatten
end
describe 'sort array of strings' do
it 'sort_array_of_strings_for_anagrams' do
array = ['god', 'dog', 'abc', 'cba', 'man']
table = sort_array_of_strings_for_anagrams(array)
table.should eql(['god', 'dog', 'abc', 'cba', 'man'])
end
end