PYTHON:
Defining a Figure of Merit
Consider a string-matching figure of merit. That is, it tells you how close to a given string another string is. Each matching letter in the same spot is worth one point. Only letters need be considered.
For instance, if the secret string reads 'BLACKBEARD', then 'BEACKBEARD' is worth 9 points, 'WHITEBEARD' is worth 5 points, 'BEARDBLACK' is worth 4 points, and 'CALICOJACK' is worth 1 point.
Compose a function pirate which accepts a string of characters guess and returns the number of characters which match the secret string 'BLACKBEARD'. It should be case-insensitive; that is, you should convert input to upper-case letters. It should return zero for strings which are not ten characters in length.
Your submission should include a function pirate( guess ) which returns a float or int representing the number of matching characters. (You should provide the secret string 'BLACKBEARD' inside the function, not outside of it.)
strings should not have same lengths

Respuesta :

Answer:

figure of merit is a quantity used to characterize the performance of a device, system or method, relative to its alternatives. In engineering, figures of merit are often defined for particular materials or devices in order to determine their relative utility for an application.

Following are the Python program to find the string-matching figure:

Python Program:

def pirate(g):#defining the method pirate that takes one variable in parameter

 if len(g)!= 10:#defining if block that check the parameter length value that is not equal to 10

   return 0   #using the return keyword that return a value that is 0

 else:#defining else block

   g= g.upper()#defining g variable that converts the parameter value into upper value

   secretString = "BLACKBEARD"#defining string variable secretString that holds string value

   c = 0#defining integer variable c that holds integer value

 for i in range(0, len(g)):#defining loop that counts and check value is in string variable

   if g[i] == secretString[i]:#defining if block that checks value in secretString variable

     c += 1; #defining c variable that increments its value

   return c;#using return keyword that return c value

print(pirate("BEACKBEARD"))#using print method that calls pirate method and prints its value

print(pirate("WHITEBEARD"))#using print method that calls pirate method and prints its value

print(pirate("BEARDBLACK"))#using print method that calls pirate method and prints its value

print(pirate("CALICOJACK"))#using print method that calls pirate method and prints its value

Output:

Please find the attached file.

Program Explanation:

  • Defining the method "pirate" that takes one variable "g" in parameter.
  • Inside the method an if block that checks the parameter length value that is not equal to 10, and uses the return keyword that returns a value that is 0
  • In the else block, define the "g" variable that converts the parameter value into the upper value, and use a string variable "secretString" that holds the string value.
  • In the next step, define an integer variable "c" that holds an integer value, and define a loop that counts and checks the value in a string variable.
  • Inside this, define if block that checks the value in the "secretString" variable, increments the "c" value, and returns the "c" value.
  • Outside the method, a print method that calls the "pirate" method prints its value.

Find out more about the string-matching here:

brainly.com/question/16717135

Ver imagen codiepienagoya