Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. # This program distinguishes an unknown word at any place against two known words
  2.  
  3. list = ['I', 'am'] # two known words
  4. entered_line = input('Enter the line')
  5.  
  6. def word_in_line(line): # checks if the line has a word in the begining
  7.  
  8. new_word = '' # dummy
  9. for x in line:
  10. new_word += x # adds from the begining of string
  11.  
  12. if new_word in list: # to see if a word is formed
  13. return True, new_word, line[len(new_word):] # returns true, the word found, and the rest of string
  14. else:
  15. return False, new_word # returning false means there is no known words in the begining of the string
  16.  
  17. def word_in_later(line): # checks if there is a word in the middle of the string
  18.  
  19. left_word = '' # dummy
  20. rest_of_line = line
  21.  
  22. for x in range(0, len(rest_of_line)):
  23. left_word += rest_of_line[0] # takes one letter off at a time
  24. rest_of_line = rest_of_line[1:] # to make a new string
  25.  
  26. if word_in_line(rest_of_line)[0] == True: # and checks if this now has a word
  27. 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
  28. else:
  29. continue
  30.  
  31.  
  32. def final_line(line):
  33.  
  34. final_line = ''
  35.  
  36. if word_in_line(line)[0] == True: # Is there a word in the begining ? yes
  37. final_line += word_in_line(line)[1] + ' ' # add that word
  38. rest = word_in_line(line)[2] # finds the rest of the string
  39.  
  40. if word_in_line(rest)[0] == True: # is there a word in the begining of the rest ? yes
  41. final_line += word_in_line(rest)[1] + ' ' + word_in_line(rest)[2] # adds this known word and the remaining unknown word
  42. else:
  43. final_line += word_in_later(rest)[0] + ' ' + word_in_later(rest)[1] # No, meaning the unknown word is in the middle of 3
  44. else:
  45. final_line += word_in_later(line)[0] + ' ' + word_in_later(line)[1] + ' ' + word_in_later(line)[2] # means unknown word in begining
  46.  
  47. return final_line
  48.  
  49. print(final_line(entered_line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement