Question

#Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can use the below logic to implement our rules for the arithmetic string

  • Look at each character in the string
    • if character is one of the allowed operator
      • return False if it is a consecutive operator
      • return False if it is at the start or at the end of the string
      • return False if there are no digits on either side of the character
      • set found_operator flag to True (to keep track of consecutive operators)
      • set found_equals flag to True if the character is = symbol
    • if character is a digit
      • set found_operator flag to False (as we did not have a consecutive operator)
    • return False (as the character is neither an allowed operator nor a digit)
  • return False if found_equals flag is False
  • return True (as all rules were satisfied)

Python code

def check_formula(expression):
   """returns True if expression holds a correctly formatted formula"""
   operators = ['+', '-', '*', '/', '='] # allowed operators
   found_equals = False
   found_operator = False
   for i in range(len(expression)):
       character = expression[i] # look at each character
       # checks if character is an allowed operator
       if character in operators:
           if found_operator: # checks if found_operator flag is already True
               return False # character is a consecutive operator
           if i == 0 or i == len(expression) - 1: # checks if operator is at the beginning or end
               return False
           # checks if there is a digit on either side of the operator
           if not expression[i-1].isdigit() or not expression[i+1].isdigit():
               return False
           found_operator = True # sets found operator flag as True
           if character == '=': # operator is an equals symbol
               found_equals = True
       # checks if character is a digit
       elif character.isdigit():
           found_operator = False
       # returns False if the character is neither an allowed operator nor a digit
       else:
           return False
   # returns False if an = symbol was not found in the expression
   if not found_equals:
       return False
   # all rules correct, so returns True
   return True

Screenshot (included for readability)

def check_formula(expression) : returns True if expression holds a correctly formatted formula operators = [+, 7, =

Output

print(check_formula(5*3=5+2)) print(check_formula(5=7)) print(check_formula(5=2-5)) print(check_formula(6/2=5/2)) pri

Add a comment
Know the answer?
Add Answer to:
#Write a function called check_formula. The check_formula #function should take as input one parameter, a string....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • On Python 3.6, write a function char_table(s), where s is a string. The function should produce...

    On Python 3.6, write a function char_table(s), where s is a string. The function should produce a table that shows the characters in the string and the number of occurrences of each character. Make the table nicely formatted, using right justification for the numbers (check out the format command.). List the characters in order (the order in the character encoding). Give some examples of the output.

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive...

    Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive repeated characters are replaced with one of the character and the number of times the character is repeated. This is also known as "Run length encoding". Examples:“aaabbc” -> “a3b2c”“abc” -> “abc”“aaqakkaccc” -> a2qak2a2c3

  • USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take...

    USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take two (2) arguments, an integer and a string. The function should return one (1) integer calculated as follows. If the input integer is 1, the function should return the sum of the return value of the ord() function on each character of the string (e.g., varOrd(1, ‘cat’) should return the result of ord(‘c’) + ord(‘a’) + ord(‘t’)). If the input integer is 2, the...

  • Write a C function that receives a character array (string), and it will check the characters...

    Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this...

  • Write a Python function that will take a string and a list as its only parameters....

    Write a Python function that will take a string and a list as its only parameters. The function needs to return a new string that is the original string with all of the characters appearing in the list parameter replaced with an asterisk(*). The returned string needs to have the remaining characters appear in the same location as the characters they replaced. Hint/Reminder: Note you can make a string out of characters optimally without repeatedly using the concatenation operator.

  • LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius...

    LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius cipher scheme above. The start of the function call the get_polybius_square function to get the square as a string. The second scenario when the number of characters is not even, excluding ‘\n’. For instance: “71\n5” is an invalid cipher because there is no way that the last number correspond to a character (we need two numbers). A customized version of Polybius square will be...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT