def sort_common_subsequences(items):
 def contains(seq, x):
  for item in seq:
   if (item is x):
    return True
   elif (item.is_sequence_constructor and contains(item.args, x)):
    return True
  return False
 def lower_than(a, b):
  return (b.is_sequence_constructor and contains(b.args, a))
 for (pos, item) in enumerate(items):
  key = item[1]
  new_pos = pos
  for i in range((pos - 1), (-1), (-1)):
   if lower_than(key, items[i][0]):
    new_pos = i
  if (new_pos != pos):
   for i in range(pos, new_pos, (-1)):
    items[i] = items[(i - 1)]
   items[new_pos] = item

def sort_common_subsequences ( items ) : def contains ( seq , x ) : for item in seq : if ( item is x ) : return True elif ( item . is_sequence_constructor and contains ( item . args , x ) ) : return True return False def lower_than ( a , b ) : return ( b . is_sequence_constructor and contains ( b . args , a ) ) for ( pos , item ) in enumerate ( items ) : key = item [ 1 ] new_pos = pos for i in range ( ( pos - 1 ) , ( - 1 ) , ( - 1 ) ) : if lower_than ( key , items [ i ] [ 0 ] ) : new_pos = i if ( new_pos != pos ) : for i in range ( pos , new_pos , ( - 1 ) ) : items [ i ] = items [ ( i - 1 ) ] items [ new_pos ] = item
sort items/subsequences so that all items and subsequences that an item contains appear before the item itself .