5 kyu
Calculate the expression
226GotThat
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:
Created | Nov 15, 2016 |
Published | Nov 15, 2016 |
Warriors Trained | 677 |
Total Skips | 19 |
Total Code Submissions | 5885 |
Total Times Completed | 226 |
Python Completions | 226 |
Total Stars | 25 |
% of votes with a positive feedback rating | 73% of 98 |
Total "Very Satisfied" Votes | 63 |
Total "Somewhat Satisfied" Votes | 17 |
Total "Not Satisfied" Votes | 18 |
Total Rank Assessments | 7 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |