warray 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93c5839de5220d8480ab4d97b934a456e164a795
4
+ data.tar.gz: 9e03d55ed03a51d6d68c3c3dfbb981ad78135bae
5
+ SHA512:
6
+ metadata.gz: 66fb67af3f6a4837faf19ec4b592f54cbf9b1add3ee27e5108d888dd28cd58e8b00cbe8c0bb1a18ce49b20ab507cc393dcc68e16bd396db570bcf3fd5e49f45f
7
+ data.tar.gz: 64eb639043070bbad742860b134c154e10482407dc5feba371cbf49e6a8bd9cd4cf59b1372814c89b213b68b7283c926b929091b5b3143714750a445d6c4c1d6
Binary file
@@ -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
+ --color
2
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.2.0"
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'rspec'
5
+
6
+ # Specify your gem's dependencies in warray.gemspec
7
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Zoltan Gabulya
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
+ # Warray
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/luhn_pro.svg)](http://badge.fury.io/rb/luhn_pro)
4
+ [![Build Status](https://travis-ci.org/gabulyaz/warray.svg?branch=master)](https://travis-ci.org/gabulyaz/warray)
5
+ [![Code Climate](https://codeclimate.com/github/gabulyaz/luhn_pro/badges/gpa.svg)](https://codeclimate.com/github/gabulyaz/luhn_pro)
6
+
7
+
8
+ A simple weighted array implementation.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'warray'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install warray
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ require 'warray'
30
+
31
+ # Take an array with values and weight
32
+ c=%w(apple 60% orange 30% peach 10%)
33
+ # Initialize a Warray object
34
+ w = Warray.new(c)
35
+ # Build an array with weighted items
36
+ puts w.build.to_s
37
+ => ["apple", "apple", "apple", "apple", "apple", "apple", "orange", "orange", "orange", "peach"]
38
+
39
+ # Warray object size
40
+ puts w.size # or w.length
41
+ => 3
42
+
43
+ # Warray object summarized weight
44
+ puts w.wsum
45
+ => 100
46
+
47
+ # An another example
48
+ b=%w(girl 3 boy 5)
49
+ w = Warray.new(b)
50
+ # you can also use common array methods on the builded array
51
+ puts w.build.sample(5).to_s
52
+ => ["boy", "girl", "boy", "girl", "boy"]
53
+ puts w.build.sort.to_s
54
+ => ["boy", "boy", "girl", "girl", "girl"]
55
+
56
+ ```
57
+
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/gabulyaz/warray/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
67
+
68
+ ## License
69
+
70
+ MIT License, Copyright (c) 2015 Zoltan Gabulya
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run test"
4
+ task :test do
5
+ sh "rspec spec/lib/warray_spec.rb --format documentation" # --format documentation
6
+ end
7
+
8
+ task :default => [:test]
@@ -0,0 +1,58 @@
1
+ require "warray/version"
2
+
3
+ class Warray
4
+
5
+ attr_reader :size, :wsum, :gcd
6
+
7
+ def initialize(a=[])
8
+ # Warray object has a stucture like this: [[value,weight],[value,weight]...]
9
+ @@warray = Array.new{Array.new(2)}
10
+ # summerized weight for Warray object
11
+ @@wsum = 0
12
+ # find the greatest common divisor, if there is not @gcd = 1
13
+ @@gcd = 1
14
+ # use a temporary array to find the min weight
15
+ j = []
16
+ # flatten input array for avoid error
17
+ a.flatten!
18
+ # build Warray structure, odd items are values, even items are weights
19
+ # make also sure that not give error if input array size is not even
20
+ while a.size > 1 do
21
+ value = a.shift
22
+ weight = a.shift.to_i.abs
23
+ # summerize weights
24
+ @@wsum += weight
25
+ # make sure that is it not null in array
26
+ j << weight if weight != 0
27
+ @@warray << [value,weight]
28
+ end
29
+ # find the min weight and make gcd, if j is not empty
30
+ @@gcd = @@wsum.gcd(j.min.to_i) if !j.empty?
31
+ # return with [] if you call new method without array otherwise return with Warray
32
+ return @@warray
33
+ end
34
+
35
+ # return with size of Warray object
36
+ def size
37
+ @@warray.size
38
+ end
39
+
40
+ # you can also use length method to get size of Warray object
41
+ alias :length :size
42
+
43
+ # return with summarized weights
44
+ def wsum
45
+ @@wsum
46
+ end
47
+
48
+ # build an array with weighted items
49
+ def build
50
+ a = []
51
+ @@warray.map do |i|
52
+ a << Array.new((i[1]/@@gcd),i[0])
53
+ end
54
+ # make sure that it is flatten
55
+ return a.flatten!
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ class Warray
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'warray'
3
+
4
+ describe Warray do
5
+
6
+ it {is_expected.to respond_to(:size, :length, :wsum)}
7
+
8
+ it {is_expected.to be_kind_of(Warray)}
9
+
10
+ it "New Warray is empty." do
11
+ expect(Warray.new.size).to be == 0
12
+ end
13
+
14
+ it "Warrays size is halften of the loaded arrays size" do
15
+ a = %w(a 1 b 2 c 3 d 4 d 5 e 6 f 7 g 8 h 9 i 10)
16
+ s = a.size
17
+ w = Warray.new(a)
18
+ expect(w.size).to be == s/2
19
+ end
20
+
21
+ it "print out with to_s method" do
22
+ c=%w(apple 60% orange 30% peach 10%);w = Warray.new(c)
23
+ expect(w.build.to_s).to be == '["apple", "apple", "apple", "apple", "apple", "apple", "orange", "orange", "orange", "peach"]'
24
+ end
25
+
26
+ it "summarize the weights" do
27
+ b=%w(girl 3 boy 5)
28
+ w = Warray.new(b)
29
+ expect(w.wsum).to be == 8
30
+ end
31
+
32
+ end
@@ -0,0 +1,91 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is
54
+ # recommended. For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
data/test.rb ADDED
@@ -0,0 +1,70 @@
1
+ a=%w(a 1 b 2 c 3 d 4 d 5 e 6 f 7 g 8 h 9 i 10)
2
+ b=%w(girl 3 boy 5)
3
+ c=%w(apple 60% orange 30% peach 10%)
4
+
5
+
6
+ class Warray
7
+
8
+ attr_reader :size, :wsum, :gcd
9
+
10
+ def initialize(a=[])
11
+ # Warray object has a stucture like this: [[value,weight],[value,weight]...]
12
+ @@warray = Array.new{Array.new(2)}
13
+ # summerized weight for Warray object
14
+ @@wsum = 0
15
+ # find the greatest common divisor, if there is not @gcd = 1
16
+ @@gcd = 1
17
+ # use a temporary array to find the min weight
18
+ j = []
19
+ # flatten input array for avoid error
20
+ a.flatten!
21
+ # build Warray structure, odd items are values, even items are weights
22
+ # make also sure that not give error if input array size is not even
23
+ while a.size > 1 do
24
+ value = a.shift
25
+ weight = a.shift.to_i.abs
26
+ # summerize weights
27
+ @@wsum += weight
28
+ # make sure that is it not null in array
29
+ j << weight if weight != 0
30
+ @@warray << [value,weight]
31
+ end
32
+ # find the min weight and make gcd, if j is not empty
33
+ @@gcd = @@wsum.gcd(j.min) if !j.empty?
34
+ # return with [] if you call new method without array otherwise return with Warray
35
+ return @@warray
36
+ end
37
+
38
+ # return with size of Warray object
39
+ def size
40
+ @@warray.size
41
+ end
42
+
43
+ # you can also use length method to get size of Warray object
44
+ alias :length :size
45
+
46
+ # return with summarized weights
47
+ def wsum
48
+ @@wsum
49
+ end
50
+
51
+ # build an array with weighted items
52
+ def build
53
+ a = []
54
+ @@warray.map do |i|
55
+ a << Array.new((i[1]/@@gcd),i[0])
56
+ end
57
+ # make sure that it is flatten
58
+ return a.flatten!
59
+ end
60
+
61
+ end
62
+
63
+
64
+
65
+ w = Warray.new c
66
+ a = w.build
67
+ puts a.to_s
68
+
69
+ w = Warray.new(b)
70
+ puts w.build.sample(5).sort.to_s
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'warray/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "warray"
8
+ spec.version = Warray::VERSION
9
+ spec.authors = ["Zoltan Gabulya"]
10
+ spec.email = ["gabulyaz@gmail.com"]
11
+ spec.summary = %q{Weighted array implementation.}
12
+ spec.description = %q{Warray is a simple weighted array implementation.}
13
+ spec.homepage = "https://github.com/gabulyaz/warray"
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
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: warray
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zoltan Gabulya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 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
+ description: Warray is a simple weighted array implementation.
42
+ email:
43
+ - gabulyaz@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".DS_Store"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/warray.rb
57
+ - lib/warray/version.rb
58
+ - spec/lib/warray_spec.rb
59
+ - spec/spec_helper.rb
60
+ - test.rb
61
+ - warray.gemspec
62
+ homepage: https://github.com/gabulyaz/warray
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.5
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Weighted array implementation.
86
+ test_files:
87
+ - spec/lib/warray_spec.rb
88
+ - spec/spec_helper.rb