Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = "cat; cats; catsdogcats; dog; dogcatsdog; hippopotamuses; rat; ratcatdogcat"
- def func(data):
- WORDS_DELIMITER = '; '
- data = list(data.split(WORDS_DELIMITER))
- simple_words = [data[0]]
- composite_words = {}
- for current_word in data[1:]:
- for simple_word in simple_words:
- if simple_word in current_word and is_composite_word(current_word, simple_word, simple_words):
- composite_words[len(current_word)] = current_word
- else:
- simple_words.append(current_word)
- print("Простые слова: %s\n" % simple_words, "Длина сосотавного слова и значение: %s\n" % composite_words)
- return "Два самых длинных словосочетания: %s" % sorted(composite_words, key=lambda word_len: int(word_len))[-2:]
- def is_composite_word(current_word, delimiter, simple_words):
- print(current_word, delimiter, simple_words)
- if not current_word:
- return True
- for split_word_part in current_word.split(delimiter, 1):
- print(split_word_part)
- if not split_word_part or split_word_part in simple_words:
- print('not split part: %s' % split_word_part)
- continue
- else:
- for simple_word in simple_words:
- print(simple_word, simple_words)
- if simple_word in split_word_part:
- print('Simple word: %s ' % simple_word)
- return is_composite_word(split_word_part, simple_word, simple_words)
- else:
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement