warray 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/warray.rb +20 -21
- data/lib/warray/version.rb +1 -1
- data/warray-0.0.1.gem +0 -0
- metadata +3 -4
- data/.DS_Store +0 -0
- data/test.rb +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa73e3ab7893ded84eb407700b7820ef51cf3bef
|
4
|
+
data.tar.gz: b2cc99207ea5680b6d2da5f8cdf572b675b9c5b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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/
|
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.
|
data/lib/warray.rb
CHANGED
@@ -1,58 +1,57 @@
|
|
1
|
-
require
|
1
|
+
require 'warray/version'
|
2
2
|
|
3
|
+
# Warray
|
3
4
|
class Warray
|
4
5
|
|
5
6
|
attr_reader :size, :wsum, :gcd
|
6
7
|
|
7
|
-
|
8
|
+
# init
|
9
|
+
def initialize(a = [])
|
8
10
|
# Warray object has a stucture like this: [[value,weight],[value,weight]...]
|
9
|
-
|
11
|
+
# @warray = Array.new { Array.new(2) }
|
12
|
+
@warray = []
|
10
13
|
# summerized weight for Warray object
|
11
|
-
|
14
|
+
@wsum = 0
|
12
15
|
# find the greatest common divisor, if there is not @gcd = 1
|
13
|
-
|
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
|
23
|
+
while a.size > 1
|
21
24
|
value = a.shift
|
22
25
|
weight = a.shift.to_i.abs
|
23
26
|
# summerize weights
|
24
|
-
|
27
|
+
@wsum += weight
|
25
28
|
# make sure that is it not null in array
|
26
29
|
j << weight if weight != 0
|
27
|
-
|
30
|
+
@warray << [value, weight]
|
28
31
|
end
|
29
32
|
# find the min weight and make gcd, if j is not empty
|
30
|
-
|
31
|
-
# return with [] if you call new method without array
|
32
|
-
return
|
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
|
-
|
41
|
+
@warray.size
|
38
42
|
end
|
39
43
|
|
40
44
|
# you can also use length method to get size of Warray object
|
41
|
-
|
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
|
-
|
52
|
-
a << Array.new((i[1]
|
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
|
-
|
54
|
+
a.flatten!
|
56
55
|
end
|
57
56
|
|
58
57
|
end
|
data/lib/warray/version.rb
CHANGED
data/warray-0.0.1.gem
ADDED
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.
|
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-
|
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
|
-
-
|
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
|