closest_neighbours 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/.rubocop.yml +3 -0
- data/.travis.yml +7 -0
- data/.vscode/settings.json +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +101 -0
- data/Guardfile +31 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/closest_neighbours.gemspec +48 -0
- data/lib/closest_neighbours/errors/insufficient_groups_error.rb +7 -0
- data/lib/closest_neighbours/errors/non_enumberable_argument_error.rb +7 -0
- data/lib/closest_neighbours/errors/non_integer_groups_error.rb +7 -0
- data/lib/closest_neighbours/ordered.rb +76 -0
- data/lib/closest_neighbours/version.rb +3 -0
- data/lib/closest_neighbours.rb +29 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c924621de30b545235ce5ee7eaf16f3a22f642355d61284b4a5a3550e195075
|
4
|
+
data.tar.gz: a4511b796aa75101d38059eb960f610ce06ab87d22625dab7af2ea805f81ec5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1586442b671d00ff96f54bdce79419eebd21b29ab760524ea98454483cf191265e374c7a263351f37d22dd846ee0f78077432ac58c14f00c8d9c518e9dabbf42
|
7
|
+
data.tar.gz: e828070ebd738c968ad971d29680b689993796e5c690b1b764648a6a9e990b504ce6a65b981b89c97a8c8f08cd7adddb641bd384efbc78936ed935ca9d3ba76e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at milanese.david@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
closest_neighbours (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
docile (1.4.0)
|
13
|
+
ffi (1.15.4)
|
14
|
+
formatador (0.3.0)
|
15
|
+
guard (2.18.0)
|
16
|
+
formatador (>= 0.2.4)
|
17
|
+
listen (>= 2.7, < 4.0)
|
18
|
+
lumberjack (>= 1.0.12, < 2.0)
|
19
|
+
nenv (~> 0.1)
|
20
|
+
notiffany (~> 0.0)
|
21
|
+
pry (>= 0.13.0)
|
22
|
+
shellany (~> 0.0)
|
23
|
+
thor (>= 0.18.1)
|
24
|
+
guard-compat (1.2.1)
|
25
|
+
guard-rspec (4.7.3)
|
26
|
+
guard (~> 2.1)
|
27
|
+
guard-compat (~> 1.1)
|
28
|
+
rspec (>= 2.99.0, < 4.0)
|
29
|
+
listen (3.7.0)
|
30
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
31
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
32
|
+
lumberjack (1.2.8)
|
33
|
+
method_source (1.0.0)
|
34
|
+
nenv (0.3.0)
|
35
|
+
notiffany (0.1.3)
|
36
|
+
nenv (~> 0.1)
|
37
|
+
shellany (~> 0.0)
|
38
|
+
parallel (1.21.0)
|
39
|
+
parser (3.0.2.0)
|
40
|
+
ast (~> 2.4.1)
|
41
|
+
pry (0.14.1)
|
42
|
+
coderay (~> 1.1)
|
43
|
+
method_source (~> 1.0)
|
44
|
+
rainbow (3.0.0)
|
45
|
+
rake (10.5.0)
|
46
|
+
rb-fsevent (0.11.0)
|
47
|
+
rb-inotify (0.10.1)
|
48
|
+
ffi (~> 1.0)
|
49
|
+
regexp_parser (2.1.1)
|
50
|
+
rexml (3.2.5)
|
51
|
+
rspec (3.10.0)
|
52
|
+
rspec-core (~> 3.10.0)
|
53
|
+
rspec-expectations (~> 3.10.0)
|
54
|
+
rspec-mocks (~> 3.10.0)
|
55
|
+
rspec-core (3.10.1)
|
56
|
+
rspec-support (~> 3.10.0)
|
57
|
+
rspec-expectations (3.10.1)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.10.0)
|
60
|
+
rspec-mocks (3.10.2)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.10.0)
|
63
|
+
rspec-support (3.10.2)
|
64
|
+
rubocop (0.89.1)
|
65
|
+
parallel (~> 1.10)
|
66
|
+
parser (>= 2.7.1.1)
|
67
|
+
rainbow (>= 2.2.2, < 4.0)
|
68
|
+
regexp_parser (>= 1.7)
|
69
|
+
rexml
|
70
|
+
rubocop-ast (>= 0.3.0, < 1.0)
|
71
|
+
ruby-progressbar (~> 1.7)
|
72
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
73
|
+
rubocop-ast (0.8.0)
|
74
|
+
parser (>= 2.7.1.5)
|
75
|
+
ruby-progressbar (1.11.0)
|
76
|
+
shellany (0.0.1)
|
77
|
+
simplecov (0.21.2)
|
78
|
+
docile (~> 1.1)
|
79
|
+
simplecov-html (~> 0.11)
|
80
|
+
simplecov_json_formatter (~> 0.1)
|
81
|
+
simplecov-html (0.12.3)
|
82
|
+
simplecov-lcov (0.8.0)
|
83
|
+
simplecov_json_formatter (0.1.3)
|
84
|
+
thor (1.1.0)
|
85
|
+
unicode-display_width (1.7.0)
|
86
|
+
|
87
|
+
PLATFORMS
|
88
|
+
ruby
|
89
|
+
|
90
|
+
DEPENDENCIES
|
91
|
+
bundler (~> 2.1.4)
|
92
|
+
closest_neighbours!
|
93
|
+
guard-rspec (~> 4.7.3)
|
94
|
+
rake (~> 10.0)
|
95
|
+
rspec (~> 3.0)
|
96
|
+
rubocop (~> 0.89.0)
|
97
|
+
simplecov (~> 0.21.2)
|
98
|
+
simplecov-lcov (~> 0.8.0)
|
99
|
+
|
100
|
+
BUNDLED WITH
|
101
|
+
2.1.4
|
data/Guardfile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
28
|
+
watch(%r{^spec/.+_spec\.rb$})
|
29
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
30
|
+
watch('spec/spec_helper.rb') { "spec" }
|
31
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 David Milanese
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# ClosestNeighbours
|
2
|
+
|
3
|
+
This gem takes an array of elements, groups them by their ordered closest neighbors into `n` groups.
|
4
|
+
|
5
|
+
**Origin Story:**
|
6
|
+
|
7
|
+
Friend:
|
8
|
+
|
9
|
+
> "here's another one for you"
|
10
|
+
>
|
11
|
+
> "I have an array of DateTime objects, I want to match them up into groups of 4 that are the closest to each other"
|
12
|
+
|
13
|
+
Me:
|
14
|
+
|
15
|
+
> :thinking: _\*That's an interesting problem. Maybe put this in a little gem\*_
|
16
|
+
|
17
|
+
Later that night
|
18
|
+
|
19
|
+
Me:
|
20
|
+
|
21
|
+
> "Here is what I have so far..."
|
22
|
+
|
23
|
+
The algorithm to determine the closest neighbor is to look at the element before and after to determine which one is closer.
|
24
|
+
|
25
|
+
E.g.
|
26
|
+
Given the sequence
|
27
|
+
|
28
|
+
```plain
|
29
|
+
input: 1 5 20
|
30
|
+
\ / \ /
|
31
|
+
separated by: 4 14
|
32
|
+
```
|
33
|
+
|
34
|
+
`1` and `5` (separated by 4) are closer together than `5` and `20` (separated by 14), so `5` is grouped with `1`
|
35
|
+
|
36
|
+
**groups:**
|
37
|
+
|
38
|
+
1. `1`, `5`
|
39
|
+
2. `20`
|
40
|
+
|
41
|
+
## Installation
|
42
|
+
|
43
|
+
Add this line to your application's Gemfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 'closest_neighbours'
|
47
|
+
```
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
bundle
|
53
|
+
```
|
54
|
+
|
55
|
+
Or install it yourself as:
|
56
|
+
|
57
|
+
```bash
|
58
|
+
gem install closest_neighbours
|
59
|
+
```
|
60
|
+
|
61
|
+
## Usage
|
62
|
+
|
63
|
+
Require the gem
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require 'closest_neighbours'
|
67
|
+
```
|
68
|
+
|
69
|
+
Use the module:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
ClosestNeighbours.group(2, [1, 2, 3, 4])
|
73
|
+
# => [[1, 2], [3, 4]]
|
74
|
+
ClosestNeighbours.group(3, [10.days.ago, 9.days.ago, 1.day.ago])
|
75
|
+
# => [[10.days.ago, 9.days.ago], [1.day.ago]]
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
## Development
|
80
|
+
|
81
|
+
### Setup
|
82
|
+
|
83
|
+
After checking out the repo, run setup to install dependencies.
|
84
|
+
|
85
|
+
```bash
|
86
|
+
bin/setup
|
87
|
+
```
|
88
|
+
|
89
|
+
### Testing
|
90
|
+
|
91
|
+
Then, to run the test suite.
|
92
|
+
|
93
|
+
```bash
|
94
|
+
bundle exec rake spec
|
95
|
+
```
|
96
|
+
|
97
|
+
To run tests when a file changes.
|
98
|
+
|
99
|
+
```bash
|
100
|
+
bundle exec guard
|
101
|
+
```
|
102
|
+
|
103
|
+
### Console
|
104
|
+
|
105
|
+
You can also run an interactive prompt that will allow you to experiment.
|
106
|
+
|
107
|
+
```bash
|
108
|
+
bin/console
|
109
|
+
```
|
110
|
+
|
111
|
+
### Install
|
112
|
+
|
113
|
+
To install this gem onto your local machine.
|
114
|
+
|
115
|
+
```bash
|
116
|
+
bundle exec rake install
|
117
|
+
```
|
118
|
+
|
119
|
+
### Release
|
120
|
+
|
121
|
+
To release a new version, update the version number in `version.rb`, and then run,
|
122
|
+
|
123
|
+
```bash
|
124
|
+
bundle exec rake release
|
125
|
+
```
|
126
|
+
|
127
|
+
This will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
128
|
+
|
129
|
+
## Contributing
|
130
|
+
|
131
|
+
To experiment with that code, run `bin/console` for an interactive prompt.
|
132
|
+
|
133
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/aussidavid/closest_neighbours>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
134
|
+
|
135
|
+
## License
|
136
|
+
|
137
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
138
|
+
|
139
|
+
## Code of Conduct
|
140
|
+
|
141
|
+
Everyone interacting in the ClosestNeighbours project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/closest_neighbours/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'closest_neighbours'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'closest_neighbours/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'closest_neighbours'
|
9
|
+
spec.version = ClosestNeighbours::VERSION
|
10
|
+
spec.authors = ['David Milanese']
|
11
|
+
spec.email = ['milanese.david@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Takes an array of elements, groups them by their ordered closest neighbors into n groups'
|
14
|
+
spec.description = 'This gem takes an array of elements, groups them by their ordered closest neighbors into n groups.'
|
15
|
+
spec.homepage = 'https://github.com/aussidavid/closest_neighbours'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
spec.required_ruby_version = '>= 2.7'
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
23
|
+
|
24
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
25
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
26
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
27
|
+
else
|
28
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
29
|
+
'public gem pushes.'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Specify which files should be added to the gem when it is released.
|
33
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
34
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
35
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
36
|
+
end
|
37
|
+
spec.bindir = 'exe'
|
38
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
39
|
+
spec.require_paths = ['lib']
|
40
|
+
|
41
|
+
spec.add_development_dependency 'bundler', '~> 2.1.4'
|
42
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.7.3'
|
43
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
44
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
45
|
+
spec.add_development_dependency 'rubocop', '~> 0.89.0'
|
46
|
+
spec.add_development_dependency 'simplecov', '~> 0.21.2'
|
47
|
+
spec.add_development_dependency 'simplecov-lcov', '~> 0.8.0'
|
48
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClosestNeighbours
|
4
|
+
# Match array of times up into groups that are the closest to each other
|
5
|
+
class Ordered
|
6
|
+
def initialize(groups = 1, data = [])
|
7
|
+
@groups = groups
|
8
|
+
@data = data
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
validate!
|
13
|
+
|
14
|
+
if groups >= size
|
15
|
+
wraped_data + blanks
|
16
|
+
else
|
17
|
+
ranges.map { |range| sorted_data[range] }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :groups, :data
|
24
|
+
|
25
|
+
def validate!
|
26
|
+
raise NonIntegerGroupsError unless groups.is_a? Integer
|
27
|
+
raise NonEnumberableArgumentError unless data.is_a? Enumerable
|
28
|
+
raise InsufficientGroupsError if groups < 1
|
29
|
+
end
|
30
|
+
|
31
|
+
def size
|
32
|
+
data.to_a.size
|
33
|
+
end
|
34
|
+
|
35
|
+
def blanks
|
36
|
+
Array.new(number_of_blanks, [])
|
37
|
+
end
|
38
|
+
|
39
|
+
def number_of_blanks
|
40
|
+
[0, groups - size].max
|
41
|
+
end
|
42
|
+
|
43
|
+
def wraped_data
|
44
|
+
sorted_data.map { |x| [x] }
|
45
|
+
end
|
46
|
+
|
47
|
+
def ranges
|
48
|
+
[0..indexes[0]] +
|
49
|
+
(indexes << size).each_cons(2)
|
50
|
+
.map { |(left, right)| (left + 1)..right }
|
51
|
+
end
|
52
|
+
|
53
|
+
def indexes
|
54
|
+
@indexes ||= differences_with_indices
|
55
|
+
.lazy
|
56
|
+
.map(&:last)
|
57
|
+
.first(groups - 1)
|
58
|
+
.sort
|
59
|
+
end
|
60
|
+
|
61
|
+
def differences_between_each_pair
|
62
|
+
sorted_data.each_cons(2).map { |(a, b)| b - a }
|
63
|
+
end
|
64
|
+
|
65
|
+
def differences_with_indices
|
66
|
+
differences_between_each_pair
|
67
|
+
.map.with_index { |x, i| [x, i] }
|
68
|
+
.sort
|
69
|
+
.reverse
|
70
|
+
end
|
71
|
+
|
72
|
+
def sorted_data
|
73
|
+
@sorted_data ||= data.sort
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'closest_neighbours/version'
|
4
|
+
require 'closest_neighbours/ordered'
|
5
|
+
|
6
|
+
# Errors
|
7
|
+
require 'closest_neighbours/errors/non_enumberable_argument_error'
|
8
|
+
require 'closest_neighbours/errors/insufficient_groups_error'
|
9
|
+
require 'closest_neighbours/errors/non_integer_groups_error'
|
10
|
+
|
11
|
+
# Takes an array of elements, groups them by their ordered closest neighbors into n groups
|
12
|
+
module ClosestNeighbours
|
13
|
+
#
|
14
|
+
# Split an Enumerable into specified number of groups containing the closest elements in each group.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
#
|
18
|
+
# ClosestNeighbours.group(2, [10, 2, 11, 5])
|
19
|
+
# # => [[2, 5], [10, 11]]
|
20
|
+
#
|
21
|
+
# @param [Integer] groups The number of groups to create
|
22
|
+
# @param [Array|Set|Enumerable] data The data set of items to group
|
23
|
+
#
|
24
|
+
# @return [Array] An array of `groups` groups
|
25
|
+
#
|
26
|
+
def self.group(groups, data)
|
27
|
+
Ordered.new(groups, data).call
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: closest_neighbours
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Milanese
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-15 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: 2.1.4
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.7.3
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.7.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.89.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.89.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.21.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.21.2
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov-lcov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.8.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.8.0
|
111
|
+
description: This gem takes an array of elements, groups them by their ordered closest
|
112
|
+
neighbors into n groups.
|
113
|
+
email:
|
114
|
+
- milanese.david@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".travis.yml"
|
123
|
+
- ".vscode/settings.json"
|
124
|
+
- CODE_OF_CONDUCT.md
|
125
|
+
- Gemfile
|
126
|
+
- Gemfile.lock
|
127
|
+
- Guardfile
|
128
|
+
- LICENSE.txt
|
129
|
+
- README.md
|
130
|
+
- Rakefile
|
131
|
+
- bin/console
|
132
|
+
- bin/setup
|
133
|
+
- closest_neighbours.gemspec
|
134
|
+
- lib/closest_neighbours.rb
|
135
|
+
- lib/closest_neighbours/errors/insufficient_groups_error.rb
|
136
|
+
- lib/closest_neighbours/errors/non_enumberable_argument_error.rb
|
137
|
+
- lib/closest_neighbours/errors/non_integer_groups_error.rb
|
138
|
+
- lib/closest_neighbours/ordered.rb
|
139
|
+
- lib/closest_neighbours/version.rb
|
140
|
+
homepage: https://github.com/aussidavid/closest_neighbours
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata:
|
144
|
+
homepage_uri: https://github.com/aussidavid/closest_neighbours
|
145
|
+
source_code_uri: https://github.com/aussidavid/closest_neighbours
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '2.7'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubygems_version: 3.1.6
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Takes an array of elements, groups them by their ordered closest neighbors
|
165
|
+
into n groups
|
166
|
+
test_files: []
|