Last active
August 26, 2015 19:58
-
-
Save e-parkinson/74868e1c8522a4ef833b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Input: user enters string consisting of words without spaces | |
| Output: each word is printed on a different line. | |
| In case of overlap, favours shorter words on left hand side. | |
| words.txt is file containing all valid words to check against | |
| ''' | |
| def checkIfReal(word): | |
| real = False | |
| file = open('words.txt', 'r') | |
| word = word.lower() | |
| for line in file: | |
| if word == line.strip('\n'): | |
| real = True | |
| break | |
| return real | |
| def keepTestingString(userString, n): | |
| testString = userString[:n] | |
| #checkIfReal(testString) | |
| if checkIfReal(testString): | |
| print(testString) | |
| userString = userString[n:] | |
| iterateOverString(userString) | |
| if not checkIfReal(testString): | |
| if n < 1: | |
| print(userString) | |
| else: | |
| n = n-1 | |
| keepTestingString(userString, n) | |
| def iterateOverString(userString): | |
| n = len(userString) | |
| keepTestingString(userString, n) | |
| #main method | |
| print('Enter string: ') | |
| userString=str(input()) | |
| iterateOverString(userString) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment