-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_one_s_sorter.rb
42 lines (35 loc) · 1 KB
/
binary_one_s_sorter.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
class BinaryOneSSorter
# Sort numbers by their binary value with the highest numbers of 1s
# For conflict choose number with highest decimal value
def self.sort(items)
hash_to_sorted_array(hash_by_binary(items))
end
def self.to_binary(item)
item.to_s(2)
end
private
def self.hash_to_sorted_array(values)
items = []
values.keys.sort_by{|key| (key.count '1') * -1 }.each do |key|
items.concat(values[key].sort_by {|item| item * -1})
end
items
end
def self.get_key(item)
return '0' if item == 0
self.to_binary(item).gsub('0','')
end
def self.hash_by_binary(items)
values = {}
items.each_with_index do |item, index|
key = get_key(item)
values[key] = [] if values[key].nil?
values[key] << item
end
values
end
end
def print_custom_sorted_binary_array(items)
BinaryOneSSorter.sort(items).each {|item| puts "#{item}"}
end
print_custom_sorted_binary_array([31, 15, 7, 2, 1, 33, 35])