The local police force has placed a radar along a dirt road that they've recieved complaints about some folks speeding along the road that has the possibility of putting themselves and others at risk for injury. The radar will clock the driver's speed and record it in an array for later processing of the fines.
If the driver's speed is 10km/h to 19km/h above the speed limit, the fine will be 100 euros, if it is exceeded by 20km/h to 29km/h the fine will be 250 euros and if it is exceeded by more than 30km/h the fine will be 500 euros.
All input will be non-negative integer values.
Calculate the fine of each driver based on the speeds recorded in the array, and return an array of the fine amounts.
E.g.
array: [50,45,100,42]
limit: 50
return value: [0,0,500,100]
export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => { return driverSpeeds.map(speed => CalculateFine(speed, speedLimit)); } const CalculateFine = (speed: number, limit: number) => { if(speed > limit + 30) { return 500; } else if (speed > limit + 20 && speed < limit + 29) { return 250; } else if (speed > limit + 10 && speed < limit + 19) { return 100; } else { return 0; } }
import java.util.*;- export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
- return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
- }
class Vehicle {final int SPEED_LIMIT = 60;int currentSpeed;public void setCurrentSpeed(int[] accelerations) {this.currentSpeed = Arrays.stream(accelerations).sum();}public int getCurrentSpeed() {return this.currentSpeed;- const CalculateFine = (speed: number, limit: number) => {
- if(speed > limit + 30) {
- return 500;
- } else if (speed > limit + 20 && speed < limit + 29) {
- return 250;
- } else if (speed > limit + 10 && speed < limit + 19) {
- return 100;
- } else {
- return 0;
- }
public boolean isWithinSpeedLimit() {return getCurrentSpeed() <= this.SPEED_LIMIT;}}- }
import {checkIfAtOrBelowLimit} from './solution' import {assert} from "chai"; function testing(acceleration: number[], limit: number, expected: number[]) { assert.deepEqual(checkIfAtOrBelowLimit(acceleration, limit),expected); } describe("Fixed Tests", function() { it("Basic Tests checkIfBelowLimit", function() { testing([50,45,100,62], 50, [0,0,500,100]); testing([0,27,90,4], 50, [0,0,500,0]); }); });
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;- import {checkIfAtOrBelowLimit} from './solution'
- import {assert} from "chai";
class VehicleTest {@Testvoid test_currentSpeed() {Vehicle vehicle = new Vehicle();vehicle.setCurrentSpeed(new int[]{}); // empty arrayassertEquals(0, vehicle.getCurrentSpeed());vehicle.setCurrentSpeed(new int[]{1, 2, 3}); // array with positive numbersassertEquals(6, vehicle.getCurrentSpeed());vehicle.setCurrentSpeed(new int[]{-1, -2, -3}); // array with negative numbersassertEquals(-6, vehicle.getCurrentSpeed());vehicle.setCurrentSpeed(new int[]{1, -1, 2, -2}); // array with positive and negative numbersassertEquals(0, vehicle.getCurrentSpeed());}@Testvoid test_isWithinSpeedLimit() {Vehicle vehicle = new Vehicle();vehicle.setCurrentSpeed(new int[]{25, 25});assertTrue(vehicle.isWithinSpeedLimit());vehicle.setCurrentSpeed(new int[]{25, 50});assertFalse(vehicle.isWithinSpeedLimit());}- function testing(acceleration: number[], limit: number, expected: number[]) {
- assert.deepEqual(checkIfAtOrBelowLimit(acceleration, limit),expected);
- }
- describe("Fixed Tests", function() {
- it("Basic Tests checkIfBelowLimit", function() {
- testing([50,45,100,62], 50, [0,0,500,100]);
- testing([0,27,90,4], 50, [0,0,500,0]);
- });
- });