5 kyu

Calculate the expression

Description:

Your task is to define a function that understands basic mathematical expressions and solves them. For example:

calculate("1 + 1")        # => 2
calculate("18 + 4*6")     # => 42
calculate("245 - 826")    # => -581
calculate("09 + 000482")  # => 491
calculate("8 / 4 + 6")    # => 8
calculate("5 + 1 / 5")    # => 5.2
calculate("1+2+3")        # => 6
calculate("9 /3 + 12/ 6") # => 5

Notes:

  • Input string will contain numbers (may be integers and floats) and arithmetic operations.
  • Input string may contain spaces, and all space characters should be ignored.
  • Operations that will be used: addition (+), subtraction (-), multiplication (*), and division (/)
  • Operations must be done in the order of operations: First multiplication and division, then addition and subtraction.
  • In this kata, input expression will not have negative numbers. (ex: "-4 + 5")
  • If output is an integer, return as integer. Else return as float.
  • If input string is empty, contains letters, has a wrong syntax, contains division by zero or is not a string, return False.
Mathematics
Strings
Algorithms

Similar Kata:

Stats:

CreatedNov 15, 2016
PublishedNov 15, 2016
Warriors Trained677
Total Skips19
Total Code Submissions5885
Total Times Completed226
Python Completions226
Total Stars25
% of votes with a positive feedback rating73% of 98
Total "Very Satisfied" Votes63
Total "Somewhat Satisfied" Votes17
Total "Not Satisfied" Votes18
Total Rank Assessments7
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • GotThat Avatar
  • suic Avatar
Ad