Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # t_zbase_n_demo4all.py
- def zbase(n, b=3,length=3):
- t = b**(length)
- if n < t:
- n=n+t
- if n == 0:
- return [0]
- values = []
- for z in 'Z'*length:
- r = n % b
- n /= b
- values.append(r)
- return values[::-1]
- 0
- ttt = 'abcdefghijklmnopqrstuvwxyz'[:4]
- def base2index(base, target, length):
- result = 0
- baseLen = len(base)
- for i, value in enumerate(target):
- result += base.index(value) * baseLen**(length - 1 - i)
- return result
- def index2base(base, target, length):
- result = zbase(target, len(base), length)
- if result:
- return ''.join([base[i] for i in result])
- i = 0
- while 1:
- t = index2base(ttt, i, 4)
- if not t:
- break
- print 'from '+str(i).zfill(3), 'to', [t], 'converted back to', base2index(ttt, t, 4)
- print
- i += 1
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement