This is my solution for the below requirement.
Write some code to calculate a result from a set of instructions. Instructions comprise of a keyword and a number that are separated by a space per line. Instructions are loaded from file and results are output to the screen. Any number of Instructions can be specified. Instructions can be any binary operators of your choice (e.g., add, divide, subtract, multiply etc). The instructions will ignore mathematical precedence. The last instruction should be “apply” and a number (e.g., “apply 3”). The calculator is then initialised with that number and the previous instructions are applied to that number.
Examples of the calculator lifecycle might be:
Input from file
add 2
multiply 3
apply 3
Output to screen
15
Explanation
(3 + 2) * 3 = 15
Input from file
multiply 9
apply 5
Output to screen
45
Explanation
5 * 9 = 45
Input from file
apply 1
Output to screen
1
Below are the lists of assumptions for the tech challenge:
- Assume that if instruction “apply” is not found, then the program is stopped from executing
- Ignore “divide” instruction if divisor is 0.
- If the file is not found or is not readable then display error message.
- If no number is given on the same line as the “apply” instruction then set initial value to zero.
- Lines after instruction apply are ignored.
- No binary operator instruction is executed if there is no corresponding number.
- Instructions loaded from file can be case insensitive.
- As no language is specified, I have chosen PHP as the coding language.
- It is not specified whether the filename needs to be hard coded or entered by the user. The program allows the user to enter file as the second argument. A default file name is set if no argument is passed.
This application was written on a machine running Windows Operating System and PHP Version 8.1.6. However the minimum requirement is a machine running PHP version 7.4 or later.
The zip folder contains
- main.php
- instructions.txt
- instructions2.txt
After copying the Tech test folder, open your terminal/command prompt. Navigate to where you have installed the files.
php main.php
By default this output the total for instructions.txt.
You can specify the file that the program should run against in the command prompt/ terminal by adding filename as a second argument. E.g.
php main.php instruction2.txt
*The file must be located in same directory as main.php
In terminal following in terminal to install PHPUnit
composer update
Next, run the following to run tests from the root directory
./vendor/bin/phpunit --testdox test