Reputation: 11
I want to increment an integer in base 6 format but += 1
increments in base 10. Is there any way to change the base in which the operator increments?
val = 5
val += 1
print val
I would like to have val
be 10
.
In the end I would like to put it in a routine which outputs base 6 numbers from 0
to 215
, each on a new line. Incrementing by 1
looked like the easiest approach to me. I also thought about using a regular expression to skip all numbers which contain 6
or higher, but figured that would not be very efficient code.
Upvotes: 1
Views: 1928
Reputation: 55499
Here's an integer to base n string function, and a generator function that counts in base n.
#!/usr/bin/env python
''' base `n` conversion and counting
See http://stackoverflow.com/q/33610302/4014959
Written by PM 2Ring 2015.11.10
'''
def int_to_base_n(i, base):
''' Convert integer `i` to base `base`
base must be <= 10
'''
digits = []
while i:
i, d = divmod(i, base)
digits.append(str(d))
if not digits:
return '0'
return ''.join(digits[::-1])
def base_n_counter(base):
''' An infinite iterator that counts in base `base`
base must be <= 10
'''
digits = [0]
yield '0'
while True:
digits[0] += 1
pos = 0
while digits[pos] == base:
digits[pos] = 0
pos += 1
if pos >= len(digits):
digits.append(1)
else:
digits[pos] += 1
yield ''.join([str(d) for d in reversed(digits)])
# Test
base = 2
counter = base_n_counter(base)
print 'base', base
for i in range(16):
print i, int_to_base_n(i, base), next(counter)
print
base = 6
print 'base', base
for i, s in enumerate(base_n_counter(base)):
print i, int_to_base_n(i, base), s
if i == base ** 2:
break
output
base 2
0 0 0
1 1 1
2 10 10
3 11 11
4 100 100
5 101 101
6 110 110
7 111 111
8 1000 1000
9 1001 1001
10 1010 1010
11 1011 1011
12 1100 1100
13 1101 1101
14 1110 1110
15 1111 1111
base 6
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 10 10
7 11 11
8 12 12
9 13 13
10 14 14
11 15 15
12 20 20
13 21 21
14 22 22
15 23 23
16 24 24
17 25 25
18 30 30
19 31 31
20 32 32
21 33 33
22 34 34
23 35 35
24 40 40
25 41 41
26 42 42
27 43 43
28 44 44
29 45 45
30 50 50
31 51 51
32 52 52
33 53 53
34 54 54
35 55 55
36 100 100
Both of these functions only work correctly if base <= 10
. However, it's easy to modify them to handle larger bases: replace the str(d)
with digit_string[d]
, where digit_string
is a string containing the desired digits.
Upvotes: 0
Reputation: 122136
Just getting all valid numbers in base 6 with up to three digits is trivial, using itertools.product
:
>>> import itertools
>>> digits = '012345'
>>> for num in itertools.product(digits, repeat=3):
s = ''.join(num)
print s, int(s, 6)
000 0
001 1
002 2
...
553 213
554 214
555 215
int
can be used with the optional base parameter to convert a given string representing a number in base 6 back to decimal form:
>>> int('553', 6)
213
All that leaves for you to write is a function to take an integer and convert it to a string in base 6 representation; Python doesn't have this built in, as it does for e.g. hexadecimal:
>>> hex(213)
'0xd5'
Note that "increment an integer in base 6 format" or "[changing] the base in which the operator increments" makes no sense conceptually - base 6, like binary (base 2), decimal (base 10), octal (base 8), etc. is just a way of representing a number. Python always displays integers in decimal form by default, although they will be stored in your computer's memory in binary form. += 1
is adding one whatever base you're working in.
Upvotes: 1
Reputation: 11
def base(decimal ,base) :
list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
answer = ""
while decimal != 0 :
answer += list[decimal % base]
decimal /= base
return answer[::-1]
Upvotes: -1