Skip to content
Merged
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "tournament",
"name": "Tournament",
"uuid": "559727f9-e07a-4e55-a0b5-8b4bda10287e",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "variable-length-quantity",
"name": "Variable Length Quantity",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/tournament/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
66 changes: 66 additions & 0 deletions exercises/practice/tournament/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Instructions

Tally the results of a small football competition.

Based on an input file containing which team played against which and what the outcome was, create a file with a table like this:

```text
Team | MP | W | D | L | P
Devastating Donkeys | 3 | 2 | 1 | 0 | 7
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
Blithering Badgers | 3 | 1 | 0 | 2 | 3
Courageous Californians | 3 | 0 | 1 | 2 | 1
```

What do those abbreviations mean?

- MP: Matches Played
- W: Matches Won
- D: Matches Drawn (Tied)
- L: Matches Lost
- P: Points

A win earns a team 3 points.
A draw earns 1.
A loss earns 0.

The outcome is ordered by points, descending.
In case of a tie, teams are ordered alphabetically.

## Input

Your tallying program will receive input that looks like:

```text
Allegoric Alaskans;Blithering Badgers;win
Devastating Donkeys;Courageous Californians;draw
Devastating Donkeys;Allegoric Alaskans;win
Courageous Californians;Blithering Badgers;loss
Blithering Badgers;Devastating Donkeys;loss
Allegoric Alaskans;Courageous Californians;win
```

The result of the match refers to the first team listed.
So this line:

```text
Allegoric Alaskans;Blithering Badgers;win
```

means that the Allegoric Alaskans beat the Blithering Badgers.

This line:

```text
Courageous Californians;Blithering Badgers;loss
```

means that the Blithering Badgers beat the Courageous Californians.

And this line:

```text
Devastating Donkeys;Courageous Californians;draw
```

means that the Devastating Donkeys and Courageous Californians tied.
17 changes: 17 additions & 0 deletions exercises/practice/tournament/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"tournament.moon"
],
"test": [
"tournament_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Tally the results of a small football competition."
}
71 changes: 71 additions & 0 deletions exercises/practice/tournament/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Team
new: (name) =>
@name = name
@wins, @losses, @draws = 0, 0, 0

win: => @wins += 1
loss: => @losses += 1
draw: => @draws += 1

points: => 3 * @wins + @draws

data: =>
table.unpack {
@name
@wins + @losses + @draws
@wins
@draws
@losses
@points!
}

__lt: (other) =>
return true if @points! > other\points!
return true if @points! == other\points! and @name < other.name
false

-- ----------------------------------------------------------------
FMT = "%-30s | %2s | %2s | %2s | %2s | %2s"
HEAD = FMT\format 'Team', 'MP', 'W', 'D', 'L', 'P'

class Tournament
new: =>
@teams = {}

record: (team1, team2, result) =>
home = @teams[team1] or Team team1
away = @teams[team2] or Team team2

switch result
when "win"
home\win!
away\loss!
when "loss"
home\loss!
away\win!
when "draw"
home\draw!
away\draw!

@teams[team1] = home
@teams[team2] = away

standings: =>
teams = [team for _, team in pairs @teams]
table.sort teams
standings = [FMT\format team\data! for team in *teams]
table.insert standings, 1, HEAD
standings

-- ----------------------------------------------------------------
{
tally: (filename) ->
t = Tournament!
file = io.open filename, 'r'
for line in file\lines!
team1, team2, result = line\match '([^;]+);([^;]+);([^;]+)'
if team1
t\record team1, team2, result
file\close!
t\standings!
}
28 changes: 28 additions & 0 deletions exercises/practice/tournament/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
string_list = (list, level) ->
lines = [indent quote(elem) .. ',', level + 1 for elem in *list]
table.insert lines, 1, '{'
table.insert lines, indent('}', level)
table.concat lines, '\n'

{
module_name: 'tournament',

generate_test: (case, level) ->
lines = {
"write_file 'tournament.dsv', #{string_list case.input.rows, level}",
"result = tournament.tally 'tournament.dsv'",
"os.remove 'tournament.dsv'",
"expected = #{string_list case.expected, level}",
"assert.are.same expected, result"
}
table.concat [indent line, level for line in *lines], '\n'

test_helpers: [[


write_file = (filename, lines) ->
with io.open filename, 'w'
\write table.concat lines, '\n'
\close!
]]
}
46 changes: 46 additions & 0 deletions exercises/practice/tournament/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[67e9fab1-07c1-49cf-9159-bc8671cc7c9c]
description = "just the header if no input"

[1b4a8aef-0734-4007-80a2-0626178c88f4]
description = "a win is three points, a loss is zero points"

[5f45ac09-4efe-46e7-8ddb-75ad85f86e05]
description = "a win can also be expressed as a loss"

[fd297368-efa0-442d-9f37-dd3f9a437239]
description = "a different team can win"

[26c016f9-e753-4a93-94e9-842f7b4d70fc]
description = "a draw is one point each"

[731204f6-4f34-4928-97eb-1c307ba83e62]
description = "There can be more than one match"

[49dc2463-42af-4ea6-95dc-f06cc5776adf]
description = "There can be more than one winner"

[6d930f33-435c-4e6f-9e2d-63fa85ce7dc7]
description = "There can be more than two teams"

[97022974-0c8a-4a50-8fe7-e36bdd8a5945]
description = "typical input"

[fe562f0d-ac0a-4c62-b9c9-44ee3236392b]
description = "incomplete competition (not all pairs have played)"

[3aa0386f-150b-4f99-90bb-5195e7b7d3b8]
description = "ties broken alphabetically"

[f9e20931-8a65-442a-81f6-503c0205b17a]
description = "ensure points sorted numerically"
4 changes: 4 additions & 0 deletions exercises/practice/tournament/tournament.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
tally: (filename) ->
error 'Implement me'
}
Loading
Loading