fork download
  1. def common_count(t0, t1):
  2. "returns the length of the longest common prefix"
  3. for i, pair in enumerate(zip(t0, t1)):
  4. if pair[0] != pair[1]:
  5. return i
  6. return i
  7.  
  8. def group_by_longest_prefix(iterable):
  9. "given a sorted list of strings, group by longest common prefix"
  10. longest = 0
  11. out = []
  12.  
  13. for t in iterable:
  14. if out: # for there are previous entries
  15.  
  16. # determine length of prefix in common with prev line
  17. common = common_count(t, out[-1])
  18.  
  19. # if the current entry has a shorted prefix, output the previous
  20. # entries then start a new group
  21. if common < longest:
  22. yield out
  23. longest = 0
  24. out = []
  25. # otherwise, just update the target prefix length
  26. else:
  27. longest = common
  28.  
  29. # add the current entry to the group
  30. out.append(t)
  31.  
  32. # ouput remaining entries as the last group
  33. if out:
  34. yield out
  35.  
  36. text = """
  37. abcd
  38. abdd
  39. abde
  40. """
  41.  
  42. T = sorted(t.strip() for t in text.split("\n") if t)
  43.  
  44. for L in group_by_longest_prefix(T):
  45. print L
  46.  
  47.  
Success #stdin #stdout 0.02s 7204KB
stdin
Standard input is empty
stdout
['abcd', 'abdd', 'abde']