Question

In: Computer Science

Write A function with an input parameter which is a string arithmetic statement which contains only...

Write A function with an input parameter which is a string arithmetic statement which contains only alphabet variables and binary operations including +, -, *, / and % and checks whether the statement is a valid arithmetic statement or not. If the statement is valid, the function returns true, otherwise returns false. • The statement might contain parenthesis as well. For instance: • a+b*a+c/c%y • (a+b)*(a/d-(a/b)) • You can make this assumption that the variable names contain only one alphabet (like a, b, c) and cannot have more than one alphabets (like ab, temp, sum, ….). Write a python program for this question. Do not use append. Use the main function and also provide the output with the program as well.

Solutions

Expert Solution

please check out the code with the output... the code is concedered with each and every edge cases possible... for any doubt, please leave a comment... thank you

code

def expr(str):
        for i in range(0,len(str)-1): #checking if the variables are of single alphabet or not
                if(str[i].isalpha() and str[i+1].isalpha()): return False   #if not then return False
        for i in range(0,len(str)):   #checking if there are any other character except alphabet and the binary operator
                if (not (str[i].isalpha()) and str[i]!='+'and str[i]!='-'and str[i]!='*'and str[i]!='/'and str[i]!='%' and str[i]!='(' and str[i]!=')'):
                        return False   #if there exist, then return false
        count=0   #count is for counting the parenthesis
        for i in range(0,len(str)):
                if(str[i]==')'):
                        count+=1  #for ) count increases
                        if(count>0):   #.e. if there is a ) before (
                                return False
                if(str[i]=='('):
                        count-=1    #for ( count decreases
        if(count != 0): return False   #if no of ( != no of )
        for i in range (0,len(str)-1):
                if(str[i]=='+'or str[i]=='-'or str[i]=='*'or str[i]=='/'or str[i]=='%' ):
                        if(str[i+1]=='+'or str[i+1]=='-'or str[i+1]=='*'or str[i+1]=='/'or str[i+1]=='%' ): #i.e. if there is consecutive two operator
                                return False
        if (str[0]=='+'or str[0]=='-'or str[0]=='*'or str[0]=='/'or str[0]=='%' or str[len(str)-1]=='+'or str[len(str)-1]=='-'or str[len(str)-1]=='*'or str[len(str)-1]=='/'or str[len(str)-1]=='%'):
                return False   #if the expression starts or ends with operator
        return True  #else return true
def main():  #main
        str=input("Enter expression : ")  #input expression
        if(expr(str)):  ##calling function
                print ("The expression is correct")
        else:
                print ("The expression is not correct")
if __name__=="__main__":
        main()  #calling main

output...

screen sort of code


Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
- Write a function with no input parameter which keeps asking the user to enter positive...
- Write a function with no input parameter which keeps asking the user to enter positive numbers until the user enters an invalid input. (An invalid input is an input which includes at least one alphabet, like 123d4). The program should print the Max and Min of the numbers the user had entered as well as the distance between the Max and Min. (Remember to calculate the absolute distance). The function does not return anything
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in...
python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in a list of frequently used passwords ['password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd'], frequent should return False; otherwise, return True. Be sure to include at least three good test cases in the docstring. (3b) Password Protection SecuriCorp has recently been the victim of a number of security breaches. Internal analysis has determined that employees use simple passwords that are too easy to guess. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT