User:Alice/Code Exercises: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 6: Line 6:


To fix, I added the lower method for strings to turn all text to lowercase.
To fix, I added the lower method for strings to turn all text to lowercase.
<code>
<code>
  for word in separated:
  for word in separated:
         word = word.lower() + '\n'
         word = word.lower() + '\n'
</code>
</code>
To test it, I added a test.
To test it, I added a test.
<code>
<code>
def test_seven():
def test_seven():
     assert seven('Baboons') == 'babushkas'
     assert seven('Baboons') == 'babushkas'
</code>
</code>
Full script (in progress)
Full script (in progress)
<code>
<code>
def seven(sentence):
def seven(sentence):

Revision as of 14:29, 27 January 2018

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