Fixnum#to_a

  • Thread starter Levin Alexander
  • Start date
L

Levin Alexander

SGksCgpJJ2QgbGlrZSB0byBwcm9wb3NlIEZpeG51bSN0b19hOgoKICBjbGFzcyBGaXhudW0KICAg
IGRlZiB0b19hKGJhc2U9MTAsbWluX2xlbmd0aD0wKQogICAgICBzZWxmLnRvX3MoYmFzZSkucmp1
c3QobWluX2xlbmd0aCkuc3BsaXQoLy8pLm1hcCB7IHxjfCBjLnRvX2koYmFzZSkgfQogICAgZW5k
CiAgZW5kCgogIDEyMzQ1LnRvX2EgIz0+IFsxLDIsMyw0LDVdCiAgNDIudG9fYSgyKSAjPT4gWzEs
MCwxLDAsMSwwXQogIDIzLnRvX2EoNCw1KSAjPT4gWzAsMCwxLDEsM10KICAtMTAudG9fYSAjPT4g
Pz8/CgpUaGlzIHJlbGllcyBvbiB0b19zIGFuZCB0aGVyZWZvcmUgZG9lcyBub3Qgd29yayB3aXRo
IGEgYmFzZSA+IDM2LiAgSQpkb24ndCBrbm93IGhvdyB0aGlzIHNob3VsZCB3b3JkIGZvciBuZWdh
dGl2ZSBudW1iZXJzLgoKTWF5YmUgdGhlIGRlZmF1bHQgYmFzZSBzaG91bGQgYmUgMiB0byBiZSBj
b25zaXN0ZW50IHdpdGggRml4bnVtI1tdCgpUaG91Z2h0cz8KClZpZWxlIEdyw7zDn2UsCkxldmlu
Cg==
 
H

Harold Hausman

Happy hacking indeed!

Your code made playing with this problem a lot more fun:
http://mathschallenge.net/index.php?section=3Dproject&ref=3Dproblems&id=3D4

Regards,
-Harold

Christian Neukirchen said:
Also, you may want to extend it to be like APL's "encode" (tack), so yo= u
can do stuff like:

For fun, an implementation of encode/decode.


class Integer
def encode(restbase, *bases)
mybases =3D bases.dup
result =3D []
n =3D self

while base =3D mybases.pop || restbase
break if n < base
break if base.zero?
result << n % base
n =3D n / base
end

result << n

if (missing =3D bases.size - result.size + 1) > 0
result.concat [0] * missing
end

result.reverse!
end
end

class Array
def decode(restbase, *bases)
bases =3D bases.dup
result =3D 0
factor =3D 1

self.reverse_each { |a|
base =3D bases.pop || restbase
result +=3D a * factor
factor *=3D base
}

result
end
end

p 01776.encode(8) # =3D> [1, 0, 2, 2] (octal -> decimal)
p [1, 7, 7, 6].decode(8) # =3D> 1022

p 105246.encode(0, 1760, 3, 12) # =3D> [1, 1163, 1, 6]
# [miles, yards, feet, inches]

p [14, 12, 20, 57].decode(0, 24, 60, 60) # =3D> 1254057
# [days, hours, minutes, seconds]

duration =3D 324477
p "Runtime: %d days %02d:%02d:%02d" % duration.encode(0, 24, 60, 60)


Happy hacking,
 
L

Levin Alexander

------=_Part_18062_33073521.1139078580616
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: inline

T24gMi80LzA2LCBDaHJpc3RpYW4gTmV1a2lyY2hlbiA8Y2huZXVraXJjaGVuQGdtYWlsLmNvbT4g
d3JvdGU6Cj4gQ2hyaXN0aWFuIE5ldWtpcmNoZW4gPGNobmV1a2lyY2hlbkBnbWFpbC5jb20+IHdy
aXRlczoKPgo+ID4gQWxzbywgeW91IG1heSB3YW50IHRvIGV4dGVuZCBpdCB0byBiZSBsaWtlIEFQ
TCdzICJlbmNvZGUiICh0YWNrKSwgc28geW91Cj4gPiBjYW4gZG8gc3R1ZmYgbGlrZToKPgo+IEZv
ciBmdW4sIGFuIGltcGxlbWVudGF0aW9uIG9mIGVuY29kZS9kZWNvZGUuCgpJIHBsYXllZCB3aXRo
IHlvdXIgY29kZSBhIGJpdCBtb3JlLCByZXZlcnNlZCB0aGUgb3JkZXIgb2YgdGhlIGNyZWF0ZWQK
YXJyYXkgYW5kIGFkZGVkIHNvbWUgdGVzdCBjYXNlcy4gIFdoYXQgSSBoYXZlIG5vdyBpcyBhdHRh
Y2hlZC4KCklNSE8sIHNvbWV0aGluZyBsaWtlIHRoYXQgd291bGQgYmUgbmljZSB0byBoYXZlIGlu
IHRoZSBzdGFuZGFyZCBsaWJyYXJ5LgoKR3LDvMOfZSwKTGV2aW4K
------=_Part_18062_33073521.1139078580616
Content-Type: application/octet-stream; name=encode.rb
Content-Transfer-Encoding: 7bit
X-Attachment-Id: f_eja9vxnm
Content-Disposition: attachment; filename="encode.rb"


class Integer
def encode(restbase = 2, *bases)
mybases = bases.dup
n = self
result = []

begin
base = mybases.pop || restbase
base = n+1 if base < 2

n, val = n.divmod(base)
result << val

end until (n == 0) || (n == -1)

if (missing = bases.size - result.size + 1) > 0
result.concat [0] * missing
end

result
end
end

class Array
def decode(restbase=2, *bases)
bases = bases.dup
result = 0
factor = 1

self.each { |a|
base = bases.pop || restbase
result += a*factor
factor *= base
}
result
end
end


if __FILE__ == $0

require 'test/unit'

class TestEncodeDecode < Test::Unit::TestCase

def test_encode
assert_equal [0], 0.encode
assert_equal [1], 1.encode
assert_equal [123], 123.encode(0)

assert_equal [0,1,0,1], 10.encode
assert_equal [5,4,3,2,1], 12345.encode(10)
assert_equal [6,1,1163,1], 105246.encode(0,1760,3,12)
assert_equal [0,0,23,0], 82800.encode(0,24,60,60)

assert_equal [1], -1.encode(2) # ???
assert_equal [6,6,7,8], -1234.encode(10) # ???

assert_equal [456], 456.encode(1) # invalid base
end

def test_decode
assert_equal 0, [0].decode
assert_equal 1, [1].decode

assert_equal 12345, [5,4,3,2,1].decode(10)

assert_equal 82800, [0,0,23,0].decode(0,24,60,60)
assert_equal 105246, [6,1,1163,1].decode(0,1760,3,12)
end

end

end


------=_Part_18062_33073521.1139078580616--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

to_a 10
to_a 13
check Fixnum 2
Why Fixnum===Fixnum is false? 5
strange result from to_a 4
"".to_a isn't consistent with "string".to_a 3
fixnum problem plz help 8
Integer/Fixnum/Bignum are "immutable"? 4

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top