User:Alice/Code Exercises
Oulipo exercise
Improved the N + 7 code we wrote in Prototyping, by debugging and adding some tests. Work in progress.
Improvements
Bug example: the for loop was skipping capitalized words, because it could not find them in the input file with nouns.
To fix, I added the lower method for strings to turn all text to lowercase.
for word in separated:
word = word.lower() + '\n'
To test it, I added a test.
def test_seven():
assert seven('Baboons') == 'babushkas'
Full script (in progress)
def seven(sentence):
fpath = open('91K nouns.txt')
nouns = fpath.readlines()
separated = sentence.split()
#print(separated)
new_separated = []
for word in separated:
word = word.lower() + '\n'
if word in nouns:
position = nouns.index(word)
new_word = nouns[position + 7]
#print(" replacing", new_position)
new_separated.append(new_word.strip())
else:
#print("notinlist")
#print("adding to new_separated ", word)
new_separated.append(word.strip())
#print(new_separated)
return ' '.join(new_separated)
- sentence = input('What is your sentence? ')
- seven(sentence)
- pytest requires that you name your tests with test_<your-name>
- run with the 'pytest' command in your terminal
def test_seven():
assert seven('Baboons') == 'babushkas'
assert seven('Baboons,') == 'babushkas'