28 de julio de 2018

Repasando python. Solución a "Stressful subject - SENDGRID"

def is_stressful(subj):
    """
        recoognise stressful subject       
    """
    stwords=["help","asap","urgent"]
    condition = False
    if subj==subj.upper(): condition = True
    if subj[-3:]=="!!!": condition = True
    # filtrando caracteres y quedándose con letras
    newSubj1=[]
    for a in subj.upper():
        if a.isalpha():
            newSubj1.append(a)
    # Quitando caracteres repetidos
    newSubj2=[]
    for indice, a in enumerate(newSubj1):
        if not (indice>1 and a==newSubj1[indice-1]):
            newSubj2.append(a)
    newSubj2Str="".join(newSubj2)
    print(newSubj2Str)
    # Reconociendo palabras de stwords
    for a in stwords:
        if newSubj2Str.find(a.upper())>=0:
            condition = True
    return condition

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert is_stressful("Aquí hay una HHHELLLLPPPP!!!")==True,"Mine"
    assert is_stressful("Hi") == False, "First"
    assert is_stressful("I neeed HELP") == True, "Second"
    print('Done! Go Check it!')

No hay comentarios:

Publicar un comentario