baseanything 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 +4 -4
- data/lib/baseanything.rb +158 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9463145080f38fc0dbc41ce54160a31806f763f0
|
|
4
|
+
data.tar.gz: 31536076f2717dffbb370c45a9ee7f0f9829384d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4e706ff9c022008ea73ff44c6dc2d335041e4a0122d0b026f4581b5fd01ceacb6bb78944f91687c441ae76d498c82c5084a2773e871d29790d417b2bc524827f
|
|
7
|
+
data.tar.gz: 063c0d0e1b0637c2ab22401cad70b4614e8ee637f899f2dbe036e4fdcd217ca7d2f43946c6bc77ed1d8e626dd292f06374bef203c1d49099993bd4fafd3d2974
|
data/lib/baseanything.rb
CHANGED
|
@@ -1 +1,158 @@
|
|
|
1
|
-
|
|
1
|
+
module BaseAnything
|
|
2
|
+
class NumberSystem
|
|
3
|
+
|
|
4
|
+
def initialize(arr)
|
|
5
|
+
@correspondences = make_correspondance_hash(arr)
|
|
6
|
+
@base = arr.length
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_base(num, base)
|
|
10
|
+
num = own_to_dec(num)
|
|
11
|
+
output = ""
|
|
12
|
+
highest_quot = 0
|
|
13
|
+
|
|
14
|
+
while (base ** (highest_quot+1)) <= num
|
|
15
|
+
highest_quot += 1
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
while highest_quot >= 0
|
|
19
|
+
next_digit = num / (base ** highest_quot)
|
|
20
|
+
output << ANY_BASE[next_digit.to_s]
|
|
21
|
+
to_remove = next_digit * (base ** highest_quot)
|
|
22
|
+
num -= to_remove
|
|
23
|
+
highest_quot -= 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
output
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def from_base(num, base)
|
|
30
|
+
num = other_to_dec(num, base)
|
|
31
|
+
|
|
32
|
+
output = ""
|
|
33
|
+
highest_quot = 0
|
|
34
|
+
|
|
35
|
+
while (@base ** (highest_quot+1) <= num)
|
|
36
|
+
highest_quot += 1
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
while highest_quot >= 0
|
|
40
|
+
next_digit = num / (@base ** highest_quot)
|
|
41
|
+
digit_value = @correspondences.select{|k,v| @correspondences[k.to_s] == next_digit}
|
|
42
|
+
output << digit_value.keys[0]
|
|
43
|
+
to_remove = next_digit * (@base ** highest_quot)
|
|
44
|
+
num -= to_remove
|
|
45
|
+
highest_quot -= 1
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
output
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def method_missing(name, num)
|
|
53
|
+
name = name.to_s
|
|
54
|
+
if name[0..2] == "to_"
|
|
55
|
+
base = FIND_BASE["#{name[3..-1]}"]
|
|
56
|
+
to_base(num, base)
|
|
57
|
+
elsif name[0..4] == "from_"
|
|
58
|
+
base = FIND_BASE["#{name[5..-1]}"]
|
|
59
|
+
from_base(num, base)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
ANY_BASE = {
|
|
67
|
+
'0' => '0',
|
|
68
|
+
'1' => '1',
|
|
69
|
+
'2' => '2',
|
|
70
|
+
'3' => '3',
|
|
71
|
+
'4' => '4',
|
|
72
|
+
'5' => '5',
|
|
73
|
+
'6' => '6',
|
|
74
|
+
'7' => '7',
|
|
75
|
+
'8' => '8',
|
|
76
|
+
'9' => '9',
|
|
77
|
+
'10' => 'A',
|
|
78
|
+
'11' => 'B',
|
|
79
|
+
'12' => 'C',
|
|
80
|
+
'13' => 'D',
|
|
81
|
+
'14' => 'E',
|
|
82
|
+
'15' => 'F',
|
|
83
|
+
'16' => 'G',
|
|
84
|
+
'17' => 'H',
|
|
85
|
+
'18' => 'I',
|
|
86
|
+
'19' => 'J',
|
|
87
|
+
'20' => 'K',
|
|
88
|
+
'21' => 'L',
|
|
89
|
+
'22' => 'M',
|
|
90
|
+
'23' => 'N',
|
|
91
|
+
'24' => 'O',
|
|
92
|
+
'25' => 'P',
|
|
93
|
+
'26' => 'Q',
|
|
94
|
+
'27' => 'R',
|
|
95
|
+
'28' => 'S',
|
|
96
|
+
'29' => 'T',
|
|
97
|
+
'30' => 'U',
|
|
98
|
+
'31' => 'V',
|
|
99
|
+
'32' => 'W',
|
|
100
|
+
'33' => 'X',
|
|
101
|
+
'34' => 'Y',
|
|
102
|
+
'35' => 'Z'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
FIND_BASE = {
|
|
106
|
+
'binary' => 2,
|
|
107
|
+
'ternary' => 3,
|
|
108
|
+
'quaternary' => 4,
|
|
109
|
+
'quinary' => 5,
|
|
110
|
+
'senary' => 6,
|
|
111
|
+
'heptary' => 7,
|
|
112
|
+
'octal' => 8,
|
|
113
|
+
'nonary' => 9,
|
|
114
|
+
'decimal' => 10,
|
|
115
|
+
'undecimal' => 11,
|
|
116
|
+
'duodecimal' => 12,
|
|
117
|
+
'tridecimal' => 13,
|
|
118
|
+
'tetradecimal' => 14,
|
|
119
|
+
'pentadecimal' => 15,
|
|
120
|
+
'hexadecimal' => 16,
|
|
121
|
+
'vigesimal' => 20,
|
|
122
|
+
'hexavigesimal' => 24,
|
|
123
|
+
'heptavigesimal' => 27,
|
|
124
|
+
'trigesimal' => 30,
|
|
125
|
+
'duotrigesimal' => 32,
|
|
126
|
+
'hexatrigesimal' => 36
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
def make_correspondance_hash(arr)
|
|
130
|
+
hash = {}
|
|
131
|
+
value = 0
|
|
132
|
+
arr.each do |key|
|
|
133
|
+
hash[key] = value
|
|
134
|
+
value += 1
|
|
135
|
+
end
|
|
136
|
+
hash
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def own_to_dec(num)
|
|
140
|
+
decimal_value = 0
|
|
141
|
+
num.split("").reverse.each_with_index do |digit, i|
|
|
142
|
+
decimal_value += @correspondences[digit] * (@base**i)
|
|
143
|
+
end
|
|
144
|
+
decimal_value
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def other_to_dec(num, base)
|
|
148
|
+
decimal_value = 0
|
|
149
|
+
num.split("").reverse.each_with_index do |digit, i|
|
|
150
|
+
digit_value = ANY_BASE.select{|k,v| ANY_BASE[k] == digit}
|
|
151
|
+
digit_value = digit_value.keys[0].to_i
|
|
152
|
+
decimal_value += digit_value * (base**i)
|
|
153
|
+
end
|
|
154
|
+
decimal_value
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
end
|
|
158
|
+
end
|