Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
aa1bd8cec7 | |||
3ffc40596a |
4 changed files with 182 additions and 14 deletions
16
README.md
16
README.md
|
@ -1,17 +1,5 @@
|
|||
# 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`.
|
||||
|
||||
### Completed tasks:
|
||||
|
||||
- [Task 1](/femsci/ppj/src/branch/task/1)
|
||||
- [Task 2](/femsci/ppj/src/branch/task/2)
|
||||
- [Task 3](/femsci/ppj/src/branch/task/3)
|
||||
- [Task 4](/femsci/ppj/src/branch/task/4)
|
||||
- [Task 5](/femsci/ppj/src/branch/task/5)
|
||||
- [Task 6](/femsci/ppj/src/branch/task/6)
|
||||
- [Task 7](/femsci/ppj/src/branch/task/7)
|
||||
- [Task 8](/femsci/ppj/src/branch/task/8)
|
||||
- [Task 9](/femsci/ppj/src/branch/task/9)
|
||||
[Go back to main](/femsci/ppj/src/branch/nya)
|
||||
|
|
80
dev/meowmeow/Match.java
Normal file
80
dev/meowmeow/Match.java
Normal 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]));
|
||||
}
|
||||
}
|
58
dev/meowmeow/MatchCollection.java
Normal file
58
dev/meowmeow/MatchCollection.java
Normal 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
42
dev/meowmeow/Meuczki.java
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue