I am working through Learn Python The Hard Way and have reached a challenge with Exercise 48. You are given a large amount of code as a unit test and asks us to create a function in order to make the unit tests pass. I am not sure what exactly this code should look like. I have pasted in one of the functions as a reference. They all look similar to this one and I'm sure if I understand how to make this one pass, I can figure out the rest. Thanks guys!
from nose.tools import *
from ex48 import lexicon
def test_directions():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
class lexicon:
@staticmethod
def scan(s):
return [('direction',x) for x in s.split()]
print(lexicon.scan("north south east"))
Here's my caveman Python solution:
def scan(data):
data = data.split()
results = []
for l in data:
if l in directions:
results.append(('direction', l))
elif l in verbs:
results.append(('verb', l))
elif l in stop_words:
results.append(('stop', l))
elif l in nouns:
results.append(('noun', l))
elif convert_number(l) in numbers:
results.append(('number', convert_number(l)))
else:
results.append(('error', l))
return results
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With