Advertisement
Woobinda

Composite words

Apr 5th, 2020
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. data = "cat; cats; catsdogcats; dog; dogcatsdog; hippopotamuses; rat; ratcatdogcat"
  2.  
  3.  
  4. def func(data):
  5.     WORDS_DELIMITER = '; '
  6.     data = list(data.split(WORDS_DELIMITER))
  7.     simple_words = [data[0]]
  8.     composite_words = {}
  9.  
  10.     for current_word in data[1:]:
  11.         for simple_word in simple_words:
  12.  
  13.             if simple_word in current_word and is_composite_word(current_word, simple_word, simple_words):
  14.                 composite_words[len(current_word)] = current_word
  15.             else:
  16.                 simple_words.append(current_word)
  17.  
  18.     print("Простые слова: %s\n" % simple_words, "Длина сосотавного слова и значение: %s\n" % composite_words)
  19.     return "Два самых длинных словосочетания: %s" % sorted(composite_words, key=lambda word_len: int(word_len))[-2:]
  20.  
  21.  
  22. def is_composite_word(current_word, delimiter, simple_words):
  23.     print(current_word, delimiter, simple_words)
  24.     if not current_word:
  25.         return True
  26.  
  27.     for split_word_part in current_word.split(delimiter, 1):
  28.  
  29.         print(split_word_part)
  30.  
  31.         if not split_word_part or split_word_part in simple_words:
  32.             print('not split part: %s' % split_word_part)
  33.             continue
  34.  
  35.         else:
  36.             for simple_word in simple_words:
  37.                 print(simple_word, simple_words)
  38.                 if simple_word in split_word_part:
  39.                     print('Simple word: %s ' % simple_word)
  40.                     return is_composite_word(split_word_part, simple_word, simple_words)
  41.                 else:
  42.                     return False
  43.  
  44.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement