Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/BusStation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/HelpCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,13 @@ public static Node<String> setFirst(Node<String> node, String value) {
return ret;
}

public static <T> void setLast(Node<T> 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
Expand Down
2 changes: 2 additions & 0 deletions src/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public record Item(int year, int score) {
}
86 changes: 86 additions & 0 deletions src/L21.java
Original file line number Diff line number Diff line change
@@ -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<Item> 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<Runner> runners = mrtnRace.lstRunners();
int numberOfRunnersParticipatedIn3ConsecutiveYears = 0;
while (runners != null) {
if (hasParticipatedIn3ConsecutiveYears(runners.getValue())) {
numberOfRunnersParticipatedIn3ConsecutiveYears++;
}
runners = runners.getNext();
}
return numberOfRunnersParticipatedIn3ConsecutiveYears;
}

public static Node<Integer> allStations(BusStation[] busStations, int[] autobusim){
Node<Integer> 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));

}

}
42 changes: 42 additions & 0 deletions src/MrtnRace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class MrtnRace {

public String country;
public int yearMrtn;
private Node<Runner> lstRunners;

public MrtnRace(String country, int yearMrtn) {
this.country = country;
this.yearMrtn = yearMrtn;
lstRunners = new Node<>(new Runner("-1", -1));
}

public Node<Runner> 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<Runner> findRunner(String id){
Node<Runner> 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));
}

}
26 changes: 26 additions & 0 deletions src/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Runner {

public String id;
public int birthYear;
private Node<Item> 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<Item> lstItems() {
return lstItems.getNext();
}
}