Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program distinguishes an unknown word at any place against two known words
- list = ['I', 'am'] # two known words
- entered_line = input('Enter the line')
- def word_in_line(line): # checks if the line has a word in the begining
- new_word = '' # dummy
- for x in line:
- new_word += x # adds from the begining of string
- if new_word in list: # to see if a word is formed
- return True, new_word, line[len(new_word):] # returns true, the word found, and the rest of string
- else:
- return False, new_word # returning false means there is no known words in the begining of the string
- def word_in_later(line): # checks if there is a word in the middle of the string
- left_word = '' # dummy
- rest_of_line = line
- for x in range(0, len(rest_of_line)):
- left_word += rest_of_line[0] # takes one letter off at a time
- rest_of_line = rest_of_line[1:] # to make a new string
- if word_in_line(rest_of_line)[0] == True: # and checks if this now has a word
- return left_word, word_in_line(rest_of_line)[1], word_in_line(rest_of_line)[2] # returns the left, the word, and the rest
- else:
- continue
- def final_line(line):
- final_line = ''
- if word_in_line(line)[0] == True: # Is there a word in the begining ? yes
- final_line += word_in_line(line)[1] + ' ' # add that word
- rest = word_in_line(line)[2] # finds the rest of the string
- if word_in_line(rest)[0] == True: # is there a word in the begining of the rest ? yes
- final_line += word_in_line(rest)[1] + ' ' + word_in_line(rest)[2] # adds this known word and the remaining unknown word
- else:
- final_line += word_in_later(rest)[0] + ' ' + word_in_later(rest)[1] # No, meaning the unknown word is in the middle of 3
- else:
- final_line += word_in_later(line)[0] + ' ' + word_in_later(line)[1] + ' ' + word_in_later(line)[2] # means unknown word in begining
- return final_line
- print(final_line(entered_line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement