Skip to content

Instantly share code, notes, and snippets.

@rlb3
Last active December 15, 2015 05:49
Show Gist options
  • Select an option

  • Save rlb3/5211897 to your computer and use it in GitHub Desktop.

Select an option

Save rlb3/5211897 to your computer and use it in GitHub Desktop.
require 'rspec'
require 'ap'
FILE = <<EOF
10.1.4.4 165.91.182.7
10.1.4.5 165.91.182.7
10.1.4.6 165.91.182.8
165.91.187.3 165.91.187.3
192.168.1.3
10.0.0.5 10.0.0.1
10.5.5.5
10.1.4.10 165.91.182.7
EOF
def calulate_structure
stash = []
group = {}
order = []
FILE.split("\n").each do |line|
local, public = line.split("\s")
key = public || local
group[key] ||= []
group[key].push [local, public]
order.push(key) if !order.member? key
end
order.each do |key|
if group[key].size == 1
stash.push group[key].pop
else
stash.push group[key]
end
end
stash
end
describe 'The Data Structure' do
expected = [
[
['10.1.4.4', '165.91.182.7'],
['10.1.4.5', '165.91.182.7'],
['10.1.4.10','165.91.182.7'],
],
['10.1.4.6', '165.91.182.8'],
['165.91.187.3', '165.91.187.3'],
['192.168.1.3', nil],
['10.0.0.5', '10.0.0.1'],
['10.5.5.5', nil],
]
it 'should be the same' do
calulate_structure.should == expected
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment