Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count upward in python with variable base

Tags:

python

range

base

I would like to know how to do an equivalent of the range function in python, but with the ability to specify the base number. For example:

countUp(start=0, end=1010, base=2)
countUp(start=0, end=101, base=3)
countUp(start=0, end=22, base=4)

Example output for base 2 counting:

[0, 1, 10, 11, 100, ...]

Is there a function I'm missing that does this? Or what could I do instead?

like image 711
Spooky Avatar asked Dec 01 '15 21:12

Spooky


People also ask

How do you count up to a number in Python?

The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.

How do you count the value of a variable in Python?

Count() is a Python built-in function that returns the number of times an object appears in a list. The count() method is one of Python's built-in functions. It returns the number of times a given value occurs in a string or a list, as the name implies.

What does count () do in Python?

The count() method returns the number of elements with the specified value.

How do you count the number of outcomes in Python?

The count() function of the python is used to count the frequency of the character or a substring in a string. It simply gives the count of occurrences as a return value.


2 Answers

You are apparently confusing numbers with the representation of numbers.

A number does not have a base... it's the number representation that has a base... for example the number represented as "101" in base 2 is the same as the number represented with "5" in base 10.

The range function will count successive numbers, and you can get their representation in any base you like with something like:

digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def int2str(x, base):
    if x < 0:
        return "-" + int2str(-x, base)
    return ("" if x < base else int2str(x//base, base)) + digits[x % base]
like image 178
6502 Avatar answered Oct 15 '22 00:10

6502


You can do it with a custom iterator:

I took the iterater code from here and the base conversion from here

import string
class BaseRange:
    def __init__(self, low, high, base):
        digs = string.digits + string.letters
        self.current = low
        self.high = high
        self.base = base
    def __iter__(self):
        return self
    def next(self):  # Python 3 requires this to be __next__
        if self.current > self.high:
            raise StopIteration
        else:
            self.current += 1
            return self.int2base(self.current - 1, self.base)
    def int2base(self, x, base):
        if x < 0: sign = -1
        elif x == 0: return digs[0]
        else: sign = 1
        x *= sign
        digits = []
        while x:
            digits.append(digs[x % base])
            x /= base
        if sign < 0:
            digits.append('-')
            digits.reverse()
        return ''.join(digits)

A Few Example runs produces:

>>> for c in BaseRange(0, 10, 2):
    print(c)


0
1
01
11
001
101
011
111
0001
1001
0101
>>> for c in BaseRange(0, 10, 3):
    print(c)


0
1
2
01
11
21
02
12
22
001
101
like image 45
Brian Cain Avatar answered Oct 15 '22 00:10

Brian Cain