-
-
Save andyl/2572454 to your computer and use it in GitHub Desktop.
Convert in less than 30 lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
DATA = <<-EOF | |
A, B, C | |
A, C, E | |
E, F, D | |
D, A, J | |
E, D, J | |
EOF | |
x = DATA.lines.reduce([]) do |a,line| | |
v = line.chomp.gsub(' ','').split(',') | |
r1 = ["#{v[0]}#{v[1]}", "#{v[0]}#{v[2]}", "#{v[1]}#{v[2]}"] | |
r2 = ["#{v[1]}#{v[0]}", "#{v[2]}#{v[0]}", "#{v[2]}#{v[1]}"] | |
a + r1 + r2 | |
end.sort | |
y = x.reduce({}) {|a,v| a[v] = a[v].nil? ? 1 : a[v] + 1; a} | |
y.each_pair {|k,v| puts "#{k[0]}, #{k[1]}, #{v}" } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
x = %w(ABC ACE EFD DAJ EDJ).map do |v| | |
[v[0],v[1],v[2]].permutation(2).to_a | |
end.flatten(1).sort | |
x.uniq.each {|y| puts "#{y.join(', ')}, #{x.count(y)}"} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment