warray 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93c5839de5220d8480ab4d97b934a456e164a795
4
- data.tar.gz: 9e03d55ed03a51d6d68c3c3dfbb981ad78135bae
3
+ metadata.gz: aa73e3ab7893ded84eb407700b7820ef51cf3bef
4
+ data.tar.gz: b2cc99207ea5680b6d2da5f8cdf572b675b9c5b4
5
5
  SHA512:
6
- metadata.gz: 66fb67af3f6a4837faf19ec4b592f54cbf9b1add3ee27e5108d888dd28cd58e8b00cbe8c0bb1a18ce49b20ab507cc393dcc68e16bd396db570bcf3fd5e49f45f
7
- data.tar.gz: 64eb639043070bbad742860b134c154e10482407dc5feba371cbf49e6a8bd9cd4cf59b1372814c89b213b68b7283c926b929091b5b3143714750a445d6c4c1d6
6
+ metadata.gz: 293b74d183e7ea4172cf8fcebb988c0060204aac62b72c5ddb7d25e18cd09a9e3ec380bc7ff4bcd6f257ee817ff176cf0f2cb0fee016b80748f2fc11e19b3627
7
+ data.tar.gz: 3942e871388177c4c2fc8ebc8d758f69dbb52e44adcf2964410a1834dd7110b4a6b98e65e8bfadec261186a02df4506d9be7121584d49f240c927de2e8aa5795
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Warray
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/luhn_pro.svg)](http://badge.fury.io/rb/luhn_pro)
3
+ [![Gem Version](https://badge.fury.io/rb/warray.svg)](http://badge.fury.io/rb/warray)
4
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)
5
+ [![Code Climate](https://codeclimate.com/repos/55252920e30ba060cd004967/badges/5089577f6fdc90e421c5/gpa.svg)](https://codeclimate.com/repos/55252920e30ba060cd004967/feed)
6
6
 
7
7
 
8
8
  A simple weighted array implementation.
@@ -1,58 +1,57 @@
1
- require "warray/version"
1
+ require 'warray/version'
2
2
 
3
+ # Warray
3
4
  class Warray
4
5
 
5
6
  attr_reader :size, :wsum, :gcd
6
7
 
7
- def initialize(a=[])
8
+ # init
9
+ def initialize(a = [])
8
10
  # Warray object has a stucture like this: [[value,weight],[value,weight]...]
9
- @@warray = Array.new{Array.new(2)}
11
+ # @warray = Array.new { Array.new(2) }
12
+ @warray = []
10
13
  # summerized weight for Warray object
11
- @@wsum = 0
14
+ @wsum = 0
12
15
  # find the greatest common divisor, if there is not @gcd = 1
13
- @@gcd = 1
16
+ @gcd = 1
14
17
  # use a temporary array to find the min weight
15
18
  j = []
16
19
  # flatten input array for avoid error
17
20
  a.flatten!
18
21
  # build Warray structure, odd items are values, even items are weights
19
22
  # make also sure that not give error if input array size is not even
20
- while a.size > 1 do
23
+ while a.size > 1
21
24
  value = a.shift
22
25
  weight = a.shift.to_i.abs
23
26
  # summerize weights
24
- @@wsum += weight
27
+ @wsum += weight
25
28
  # make sure that is it not null in array
26
29
  j << weight if weight != 0
27
- @@warray << [value,weight]
30
+ @warray << [value, weight]
28
31
  end
29
32
  # 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
+ @gcd = @wsum.gcd(j.min.to_i) unless j.empty?
34
+ # return with [] if you call new method without array
35
+ # otherwise return with Warray
36
+ @warray
33
37
  end
34
38
 
35
39
  # return with size of Warray object
36
40
  def size
37
- @@warray.size
41
+ @warray.size
38
42
  end
39
43
 
40
44
  # 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
45
+ alias_method :length, :size
47
46
 
48
47
  # build an array with weighted items
49
48
  def build
50
49
  a = []
51
- @@warray.map do |i|
52
- a << Array.new((i[1]/@@gcd),i[0])
50
+ @warray.map do |i|
51
+ a << Array.new((i[1] / @gcd), i[0])
53
52
  end
54
53
  # make sure that it is flatten
55
- return a.flatten!
54
+ a.flatten!
56
55
  end
57
56
 
58
57
  end
@@ -1,3 +1,3 @@
1
1
  class Warray
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warray
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zoltan Gabulya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -45,7 +45,6 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - ".DS_Store"
49
48
  - ".gitignore"
50
49
  - ".rspec"
51
50
  - ".travis.yml"
@@ -57,7 +56,7 @@ files:
57
56
  - lib/warray/version.rb
58
57
  - spec/lib/warray_spec.rb
59
58
  - spec/spec_helper.rb
60
- - test.rb
59
+ - warray-0.0.1.gem
61
60
  - warray.gemspec
62
61
  homepage: https://github.com/gabulyaz/warray
63
62
  licenses:
data/.DS_Store DELETED
Binary file
data/test.rb DELETED
@@ -1,70 +0,0 @@
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