Last Sunday I was taking part in Basic Java Exam at the SoftUni. I managed to get 130 points (out of 400) and thus to be somewhere in top 50% of the participants. I had 100 points at this problem, and somehow I made 20 points at a problem, asking you to make a simple program, which calculates strings. The 20 points were mainly from code refactoring, found in Stack Overflow, in which I was quite lucky to find some samples. Anyway, JavaScript libraries were not allowed to be included and thus I had to give my best on Sunday, which resulted in 20% of the tests.
So, the Simple Expression asks you to create a code, which calculates string. Problem 3 from here.
How to do it? The worst part is that you do not know how many numbers you are going to sum – that was the reason why I got only 20% of the correct results. In order to define this, a clever idea is to count the operators in the expression – if you have 6 operators, the numbers are 7 and so on. A second problem for me was the BigDecimal type in Java – I simply was not aware of it 🙂 In order to save you from more lame excuses, I will present the original code here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.math.BigDecimal; import java.util.Scanner; public class SimpleExpression { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String expression = scanner.nextLine(); expression = expression.replace(" ", ""); String[] numbers = expression.split("[^0-9.]+"); String[] operators = expression.split("[0-9.]+"); BigDecimal sum = new BigDecimal(numbers[0]); for (int i = 1; i < operators.length; i++) { BigDecimal number = new BigDecimal(numbers[i]); if (operators[i].equals("+")) { sum = sum.add(number); } else if (operators[i].equals("-")) { sum = sum.subtract(number); } else { throw new IllegalArgumentException( "Invalid operator: " + operators[i]); } } System.out.println(sum.toPlainString()); } } |
What it does?
- The string expression reads the line;
- Then we simply replace the space with nothing;
- The arrays numbers and operators are filled with regular expressions (quite a good move!);
- The first number in the array numbers is allocated to the BigDecimal sum;
- Then for every position in the array of the operators we run a loop;
- In the loop we declare a BigDecimal number and we assign value from the array numbers to it;
- Then we have two options, depending on the operator – to add or to subtract this number from the sum;
- At the end, we print the sum with the .toPlainString() function.
Pretty much, when you see it written down, it is really easy. What was hard for me at the exam was that I was thinking that it should be done a little weird – starting from the back and then reading the numbers and performing calculations until we reach the first one, which always should be added – e.g. if we have “5+6+8-9”, then I would do the following – “0-9+8+6” and then always make sure that I add 5 at the end. Anyway, the idea is doable, but the fact that I did not know the Java syntax quite well (or at all) at that time was the reason I had only 20% at this problem.
Enjoy it!