How could I resolve this issue using Python dictionaries?

Asked

Viewed 86 times

-4

Make a program that reads two keyboard strings and tells:

• The characters of the first text that are not in the second;

• The characters of the second text that are not in the first;

• The characters that are in both texts;

• The total number of distinct characters in both texts.

Example: Enter with the first text: Wendel

Enter with the second text: Lexandre Characters from the first text that are not in the second: w

Characters from the second text that are not in the first: r to x

Characters in both texts: and l n d

Total number of distinct characters in both texts: 8

He gave example of how to make the character frequency

def contaFrequencia(texto):
    frequencia = {}

    for c in texto:
        if c not in frequencia:
            frequencia[c] = 0

        frequencia[c] = frequencia[c] + 1
    return frequencia

if __name__ == "__main__":
    texto = input('Entre com um texto: ')
    freqs = contaFrequencia(texto)

    print('Frequência de caracteres:')
    for c in freqs.keys():
        print('%s : %s ' % (c, freqs[c]))

1 answer

2


One way to solve this problem is by using ensembles.
A set object is an unordered collection of distinct hasheable objects, there is no repetition of elements. Common uses include association tests, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Convert each input string into a character set and apply relational operators to get the desired information:

  • To know the characters of the first text that are not in the second do the difference amid texto1 and texto2.

  • To know the characters of the second text that are not in the first make the difference between texto2 and texto1.

  • To know the characters that are in both texts do a intercession between the two texts.

  • To know the total number of distinct characters in both texts do the union between the two sets and measure the length of the result.

texto1 = "wendel"#input('Entre com um texto: ')
texto2 = "alexandre"#input('Entre com outro texto: ')

#Converte as entradas em conjuntos.
s1 = set(texto1)
s2 = set(texto2)

r1 = s1 - s2      #Os caracteres do primeiro texto que não estão no segundo.
r2 = s2 - s1      #Os caracteres do segundo texto que não estão no primeiro.
r3 = s1 & s2      #Os caracteres que estão em ambos os textos.
r4 = len(s1 | s2) #O número total de caracteres distintos em ambos os textos.

print(f'Os caracteres do primeiro texto que não estão no segundo:\n {r1}')
print(f'Os caracteres do segundo texto que não estão no primeiro:\n {r2}')
print(f'Os caracteres que estão em ambos os textos:\n {r3}')
print(f'O número total de caracteres distintos em ambos os textos:\n {r4}')

Resulting:

Os caracteres do primeiro texto que não estão no segundo:
 {'w'}
Os caracteres do segundo texto que não estão no primeiro:
 {'a', 'x', 'r'}
Os caracteres que estão em ambos os textos:
 {'n', 'e', 'd', 'l'}
O número total de caracteres distintos em ambos os textos:
 8

Test the example on Ideone

Browser other questions tagged

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