diff --git a/src/BusStation.java b/src/BusStation.java new file mode 100644 index 0000000..8823481 --- /dev/null +++ b/src/BusStation.java @@ -0,0 +1,45 @@ +public class BusStation { + + private int num; + private int[] arr; + private int amount; + + public BusStation(int num) { + this.num = num; + this.arr = new int[10]; + this.amount = 1; + } + + public boolean isStopping(int n){ + for (int bus : arr) { + if (bus == n) { + return true; + } + } + return false; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + public int[] getArr() { + return arr; + } + + public void setArr(int[] arr) { + this.arr = arr; + } +} diff --git a/src/HelpCommands.java b/src/HelpCommands.java index 9350cd2..98e91ea 100644 --- a/src/HelpCommands.java +++ b/src/HelpCommands.java @@ -621,6 +621,13 @@ public static Node setFirst(Node node, String value) { return ret; } + public static void setLast(Node node, T value) { + while (node.getNext() != null) { + node = node.getNext(); + } + node.setNext(new Node<>(value)); + } + /** * @param fst the node to reverse * @return a reversed form of the given node diff --git a/src/Item.java b/src/Item.java new file mode 100644 index 0000000..9e25e85 --- /dev/null +++ b/src/Item.java @@ -0,0 +1,2 @@ +public record Item(int year, int score) { +} diff --git a/src/L21.java b/src/L21.java new file mode 100644 index 0000000..32c8fef --- /dev/null +++ b/src/L21.java @@ -0,0 +1,86 @@ +public class L21 extends HelpCommands { + + public static boolean is3ConsecutiveNumbers(int n1, int n2, int n3) { + return (n2 - n1 == 1) && (n3 - n2 == 1); + } + + public static boolean hasParticipatedIn3ConsecutiveYears(Runner runner) { + Node runnerItems = runner.lstItems(); + while (runnerItems.getNext().getNext() != null) { + if (is3ConsecutiveNumbers( + runnerItems.getValue().year(), + runnerItems.getNext().getValue().year(), + runnerItems.getNext().getNext().getValue().year() + )) { + return true; + } + runnerItems = runnerItems.getNext(); + } + return false; + } + + public static int numberOfRunnersParticipatedIn3ConsecutiveYears(MrtnRace mrtnRace) { + Node runners = mrtnRace.lstRunners(); + int numberOfRunnersParticipatedIn3ConsecutiveYears = 0; + while (runners != null) { + if (hasParticipatedIn3ConsecutiveYears(runners.getValue())) { + numberOfRunnersParticipatedIn3ConsecutiveYears++; + } + runners = runners.getNext(); + } + return numberOfRunnersParticipatedIn3ConsecutiveYears; + } + + public static Node allStations(BusStation[] busStations, int[] autobusim){ + Node allStations = new Node<>(-1); + for (int autobus : autobusim){ + boolean wasStopping = true; + boolean isStopping = true; + for (BusStation busStation : busStations){ + if (wasStopping && busStation.isStopping(autobus)) { + isStopping = true; + } else { + wasStopping = false; + isStopping = false; + } + } + if (wasStopping && isStopping) { + HelpCommands.setLast(allStations, autobus); + } + } + return allStations.getNext(); + } + + public static void main(String[] args) { +// Runner r1 = new Runner("1", 1); +// Runner r2 = new Runner("2", 2); +// Runner r3 = new Runner("3", 3); +// +// r1.addItems(new Item(1, 2), new Item(2, 2), new Item(3, 3)); +// r2.addItems(new Item(1, 2), new Item(5, 2), new Item(3, 3)); +// +// MrtnRace mrtnRace = new MrtnRace("Israel", 1); +// mrtnRace.addRunner(r1, r2); +// +// mrtnRace.addScoreToRunner("1", 100); +// printNodes(r1.lstItems()); +// +// System.out.println(numberOfRunnersParticipatedIn3ConsecutiveYears(mrtnRace)); + + BusStation b1 = new BusStation(1); + b1.setArr(new int[]{49, 47}); + + BusStation b2 = new BusStation(2); + b2.setArr(new int[]{49, 47}); + + BusStation b3 = new BusStation(3); + b3.setArr(new int[]{49, 47}); + + BusStation[] busStations = new BusStation[]{b1, b2, b3}; + int[] autobusim = new int[]{24, 47, 49, 42}; + + printNodes(allStations(busStations, autobusim)); + + } + +} diff --git a/src/MrtnRace.java b/src/MrtnRace.java new file mode 100644 index 0000000..4e7f80e --- /dev/null +++ b/src/MrtnRace.java @@ -0,0 +1,42 @@ +public class MrtnRace { + + public String country; + public int yearMrtn; + private Node lstRunners; + + public MrtnRace(String country, int yearMrtn) { + this.country = country; + this.yearMrtn = yearMrtn; + lstRunners = new Node<>(new Runner("-1", -1)); + } + + public Node lstRunners(){ + return lstRunners.getNext(); + } + + public void addRunner(Runner runner){ + HelpCommands.setLast(lstRunners, runner); + } + + public void addRunner(Runner... runners){ + for (Runner runner : runners){ + HelpCommands.setLast(lstRunners, runner); + } + } + + public Node findRunner(String id){ + Node temp = lstRunners; + while (temp != null) { + if (temp.getValue().id.equals(id)) { + return temp; + } + temp = temp.getNext(); + } + return new Node<>(new Runner("-1", -1)); + } + + public void addScoreToRunner(String id, int score){ + findRunner(id).getValue().addItem(new Item(yearMrtn, score)); + } + +} diff --git a/src/Runner.java b/src/Runner.java new file mode 100644 index 0000000..af7589b --- /dev/null +++ b/src/Runner.java @@ -0,0 +1,26 @@ +public class Runner { + + public String id; + public int birthYear; + private Node lstItems; + + public Runner(String id, int birthYear) { + this.id = id; + this.birthYear = birthYear; + lstItems = new Node<>(new Item(-1, -1)); + } + + public void addItem(Item item){ + HelpCommands.setLast(lstItems, item); + } + + public void addItems(Item... items){ + for (Item item : items){ + HelpCommands.setLast(lstItems, item); + } + } + + public Node lstItems() { + return lstItems.getNext(); + } +}