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...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do...
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT