round_robin_tournament 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f65f228a5f469fd5296bc574bfaa98e98462dd4a
4
+ data.tar.gz: adfbeb6ce95ef9b3bbbb7ed7310b1864563c83fa
5
+ SHA512:
6
+ metadata.gz: 9ef29e4835a407c5de6f39a4db7725bc29bf88f6677656a410c84faad0ecb441b2d85e804373d089c7143553a7583d467e0b2089e0f510a3f100e16a6bc24997
7
+ data.tar.gz: 63a6bbeddbde24e1a366f56ddebaef6f0054d9c63be2a75d9f0d9b58e6045dabd329f5b35a955f314a450d2bb04c42fd73c4dc44d0b9c57baa97bdb612202d09
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in round_robin_tournament.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sebastien Saunier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ [![Build Status](https://travis-ci.org/ssaunier/round_robin_tournament.svg?branch=master)](https://travis-ci.org/ssaunier/round_robin_tournament)
2
+
3
+ # Round Robin Tournament
4
+
5
+ This little ruby gem implements the [Round Robin Tournament](http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm) scheduling. It is useful when you want a competition
6
+ "in which each contestant meets all other contestants in turn", or if you have a classroom
7
+ of students and want them to work in pairs, but with a different partner every day.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'round_robin_tournament'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ Call the method `RoundRobinTournament.schedule` with one argument,
24
+ an `Array` of size `n`. It will return an array of `n - 1` elements,
25
+ the days, and each element is an array of two elements, the game competitors.
26
+
27
+ If `n` is off, then the first element of each array element will be
28
+ an array of two element, the last one being `nil`.
29
+
30
+ ## Example (`n` is even)
31
+
32
+ ```ruby
33
+ require "round_robin_tournament"
34
+
35
+ # Compute all the possible teams for each day in the classroom
36
+ students = %w(John Paul Ringo George)
37
+ teams = RoundRobinTournament.schedule(students)
38
+
39
+ # Print for each day, each team
40
+ teams.each_with_index do |day, index|
41
+ day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ")
42
+ puts "Day #{index + 1}: #{day_teams}"
43
+ end
44
+
45
+ # Day 1: (Paul, George), (Ringo, John)
46
+ # Day 2: (Ringo, George), (John, Paul)
47
+ # Day 3: (John, George), (Paul, Ringo)
48
+ ```
49
+
50
+ ## Example (`n` is odd)
51
+
52
+ ```ruby
53
+ require "round_robin_tournament"
54
+
55
+ # Compute all the possible teams for each day in the classroom
56
+ students = %w(John Paul Ringo George OtherGuy)
57
+ teams = RoundRobinTournament.schedule(students)
58
+
59
+ # Print for each day, each team
60
+ teams.each_with_index do |day, index|
61
+ day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ")
62
+ puts "Day #{index + 1}: #{day_teams}"
63
+ end
64
+
65
+ # Day 1: (Paul, ), (Ringo, John), (George, OtherGuy)
66
+ # Day 2: (Ringo, ), (George, Paul), (OtherGuy, John)
67
+ # Day 3: (George, ), (OtherGuy, Ringo), (John, Paul)
68
+ # Day 4: (OtherGuy, ), (John, George), (Paul, Ringo)
69
+ # Day 5: (John, ), (Paul, OtherGuy), (Ringo, George)
70
+ ```
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ require 'round_robin_tournament/version'
2
+
3
+ module RoundRobinTournament
4
+
5
+ def self.schedule(array)
6
+ array.push nil if array.size.odd?
7
+ n = array.size
8
+ pivot = array.pop
9
+ games = (n - 1).times.map do
10
+ array.rotate!
11
+ [[array.first, pivot]] + (1...(n / 2)).map { |j| [array[j], array[n - 1 - j]] }
12
+ end
13
+ array.push pivot unless pivot.nil?
14
+ games
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ module RoundRobinTournament
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'round_robin_tournament/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'round_robin_tournament'
8
+ spec.version = RoundRobinTournament::VERSION
9
+ spec.authors = ['Sebastien Saunier']
10
+ spec.email = ['seb@saunier.me']
11
+ spec.summary = 'Round Robin Tournament schedule for competitions or classroom teams'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/ssaunier/round_robin_tournament'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe RoundRobinTournament do
4
+ it 'has a version number' do
5
+ expect(RoundRobinTournament::VERSION).not_to be nil
6
+ end
7
+
8
+ describe '#schedule' do
9
+ (2..20).to_a.each do |n|
10
+ it "is correct for #{n} numbers" do
11
+ schedule = RoundRobinTournament.schedule((1..n).to_a)
12
+ expect { check_schedule!(schedule) }.not_to raise_error
13
+ end
14
+ end
15
+
16
+ it 'works with array of any stuff' do
17
+ Student = Struct.new(:name)
18
+ students = %w(John Paul Ringo George).map { |beatle| Student.new beatle }
19
+
20
+ teams = RoundRobinTournament.schedule(students)
21
+ expect { check_schedule!(teams) }.not_to raise_error
22
+ end
23
+ end
24
+
25
+ def check_schedule!(schedule)
26
+ schedule.each_with_index do |games, day|
27
+ games.each_with_index do |game, game_id|
28
+ schedule.each_with_index do |other_games, other_day|
29
+ next if day == other_day
30
+ other_games.each_with_index do |other_game, other_game_id|
31
+ fail if game_id != other_game_id && same_game?(game, other_game)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def same_game?(game, other_game)
39
+ (game.first == other_game.first && game.last == other_game.last) ||
40
+ (game.last == other_game.first && game.first == other_game.last)
41
+ end
42
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'round_robin_tournament'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: round_robin_tournament
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sebastien Saunier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Round Robin Tournament schedule for competitions or classroom teams
56
+ email:
57
+ - seb@saunier.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/round_robin_tournament.rb
70
+ - lib/round_robin_tournament/version.rb
71
+ - round_robin_tournament.gemspec
72
+ - spec/round_robin_tournament_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/ssaunier/round_robin_tournament
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Round Robin Tournament schedule for competitions or classroom teams
98
+ test_files:
99
+ - spec/round_robin_tournament_spec.rb
100
+ - spec/spec_helper.rb