Skip to content

Instantly share code, notes, and snippets.

@CamonZ

CamonZ/file.rb Secret

Last active December 18, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamonZ/4aad283de81902e035b4 to your computer and use it in GitHub Desktop.
Save CamonZ/4aad283de81902e035b4 to your computer and use it in GitHub Desktop.
def _simple_compress(nodes)
# dedup nodes
set = {}
nodes.each do |node|
set[node] = true
end
nodes = set.keys
nodes = _sort_nodes(nodes)
result = []
prev_prefix, prev_digits, prev_suffix = "", nil, ""
prev_n = nil
count = 0
nodes.each do |n|
if n =~ /\A#{@@NodeRegx}\z/
# foo100abc => foo 100 abc
prefix, digits, suffix = $1, $2, $3
prefix = "" if prefix.nil?
suffix = "" if suffix.nil?
else
prefix, digits, suffix = n, nil, nil
end
if (not digits.to_i.zero?) and
(prefix == prev_prefix) and
(suffix == prev_suffix) and
(not prev_digits.nil?) and
(digits.to_i == prev_digits.to_i + count + 1)
count += 1
next
end
if prev_n
if count > 0
result << _get_group(prev_prefix, prev_digits, count, prev_suffix)
else
result << prev_n
end
end
prev_n = n
prev_prefix = prefix
prev_digits = digits
prev_suffix = suffix
count = 0
end #nodes.each
if count > 0
result << _get_group(prev_prefix, prev_digits, count, prev_suffix)
else
result << prev_n
end
return result.join ","
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment