Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learn Python the Hard Way Exercise 48 help

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')])
like image 429
Adam Avatar asked Aug 18 '26 23:08

Adam


2 Answers

class lexicon:
    @staticmethod
    def scan(s):
        return [('direction',x) for x in s.split()]

print(lexicon.scan("north south east")) 
like image 85
Petar Ivanov Avatar answered Aug 20 '26 13:08

Petar Ivanov


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
like image 31
robbyt Avatar answered Aug 20 '26 13:08

robbyt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!