digit_array 1.0.0
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 +7 -0
- data/lib/digit_array.rb +33 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66b41635169a1de62067c1778813959cc8a0a760badc520de8ea863a2c571325
|
4
|
+
data.tar.gz: db10ddb272a573f7107d78fa965acf55f0da1177d8c9bb2d427179649b1480b8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e2f937faf991a23a0c31883985328d31f5458962e34239981c744fe947c4642707bb0d9229618a227c277937e5b47eace49bfe475c0dd821e9be03cc68768fd
|
7
|
+
data.tar.gz: 5574ce72ca5f7bb0796a04b73d17877df929b5f6feef821f6bfb885e1c18e0810f1a4d1a901f7e3f5812e2a622a913b9cca8a782347c8935cb7640b682cc4a0f
|
data/lib/digit_array.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module DigitArray
|
2
|
+
##
|
3
|
+
# n - an integer (try 1, or 5, for example)
|
4
|
+
# digits - list of digits in this imaginary number - try weird lists like:
|
5
|
+
# - [0, 1, 2]
|
6
|
+
# - [nil, :foo, :bar]
|
7
|
+
# - ['hey', 'ho', 'let', 'us', 'go']
|
8
|
+
# places - the number of places you want in the output array
|
9
|
+
#
|
10
|
+
# ex:
|
11
|
+
# convert(3, [0, 1, 2, 3, 4, 5], 2)
|
12
|
+
# => [0, 3]
|
13
|
+
#
|
14
|
+
# convert(6, ['-', 'a', 'b', 'c'], 3)
|
15
|
+
# => ['-', 'a', 'b']
|
16
|
+
#
|
17
|
+
# convert(11, ['Foo', 'Bar', 'Baz'], 4).join
|
18
|
+
# => "FooBarFooBaz"
|
19
|
+
def self.convert(n, digits, places)
|
20
|
+
base = digits.length
|
21
|
+
result = [digits[0]] * places
|
22
|
+
remaining = n
|
23
|
+
|
24
|
+
(-places..-1).each do |i|
|
25
|
+
place_value = base**(i.abs-1)
|
26
|
+
digit = remaining / place_value
|
27
|
+
result[i] = digits[digit]
|
28
|
+
remaining -= digit*place_value
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digit_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Lunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: jefflunt@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/digit_array.rb
|
20
|
+
homepage: https://github.com/jefflunt/digit_array
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata:
|
24
|
+
source_code_uri: https://github.com/jefflunt/digit_array
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.4.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: digit_array expresses a number, `n`, as a series of `digits`
|
44
|
+
test_files: []
|