ruby - clean "prefixes" in a set of arrays? -


i wondering, i've got set of arrays containing of own datatype. looking like:

traces = {[<label1>, <label2>], [<label1>], [<label1>,<label2>,<label3>]} 

now, have method cleans 'prefix'-existing arrays in set, new set in example:

traces = {[<label1>,<label2>,<label3>]}  

anybody idea how make clean implementation out of this? hope there neater solution stepping through traces , set new_traces , comparing every array-item several times.

note: define array prefix of array b iff first items of array b array a

not quite fastest solution, rather simple:

s = set.new([[1,2],[1],[1,2,3]]) s.reject{|prefix|   s.any?{|array|     array.length > prefix.length && array[0,prefix.length] == prefix   } } #=>[[1, 2, 3]] 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -