Skip to content

Commit f65a90b

Browse files
committed
Add tournament
1 parent c8f0a35 commit f65a90b

9 files changed

Lines changed: 434 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,14 @@
746746
"prerequisites": [],
747747
"difficulty": 5
748748
},
749+
{
750+
"slug": "tournament",
751+
"name": "Tournament",
752+
"uuid": "559727f9-e07a-4e55-a0b5-8b4bda10287e",
753+
"practices": [],
754+
"prerequisites": [],
755+
"difficulty": 5
756+
},
749757
{
750758
"slug": "variable-length-quantity",
751759
"name": "Variable Length Quantity",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Instructions
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 outcome was, create a file with a table like this:
6+
7+
```text
8+
Team | MP | W | D | L | P
9+
Devastating Donkeys | 3 | 2 | 1 | 0 | 7
10+
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
11+
Blithering Badgers | 3 | 1 | 0 | 2 | 3
12+
Courageous Californians | 3 | 0 | 1 | 2 | 1
13+
```
14+
15+
What do those abbreviations mean?
16+
17+
- MP: Matches Played
18+
- W: Matches Won
19+
- D: Matches Drawn (Tied)
20+
- L: Matches Lost
21+
- P: Points
22+
23+
A win earns a team 3 points.
24+
A draw earns 1.
25+
A loss earns 0.
26+
27+
The outcome is ordered by points, descending.
28+
In case of a tie, teams are ordered alphabetically.
29+
30+
## Input
31+
32+
Your tallying program will receive input that looks like:
33+
34+
```text
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.
44+
So this line:
45+
46+
```text
47+
Allegoric Alaskans;Blithering Badgers;win
48+
```
49+
50+
means that the Allegoric Alaskans beat the Blithering Badgers.
51+
52+
This line:
53+
54+
```text
55+
Courageous Californians;Blithering Badgers;loss
56+
```
57+
58+
means that the Blithering Badgers beat the Courageous Californians.
59+
60+
And this line:
61+
62+
```text
63+
Devastating Donkeys;Courageous Californians;draw
64+
```
65+
66+
means that the Devastating Donkeys and Courageous Californians tied.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"tournament.moon"
8+
],
9+
"test": [
10+
"tournament_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Tally the results of a small football competition."
17+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Team
2+
new: (name) =>
3+
@name = name
4+
@wins, @losses, @draws = 0, 0, 0
5+
6+
win: => @wins += 1
7+
loss: => @losses += 1
8+
draw: => @draws += 1
9+
10+
points: => 3 * @wins + @draws
11+
12+
data: =>
13+
table.unpack {
14+
@name
15+
@wins + @losses + @draws
16+
@wins
17+
@draws
18+
@losses
19+
@points!
20+
}
21+
22+
__lt: (other) =>
23+
return true if @points! > other\points!
24+
return true if @points! == other\points! and @name < other.name
25+
false
26+
27+
-- ----------------------------------------------------------------
28+
FMT = "%-30s | %2s | %2s | %2s | %2s | %2s"
29+
HEAD = FMT\format 'Team', 'MP', 'W', 'D', 'L', 'P'
30+
31+
class Tournament
32+
new: =>
33+
@teams = {}
34+
35+
record: (team1, team2, result) =>
36+
home = @teams[team1] or Team team1
37+
away = @teams[team2] or Team team2
38+
39+
switch result
40+
when "win"
41+
home\win!
42+
away\loss!
43+
when "loss"
44+
home\loss!
45+
away\win!
46+
when "draw"
47+
home\draw!
48+
away\draw!
49+
50+
@teams[team1] = home
51+
@teams[team2] = away
52+
53+
standings: =>
54+
teams = [team for _, team in pairs @teams]
55+
table.sort teams
56+
standings = [FMT\format team\data! for team in *teams]
57+
table.insert standings, 1, HEAD
58+
standings
59+
60+
-- ----------------------------------------------------------------
61+
{
62+
tally: (filename) ->
63+
t = Tournament!
64+
file = io.open filename, 'r'
65+
for line in file\lines!
66+
team1, team2, result = line\match '([^;]+);([^;]+);([^;]+)'
67+
if team1
68+
t\record team1, team2, result
69+
file\close!
70+
t\standings!
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
string_list = (list, level) ->
2+
lines = [indent quote(elem) .. ',', level + 1 for elem in *list]
3+
table.insert lines, 1, '{'
4+
table.insert lines, indent('}', level)
5+
table.concat lines, '\n'
6+
7+
{
8+
module_name: 'tournament',
9+
10+
generate_test: (case, level) ->
11+
lines = {
12+
"write_file 'tournament.dsv', #{string_list case.input.rows, level}",
13+
"result = tournament.tally 'tournament.dsv'",
14+
"os.remove 'tournament.dsv'",
15+
"expected = #{string_list case.expected, level}",
16+
"assert.are.same expected, result"
17+
}
18+
table.concat [indent line, level for line in *lines], '\n'
19+
20+
test_helpers: [[
21+
22+
23+
write_file = (filename, lines) ->
24+
with io.open filename, 'w'
25+
\write table.concat lines, '\n'
26+
\close!
27+
]]
28+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[67e9fab1-07c1-49cf-9159-bc8671cc7c9c]
13+
description = "just the header if no input"
14+
15+
[1b4a8aef-0734-4007-80a2-0626178c88f4]
16+
description = "a win is three points, a loss is zero points"
17+
18+
[5f45ac09-4efe-46e7-8ddb-75ad85f86e05]
19+
description = "a win can also be expressed as a loss"
20+
21+
[fd297368-efa0-442d-9f37-dd3f9a437239]
22+
description = "a different team can win"
23+
24+
[26c016f9-e753-4a93-94e9-842f7b4d70fc]
25+
description = "a draw is one point each"
26+
27+
[731204f6-4f34-4928-97eb-1c307ba83e62]
28+
description = "There can be more than one match"
29+
30+
[49dc2463-42af-4ea6-95dc-f06cc5776adf]
31+
description = "There can be more than one winner"
32+
33+
[6d930f33-435c-4e6f-9e2d-63fa85ce7dc7]
34+
description = "There can be more than two teams"
35+
36+
[97022974-0c8a-4a50-8fe7-e36bdd8a5945]
37+
description = "typical input"
38+
39+
[fe562f0d-ac0a-4c62-b9c9-44ee3236392b]
40+
description = "incomplete competition (not all pairs have played)"
41+
42+
[3aa0386f-150b-4f99-90bb-5195e7b7d3b8]
43+
description = "ties broken alphabetically"
44+
45+
[f9e20931-8a65-442a-81f6-503c0205b17a]
46+
description = "ensure points sorted numerically"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
tally: (filename) ->
3+
error 'Implement me'
4+
}

0 commit comments

Comments
 (0)