find a word in a random character list

Asked

Viewed 64 times

0

I need help in this python exercise

Marlon likes to cut letters from his magazines, then reorganize them to form sentences. Once, he cut out the letters ZEPUAMAOIZ and realized that changing the order of the letters could form the phrase EUAMOPIZZA. As it is near Christmas, he decided that he would make decorations with the word CHRISTMAS to distribute to his friends! For this, Marlon cut out several letters from several different magazines. But now it has a huge amount of scrambled letters, and you don’t know how many words NATAL will be able to form with them!

He knows you understand computing, so he asked you to make a program that, given the scrambled letters, finds out how many times it is possible to form the word he wants. So Marlon will know how many friends he can present with the lyrics he has!

Input Specification:

The first line of entry contains a single integer N, the number of letters Marlon has.

The second line of input has N characters of the alphabet, lowercase, are the letters that Marlon cut out.

Output Specification:

You must produce a single output line, containing an integer indicating the amount of times it is possible to form the word NATAL with the letters obtained from input.

example:

input                             

    9                                      
    
    natalnatl

output

    1

i tried with this code which in practice is functional but half 'gambiara', and the Obs: exercise has the execution time limit of the code a second

a = str(input())

b = a.count('n')
c = a.count('a')
d = a.count('t')
e = a.count('l')

if 5 <= b + c + d + e < 10:
    print(1)

1 answer

1


it is easy to see with the account you are doing that - first, it does not take into account that the number of lets can change, second, if the input is "nnnnnnnnn" will result in "2".

Try this: a program that picks up count the letters manually - one by one, and at the end of the count use the "min" function to know the minimum of each letter that was read.

It is also a good time to understand how Python dictionaries work, and avoid 26 different lines (or 26 "if"/"Elif" commands) and the same number of variables.

Another cool tip is to make the program not depend on the word "christmas", and can be easily adjusted to any other word. (has an extra difficulty because of duplicated letters, like "a" in "natal", but that’s the fun)

Browser other questions tagged

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