Write a function valueOfNameslistOfNames which Computes and

Write a function, valueOfNames(listOfNames), which Computes and returns the value of your full name. Make sure you do this for YOUR full name. listOfNames is a list of each of your names, e.g., [1john\', 1stewart1, \'buckminster1].| The value of your full name is computed as follows: Compute the value of each name, using value Of(Word) Multiply the value of each name by the number of letters that have a number using numberOf(Letter) that is prime. Sum the result of this computation for all your names. Return the result You will need the function is Prime(n) as a helper function that we wrote in class. Use any other helper functions that make your code easy to write and to read. Sample run: >>> print(valueOfNames([\'john1, 1stewart\', 1buckminster\'])) 1128 >>> In more detail: value of \'john1 number of letters in \'john\' that have a prime number 47 * 0 value of \'stewart\' number of letters in \'stewart\' that have a prime number 106 * 3 + value of \'buckminster\' number of letters in \'buckminster\' tthat have a prime number 135 * 6 = 1128

Solution

def is_prime(n):
   if n == 1 :
       return False
   for i in range(3, n):
       if n % i == 0:
           return False
   return True

def valueOfNames(lis):
   total_sum = 0 # this variable will contain total sum of all names
   for name in lis:
       sum_of_letters = 0 # this variable will contain sum of all letters of a name
       number_of_primes = 0 # contain total no of primes
       for letter in name: # loop through the whole list
           number = ord(letter)-ord(\'a\') + 1 # a->1, b->2 and so on
           # ord(\'a\') = 97, ord(\'b\') = 98 so ord(\'b\') - ord(\'a\') + 1 = 2
           sum_of_letters += number
           if is_prime(number):
               number_of_primes+=1 # increment if prime
       total_sum += sum_of_letters*number_of_primes # calculate total sum
   return total_sum


print valueOfNames([\"john\", \"stewart\" , \"buckminster\"])

ans 1128

 Write a function, valueOfNames(listOfNames), which Computes and returns the value of your full name. Make sure you do this for YOUR full name. listOfNames is a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site