DEV Community

loading...

Discussion on: Daily Challenge #307 - Spanish Conjugator

Collapse
peter279k profile image
peter279k

Here is the simple solution in Python:

def conjugate(verb):
    verb = verb.lower()
    ans = {}
    if (verb[-2] + verb[-1]) == 'ar':
        ans[verb] = [
            verb[0:-2] + 'o',
            verb[0:-2] + 'as',
            verb[0:-1] + '',
            verb[0:-2] + 'amos',
            verb[0:-2] + 'ais',
            verb[0:-2] + 'an',
        ]
    if (verb[-2] + verb[-1]) == 'er':
        ans[verb] = [
            verb[0:-2] + 'o',
            verb[0:-2] + 'es',
            verb[0:-1] + '',
            verb[0:-2] + 'emos',
            verb[0:-2] + 'eis',
            verb[0:-2] + 'en',
        ]
    if (verb[-2] + verb[-1]) == 'ir':
        ans[verb] = [
            verb[0:-2] + 'o',
            verb[0:-2] + 'es',
            verb[0:-2] + 'e',
            verb[0:-2] + 'imos',
            verb[0:-2] + 'is',
            verb[0:-2] + 'en',
        ]

    return ans
Enter fullscreen mode Exit fullscreen mode
Collapse
agtoever profile image
agtoever

Nice answer. Here is a suggestion that gets rid of the duplicate code by putting the verb endings in a dict and using a comprehension to give the result:

def conjugate(verb):
    verb = verb.lower()
    conj = {
        'ar': ['o', 'as', '', 'amos', 'ais', 'an'],
        'er': ['o', 'es', '', 'emos', 'eis', 'en'],
        'ir': ['o', 'es', 'e', 'imos', 'is', 'en']
    }
    return [verb[:-2] + c if len(c) > 0 else verb[:-1] + c for c in conj[verb[-2:]]]
Enter fullscreen mode Exit fullscreen mode