Skip to content

Commit ef1931e

Browse files
Add tournament exercise (#276)
1 parent 61451e7 commit ef1931e

File tree

7 files changed

+340
-0
lines changed

7 files changed

+340
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,14 @@
784784
"prerequisites": [],
785785
"difficulty": 5
786786
},
787+
{
788+
"slug": "tournament",
789+
"name": "Tournament",
790+
"uuid": "50ee78fb-2aef-4f07-9ffa-bb5d704e8d6a",
791+
"practices": [],
792+
"prerequisites": [],
793+
"difficulty": 5
794+
},
787795
{
788796
"slug": "two-bucket",
789797
"name": "Two Bucket",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"tournament.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
14+
]
15+
},
16+
"blurb": "Tally the results of a small football competition."
17+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module main
2+
3+
struct Team {
4+
name string
5+
mut:
6+
won int
7+
drawn int
8+
lost int
9+
}
10+
11+
fn (team Team) matches_played() int {
12+
return team.won + team.drawn + team.lost
13+
}
14+
15+
fn (team Team) points() int {
16+
return 3 * team.won + team.drawn
17+
}
18+
19+
fn lookup(mut teams []Team, name string) int {
20+
n := teams.len
21+
for i in 0 .. n {
22+
if teams[i].name == name {
23+
return i
24+
}
25+
}
26+
teams << Team{
27+
name: name
28+
}
29+
return n
30+
}
31+
32+
fn compare_teams(a &Team, b &Team) int {
33+
if a.points() > b.points() {
34+
return -1
35+
}
36+
if b.points() > a.points() {
37+
return 1
38+
}
39+
if a.name < b.name {
40+
return -1
41+
}
42+
if b.name < a.name {
43+
return 1
44+
}
45+
return 0
46+
}
47+
48+
pub fn tally(rows string) string {
49+
mut teams := []Team{}
50+
for row in rows.split('\n') {
51+
if row == '' {
52+
continue
53+
}
54+
55+
line := row.split(';')
56+
first := lookup(mut teams, line[0])
57+
second := lookup(mut teams, line[1])
58+
match line[2] {
59+
'win' {
60+
teams[first].won = teams[first].won + 1
61+
teams[second].lost = teams[second].lost + 1
62+
}
63+
'draw' {
64+
teams[first].drawn = teams[first].drawn + 1
65+
teams[second].drawn = teams[second].drawn + 1
66+
}
67+
'loss' {
68+
teams[first].lost = teams[first].lost + 1
69+
teams[second].won = teams[second].won + 1
70+
}
71+
else {}
72+
}
73+
}
74+
teams.sort_with_compare(compare_teams)
75+
76+
mut result := []string{cap: 1 + teams.len}
77+
result << 'Team | MP | W | D | L | P'
78+
for team in teams {
79+
result << '${team.name:-31}|${team.matches_played():3} |${team.won:3} |${team.drawn:3} |${team.lost:3} |${team.points():3}'
80+
}
81+
assert result.len == 1 + teams.len
82+
return result.join('\n')
83+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[67e9fab1-07c1-49cf-9159-bc8671cc7c9c]
6+
description = "just the header if no input"
7+
8+
[1b4a8aef-0734-4007-80a2-0626178c88f4]
9+
description = "a win is three points, a loss is zero points"
10+
11+
[5f45ac09-4efe-46e7-8ddb-75ad85f86e05]
12+
description = "a win can also be expressed as a loss"
13+
14+
[fd297368-efa0-442d-9f37-dd3f9a437239]
15+
description = "a different team can win"
16+
17+
[26c016f9-e753-4a93-94e9-842f7b4d70fc]
18+
description = "a draw is one point each"
19+
20+
[731204f6-4f34-4928-97eb-1c307ba83e62]
21+
description = "There can be more than one match"
22+
23+
[49dc2463-42af-4ea6-95dc-f06cc5776adf]
24+
description = "There can be more than one winner"
25+
26+
[6d930f33-435c-4e6f-9e2d-63fa85ce7dc7]
27+
description = "There can be more than two teams"
28+
29+
[97022974-0c8a-4a50-8fe7-e36bdd8a5945]
30+
description = "typical input"
31+
32+
[fe562f0d-ac0a-4c62-b9c9-44ee3236392b]
33+
description = "incomplete competition (not all pairs have played)"
34+
35+
[3aa0386f-150b-4f99-90bb-5195e7b7d3b8]
36+
description = "ties broken alphabetically"
37+
38+
[f9e20931-8a65-442a-81f6-503c0205b17a]
39+
description = "ensure points sorted numerically"
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
module main
2+
3+
fn test_just_the_header_if_no_input() {
4+
rows := ''
5+
expected := ('Team | MP | W | D | L | P')
6+
assert tally(rows) == expected
7+
}
8+
9+
fn test_a_win_is_three_points_a_loss_is_zero_points() {
10+
rows := ('Allegoric Alaskans;Blithering Badgers;win')
11+
expected := ('Team | MP | W | D | L | P
12+
Allegoric Alaskans | 1 | 1 | 0 | 0 | 3
13+
Blithering Badgers | 1 | 0 | 0 | 1 | 0')
14+
assert tally(rows) == expected
15+
}
16+
17+
fn test_a_win_can_also_be_expressed_as_a_loss() {
18+
rows := ('Blithering Badgers;Allegoric Alaskans;loss')
19+
expected := ('Team | MP | W | D | L | P
20+
Allegoric Alaskans | 1 | 1 | 0 | 0 | 3
21+
Blithering Badgers | 1 | 0 | 0 | 1 | 0')
22+
assert tally(rows) == expected
23+
}
24+
25+
fn test_a_different_team_can_win() {
26+
rows := ('Blithering Badgers;Allegoric Alaskans;win')
27+
expected := ('Team | MP | W | D | L | P
28+
Blithering Badgers | 1 | 1 | 0 | 0 | 3
29+
Allegoric Alaskans | 1 | 0 | 0 | 1 | 0')
30+
assert tally(rows) == expected
31+
}
32+
33+
fn test_a_draw_is_one_point_each() {
34+
rows := ('Allegoric Alaskans;Blithering Badgers;draw')
35+
expected := ('Team | MP | W | D | L | P
36+
Allegoric Alaskans | 1 | 0 | 1 | 0 | 1
37+
Blithering Badgers | 1 | 0 | 1 | 0 | 1')
38+
assert tally(rows) == expected
39+
}
40+
41+
fn test_there_can_be_more_than_one_match() {
42+
rows := ('Allegoric Alaskans;Blithering Badgers;win
43+
Allegoric Alaskans;Blithering Badgers;win')
44+
expected := ('Team | MP | W | D | L | P
45+
Allegoric Alaskans | 2 | 2 | 0 | 0 | 6
46+
Blithering Badgers | 2 | 0 | 0 | 2 | 0')
47+
assert tally(rows) == expected
48+
}
49+
50+
fn test_there_can_be_more_than_one_winner() {
51+
rows := ('Allegoric Alaskans;Blithering Badgers;loss
52+
Allegoric Alaskans;Blithering Badgers;win')
53+
expected := ('Team | MP | W | D | L | P
54+
Allegoric Alaskans | 2 | 1 | 0 | 1 | 3
55+
Blithering Badgers | 2 | 1 | 0 | 1 | 3')
56+
assert tally(rows) == expected
57+
}
58+
59+
fn test_there_can_be_more_than_two_teams() {
60+
rows := ('Allegoric Alaskans;Blithering Badgers;win
61+
Blithering Badgers;Courageous Californians;win
62+
Courageous Californians;Allegoric Alaskans;loss')
63+
expected := ('Team | MP | W | D | L | P
64+
Allegoric Alaskans | 2 | 2 | 0 | 0 | 6
65+
Blithering Badgers | 2 | 1 | 0 | 1 | 3
66+
Courageous Californians | 2 | 0 | 0 | 2 | 0')
67+
assert tally(rows) == expected
68+
}
69+
70+
fn test_typical_input() {
71+
rows := ('Allegoric Alaskans;Blithering Badgers;win
72+
Devastating Donkeys;Courageous Californians;draw
73+
Devastating Donkeys;Allegoric Alaskans;win
74+
Courageous Californians;Blithering Badgers;loss
75+
Blithering Badgers;Devastating Donkeys;loss
76+
Allegoric Alaskans;Courageous Californians;win')
77+
expected := ('Team | MP | W | D | L | P
78+
Devastating Donkeys | 3 | 2 | 1 | 0 | 7
79+
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
80+
Blithering Badgers | 3 | 1 | 0 | 2 | 3
81+
Courageous Californians | 3 | 0 | 1 | 2 | 1')
82+
assert tally(rows) == expected
83+
}
84+
85+
fn test_incomplete_competition_not_all_pairs_have_played() {
86+
rows := ('Allegoric Alaskans;Blithering Badgers;loss
87+
Devastating Donkeys;Allegoric Alaskans;loss
88+
Courageous Californians;Blithering Badgers;draw
89+
Allegoric Alaskans;Courageous Californians;win')
90+
expected := ('Team | MP | W | D | L | P
91+
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
92+
Blithering Badgers | 2 | 1 | 1 | 0 | 4
93+
Courageous Californians | 2 | 0 | 1 | 1 | 1
94+
Devastating Donkeys | 1 | 0 | 0 | 1 | 0')
95+
assert tally(rows) == expected
96+
}
97+
98+
fn test_ties_broken_alphabetically() {
99+
rows := ('Courageous Californians;Devastating Donkeys;win
100+
Allegoric Alaskans;Blithering Badgers;win
101+
Devastating Donkeys;Allegoric Alaskans;loss
102+
Courageous Californians;Blithering Badgers;win
103+
Blithering Badgers;Devastating Donkeys;draw
104+
Allegoric Alaskans;Courageous Californians;draw')
105+
expected := ('Team | MP | W | D | L | P
106+
Allegoric Alaskans | 3 | 2 | 1 | 0 | 7
107+
Courageous Californians | 3 | 2 | 1 | 0 | 7
108+
Blithering Badgers | 3 | 0 | 1 | 2 | 1
109+
Devastating Donkeys | 3 | 0 | 1 | 2 | 1')
110+
assert tally(rows) == expected
111+
}
112+
113+
fn test_ensure_points_sorted_numerically() {
114+
rows := ('Devastating Donkeys;Blithering Badgers;win
115+
Devastating Donkeys;Blithering Badgers;win
116+
Devastating Donkeys;Blithering Badgers;win
117+
Devastating Donkeys;Blithering Badgers;win
118+
Blithering Badgers;Devastating Donkeys;win')
119+
expected := ('Team | MP | W | D | L | P
120+
Devastating Donkeys | 5 | 4 | 0 | 1 | 12
121+
Blithering Badgers | 5 | 1 | 0 | 4 | 3')
122+
assert tally(rows) == expected
123+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
pub fn tally(rows string) string {
4+
}

0 commit comments

Comments
 (0)