char_pool 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 883167af7c6679640eb8b85bde9a74faa177ff7a
4
+ data.tar.gz: 2118dc429db7e9edd5204fc2520bd53f692bea26
5
+ SHA512:
6
+ metadata.gz: a0c7b7ec2e65e8f96316a1dd45acf87c65387366b39fb84f906c196a355962030580c8f2bff735acaa115a1bdd599df7ed91d6d5aeb8f87a22f5809f53ae8ee0
7
+ data.tar.gz: e2732712b2dd1ad81249235b4e25dd23a74b76acf7170a90057e394e93a818f5880728dab16382a828a9f5a1798f317d317d96d8316a674593a69fc97f5a9f5f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in char_pool.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # CharPool
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/char_pool`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ This gem will iterate over your chars set, for example if you have chars set ['a', 'b', 'c'], this gem will produce
6
+
7
+ ```ruby
8
+ 0 = a
9
+ 1 = b
10
+ 2 = c
11
+
12
+ 3 = aa
13
+ 4 = ab
14
+ 5 = ac
15
+
16
+ 6 = ba
17
+ 7 = bb
18
+ 8 = bc
19
+
20
+ 9 = ca
21
+ 10 = cb
22
+ 11 = cc
23
+
24
+ 12 = aaa
25
+ 13 = aab
26
+ 14 = aac
27
+
28
+ 15 = aba
29
+ 16 = abb
30
+ 17 = abc
31
+
32
+ 18 = aca
33
+ 19 = acb
34
+ 20 = acc
35
+
36
+ 21 = baa
37
+ 22 = bab
38
+ 23 = bac
39
+
40
+ 24 = bba
41
+ 25 = bbb
42
+ 26 = bbc
43
+
44
+ 27 = bca
45
+ 28 = bcb
46
+ 29 = bcc
47
+
48
+ 30 = caa
49
+ 31 = cab
50
+ 32 = cac
51
+
52
+ 33 = cba
53
+ 34 = cbb
54
+ 35 = cbc
55
+
56
+ 36 = cca
57
+ 37 = ccb
58
+ 38 = ccc
59
+
60
+ 39 = aaaa
61
+
62
+ ...
63
+ ```
64
+
65
+ You got the idea.
66
+
67
+ ## Installation
68
+
69
+ Add this line to your application's Gemfile:
70
+
71
+ ```ruby
72
+ gem 'char_pool'
73
+ ```
74
+
75
+ And then execute:
76
+
77
+ $ bundle
78
+
79
+ Or install it yourself as:
80
+
81
+ $ gem install char_pool
82
+
83
+ ## Usage
84
+
85
+ ```ruby
86
+ char_pool = CharPool.new(%w(a b c))
87
+ char_pool.start #=> a
88
+ char_pool.next #=> b
89
+ char_pool.next #=> c
90
+
91
+ char_pool.next #=> aa
92
+ char_pool.next #=> ab
93
+ char_pool.next #=> ac
94
+
95
+ char_pool.index_of(9) #=> ca
96
+ char_pool.index_of(39) #=> aaaa
97
+
98
+ char_pool.index('ca') #=> 9
99
+ char_pool.index('aaaa') #=> 39
100
+ ```
101
+
102
+ TODO: more example and usage
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tejanium/char_pool.
113
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "char_pool"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/char_pool.gemspec ADDED
@@ -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 'char_pool/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "char_pool"
8
+ spec.version = CharPool::VERSION
9
+ spec.authors = ["Teja Sophista V.R."]
10
+ spec.email = ["tejanium@yahoo.com"]
11
+
12
+ spec.summary = "Itearte, Encode and Decode your chars set"
13
+ spec.homepage = "https://github.com/tejanium/char_pool"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.11"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ class CharPool
2
+ VERSION = "1.0.0"
3
+ end
data/lib/char_pool.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'char_pool/version'
2
+
3
+ class CharPool
4
+ def initialize(char_pool)
5
+ @char_pool = char_pool
6
+ end
7
+
8
+ def start(current = @char_pool.first)
9
+ @current_index = index(current)
10
+
11
+ current
12
+ end
13
+
14
+ def print
15
+ index_at @current_index
16
+ end
17
+
18
+ def next
19
+ @current_index = 0 and return print if @current_index.nil?
20
+
21
+ @current_index += 1
22
+
23
+ print
24
+ end
25
+
26
+ def index_at(number)
27
+ encode(number).map{|i| @char_pool[i]}
28
+ .join
29
+ end
30
+
31
+ def index(string)
32
+ decode(string)
33
+ end
34
+
35
+ private
36
+ def decode(string)
37
+ array = string.split('').map do |char|
38
+ @char_pool.index(char)
39
+ end
40
+
41
+ length = @char_pool.length
42
+ offset = sum_of_geometry(length, array.length - 1)
43
+
44
+ index_from_offset = count_array(array)
45
+ number = offset + index_from_offset
46
+ end
47
+
48
+ def encode(number)
49
+ return [0] if number.zero?
50
+
51
+ seq_index = 0
52
+ length = @char_pool.length
53
+
54
+ while number >= sum_of_geometry(length, seq_index)
55
+ seq_index = seq_index + 1
56
+ end
57
+
58
+ offset = sum_of_geometry(length, seq_index - 1)
59
+
60
+ index_from_offset = number - offset
61
+
62
+ build_array(seq_index, index_from_offset)
63
+ end
64
+
65
+ def sum_of_geometry(length, index)
66
+ first = ratio = length
67
+
68
+ # https://en.wikipedia.org/wiki/Geometric_series#Sum
69
+ first * ((1 - (ratio ** index)) / ( 1 - ratio ))
70
+ end
71
+
72
+ def build_array(length, number)
73
+ return [number] if length == 1
74
+
75
+ array = []
76
+
77
+ while number != 0
78
+ array.unshift number % @char_pool.length
79
+ number = number / @char_pool.length
80
+ end
81
+
82
+ ([0] * (length - array.length)) + array
83
+ end
84
+
85
+ def count_array(array)
86
+ array.reverse.each.with_index.reduce(0) do |sum, (num, index)|
87
+ sum + (num * (@char_pool.length ** index))
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: char_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Teja Sophista V.R.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - tejanium@yahoo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - char_pool.gemspec
71
+ - lib/char_pool.rb
72
+ - lib/char_pool/version.rb
73
+ homepage: https://github.com/tejanium/char_pool
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Itearte, Encode and Decode your chars set
96
+ test_files: []