Skip to content

Instantly share code, notes, and snippets.

@e-parkinson
Last active August 26, 2015 19:58
Show Gist options
  • Select an option

  • Save e-parkinson/74868e1c8522a4ef833b to your computer and use it in GitHub Desktop.

Select an option

Save e-parkinson/74868e1c8522a4ef833b to your computer and use it in GitHub Desktop.
'''
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