User:Alice/Code Exercises

From XPUB & Lens-Based wiki
< User:Alice
Revision as of 13:04, 27 January 2018 by Alice (talk | contribs)

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)
  1. sentence = input('What is your sentence? ')
  2. seven(sentence)
  1. pytest requires that you name your tests with test_<your-name>
  2. run with the 'pytest' command in your terminal

def test_seven():

   assert seven('Baboons') == 'babushkas'
   assert seven('Baboons,') == 'babushkas'

Python exercise inspired by the work of Carl Andre

Andre-poems-18.jpg