Skip to content

Instantly share code, notes, and snippets.

@jeffsaracco
Forked from isa/gist:2571012
Created May 1, 2012 23:36
Show Gist options
  • Save jeffsaracco/2572386 to your computer and use it in GitHub Desktop.
Save jeffsaracco/2572386 to your computer and use it in GitHub Desktop.
Convert in less than 30 lines
Question: Convert following into the latter data structure in less than 30 lines:
List:
A, B, C
A, C, E
E, F, D
D, A, J
E, D, J
List
A, B, 1 (frequency)
A, C, 2
A, D, 1
A, E, 1
A, J, 1
B, C, 1
C, E, 1
D, E, 2
D, F, 1
D, J, 2
E, F, 1
E, J, 1
list = [["A", "B", "C"], ["A", "C", "E"], ["E", "F", "D"], ["D", "A", "J"], ["E", "D", "J"]].map { |item| item.permutation(2).to_a.map(&:sort).uniq }.flatten(1)
combo = list.uniq.map {|c| c + [list.count(c)] }
combo
@andyl
Copy link

andyl commented May 2, 2012

dude you could have done it in two lines!!

@jeffsaracco
Copy link
Author

Yeah I realized that after I posted it. Figured it didn't matter though since, like you, anyone who knows ruby knows that the assignment in line 2 and all of line 3 do nothing. They were left over from my testing where I was inspecting 'combo'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment