base_n 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/lib/base_n/version.rb +1 -1
- data/lib/base_n.rb +22 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87a9bdfaeb0fb3e0d1135b287b228ed7bb5ccd3d
|
4
|
+
data.tar.gz: 39417a5452a106fdab6aaba753f3a0ae750bedde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f60600856bcfefb2dc5202c5c9e18977377fd3a99d465306e121f25021149d01ba4d300ba9d6bd5fdee6e96093a5161742eeda69aa0e95b6b632f0fdb0136ab4
|
7
|
+
data.tar.gz: bce44fdd41dafe2b651f35ed8915c87f94702fab5ba79183f41fbc820790ec3f3212de06b313696fffef25551492b7d4f2cb5d8ddbaf9aa2a43022580de85015
|
data/lib/base_n/version.rb
CHANGED
data/lib/base_n.rb
CHANGED
@@ -16,7 +16,7 @@ module BaseN
|
|
16
16
|
end
|
17
17
|
|
18
18
|
class Encoder
|
19
|
-
|
19
|
+
PRIMITIVES = ((0..9).collect { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a + %w(- _)).map(&:freeze).freeze
|
20
20
|
|
21
21
|
attr_accessor :base
|
22
22
|
|
@@ -31,7 +31,7 @@ module BaseN
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def primitives
|
34
|
-
PRIMITIVES
|
34
|
+
PRIMITIVES[0, base]
|
35
35
|
end
|
36
36
|
|
37
37
|
def decode subject
|
@@ -41,11 +41,11 @@ module BaseN
|
|
41
41
|
|
42
42
|
total = 0
|
43
43
|
s.each_with_index do |char, index|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
if ord = primitives.index(char)
|
45
|
+
total += ord * (base ** index)
|
46
|
+
else
|
47
|
+
raise ArgumentError, "#{subject} has #{char} which is not valid"
|
48
|
+
end
|
49
49
|
end
|
50
50
|
total
|
51
51
|
end
|
@@ -59,11 +59,24 @@ module BaseN
|
|
59
59
|
s = ''
|
60
60
|
|
61
61
|
while i > 0
|
62
|
-
|
63
|
-
|
62
|
+
s << primitives[i.modulo(base)]
|
63
|
+
i /= base
|
64
64
|
end
|
65
65
|
s = '0' if s == ''
|
66
66
|
Number.new s.reverse, base
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
if __FILE__ == $0
|
72
|
+
include BaseN
|
73
|
+
2.upto Encoder::PRIMITIVES.size do |n|
|
74
|
+
puts "Base 10 | Base #{n} converted"
|
75
|
+
e = Encoder.new n
|
76
|
+
0.upto Encoder::PRIMITIVES.size + 1 do |n|
|
77
|
+
s = e.encode(n).to_s
|
78
|
+
puts "%7s | %s" % [ n, s ]
|
79
|
+
end
|
80
|
+
puts "-" * 80
|
81
|
+
end
|
82
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_n
|
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
|
- Craig R Webster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
75
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.2.
|
76
|
+
rubygems_version: 2.2.2
|
77
77
|
signing_key:
|
78
78
|
specification_version: 4
|
79
79
|
summary: Working with numbers of arbitrary base.
|