Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Created June 24, 2015 11:30
Show Gist options
  • Select an option

  • Save Vaguery/9e4deff81247f381f249 to your computer and use it in GitHub Desktop.

Select an option

Save Vaguery/9e4deff81247f381f249 to your computer and use it in GitHub Desktop.
Counting sub-sequence patterns in an array of unique numbers
Patterns in [10, 1, 3, 2, 8, 9, 5, 4, 6, 7]:
[10, 1, 3] => 312
[10, 1, 2] => 312
[10, 1, 8] => 312
[10, 1, 9] => 312
[10, 1, 5] => 312
[10, 1, 4] => 312
[10, 1, 6] => 312
[10, 1, 7] => 312
[10, 3, 2] => 321
[10, 3, 8] => 312
[10, 3, 9] => 312
[10, 3, 5] => 312
[10, 3, 4] => 312
[10, 3, 6] => 312
[10, 3, 7] => 312
[10, 2, 8] => 312
[10, 2, 9] => 312
[10, 2, 5] => 312
[10, 2, 4] => 312
[10, 2, 6] => 312
[10, 2, 7] => 312
[10, 8, 9] => 312
[10, 8, 5] => 321
[10, 8, 4] => 321
[10, 8, 6] => 321
[10, 8, 7] => 321
[10, 9, 5] => 321
[10, 9, 4] => 321
[10, 9, 6] => 321
[10, 9, 7] => 321
[10, 5, 4] => 321
[10, 5, 6] => 312
[10, 5, 7] => 312
[10, 4, 6] => 312
[10, 4, 7] => 312
[10, 6, 7] => 312
[1, 3, 2] => 132
[1, 3, 8] => 123
[1, 3, 9] => 123
[1, 3, 5] => 123
[1, 3, 4] => 123
[1, 3, 6] => 123
[1, 3, 7] => 123
[1, 2, 8] => 123
[1, 2, 9] => 123
[1, 2, 5] => 123
[1, 2, 4] => 123
[1, 2, 6] => 123
[1, 2, 7] => 123
[1, 8, 9] => 123
[1, 8, 5] => 132
[1, 8, 4] => 132
[1, 8, 6] => 132
[1, 8, 7] => 132
[1, 9, 5] => 132
[1, 9, 4] => 132
[1, 9, 6] => 132
[1, 9, 7] => 132
[1, 5, 4] => 132
[1, 5, 6] => 123
[1, 5, 7] => 123
[1, 4, 6] => 123
[1, 4, 7] => 123
[1, 6, 7] => 123
[3, 2, 8] => 213
[3, 2, 9] => 213
[3, 2, 5] => 213
[3, 2, 4] => 213
[3, 2, 6] => 213
[3, 2, 7] => 213
[3, 8, 9] => 123
[3, 8, 5] => 132
[3, 8, 4] => 132
[3, 8, 6] => 132
[3, 8, 7] => 132
[3, 9, 5] => 132
[3, 9, 4] => 132
[3, 9, 6] => 132
[3, 9, 7] => 132
[3, 5, 4] => 132
[3, 5, 6] => 123
[3, 5, 7] => 123
[3, 4, 6] => 123
[3, 4, 7] => 123
[3, 6, 7] => 123
[2, 8, 9] => 123
[2, 8, 5] => 132
[2, 8, 4] => 132
[2, 8, 6] => 132
[2, 8, 7] => 132
[2, 9, 5] => 132
[2, 9, 4] => 132
[2, 9, 6] => 132
[2, 9, 7] => 132
[2, 5, 4] => 132
[2, 5, 6] => 123
[2, 5, 7] => 123
[2, 4, 6] => 123
[2, 4, 7] => 123
[2, 6, 7] => 123
[8, 9, 5] => 231
[8, 9, 4] => 231
[8, 9, 6] => 231
[8, 9, 7] => 231
[8, 5, 4] => 321
[8, 5, 6] => 312
[8, 5, 7] => 312
[8, 4, 6] => 312
[8, 4, 7] => 312
[8, 6, 7] => 312
[9, 5, 4] => 321
[9, 5, 6] => 312
[9, 5, 7] => 312
[9, 4, 6] => 312
[9, 4, 7] => 312
[9, 6, 7] => 312
[5, 4, 6] => 213
[5, 4, 7] => 213
[5, 6, 7] => 123
[4, 6, 7] => 123
{"123"=>32, "132"=>28, "213"=>8, "231"=>4, "312"=>36, "321"=>12}
# build a table of pattern recognizers based on < relations between all elements of an array
def less_than_vector(numbers)
numbers.combination(2).collect {|pair| (pair[0]<pair[1])}
end
@pattern_length = 3
def pattern_dictionary(length)
lookups = (1..length).to_a.permutation().inject([]) do |array,pattern|
key = pattern.join
value = less_than_vector(pattern)
array << value << key
end
Hash[*lookups]
end
# puts pattern_dictionary(3).inspect
# convert a sequence into a collection of sub-sequences
@sequence = (1..10).to_a.shuffle
puts "Patterns in #{@sequence.inspect}:"
def count_every_pattern(numbers,size)
matcher = pattern_dictionary(size)
counts = Hash.new(0)
(0...numbers.length).to_a.combination(size) do |indices|
subsequence = indices.collect{|idx| numbers[idx]}
puts "#{subsequence.inspect} => #{matcher[less_than_vector(subsequence)]}"
counts[matcher[less_than_vector(subsequence)]] += 1
end
return Hash[counts.sort]
end
puts count_every_pattern(@sequence,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment