-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.rb
executable file
·41 lines (32 loc) · 929 Bytes
/
x.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
require 'benchmark'
require 'pry'
require 'awesome_print'
F = Struct.new(:name, :size) do
def grouping
"#{name}/#{size}"
end
end
N = 3
files = []
3.times { files << F.new("#{%w(a b c).sample}.txt", (1..5).to_a.sample) }
files = [F.new('a', 1), F.new('b', 1), F.new('a', 1), F.new('a', 2), F.new('a', 3), F.new('a', 3)]
def make_hash(files)
result = {}
files.each do |file|
result[file.name] ||= []
result[file.name] << files.select{|f| f.size == file.size && file.name == f.name}
end
result.each do |key, val|
result[key] = val.select{|i| val.count(i) > 1}.uniq
end
result = result.reject{|k,v| v.empty? }
result
end
def test_inject(files)
files.group_by{|e|"#{e.name}/#{e.size}"}.inject({}){|res, (k, v)| res[k.split('/')[0]] = v if v.size > 1; res}
end
ap files
ap '------------'
ap make_hash(files)
ap '------------'
ap test_inject(files)