Source

Coderbyte: Longest Word

My solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import doctest
import pytest
from os import system

from string import punctuation as punct


def longest_word(mystr: str) -> str:
    '''Return the longest word in the string, ignoring punctuation
    and returning the first of multiple longest words.

    >>> longest_word('foobar baz')
    'foobar'
    '''
    if mystr == '' or ' ' not in mystr:
        return mystr
    words: list = list(x.strip(punct) for x in mystr.split(' '))
    long_len: int = max(len(x) for x in words)
    ret: list = list(x for x in words if len(x) == long_len)
    return ret[0]


def test_mytest():
    assert longest_word('') == '', 'failed on empty string'
    assert longest_word('foo') == 'foo', 'failed on single word'
    assert longest_word('foo bar') == 'foo', 'failed on words of same length'
    assert longest_word('foobar baz') == 'foobar'


def test_provided():
    assert longest_word('I love dogs') == 'love'
    assert longest_word('fun&!! time') == 'time'
    mystr = "a confusing /:sentence:/[ this is not!!!!!!!~"
    assert longest_word(mystr) == 'confusing'


if __name__ == '__main__':
    pytest.main(['-q', __file__])
    doctest.testmod(optionflags=doctest.FAIL_FAST)
    system(f'mypy {__file__} --ignore-missing-imports')
    system(f'flake8 {__file__}')