Просмотр исходного кода

Tournament (#890)

* Added Tournament exercise

* Added to config and refactored after review

* Added topics and fixed indentation

* Must be unlocked by a core exercise
Morten Nygaard Åsnes 9 лет назад
Родитель
Сommit
5263bcb032

+ 14
- 0
config.json Просмотреть файл

@@ -692,6 +692,20 @@
692 692
     },
693 693
     {
694 694
       "core": false,
695
+      "difficulty": 6,
696
+      "slug": "tournament",
697
+      "topics": [
698
+        "loops",
699
+        "maps",
700
+        "sorting",
701
+        "parsing",
702
+        "text_formatting"
703
+      ],
704
+      "unlocked_by": "matrix",
705
+      "uuid": "99191d53-0fed-f680-de7a-55cfb872de17bc58629"
706
+    },
707
+    {
708
+      "core": false,
695 709
       "difficulty": 7,
696 710
       "slug": "anagram",
697 711
       "topics": [

+ 1
- 0
exercises/settings.gradle Просмотреть файл

@@ -75,6 +75,7 @@ include 'spiral-matrix'
75 75
 include 'strain'
76 76
 include 'sublist'
77 77
 include 'sum-of-multiples'
78
+include 'tournament'
78 79
 include 'transpose'
79 80
 include 'triangle'
80 81
 include 'trinary'

+ 5
- 0
exercises/tournament/.meta/src/reference/java/Result.java Просмотреть файл

@@ -0,0 +1,5 @@
1
+enum Result {
2
+    WIN,
3
+    DRAW,
4
+    LOSS
5
+}

+ 41
- 0
exercises/tournament/.meta/src/reference/java/TeamResult.java Просмотреть файл

@@ -0,0 +1,41 @@
1
+class TeamResult {
2
+
3
+    private int wins;
4
+    private int losses;
5
+    private int draws;
6
+
7
+    TeamResult() {
8
+        wins = 0;
9
+        losses = 0;
10
+        draws = 0;
11
+    }
12
+
13
+    int getPlayed() {
14
+        return wins + losses + draws;
15
+    }
16
+
17
+    int getPoints() {
18
+        return wins * 3 + draws;
19
+    }
20
+
21
+    int getWins() {
22
+        return wins;
23
+    }
24
+
25
+    int getLosses() {
26
+        return losses;
27
+    }
28
+
29
+    int getDraws() {
30
+        return draws;
31
+    }
32
+
33
+    void applyResult(final Result result) {
34
+        switch (result) {
35
+            case WIN: wins++; break;
36
+            case DRAW: draws++; break;
37
+            case LOSS: losses++; break;
38
+        }
39
+    }
40
+
41
+}

+ 62
- 0
exercises/tournament/.meta/src/reference/java/Tournament.java Просмотреть файл

@@ -0,0 +1,62 @@
1
+import java.util.HashMap;
2
+import java.util.Map;
3
+import java.util.regex.Pattern;
4
+
5
+class Tournament {
6
+
7
+    private static Pattern separatorPattern = Pattern.compile(";");
8
+    private static Pattern newlinePattern = Pattern.compile("\\n");
9
+
10
+    private Map<String, TeamResult> results;
11
+
12
+    Tournament() {
13
+        results = new HashMap<>();
14
+    }
15
+
16
+    String printTable() {
17
+        StringBuilder sb = new StringBuilder();
18
+        sb.append(String.format("%-30s | %2s | %2s | %2s | %2s | %2s\n",
19
+                "Team", "MP", "W", "D", "L", "P"));
20
+        results.entrySet()
21
+                .stream()
22
+                .sorted(Tournament::comparator)
23
+                .forEach(e -> sb.append(
24
+                        String.format("%-30s | %2d | %2d | %2d | %2d | %2d\n",
25
+                                e.getKey(), e.getValue().getPlayed(), e.getValue().getWins(), e.getValue().getDraws(),
26
+                                e.getValue().getLosses(), e.getValue().getPoints())));
27
+        return sb.toString();
28
+    }
29
+
30
+    private static int comparator(Map.Entry<String, TeamResult> teamA, Map.Entry<String, TeamResult> teamB) {
31
+        int compareByPoints = Integer.compare(teamB.getValue().getPoints(), teamA.getValue().getPoints());
32
+        return compareByPoints == 0 ? teamA.getKey().compareTo(teamB.getKey()) : compareByPoints;
33
+    }
34
+
35
+    void applyResults(final String resultString) {
36
+        String[] matches = newlinePattern.split(resultString);
37
+        for (String matchString: matches) {
38
+            String[] column = separatorPattern.split(matchString);
39
+            final TeamResult home = results.getOrDefault(column[0], new TeamResult());
40
+            final TeamResult away = results.getOrDefault(column[1], new TeamResult());
41
+            final Result result = Result.valueOf(column[2].toUpperCase());
42
+            switch (result) {
43
+                case WIN:
44
+                    home.applyResult(Result.WIN);
45
+                    away.applyResult(Result.LOSS);
46
+                    break;
47
+                case LOSS:
48
+                    home.applyResult(Result.LOSS);
49
+                    away.applyResult(Result.WIN);
50
+                    break;
51
+                case DRAW:
52
+                    home.applyResult(Result.DRAW);
53
+                    away.applyResult(Result.DRAW);
54
+                    break;
55
+            }
56
+            results.put(column[0], home);
57
+            results.put(column[1], away);
58
+        }
59
+    }
60
+
61
+
62
+}

+ 78
- 0
exercises/tournament/README.md Просмотреть файл

@@ -0,0 +1,78 @@
1
+# Tournament
2
+
3
+Tally the results of a small football competition.
4
+
5
+Based on an input file containing which team played against which and what the
6
+outcome was, create a file with a table like this:
7
+
8
+```
9
+Team                           | MP |  W |  D |  L |  P
10
+Devastating Donkeys            |  3 |  2 |  1 |  0 |  7
11
+Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6
12
+Blithering Badgers             |  3 |  1 |  0 |  2 |  3
13
+Courageous Californians        |  3 |  0 |  1 |  2 |  1
14
+```
15
+
16
+What do those abbreviations mean?
17
+
18
+- MP: Matches Played
19
+- W: Matches Won
20
+- D: Matches Drawn (Tied)
21
+- L: Matches Lost
22
+- P: Points
23
+
24
+A win earns a team 3 points. A draw earns 1. A loss earns 0.
25
+
26
+The outcome should be ordered by points, descending. In case of a tie, teams are ordered alphabetically.
27
+
28
+###
29
+
30
+Input
31
+
32
+Your tallying program will receive input that looks like:
33
+
34
+```
35
+Allegoric Alaskans;Blithering Badgers;win
36
+Devastating Donkeys;Courageous Californians;draw
37
+Devastating Donkeys;Allegoric Alaskans;win
38
+Courageous Californians;Blithering Badgers;loss
39
+Blithering Badgers;Devastating Donkeys;loss
40
+Allegoric Alaskans;Courageous Californians;win
41
+```
42
+
43
+The result of the match refers to the first team listed. So this line
44
+
45
+```
46
+Allegoric Alaskans;Blithering Badgers;win
47
+```
48
+
49
+Means that the Allegoric Alaskans beat the Blithering Badgers.
50
+
51
+This line:
52
+
53
+```
54
+Courageous Californians;Blithering Badgers;loss
55
+```
56
+
57
+Means that the Blithering Badgers beat the Courageous Californians.
58
+
59
+And this line:
60
+
61
+```
62
+Devastating Donkeys;Courageous Californians;draw
63
+```
64
+
65
+Means that the Devastating Donkeys and Courageous Californians tied.
66
+
67
+
68
+To run the tests:
69
+
70
+```sh
71
+$ gradle test
72
+```
73
+
74
+For more detailed info about the Java track see the [help page](http://exercism.io/languages/java).
75
+
76
+
77
+## Submitting Incomplete Solutions
78
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 18
- 0
exercises/tournament/build.gradle Просмотреть файл

@@ -0,0 +1,18 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+
13
+test {
14
+    testLogging {
15
+        exceptionFormat = 'full'
16
+        events = ["passed", "failed", "skipped"]
17
+    }
18
+}

+ 0
- 0
exercises/tournament/src/main/java/.keep Просмотреть файл


+ 160
- 0
exercises/tournament/src/test/java/TournamentTest.java Просмотреть файл

@@ -0,0 +1,160 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+
7
+public class TournamentTest {
8
+
9
+    private Tournament tournament;
10
+
11
+    @Before
12
+    public void setUp() {
13
+        tournament = new Tournament();
14
+    }
15
+
16
+    @Test
17
+    public void justTheHeaderIfNoInput() {
18
+        assertEquals("Team                           | MP |  W |  D |  L |  P\n", tournament.printTable());
19
+    }
20
+
21
+    @Ignore("Remove to run test")
22
+    @Test
23
+    public void aWinIsThreePointsALossIsZeroPoints() {
24
+        tournament.applyResults("Allegoric Alaskans;Blithering Badgers;win");
25
+        assertEquals(
26
+                "Team                           | MP |  W |  D |  L |  P\n" +
27
+                "Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
28
+                "Blithering Badgers             |  1 |  0 |  0 |  1 |  0\n",
29
+                tournament.printTable());
30
+    }
31
+
32
+    @Ignore("Remove to run test")
33
+    @Test
34
+    public void aWinCanAlsoBeExpressedAsALoss() {
35
+        tournament.applyResults("Blithering Badgers;Allegoric Alaskans;loss");
36
+        assertEquals(
37
+                "Team                           | MP |  W |  D |  L |  P\n" +
38
+                "Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
39
+                "Blithering Badgers             |  1 |  0 |  0 |  1 |  0\n",
40
+                tournament.printTable());
41
+    }
42
+
43
+    @Ignore("Remove to run test")
44
+    @Test
45
+    public void aDifferentTeamCanWin() {
46
+        tournament.applyResults("Blithering Badgers;Allegoric Alaskans;win");
47
+        assertEquals(
48
+                "Team                           | MP |  W |  D |  L |  P\n" +
49
+                "Blithering Badgers             |  1 |  1 |  0 |  0 |  3\n" +
50
+                "Allegoric Alaskans             |  1 |  0 |  0 |  1 |  0\n",
51
+                tournament.printTable());
52
+    }
53
+
54
+    @Ignore("Remove to run test")
55
+    @Test
56
+    public void aDrawIsOnePointEach() {
57
+        tournament.applyResults( "Allegoric Alaskans;Blithering Badgers;draw");
58
+        assertEquals(
59
+                "Team                           | MP |  W |  D |  L |  P\n" +
60
+                "Allegoric Alaskans             |  1 |  0 |  1 |  0 |  1\n" +
61
+                "Blithering Badgers             |  1 |  0 |  1 |  0 |  1\n",
62
+                tournament.printTable());
63
+    }
64
+
65
+    @Ignore("Remove to run test")
66
+    @Test
67
+    public void thereCanBeMoreThanOneMatch() {
68
+        tournament.applyResults(
69
+                "Allegoric Alaskans;Blithering Badgers;win\n" +
70
+                "Allegoric Alaskans;Blithering Badgers;win");
71
+        assertEquals(
72
+                "Team                           | MP |  W |  D |  L |  P\n" +
73
+                "Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
74
+                "Blithering Badgers             |  2 |  0 |  0 |  2 |  0\n",
75
+                tournament.printTable());
76
+    }
77
+
78
+    @Ignore("Remove to run test")
79
+    @Test
80
+    public void thereCanBeMoreThanOneWinner() {
81
+        tournament.applyResults(
82
+                "Allegoric Alaskans;Blithering Badgers;loss\n" +
83
+                "Allegoric Alaskans;Blithering Badgers;win");
84
+        assertEquals(
85
+                "Team                           | MP |  W |  D |  L |  P\n" +
86
+                "Allegoric Alaskans             |  2 |  1 |  0 |  1 |  3\n" +
87
+                "Blithering Badgers             |  2 |  1 |  0 |  1 |  3\n",
88
+                tournament.printTable());
89
+    }
90
+
91
+    @Ignore("Remove to run test")
92
+    @Test
93
+    public void thereCanBeMoreThanTwoTeams() {
94
+        tournament.applyResults(
95
+                "Allegoric Alaskans;Blithering Badgers;win\n" +
96
+                "Blithering Badgers;Courageous Californians;win\n" +
97
+                "Courageous Californians;Allegoric Alaskans;loss");
98
+        assertEquals(
99
+                "Team                           | MP |  W |  D |  L |  P\n" +
100
+                "Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
101
+                "Blithering Badgers             |  2 |  1 |  0 |  1 |  3\n" +
102
+                "Courageous Californians        |  2 |  0 |  0 |  2 |  0\n",
103
+                tournament.printTable());
104
+    }
105
+
106
+    @Ignore("Remove to run test")
107
+    @Test
108
+    public void typicalInput() {
109
+        tournament.applyResults(
110
+                "Allegoric Alaskans;Blithering Badgers;win\n" +
111
+                "Devastating Donkeys;Courageous Californians;draw\n" +
112
+                "Devastating Donkeys;Allegoric Alaskans;win\n" +
113
+                "Courageous Californians;Blithering Badgers;loss\n" +
114
+                "Blithering Badgers;Devastating Donkeys;loss\n" +
115
+                "Allegoric Alaskans;Courageous Californians;win");
116
+        assertEquals(
117
+                "Team                           | MP |  W |  D |  L |  P\n" +
118
+                "Devastating Donkeys            |  3 |  2 |  1 |  0 |  7\n" +
119
+                "Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
120
+                "Blithering Badgers             |  3 |  1 |  0 |  2 |  3\n" +
121
+                "Courageous Californians        |  3 |  0 |  1 |  2 |  1\n",
122
+                tournament.printTable());
123
+    }
124
+
125
+    @Ignore("Remove to run test")
126
+    @Test
127
+    public void incompleteCompetition() {
128
+        tournament.applyResults(
129
+                "Allegoric Alaskans;Blithering Badgers;loss\n" +
130
+                "Devastating Donkeys;Allegoric Alaskans;loss\n" +
131
+                "Courageous Californians;Blithering Badgers;draw\n" +
132
+                "Allegoric Alaskans;Courageous Californians;win");
133
+        assertEquals(
134
+                "Team                           | MP |  W |  D |  L |  P\n" +
135
+                "Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
136
+                "Blithering Badgers             |  2 |  1 |  1 |  0 |  4\n" +
137
+                "Courageous Californians        |  2 |  0 |  1 |  1 |  1\n" +
138
+                "Devastating Donkeys            |  1 |  0 |  0 |  1 |  0\n",
139
+                tournament.printTable());
140
+    }
141
+
142
+    @Ignore("Remove to run test")
143
+    @Test
144
+    public void tiesBrokenAlphabetically() {
145
+        tournament.applyResults(
146
+                "Courageous Californians;Devastating Donkeys;win\n" +
147
+                "Allegoric Alaskans;Blithering Badgers;win\n" +
148
+                "Devastating Donkeys;Allegoric Alaskans;loss\n" +
149
+                "Courageous Californians;Blithering Badgers;win\n" +
150
+                "Blithering Badgers;Devastating Donkeys;draw\n" +
151
+                "Allegoric Alaskans;Courageous Californians;draw");
152
+        assertEquals(
153
+                "Team                           | MP |  W |  D |  L |  P\n" +
154
+                "Allegoric Alaskans             |  3 |  2 |  1 |  0 |  7\n" +
155
+                "Courageous Californians        |  3 |  2 |  1 |  0 |  7\n" +
156
+                "Blithering Badgers             |  3 |  0 |  1 |  2 |  1\n" +
157
+                "Devastating Donkeys            |  3 |  0 |  1 |  2 |  1\n",
158
+                tournament.printTable());
159
+    }
160
+}