Compare commits

...

2 commits
nya ... task/6

Author SHA1 Message Date
aa1bd8cec7
task 6 2024-05-11 23:30:10 +02:00
3ffc40596a
skele 2024-03-28 19:25:56 +01:00
4 changed files with 182 additions and 3 deletions

View file

@ -1,6 +1,5 @@
# PPJ # PPJ
This repository exists to archive PJATK tasks for the PPJ curriculum. This branch contains the solution for the **6th task**.
Each task can be found under its **corresponding branch**: `task/n`. [Go back to main](/femsci/ppj/src/branch/nya)
Example: `task/2`.

80
dev/meowmeow/Match.java Normal file
View file

@ -0,0 +1,80 @@
/*
* TASK 6
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/6
* by femsci
*/
package dev.meowmeow;
// no time to refactor ;w;
public class Match {
public Match(String cA, String cB, int sA, int sB) {
if (Math.max(sA, sB) == sA) {
countryWinner = cA;
scoreWinner = sA;
countryLoser = cB;
scoreLoser = sB;
} else {
countryWinner = cB;
scoreWinner = sB;
countryLoser = cA;
scoreLoser = sA;
}
}
private String countryWinner, countryLoser;
private int scoreWinner, scoreLoser;
public String[] getParticipants() {
return new String[] { countryWinner, countryLoser };
}
public boolean isDraw() {
return scoreLoser == scoreWinner;
}
public String getWinner() {
if (isDraw()) {
throw new IllegalAccessError("draw");
}
return countryWinner;
}
public int getWinnerScore() {
if (isDraw()) {
throw new IllegalAccessError("draw");
}
return scoreWinner;
}
public String getLoser() {
if (isDraw()) {
throw new IllegalAccessError("draw");
}
return countryLoser;
}
public int getLoserScore() {
if (isDraw()) {
throw new IllegalAccessError("draw");
}
return scoreLoser;
}
public int getPoints() {
return isDraw() ? 1 : 3;
}
public static Match Parse(String[] data) {
if (data.length != 4) {
throw new IllegalArgumentException("Invalid data");
}
return new Match(data[0], data[2], Integer.parseInt(data[1]), Integer.parseInt(data[3]));
}
}

View file

@ -0,0 +1,58 @@
/*
* TASK 6
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/6
* by femsci
*/
package dev.meowmeow;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
public class MatchCollection {
public MatchCollection(List<Match> matches) {
_matches = matches;
}
private List<Match> _matches;
public List<Match> getMatches() {
return _matches;
}
public Map<String, Integer> getPointsDistribution() {
Map<String, Integer> pointMap = new HashMap<>();
Function<String, Consumer<Integer>> incrementBy = c -> i -> pointMap.put(c, pointMap.getOrDefault(c, 0) + i);
// calculate scores from each match
for (Match match : _matches) {
if (match.isDraw()) {
// increment by one for each participant during draw
incrementBy.apply(match.getParticipants()[0]).accept(1);
incrementBy.apply(match.getParticipants()[1]).accept(1);
continue;
}
incrementBy.apply(match.getWinner()).accept(3);
// ensure that the loser is embedded in the map
pointMap.putIfAbsent(match.getLoser(), 0);
}
return pointMap;
}
// parse this silly structure
public static MatchCollection Parse(String[][] data) {
return new MatchCollection(
Arrays.asList(data)
.stream()
.map(Match::Parse)
.toList());
}
}

42
dev/meowmeow/Meuczki.java Normal file
View file

@ -0,0 +1,42 @@
/*
* TASK 6
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/6
* by femsci
*/
package dev.meowmeow;
import java.util.Arrays;
import java.util.Map;
public class Meuczki {
public static void main(String[] args) {
// this cute lil copypasted structure, very stringified <3
String[][] arr = { { "Germany", "2", "Scotland", "1" },
{ "Poland", "2", "Germany", "0" },
{ "Germany", "1", "Ireland", "1" },
{ "Poland", "2", "Scotland", "2" },
{ "Scotland", "1", "Ireland", "0" },
{ "Ireland", "1", "Poland", "1" },
{ "Ireland", "1", "Scotland", "1" },
{ "Germany", "3", "Poland", "1" },
{ "Scotland", "2", "Germany", "3" },
{ "Ireland", "1", "Germany", "0" },
{ "Scotland", "2", "Poland", "2" },
{ "Poland", "2", "Ireland", "1" } };
// twoliner 🥺🥺
MatchCollection tournament = MatchCollection.Parse(arr);
Map<String, Integer> points = tournament.getPointsDistribution();
int[] pointVector = new int[] {
points.get("Germany"),
points.get("Ireland"),
points.get("Poland"),
points.get("Scotland")
};
// :3
System.out.println(Arrays.toString(pointVector));
}
}