Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 0.47 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def lcs(list1, list2):
  2.     real_lcs = []
  3.     if len(list1) == 1 or len(list2) == 1:
  4.         if len(list1) == 1 and list1[0] in list2:
  5.             return list1[0]
  6.         if len(list2) == 1 and list2[0] in list1:
  7.             return list2[0]
  8.         return []
  9.     if list1[0] == list2[0]:
  10.         real_lcs += list1[0]
  11.         real_lcs += lcs(list1[1:], list2[1:])
  12.     else:
  13.         real_lcs += longer(lcs(list1[1:], list2), lcs(list1, list2[1:]))
  14.     return real_lcs