Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, valueOfNames(listOfNames), which Computes and returns the valu

ID: 3936664 • Letter: W

Question

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

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote