6
1
Create a function 'baseXtoY' which:
converts an integer or string n
, which is a base x
number (hexadecimal, binary, etc)
into a base y
number, reliably, including numbers like 10e50
or higher, using the least bytes possible, and using the following constraints:
- Provide built-in support for bases up to 64 using the following sequence: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=/
- Provide support for bases higher than 64 by allowing a different sequence to be used in the input
- Provide support for multi-byte sequences eg ('aa','bb','cc'...) in cases rally high value bases are needed (could also support unicode instead)
- You cannot have a base less than 2. Show an error
- You cannot use any built-in functions that convert base to base for you
- You need not worry about floating point
parameters should be:
n
- any number, as a stringx
- the base ofn
, integery
- the base of the return values1
- the sequence the given number's base usess2
- the sequence the desired base usesb
- the bytesize of the digits ins1
- return value should be a string
example inputs:
using '88888'
base 10 to 2: baseXtoY('88888',10,2) >>> '10101101100111000'
base 2 to 5: baseXtoY('10101101100111000',2,5) >>> '10321023'
base 5 to 32: baseXtoY('10321023',5,32) >>> '2mpo'
base 32 to 10, using a new sequence, s=the above 64bit sequence, a=the alphabet: baseXtoY('2mpo',32,10,s,a) >>> 'hhhhh'
base 5 to 10, input base uses more than 1 byte per digit: baseXtoY('bbaaddccbbaaccdd',5,10,('aa','bb','cc','dd','ee'),'0123456789',2))
> '88888'`
you do not have to inlude the character length of the testing or input code, just the function(s).
edit: I've given up trying to shorten my Python code lower than the answers, so I'll just show what I have. 585 characters python 2.7
r=range
def b1(n,x,s):
if n==0:return s[0]
if x<=1:raise
l=[];p=n;i=0
while 1:
y=x**i;l.append(y)
if y>p:break
else:i+=1
l.reverse();f=[]
for p in l:h=int(n//p);f.append(h);n-=h*p
while f[0]==0:f.pop(0)
q=''
for p in r(len(f)):q+=s[f[p]]
return q
def b2(n,x,s,b):
n=list(str(n));l=[]
while n:
a=''
for p in r(b):a+=n.pop(0)
l.append(a)
m=0;q=0
while l:c=s.index(l.pop());q+=c*(x**m);m+=1
return q
s=''.join(map(chr,r(48,58)+r(97,123)+r(65,91)))+'=/'
def baseXtoY(n,x=10,y=2,s1=s,s2=s,b=1):return n if x==y else b2(n,x,s1,b)if y==10 else b1(b2(n,x,s1,b),y,s2)
test cases:
32 10 5 >>> 112
112 5 2 >>> 100000
100000 2 10 >>> 32
88888 10 5 0123456789 ('aa', 'bb', 'cc', 'dd', 'ee') 1 >>> bbaaddccbbaaccdd
bbaaddccbbaaccdd 5 10 ('aa', 'bb', 'cc', 'dd', 'ee') 0123456789 2 >>> 88888
@Mego: This should not be a dupe. Also, regardless of quality, this question was here first (making the newer one a dupe of this one). Personally, I think these questions are dissimilar enough to be left alone.
– Zach Gates – 2016-01-12T15:23:23.673@ZachGates There is no clear consensus on that post. This much more recent answer of mine has very strong support for closing old challenges as dupes of new ones.
– Martin Ender – 2016-01-12T15:42:05.397The defaulting makes no sense, and restricts several languages. – Pubby – 2011-12-29T06:05:09.043
Can you elaborate more on what is meant by multi-byte sequences (aa, bb, ..)? – captncraig – 2011-12-29T06:12:46.830
@Pubby: perhaps it could be optional? – Blazer – 2011-12-29T07:13:14.437
@CMP: incase it was impossible for the desired output to be represented by single-byte digits – Blazer – 2011-12-29T07:16:04.043
Also allowing string and int as first argument rules out several languages. May we change this to string-only? – Howard – 2011-12-29T09:04:38.863
@howard: acceptable. you could also write more than one function if you did desire both int and string compatability, but since this is for shortest code, that's not really needed – Blazer – 2011-12-29T09:52:11.330
I don't understand the "more than one byte" bit. We're in the 21st century: ignoring Unicode and equating bytes with characters is prehistoric. – Peter Taylor – 2011-12-29T21:44:40.900
Can we assume that the bases will always be integers? For example, does the program have to be able to handle conversion from base 6 to base 3.14? – Peter Olson – 2011-12-30T07:34:47.357
The second last test case has to be '...aaaccdd' instead of '...aaddcc'. – user unknown – 2011-12-30T18:08:18.640
updated the challenge, multi-byte is no longer needed, bases should be integers – Blazer – 2011-12-31T02:07:37.717
2Please do not change the task after some people already have answered your question. It is fine to clarify some points or define some corner cases but not such a fundamental change as you did. E.g. the removal of multi-byte would mean a complete rewrite of my code since many things were done with this feature in mind (and I think this is also true for user unknown). – Howard – 2011-12-31T08:09:27.130
This is also true for user unknown, yes. If you have a new quizzle in mind, open a new one, and prepare it in the chat or in the sandbox- http://meta.codegolf.stackexchange.com/q/423/373 -meta-thread, to handle questions beforehand. Thank you. Clarify the meaning of questions, but don't change them. So long -1 from me.
– user unknown – 2011-12-31T12:39:43.213ack.. I'm new to this. Ineed to be more acquainted with the rules. sorry guys. I'll re-update the qustion to fit the answers already given – Blazer – 2011-12-31T21:19:51.043
So I revoke my downvote and turn it into an upvote. – user unknown – 2012-01-01T22:59:58.417
I've added my own attempt.. I'm never going to beat 128 characters :P. I think it's been long enough to choose the winner – Blazer – 2012-01-02T01:56:39.917