You work as a driving test examiner in an alien planet. Your task is to evaluate whether an examinee has passed the exam based on the exam data you receive and the rules described below.
You receive the exam data in two Strings, the first one represents all the indications the driver has received during the exam, from road signals and the examiner. The second String represents the actions of the examinee which are registered every second from the start to the end of the exam.
The format of the strings is the following:
indications: String of symbols separated by " " which represent one indication each following the following legend.
Indication | Symbol | Expected Behaviour |
---|---|---|
STOP | STOP -> No cars passing by STOP3 -> 3 cars passing by |
Stop at least 3 seconds + 2 seconds for each passing car |
YIELD | YIELD -> No cars passing by YIELD2 -> 2 cars passing by |
Stop ONLY if there are cars passing by (2 seconds/car) |
SPEED LIMIT | SL50_100 -> Sets a speed limit of 50 for the next 100 seconds SL35_60 -> Sets a speed limit of 35 for the next 60 seconds |
Don't go over the set speed limit |
TURN | TURNL -> Turn left TURNR -> Turn right |
Turn as indicated |
Red light | R3 -> Red light for 3 seconds R20 -> Red light for 20 seconds |
Stop for the given amount of time |
actions: String of symbols separated by " " which contain the speed of the vehicle and the turn, if any, taken each second of the exam. Turns are represented with "<" (left) and ">" (right).
Input example:
indications = "SL30_5 YIELD2 SL25_40 R8 YIELD TURNR STOP "
actions = "29 15 0 0 0 0 25 28 35 24 0 0 0 0 0 0 0 0 13 24 35 38> 35 37 36 24 12 0 0 0"
The Rules
- There are minor and eliminatory infractions. Three minor infractions or one eliminatory infraction will disqualify the examinee.
- As the information you received is incomplete (there is no way to know when each of the indications were encountered) you must assume the examinee does the correct thing UNLESS there is no other possibility. For example:
Infractions
| Minor Infraction | Description |
|---|---|
| Speed limit | Examinee drives over the speed limit for three consecutive seconds. |
| Insufficient stop time (no cars) | Examinee stops at STOP signal but does so for less than three seconds (no cars passing by). |
| Insufficient stop time (cars) | Examinee waits the necessary time for all cars to pass by but does not wait or waits partially the mandatory 3 seconds. |
| Not turning | Examinee does not turn when indicated. |
Eliminatory Infraction | Description |
---|---|
Unnecessary stopping | Examinee stops for no reason at all. |
Running a red light | Examinee doesn't stop or starts moving before a red light turns green. |
Ignoring STOP | Examinee doesn't stop at all at a STOP sign. |
Running into traffic. | Examinee doesn't wait the necessary time at a STOP or yield sign for other cars to pass by. |
Reckless driving | Examinee goes over the speed limit for over 10 speed units at any given time. |
All inputs will be valid so no validation is required.
You must return true if the examinee has passed the exam and false if they haven't.
import java.util.Arrays; import java.util.List; public class DrivingTestEvaluator { static int knockoutFoul; static int mildFoul; static List<String> actions; static int currentIndex; public static boolean evaluate(String exam, String road) { knockoutFoul = 0; mildFoul = 0; currentIndex = 0; // Corrige el orden: exam contiene las indicaciones, road contiene las acciones List<String> rules = Arrays.asList(exam.trim().split("\\s+")); actions = Arrays.asList(road.trim().split("\\s+")); for (String rule : rules) { if (rule.startsWith("SL")) { comprobateSpeedLimit(rule); } else if (rule.startsWith("STOP")) { comprobateStop(rule); } else if (rule.startsWith("YIELD")) { comprobateYield(rule); } else if (rule.startsWith("TURN")) { comprobateTurn(rule); } else if (rule.startsWith("R")) { comprobateRedLight(rule); } if (knockoutFoul > 0) return false; } return mildFoul < 3; } public static void comprobateSpeedLimit(String rule) { String[] parts = rule.substring(2).split("_"); int speedLimit = Integer.parseInt(parts[0]); int duration = Integer.parseInt(parts[1]); int overLimitCounter = 0; boolean minorCounted = false; for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) { int speed = extractSpeed(actions.get(currentIndex + i)); if (speed > speedLimit + 10) { knockoutFoul++; return; } if (speed > speedLimit) { overLimitCounter++; } else { overLimitCounter = 0; } if (overLimitCounter >= 3 && !minorCounted) { mildFoul++; minorCounted = true; } } currentIndex += Math.min(duration, actions.size() - currentIndex); } public static void comprobateStop(String rule) { int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0; int requiredStop = 3 + (2 * cars); int count = 0; int i = currentIndex; while (i < actions.size() && extractSpeed(actions.get(i)) == 0) { count++; i++; } if (count != requiredStop) { knockoutFoul++; } currentIndex = i; } public static void comprobateYield(String rule) { int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0; int requiredStop = 2 * cars; int stopCount = 0; int i = currentIndex; while (i < actions.size() && extractSpeed(actions.get(i)) == 0) { stopCount++; i++; } if (cars == 0) { if (stopCount > 0) { knockoutFoul++; } } else { if (stopCount < requiredStop) { knockoutFoul++; } } currentIndex = i; } public static void comprobateTurn(String rule) { String requiredDirection = rule.equals("TURNR") ? ">" : "<"; boolean turned = false; int i = currentIndex; while (i < actions.size()) { String action = actions.get(i); if (action.endsWith(requiredDirection)) { turned = true; currentIndex = i + 1; break; } if ((requiredDirection.equals(">") && action.endsWith("<")) || (requiredDirection.equals("<") && action.endsWith(">"))) { mildFoul++; currentIndex = i + 1; return; } i++; } if (!turned) { mildFoul++; } } public static void comprobateRedLight(String rule) { int duration = rule.length() > 1 ? Integer.parseInt(rule.substring(1)) : 1; for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) { if (extractSpeed(actions.get(currentIndex + i)) != 0) { knockoutFoul++; currentIndex += i + 1; return; } } currentIndex += Math.min(duration, actions.size() - currentIndex); } public static int extractSpeed(String action) { if (action.endsWith("<") || action.endsWith(">")) { return Integer.parseInt(action.substring(0, action.length() - 1)); } return Integer.parseInt(action); } }
- import java.util.Arrays;
- import java.util.List;
- public class DrivingTestEvaluator {
- static int knockoutFoul;
- static int mildFoul;
- static List<String> actions;
- static int currentIndex;
- public static boolean evaluate(String exam, String road) {
- knockoutFoul = 0;
- mildFoul = 0;
- currentIndex = 0;
actions = Arrays.asList(exam.trim().split("\\s+"));List<String> rules = Arrays.asList(road.trim().split("\\s+"));- // Corrige el orden: exam contiene las indicaciones, road contiene las acciones
- List<String> rules = Arrays.asList(exam.trim().split("\\s+"));
- actions = Arrays.asList(road.trim().split("\\s+"));
- for (String rule : rules) {
- if (rule.startsWith("SL")) {
- comprobateSpeedLimit(rule);
- } else if (rule.startsWith("STOP")) {
- comprobateStop(rule);
- } else if (rule.startsWith("YIELD")) {
- comprobateYield(rule);
- } else if (rule.startsWith("TURN")) {
- comprobateTurn(rule);
- } else if (rule.startsWith("R")) {
- comprobateRedLight(rule);
- }
- if (knockoutFoul > 0) return false;
- }
- return mildFoul < 3;
- }
- public static void comprobateSpeedLimit(String rule) {
- String[] parts = rule.substring(2).split("_");
- int speedLimit = Integer.parseInt(parts[0]);
- int duration = Integer.parseInt(parts[1]);
- int overLimitCounter = 0;
- boolean minorCounted = false;
- for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) {
- int speed = extractSpeed(actions.get(currentIndex + i));
- if (speed > speedLimit + 10) {
- knockoutFoul++;
- return;
- }
- if (speed > speedLimit) {
- overLimitCounter++;
- } else {
- overLimitCounter = 0;
- }
- if (overLimitCounter >= 3 && !minorCounted) {
- mildFoul++;
- minorCounted = true;
- }
- }
- currentIndex += Math.min(duration, actions.size() - currentIndex);
- }
- public static void comprobateStop(String rule) {
- int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0;
- int requiredStop = 3 + (2 * cars);
int stopCount = 0;while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) {stopCount++;currentIndex++;- int count = 0;
- int i = currentIndex;
- while (i < actions.size() && extractSpeed(actions.get(i)) == 0) {
- count++;
- i++;
- }
if (stopCount == 0) {- if (count != requiredStop) {
- knockoutFoul++;
} else if (stopCount < requiredStop) {if (stopCount >= 2 * cars) mildFoul++;else knockoutFoul++;- }
- currentIndex = i;
- }
- public static void comprobateYield(String rule) {
- int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0;
- int requiredStop = 2 * cars;
- int stopCount = 0;
while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) {- int i = currentIndex;
- while (i < actions.size() && extractSpeed(actions.get(i)) == 0) {
- stopCount++;
currentIndex++;- i++;
- }
- if (cars == 0) {
if (stopCount >= 2) knockoutFoul++;- if (stopCount > 0) {
- knockoutFoul++;
- }
- } else {
- if (stopCount < requiredStop) {
- knockoutFoul++;
} else if (stopCount < requiredStop + 3) {mildFoul++;- }
- }
- currentIndex = i;
- }
- public static void comprobateTurn(String rule) {
- String requiredDirection = rule.equals("TURNR") ? ">" : "<";
- boolean turned = false;
for (int i = currentIndex; i < actions.size(); i++) {if (actions.get(i).endsWith(requiredDirection)) {- int i = currentIndex;
- while (i < actions.size()) {
- String action = actions.get(i);
- if (action.endsWith(requiredDirection)) {
- turned = true;
- currentIndex = i + 1;
- break;
- }
- if ((requiredDirection.equals(">") && action.endsWith("<")) ||
- (requiredDirection.equals("<") && action.endsWith(">"))) {
- mildFoul++;
- currentIndex = i + 1;
- return;
- }
- i++;
- }
- if (!turned) {
- mildFoul++;
- }
- }
- public static void comprobateRedLight(String rule) {
int duration = Integer.parseInt(rule.substring(1));boolean failed = false;- int duration = rule.length() > 1 ? Integer.parseInt(rule.substring(1)) : 1;
- for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) {
- if (extractSpeed(actions.get(currentIndex + i)) != 0) {
failed = true;- knockoutFoul++;
- currentIndex += i + 1;
- return;
- }
- }
if (failed) {knockoutFoul++;}- currentIndex += Math.min(duration, actions.size() - currentIndex);
- }
- public static int extractSpeed(String action) {
- if (action.endsWith("<") || action.endsWith(">")) {
- return Integer.parseInt(action.substring(0, action.length() - 1));
- }
- return Integer.parseInt(action);
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue;// TODO: Replace examples and use TDD by writing your own tests import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; class SolutionTest { @Test void SampleTest() { assertTrue(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 35 35 35 35 35 35 35 35 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 0 0 50 30")); assertTrue(DrivingTestEvaluator.evaluate("SL50_2 SL30_4 TURNR TURNL", "48 51 21> 28 30< 22")); assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_7 YIELD2", "28 27 30 32> 36 38 0 0 0 0 60")); assertTrue(DrivingTestEvaluator.evaluate("SL40_3 YIELD SL25_5 TURNR", "39 40 41 24 25 23 25> 22")); } @Test void ComplexesTest() { assertTrue(DrivingTestEvaluator.evaluate( "SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1", "25 24 25 24 24< 30 29 30 20 18 20 17 20> 40 39 40 40 40 40 40 40 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate( "SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 24 30 30< 40 39 38 40 39 40 22 23 24 25 20> 50 49 50 48 50 49 50 50 35 34 35 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate( "SL20_4 TURNR SL30_5 SL45_6 TURNL SL50_7 SL40_4 STOP1", "20 19 20 21> 30 29 30 31 32> 45 44 46 45 47 48> 50 49 50 51 50 52 53> 40 39 40 10 5 5 5 5")); assertFalse(DrivingTestEvaluator.evaluate( "SL25_4 STOP1 SL20_4", "30 29 30 31 0 0 0 0 0 35< 13 15 18 15")); assertTrue(DrivingTestEvaluator.evaluate( "SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1", "25 24 25 26 25< 30 29 30 20 18 20 24 18> 40 39 40 35 36 37 38 39 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate( "SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate( "SL50_10 TURNL R", "50 49 50 48 50 49 50 50 42 48 25< 0")); assertFalse(DrivingTestEvaluator.evaluate( "SL30_5 TURNL SL40_6 YIELD SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 31 30> 40 39 38 40 39 40 0 0 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate( "SL25_4 TURNL SL30_3 YIELD SL20_4 TURNR SL40_8 STOP1", "25 24 25 26 23< 30 29 30 20 18 20 24 20> 40 39 40 31 3 33 34 35 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate( "SL25_4 TURNL SL30_3 YIELD2 SL20_4 TURNR SL40_8 STOP1", "25 24 25 26 23< 30 29 30 0 0 0 0 22 18 20 16 17> 40 39 40 35 36 37 38 36 0 0 0 0 0")); } @Test void StopTest() { assertTrue(DrivingTestEvaluator.evaluate("STOP", "0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("STOP1", "0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("STOP", "0 ")); assertFalse(DrivingTestEvaluator.evaluate("STOP2", "0 0 ")); } @Test void TurnTest() { assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1", "20> 24")); assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 TURNR SL25_1 TURNL SL25_1", "20> 24 20> 24 20< 14")); assertTrue(DrivingTestEvaluator.evaluate( "TURNR SL25_1 SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 TURNR SL25_1", "20> 10 20 20> 20 24< 20 15> 20 20> 23")); assertFalse(DrivingTestEvaluator.evaluate( "TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1", "20> 24 20< 24 20> 24 20> 24 20< 24")); assertFalse(DrivingTestEvaluator.evaluate( "TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1", "20> 24 20< 24 20< 24 20< 24 20> 24")); assertFalse(DrivingTestEvaluator.evaluate( "TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1", "20> 24 20< 24 20< 24 20> 24 20> 24")); assertFalse(DrivingTestEvaluator.evaluate("TURNR SL25_1", "20< 40")); assertFalse(DrivingTestEvaluator.evaluate( "TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1", "20> 24 20< 24 20< 24 20< 24 20> 24")); } @Test void SpeedLimitTest() { } @Test void YieldTest() { } public static String generateIndications() { ArrayList<String> types = new ArrayList<>(Arrays.asList("SL", "TURNL", "TURNR", "YIELD", "STOP", "R")); StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); Random random = new Random(); sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10)); sb.append(sb2 + " "); for (int i = 0; i < 8; i++) { sb2.setLength(0); Collections.shuffle(types); if (types.get(0) == "YIELD" || types.get(0) == "STOP" || types.get(0) == "R") { int aleatorio = random.nextInt(0, 5); if (aleatorio == 0) { sb2.append(types.get(0)); } else { sb2.append(types.get(0) + aleatorio); } } else if (types.get(0) == "SL") { sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10)); } else { sb2.append(types.get(0)); } sb.append(sb2 + " "); } return sb.toString().trim(); } public static String generateGoodActions(String indications) { String[] trafficSigns = indications.split("\\s+"); List<String> actions = new ArrayList<>(); Random random = new Random(); Matcher matcher; for (String sing : trafficSigns) { if ((matcher = Pattern.compile("SL(\\d+)_(\\d+)").matcher(sing)).matches()) { int limit = Integer.parseInt(matcher.group(1)); int duration = Integer.parseInt(matcher.group(2)); for (int i = 0; i < duration; i++) { actions.add(String.valueOf(random.nextInt(limit + 1))); } } else if ((matcher = Pattern.compile("YIELD(\\d*)").matcher(sing)).matches()) { int cars = matcher.group(1).isEmpty() ? 1 : Integer.parseInt(matcher.group(1)); for (int i = 0; i < cars * 2; i++) { actions.add("0"); } } else if ((matcher = Pattern.compile("STOP(\\d*)").matcher(sing)).matches()) { int cars = matcher.group(1).isEmpty() ? 0 : Integer.parseInt(matcher.group(1)); for (int i = 0; i < 3 + 2 * cars; i++) { actions.add("0"); } } else if ((matcher = Pattern.compile("R(\\d+)").matcher(sing)).matches()) { int secs = Integer.parseInt(matcher.group(1)); for (int i = 0; i < secs; i++) { actions.add("0"); } } else if ("TURNL".equals(sing) && !actions.isEmpty()) { int last = actions.size() - 1; actions.set(last, actions.get(last) + "<"); } else if ("TURNR".equals(sing) && !actions.isEmpty()) { int last = actions.size() - 1; actions.set(last, actions.get(last) + ">"); } } return String.join(" ", actions); } @Test void RandomTest() { String indications = generateIndications(); String actions = generateGoodActions(indications); System.out.println(indications); System.out.println(actions); } }
- import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;// TODO: Replace examples and use TDD by writing your own tests- import static org.junit.jupiter.api.Assertions.assertFalse;
- import static org.junit.jupiter.api.Assertions.assertTrue;// TODO: Replace examples and use TDD by writing your own tests
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- class SolutionTest {
- @Test
- void SampleTest() {
assertTrue(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 0 0"));assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 50 30"));- assertTrue(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 35 35 35 35 35 35 35 35 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 0 0 50 30"));
- assertTrue(DrivingTestEvaluator.evaluate("SL50_2 SL30_4 TURNR TURNL", "48 51 21> 28 30< 22"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_7 YIELD2", "28 27 30 32> 36 38 0 0 0 0 60"));
- assertTrue(DrivingTestEvaluator.evaluate("SL40_3 YIELD SL25_5 TURNR", "39 40 41 24 25 23 25> 22"));
- }
- @Test
- void ComplexesTest() {
assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1","25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));assertTrue(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1","30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));assertFalse(DrivingTestEvaluator.evaluate("SL20_4 TURNR SL30_5 SL45_6 TURNL SL50_7 SL40_4 STOP1",- assertTrue(DrivingTestEvaluator.evaluate(
- "SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 24 24< 30 29 30 20 18 20 17 20> 40 39 40 40 40 40 40 40 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate(
- "SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 24 30 30< 40 39 38 40 39 40 22 23 24 25 20> 50 49 50 48 50 49 50 50 35 34 35 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "SL20_4 TURNR SL30_5 SL45_6 TURNL SL50_7 SL40_4 STOP1",
- "20 19 20 21> 30 29 30 31 32> 45 44 46 45 47 48> 50 49 50 51 50 52 53> 40 39 40 10 5 5 5 5"));
assertFalse(DrivingTestEvaluator.evaluate("SL25_4 STOP1 SL20_4", "30 29 30 31 0 0 0 0 0 < 13 15 18 15"));assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1","25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1",- assertFalse(DrivingTestEvaluator.evaluate(
- "SL25_4 STOP1 SL20_4", "30 29 30 31 0 0 0 0 0 35< 13 15 18 15"));
- assertTrue(DrivingTestEvaluator.evaluate(
- "SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26 25< 30 29 30 20 18 20 24 18> 40 39 40 35 36 37 38 39 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));
assertTrue(DrivingTestEvaluator.evaluate("SL50_10 TURNL R", "50 49 50 48 50 49 50 50 > 0 0 0 0 0"));assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 YIELD SL25_4 TURNR SL50_8 SL35_3 STOP1",- assertTrue(DrivingTestEvaluator.evaluate(
- "SL50_10 TURNL R", "50 49 50 48 50 49 50 50 42 48 25< 0"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "SL30_5 TURNL SL40_6 YIELD SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 31 30> 40 39 38 40 39 40 0 0 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));
assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD SL20_4 TURNR SL40_8 STOP1","25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD2 SL20_4 TURNR SL40_8 STOP1","25 24 25 26> 30 29 30 0 0 0 0 22 18 20 24> 40 39 40 0 0 0 0 0"));- assertTrue(DrivingTestEvaluator.evaluate(
- "SL25_4 TURNL SL30_3 YIELD SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26 23< 30 29 30 20 18 20 24 20> 40 39 40 31 3 33 34 35 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate(
- "SL25_4 TURNL SL30_3 YIELD2 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26 23< 30 29 30 0 0 0 0 22 18 20 16 17> 40 39 40 35 36 37 38 36 0 0 0 0 0"));
- }
- @Test
- void StopTest() {
assertTrue(DrivingTestEvaluator.evaluate("STOP", "0 0"));assertTrue(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 0 0 0 0"));assertTrue(DrivingTestEvaluator.evaluate("STOP1", "0 0 0 0"));assertFalse(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 2 2 2"));- assertTrue(DrivingTestEvaluator.evaluate("STOP", "0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("STOP1", "0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("STOP", "0 "));
- assertFalse(DrivingTestEvaluator.evaluate("STOP2", "0 0 "));
- }
- @Test
- void TurnTest() {
assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "> 60"));assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 ", "> 20 > 25 < 15 "));assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 ","> 10 > 20 < 25 > 15 > 20"));assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ","> 50 > 60 < 40 < 30 > 70 "));assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ","> 50 > 60 > 70 < 40 > 30"));assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ","> 50 < 30 < 40 > 60 > 70 "));assertFalse(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "< 40 "));assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ","> 50 > 60 > 70 < 40 > 30"));- assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1",
- "20> 24"));
- assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 TURNR SL25_1 TURNL SL25_1",
- "20> 24 20> 24 20< 14"));
- assertTrue(DrivingTestEvaluator.evaluate(
- "TURNR SL25_1 SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 TURNR SL25_1",
- "20> 10 20 20> 20 24< 20 15> 20 20> 23"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1",
- "20> 24 20< 24 20> 24 20> 24 20< 24"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1",
- "20> 24 20< 24 20< 24 20< 24 20> 24"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1",
- "20> 24 20< 24 20< 24 20> 24 20> 24"));
- assertFalse(DrivingTestEvaluator.evaluate("TURNR SL25_1", "20< 40"));
- assertFalse(DrivingTestEvaluator.evaluate(
- "TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1",
- "20> 24 20< 24 20< 24 20< 24 20> 24"));
- }
- @Test
- void SpeedLimitTest() {
- }
- @Test
- void YieldTest() {
- }
- public static String generateIndications() {
- ArrayList<String> types = new ArrayList<>(Arrays.asList("SL", "TURNL", "TURNR", "YIELD", "STOP", "R"));
- StringBuilder sb = new StringBuilder();
- StringBuilder sb2 = new StringBuilder();
- Random random = new Random();
- sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10));
- sb.append(sb2 + " ");
- for (int i = 0; i < 8; i++) {
- sb2.setLength(0);
- Collections.shuffle(types);
- if (types.get(0) == "YIELD" || types.get(0) == "STOP" || types.get(0) == "R") {
- int aleatorio = random.nextInt(0, 5);
- if (aleatorio == 0) {
- sb2.append(types.get(0));
- } else {
- sb2.append(types.get(0) + aleatorio);
- }
- } else if (types.get(0) == "SL") {
- sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10));
- } else {
- sb2.append(types.get(0));
- }
- sb.append(sb2 + " ");
- }
- return sb.toString().trim();
- }
- public static String generateGoodActions(String indications) {
- String[] trafficSigns = indications.split("\\s+");
- List<String> actions = new ArrayList<>();
- Random random = new Random();
- Matcher matcher;
- for (String sing : trafficSigns) {
- if ((matcher = Pattern.compile("SL(\\d+)_(\\d+)").matcher(sing)).matches()) {
- int limit = Integer.parseInt(matcher.group(1));
- int duration = Integer.parseInt(matcher.group(2));
- for (int i = 0; i < duration; i++) {
- actions.add(String.valueOf(random.nextInt(limit + 1)));
- }
- } else if ((matcher = Pattern.compile("YIELD(\\d*)").matcher(sing)).matches()) {
- int cars = matcher.group(1).isEmpty() ? 1 : Integer.parseInt(matcher.group(1));
- for (int i = 0; i < cars * 2; i++) {
- actions.add("0");
- }
- } else if ((matcher = Pattern.compile("STOP(\\d*)").matcher(sing)).matches()) {
- int cars = matcher.group(1).isEmpty() ? 0 : Integer.parseInt(matcher.group(1));
- for (int i = 0; i < 3 + 2 * cars; i++) {
- actions.add("0");
- }
- } else if ((matcher = Pattern.compile("R(\\d+)").matcher(sing)).matches()) {
- int secs = Integer.parseInt(matcher.group(1));
- for (int i = 0; i < secs; i++) {
- actions.add("0");
- }
- } else if ("TURNL".equals(sing) && !actions.isEmpty()) {
- int last = actions.size() - 1;
- actions.set(last, actions.get(last) + "<");
- } else if ("TURNR".equals(sing) && !actions.isEmpty()) {
- int last = actions.size() - 1;
- actions.set(last, actions.get(last) + ">");
- }
- }
- return String.join(" ", actions);
- }
- @Test
- void RandomTest() {
- String indications = generateIndications();
- String actions = generateGoodActions(indications);
- System.out.println(indications);
- System.out.println(actions);
- }
- }
You work as a driving test examiner in an alien planet. Your task is to evaluate whether an examinee has passed the exam based on the exam data you receive and the rules described below.
You receive the exam data in two Strings, the first one represents all the indications the driver has received during the exam, from road signals and the examiner. The second String represents the actions of the examinee which are registered every second from the start to the end of the exam.
The format of the strings is the following:
indications: String of symbols separated by " " which represent one indication each following the following legend.
Indication | Symbol | Expected Behaviour |
---|---|---|
STOP | STOP -> No cars passing by STOP3 -> 3 cars passing by |
Stop at least 3 seconds + 2 seconds for each passing car |
YIELD | YIELD -> No cars passing by YIELD2 -> 2 cars passing by |
Stop ONLY if there are cars passing by (2 seconds/car) |
SPEED LIMIT | SL50_100 -> Sets a speed limit of 50 for the next 100 seconds SL35_60 -> Sets a speed limit of 35 for the next 60 seconds |
Don't go over the set speed limit |
TURN | TURNL -> Turn left TURNR -> Turn right |
Turn as indicated |
Red light | R3 -> Red light for 3 seconds R20 -> Red light for 20 seconds |
Stop for the given amount of time |
actions: String of symbols separated by " " which contain the speed of the vehicle and the turn, if any, taken each second of the exam. Turns are represented with "<" (left) and ">" (right).
Input example:
indications = "SL30_5 YIELD2 SL25_40 R8 YIELD TURNR STOP "
actions = "29 15 0 0 0 0 25 28 35 24 0 0 0 0 0 0 0 0 13 24 35 38> 35 37 36 24 12 0 0 0"
The Rules
- There are minor and eliminatory infractions. Three minor infractions or one eliminatory infraction will disqualify the examinee.
- As the information you received is incomplete (there is no way to know when each of the indications were encountered) you must assume the examinee does the correct thing UNLESS there is no other possibility. For example:
Infractions
| Minor Infraction | Description |
|---|---|
| Speed limit | Examinee drives over the speed limit for three consecutive seconds. |
| Insufficient stop time (no cars) | Examinee stops at STOP signal but does so for less than three seconds (no cars passing by). |
| Insufficient stop time (cars) | Examinee waits the necessary time for all cars to pass by but does not wait or waits partially the mandatory 3 seconds. |
| Not turning | Examinee does not turn when indicated. |
Eliminatory Infraction | Description |
---|---|
Unnecessary stopping | Examinee stops for no reason at all. |
Running a red light | Examinee doesn't stop or starts moving before a red light turns green. |
Ignoring STOP | Examinee doesn't stop at all at a STOP sign. |
Running into traffic. | Examinee doesn't wait the necessary time at a STOP or yield sign for other cars to pass by. |
Reckless driving | Examinee goes over the speed limit for over 10 speed units at any given time. |
All inputs will be valid so no validation is required.
You must return true if the examinee has passed the exam and false if they haven't.
import java.util.Arrays; import java.util.List; public class DrivingTestEvaluator { static int knockoutFoul; static int mildFoul; static List<String> actions; static int currentIndex; public static boolean evaluate(String exam, String road) { knockoutFoul = 0; mildFoul = 0; currentIndex = 0; actions = Arrays.asList(exam.trim().split("\\s+")); List<String> rules = Arrays.asList(road.trim().split("\\s+")); for (String rule : rules) { if (rule.startsWith("SL")) { comprobateSpeedLimit(rule); } else if (rule.startsWith("STOP")) { comprobateStop(rule); } else if (rule.startsWith("YIELD")) { comprobateYield(rule); } else if (rule.startsWith("TURN")) { comprobateTurn(rule); } else if (rule.startsWith("R")) { comprobateRedLight(rule); } if (knockoutFoul > 0) return false; } return mildFoul < 3; } public static void comprobateSpeedLimit(String rule) { String[] parts = rule.substring(2).split("_"); int speedLimit = Integer.parseInt(parts[0]); int duration = Integer.parseInt(parts[1]); int overLimitCounter = 0; boolean minorCounted = false; for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) { int speed = extractSpeed(actions.get(currentIndex + i)); if (speed > speedLimit + 10) { knockoutFoul++; return; } if (speed > speedLimit) { overLimitCounter++; } else { overLimitCounter = 0; } if (overLimitCounter >= 3 && !minorCounted) { mildFoul++; minorCounted = true; } } currentIndex += Math.min(duration, actions.size() - currentIndex); } public static void comprobateStop(String rule) { int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0; int requiredStop = 3 + (2 * cars); int stopCount = 0; while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) { stopCount++; currentIndex++; } if (stopCount == 0) { knockoutFoul++; } else if (stopCount < requiredStop) { if (stopCount >= 2 * cars) mildFoul++; else knockoutFoul++; } } public static void comprobateYield(String rule) { int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0; int requiredStop = 2 * cars; int stopCount = 0; while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) { stopCount++; currentIndex++; } if (cars == 0) { if (stopCount >= 2) knockoutFoul++; } else { if (stopCount < requiredStop) { knockoutFoul++; } else if (stopCount < requiredStop + 3) { mildFoul++; } } } public static void comprobateTurn(String rule) { String requiredDirection = rule.equals("TURNR") ? ">" : "<"; boolean turned = false; for (int i = currentIndex; i < actions.size(); i++) { if (actions.get(i).endsWith(requiredDirection)) { turned = true; currentIndex = i + 1; break; } } if (!turned) { mildFoul++; } } public static void comprobateRedLight(String rule) { int duration = Integer.parseInt(rule.substring(1)); boolean failed = false; for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) { if (extractSpeed(actions.get(currentIndex + i)) != 0) { failed = true; } } if (failed) { knockoutFoul++; } currentIndex += Math.min(duration, actions.size() - currentIndex); } public static int extractSpeed(String action) { if (action.endsWith("<") || action.endsWith(">")) { return Integer.parseInt(action.substring(0, action.length() - 1)); } return Integer.parseInt(action); } }
import java.util.HashMap;- import java.util.Arrays;
- import java.util.List;
import java.util.Map;public class DrivingTestEvaluator{static Map<String, String> rules = new HashMap<>();static int knockoutFoul;static int mildFoul;public static boolean evaluate(String road, String exam) {return false;}public static void comprobateSpeedLimit(String rule, List<String> actions) {- public class DrivingTestEvaluator {
- static int knockoutFoul;
- static int mildFoul;
- static List<String> actions;
- static int currentIndex;
- public static boolean evaluate(String exam, String road) {
- knockoutFoul = 0;
- mildFoul = 0;
- currentIndex = 0;
- actions = Arrays.asList(exam.trim().split("\\s+"));
- List<String> rules = Arrays.asList(road.trim().split("\\s+"));
- for (String rule : rules) {
- if (rule.startsWith("SL")) {
- comprobateSpeedLimit(rule);
- } else if (rule.startsWith("STOP")) {
- comprobateStop(rule);
- } else if (rule.startsWith("YIELD")) {
- comprobateYield(rule);
- } else if (rule.startsWith("TURN")) {
- comprobateTurn(rule);
- } else if (rule.startsWith("R")) {
- comprobateRedLight(rule);
- }
- if (knockoutFoul > 0) return false;
- }
- return mildFoul < 3;
- }
- public static void comprobateSpeedLimit(String rule) {
- String[] parts = rule.substring(2).split("_");
- int speedLimit = Integer.parseInt(parts[0]);
- int duration = Integer.parseInt(parts[1]);
- int overLimitCounter = 0;
- boolean minorCounted = false;
for (int i = 0; i < Math.min(duration, actions.size()); i++) {String act = actions.get(i);int speed = extractSpeed(act);- for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) {
- int speed = extractSpeed(actions.get(currentIndex + i));
- if (speed > speedLimit + 10) {
- knockoutFoul++;
- return;
- }
- if (speed > speedLimit) {
- overLimitCounter++;
- } else {
- overLimitCounter = 0;
- }
- if (overLimitCounter >= 3 && !minorCounted) {
- mildFoul++;
- minorCounted = true;
- }
- }
for (int i = 0; i < duration && !actions.isEmpty(); i++) {actions.remove(0);}- currentIndex += Math.min(duration, actions.size() - currentIndex);
- }
public static void comprobateStop(String rule, List<String> actions) {- public static void comprobateStop(String rule) {
- int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0;
- int requiredStop = 3 + (2 * cars);
- int stopCount = 0;
for (int i = 0; i < actions.size(); i++) {if (extractSpeed(actions.get(i)) == 0) {stopCount++;} else {i = actions.size();}- while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) {
- stopCount++;
- currentIndex++;
- }
- if (stopCount == 0) {
- knockoutFoul++;
- } else if (stopCount < requiredStop) {
- if (stopCount >= 2 * cars) mildFoul++;
- else knockoutFoul++;
- }
for (int i = 0; i < stopCount && !actions.isEmpty(); i++) {actions.remove(0);}- }
public static void comprobateYield(String rule, List<String> actions) {- public static void comprobateYield(String rule) {
- int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0;
- int requiredStop = 2 * cars;
- int stopCount = 0;
for (int i = 0; i < actions.size(); i++) {if (extractSpeed(actions.get(i)) == 0) {stopCount++;} else {i = actions.size();}- while (currentIndex < actions.size() && extractSpeed(actions.get(currentIndex)) == 0) {
- stopCount++;
- currentIndex++;
- }
- if (cars == 0) {
if (stopCount >= 2) {knockoutFoul++;}- if (stopCount >= 2) knockoutFoul++;
- } else {
- if (stopCount < requiredStop) {
- knockoutFoul++;
- } else if (stopCount < requiredStop + 3) {
- mildFoul++;
- }
- }
for (int i = 0; i < stopCount && !actions.isEmpty(); i++) {actions.remove(0);}- }
public static void comprobateTurn(String rule, List<String> actions) {- public static void comprobateTurn(String rule) {
- String requiredDirection = rule.equals("TURNR") ? ">" : "<";
- boolean turned = false;
int index = -1;for (int i = 0; i < actions.size(); i++) {- for (int i = currentIndex; i < actions.size(); i++) {
- if (actions.get(i).endsWith(requiredDirection)) {
- turned = true;
index = i;i = actions.size();- currentIndex = i + 1;
- break;
- }
- }
- if (!turned) {
- mildFoul++;
} else {for (int j = 0; j <= index && !actions.isEmpty(); j++) {actions.remove(0);}- }
- }
public static void comprobateRedLight(String rule, List<String> actions) {- public static void comprobateRedLight(String rule) {
- int duration = Integer.parseInt(rule.substring(1));
- boolean failed = false;
for (int i = 0; i < duration && i < actions.size(); i++) {if (extractSpeed(actions.get(i)) != 0) {- for (int i = 0; i < duration && currentIndex + i < actions.size(); i++) {
- if (extractSpeed(actions.get(currentIndex + i)) != 0) {
- failed = true;
- }
- }
- if (failed) {
- knockoutFoul++;
- }
for (int i = 0; i < duration && !actions.isEmpty(); i++) {actions.remove(0);}- currentIndex += Math.min(duration, actions.size() - currentIndex);
- }
- public static int extractSpeed(String action) {
- if (action.endsWith("<") || action.endsWith(">")) {
- return Integer.parseInt(action.substring(0, action.length() - 1));
- }
- return Integer.parseInt(action);
- }
- }
import java.util.HashMap; import java.util.List; import java.util.Map; public class DrivingTestEvaluator{ static Map<String, String> rules = new HashMap<>(); static int knockoutFoul; static int mildFoul; public static boolean evaluate(String road, String exam) { return false; } public static void comprobateSpeedLimit(String rule, List<String> actions) { String[] parts = rule.substring(2).split("_"); int speedLimit = Integer.parseInt(parts[0]); int duration = Integer.parseInt(parts[1]); int overLimitCounter = 0; boolean minorCounted = false; for (int i = 0; i < Math.min(duration, actions.size()); i++) { String act = actions.get(i); int speed = extractSpeed(act); if (speed > speedLimit + 10) { knockoutFoul++; return; } if (speed > speedLimit) { overLimitCounter++; } else { overLimitCounter = 0; } if (overLimitCounter >= 3 && !minorCounted) { mildFoul++; minorCounted = true; } } for (int i = 0; i < duration && !actions.isEmpty(); i++) { actions.remove(0); } } public static void comprobateStop(String rule, List<String> actions) { int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0; int requiredStop = 3 + (2 * cars); int stopCount = 0; for (int i = 0; i < actions.size(); i++) { if (extractSpeed(actions.get(i)) == 0) { stopCount++; } else { i = actions.size(); } } if (stopCount == 0) { knockoutFoul++; } else if (stopCount < requiredStop) { if (stopCount >= 2 * cars) mildFoul++; else knockoutFoul++; } for (int i = 0; i < stopCount && !actions.isEmpty(); i++) { actions.remove(0); } } public static void comprobateYield(String rule, List<String> actions) { int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0; int requiredStop = 2 * cars; int stopCount = 0; for (int i = 0; i < actions.size(); i++) { if (extractSpeed(actions.get(i)) == 0) { stopCount++; } else { i = actions.size(); } } if (cars == 0) { if (stopCount >= 2) { knockoutFoul++; } } else { if (stopCount < requiredStop) { knockoutFoul++; } else if (stopCount < requiredStop + 3) { mildFoul++; } } for (int i = 0; i < stopCount && !actions.isEmpty(); i++) { actions.remove(0); } } public static void comprobateTurn(String rule, List<String> actions) { String requiredDirection = rule.equals("TURNR") ? ">" : "<"; boolean turned = false; int index = -1; for (int i = 0; i < actions.size(); i++) { if (actions.get(i).endsWith(requiredDirection)) { turned = true; index = i; i = actions.size(); } } if (!turned) { mildFoul++; } else { for (int j = 0; j <= index && !actions.isEmpty(); j++) { actions.remove(0); } } } public static void comprobateRedLight(String rule, List<String> actions) { int duration = Integer.parseInt(rule.substring(1)); boolean failed = false; for (int i = 0; i < duration && i < actions.size(); i++) { if (extractSpeed(actions.get(i)) != 0) { failed = true; } } if (failed) { knockoutFoul++; } for (int i = 0; i < duration && !actions.isEmpty(); i++) { actions.remove(0); } } public static int extractSpeed(String action) { if (action.endsWith("<") || action.endsWith(">")) { return Integer.parseInt(action.substring(0, action.length() - 1)); } return Integer.parseInt(action); } }
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class DrivingTestEvaluator{
public static boolean evaluate(String road, String exam){//your code heree- static Map<String, String> rules = new HashMap<>();
- static int knockoutFoul;
- static int mildFoul;
- public static boolean evaluate(String road, String exam) {
- return false;
- }
- public static void comprobateSpeedLimit(String rule, List<String> actions) {
- String[] parts = rule.substring(2).split("_");
- int speedLimit = Integer.parseInt(parts[0]);
- int duration = Integer.parseInt(parts[1]);
- int overLimitCounter = 0;
- boolean minorCounted = false;
- for (int i = 0; i < Math.min(duration, actions.size()); i++) {
- String act = actions.get(i);
- int speed = extractSpeed(act);
- if (speed > speedLimit + 10) {
- knockoutFoul++;
- return;
- }
- if (speed > speedLimit) {
- overLimitCounter++;
- } else {
- overLimitCounter = 0;
- }
- if (overLimitCounter >= 3 && !minorCounted) {
- mildFoul++;
- minorCounted = true;
- }
- }
- for (int i = 0; i < duration && !actions.isEmpty(); i++) {
- actions.remove(0);
- }
- }
- public static void comprobateStop(String rule, List<String> actions) {
- int cars = rule.length() > 4 ? Integer.parseInt(rule.substring(4)) : 0;
- int requiredStop = 3 + (2 * cars);
- int stopCount = 0;
- for (int i = 0; i < actions.size(); i++) {
- if (extractSpeed(actions.get(i)) == 0) {
- stopCount++;
- } else {
- i = actions.size();
- }
- }
- if (stopCount == 0) {
- knockoutFoul++;
- } else if (stopCount < requiredStop) {
- if (stopCount >= 2 * cars) mildFoul++;
- else knockoutFoul++;
- }
- for (int i = 0; i < stopCount && !actions.isEmpty(); i++) {
- actions.remove(0);
- }
- }
- public static void comprobateYield(String rule, List<String> actions) {
- int cars = rule.length() > 5 ? Integer.parseInt(rule.substring(5)) : 0;
- int requiredStop = 2 * cars;
- int stopCount = 0;
- for (int i = 0; i < actions.size(); i++) {
- if (extractSpeed(actions.get(i)) == 0) {
- stopCount++;
- } else {
- i = actions.size();
- }
- }
- if (cars == 0) {
- if (stopCount >= 2) {
- knockoutFoul++;
- }
- } else {
- if (stopCount < requiredStop) {
- knockoutFoul++;
- } else if (stopCount < requiredStop + 3) {
- mildFoul++;
- }
- }
- for (int i = 0; i < stopCount && !actions.isEmpty(); i++) {
- actions.remove(0);
- }
- }
- public static void comprobateTurn(String rule, List<String> actions) {
- String requiredDirection = rule.equals("TURNR") ? ">" : "<";
- boolean turned = false;
- int index = -1;
- for (int i = 0; i < actions.size(); i++) {
- if (actions.get(i).endsWith(requiredDirection)) {
- turned = true;
- index = i;
- i = actions.size();
- }
- }
- if (!turned) {
- mildFoul++;
- } else {
- for (int j = 0; j <= index && !actions.isEmpty(); j++) {
- actions.remove(0);
- }
- }
- }
- public static void comprobateRedLight(String rule, List<String> actions) {
- int duration = Integer.parseInt(rule.substring(1));
- boolean failed = false;
- for (int i = 0; i < duration && i < actions.size(); i++) {
- if (extractSpeed(actions.get(i)) != 0) {
- failed = true;
- }
- }
- if (failed) {
- knockoutFoul++;
- }
- for (int i = 0; i < duration && !actions.isEmpty(); i++) {
- actions.remove(0);
- }
- }
return true;- public static int extractSpeed(String action) {
- if (action.endsWith("<") || action.endsWith(">")) {
- return Integer.parseInt(action.substring(0, action.length() - 1));
- }
- return Integer.parseInt(action);
- }
- }