DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #307 - Spanish Conjugator

In Spanish, verbs change according to the person we're talking about. Something similar happens in English when we use the third person singular pronouns he/she:

I run You run He/She runs 
Enter fullscreen mode Exit fullscreen mode

Spanish is similar, the suffix changes depending on who we're talking about. Your task is to write a function called conjugate which will return a Spanish verb in all of its conjugations.

Examples

conjugate(comer)
["como", "comes", "come", "comemos", "coméis", "comen"]

conjugate(vivir)
["vivo", "vives", "vive", "vivimos", "vivís", "viven"]

Tests

conjugate(comer)
conjugate(bailar)
conjugate(caminar)

Good luck!


This challenge comes from Phares on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (7)

Collapse
 
alvaromontoro profile image
Alvaro Montoro

In JavaScript:

const conjugate = verb => { switch(verb.slice(-2)) { case "ar": return ["o","as","a","amos","ais","an"].map(el => verb.slice(0, verb.length-2) + el); case "er": return ["o","es","e","emos","eis","en"].map(el => verb.slice(0, verb.length-2) + el); case "ir": return ["o","es","e","imos","is","en"].map(el => verb.slice(0, verb.length-2) + el); }; } 
Enter fullscreen mode Exit fullscreen mode
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 • Edited

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
Collapse
 
aminnairi profile image
Amin • Edited

CoffeeScript

conjugate = (verb) -> base = verb[...-2] prepend = (words) -> "#{base}#{suffix}" for suffix in words switch verb[-2..] when "ir" prepend ["o", "es", "e", "imos", "ís", "en"] when "ar" prepend ["o", "as", "a", "amos", "áis", "an"] else prepend ["o", "es", "e", "emos", "éis", "en"] 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aminnairi profile image
Amin

Haskel

conjugate :: String -> [String] conjugate verb | suffix == "er" = prepend base ["o", "es", "e", "emos", "eis", "en"] | suffix == "ir" = prepend base ["o", "es", "e", "imos", "is", "en"] | otherwise = prepend base ["o", "as", "a", "amos", "ais", "an"] where untilSuffix = length verb - 2 base = take untilSuffix verb suffix = drop untilSuffix verb prepend word words = map ((++) word) words 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zeedu_dev profile image
Eduardo Zepeda

As a spanish native this is the first time I see a spanish conjugator, I've encountered countless plural/singular converters but nothing similar to this, awesome!

Collapse
 
nosoycesaros profile image
Cesar Zapata

Same feeling!