Primes in python A-z A-Z letters

Asked

Viewed 428 times

2

In this problem you should read a set of words, where each word is composed only of letters in the a-z and A-Z range. Each letter has a specific value, the letter a is 1, the letter b is 2, and so on, down to the letter z, which is 26. Similarly, the letter A is 27, the letter B is 28 and the letter Z is 52.

You should write a program to determine whether a word is a prime word or not. A word is a prime word if the sum of its letters is a prime number.

I am in doubt of how to do it, I have already created the variables, the = []

for i in range(ord('a'), ord('z')+1):
    a.append(chr(i))
  • I don’t understand what the doubt is

  • So I have to make a is that takes words as a number, only when I pass the go to the result whether it is prime or not, it does not accept the string

1 answer

1

Just set a function that returns the number that represents each letter:

def get_ord_of_char(char):
  if 'a' <= char <= 'z':
    return ord(char) - ord('a') + 1
  else:
    return ord(char) - ord('A') + 27

Then calculate the sum of the numbers representing the word:

soma = sum(get_ord_of_char(char) for char in palavra)

And finally, check whether soma is a prime number. It would look like this:

palavra = 'teste'
soma = sum(get_ord_of_char(char) for char in palavra)
print('A palavra é prima' if is_prime(soma) else 'A palavra não é prima')

Just you implement the function is_prime that takes an integer number as a parameter.

  • Doubt I get bigger even now, and as I make the sum check if it is prime, at the time I pass sum as attribute of error

  • @William soma will be an integer number. Can you check if it is prime? Pass as attribute to where?

  • if sum % get_ord_of_char(word) == 0: print('is prime') sum+=1 Else: print('is not prime')

  • at least what I understood would be this, the user informs the word and I have to check whether it is prime or not according that each word is worth a value, however, I can’t do if in it by passing the function that each number is worth a value.

  • @William added at the end of the reply a brief example of how it would look

  • I still can’t understand, because I can’t do what goes through, I’ve been told to do even with more difficult dictionary.

  • @William part exactly did not understand? I practically put the code ready there, do not need to change anything, just implement the function is_prime.

  • i stuck at the part of implementing the function, pq when I try to implement the function I put the other function as "number" and always end up giving error.

  • Then edit the question with your code as it is difficult to understand anything.

  • 1

    This question is from an internship interview, that’s how it was asked, if you can send the whole code I thank you.

  • @William will be better if you edit the question and put what you did. Only then will I be able to see where you are struggling.

  • 4

    face I appreciate the help but I’ll give it up.

Show 7 more comments

Browser other questions tagged

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