Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
22
Task/Hash-join/Ruby/hash-join.rb
Normal file
22
Task/Hash-join/Ruby/hash-join.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def hashJoin(table1, index1, table2, index2)
|
||||
# hash phase
|
||||
h = table1.group_by {|s| s[index1]}
|
||||
h.default = []
|
||||
# join phase
|
||||
table2.collect {|r|
|
||||
h[r[index2]].collect {|s| [s, r]}
|
||||
}.flatten(1)
|
||||
end
|
||||
|
||||
table1 = [[27, "Jonah"],
|
||||
[18, "Alan"],
|
||||
[28, "Glory"],
|
||||
[18, "Popeye"],
|
||||
[28, "Alan"]]
|
||||
table2 = [["Jonah", "Whales"],
|
||||
["Jonah", "Spiders"],
|
||||
["Alan", "Ghosts"],
|
||||
["Alan", "Zombies"],
|
||||
["Glory", "Buffy"]]
|
||||
|
||||
hashJoin(table1, 1, table2, 0).each { |row| p row }
|
||||
Loading…
Add table
Add a link
Reference in a new issue