How to verify if a poetic composition is a tautograma?

Asked

Viewed 140 times

-4

The user enters a sentence and I have to check that all the initial letters of each word are equal. So far my code is like this:

frase = str(input()).split
if frase == frase[0]:
    print("tautograma")
else:
    print("Não é um tautograma")

Example of a Tautogram:

Within this wanton delusion, you have left golden days of despondency, Decorated with despot pain.

How can I solve?

2 answers

2

I did it in a simpler way to make it easier for you to understand:

Putting the sentence in all minuscule letters to avoid comparison error:

frase = 'Dentro desta devassa desilusão, Deixaste dias dourados de desalentos, Decorados de déspota dor.'.lower()

Splitting the string into slices to form a word list:

palavras = frase.split(' ')

If a letter other than the initial script stops, if it matches it continues running and counting the initial letters:

Edit: As hkotsubo’s suggestion, removing the contactor is more readable and easy to understand:

frase = 'Dentro desta devassa desilusão, Deixaste dias dourados de desalentos, Decorados de déspota dor.'.lower()
palavras = frase.split(' ')

for palavra in palavras:
    if palavra[0] != frase[0]:
        print('A frase não é um Tautograma.')
        break
else:
    print('A frase é um Tautograma.')

Another way more 'advanced':

frase = 'Dentro desta devassa desilusão, Deixaste dias dourados de desalentos, Decorados de déspota dor.'.lower()
palavras = frase.split(' ')
len(palavras) == len([i for i in palavras if i[0] == frase[0]])

Returns True if it is a Tautograma and False if it is not.

  • 1

    You don’t need that counter, you can use one for/else: https://ideone.com/xhGkap - but it doesn’t really need to either: https://ideone.com/8P4nvi

0

As I understand it, the questioner is trying to implement an algorithm that checks whether or not a word sequence is a tautograma.

Well. First of all we must define what is tautograma.

Tautograma is a poetic composition in which ALL words begin with the same letter. The beauty of tautogram lies in the composer’s ability to combine words so that they produce a poetic sense.

To resolve this issue we can use the following code:

poema = input('Composição poética: ').lower().split()

if len({i[0] for i in poema}) == 1:
    print('É tautograma')
else:
    print('Não é tautograma')

Note that in this code is captured a poetic composition. Then the block if checks that the size of the set formed by only the initial letter of each word of the poetic composition is equal to 1. If positive, the poetic composition is a tautogram. If negative, it is not tautogram.


If you prefer, we can also implement a function. This way the code would be:

def verifica_tautograma(p):
    return len({i[0] for i in p}) == 1


if __name__ == '__main__':
    poema = input('Composição poética: ').lower().split()
    resposta = verifica_tautograma(poema)

    print(resposta)

In this code the return of the function will be only a boolean value - True or False. So, if the poetic composition is a taugram, the return of the function will be True. Otherwise, the return will be False.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.