简体   繁体   English

在 python 中添加字符串(作为基数 12 数的表示)和积分器/其他字符串

[英]Adding strings (as representation for base 12 numbers) and integrers/ other strings in python

I need to create a calculator that can add numbers in base 12 and with different limits at the differents diggits.我需要创建一个计算器,它可以添加以 12 为基数的数字,并且在不同的数字上有不同的限制。

Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B"] Base 12 序列:[0,1,2,3,4,5,6,7,8,9,"A","B"]

The limits must be:限制必须是:

  • First digit: limit "B"第一个数字:限制“B”
  • Second digit: limit 4第二个数字:限制 4

That means you would count like this:[1,2,3,4,5,6,7,8,9,A,B,10,11,...48,49,4A,4B]这意味着你会这样数:[1,2,3,4,5,6,7,8,9,A,B,10,11,...48,49,4A,4B]

but I don´t know how could I make it so that I can sum 2 numbers但我不知道我怎么能做到这样我就可以总结 2 个数字

I have the following code in python:我在python中有以下代码:

list1=[0,1,2,3,4,5,6,7,8,9,"A","B"]
list2=[0,1,2,3,4]
list3=[0,1,2,3,4,5,6,7,8,9,"A","B"]
list4=[0,1,2,3,4]


def calculadora (entrada1, operacion, entrada2):
    #parte de valor 1:
    digito1_1=str(list2[int(entrada1//12)])
    digito1_2=str(list1[int(entrada1%12)])
    valor1=float(digito1_1+digito1_2)
    #parte de valor 2
    digito2_1=str(list2[int(entrada2//12)])
    digito2_2=str(list1[int(entrada2%12)])
    valor2=float(digito2_1+digito2_2)
    if operacion==str("suma") or "+":
        return float(valor1+valor2)
entrada1 = float(input("inserte primer valor"))
operacion=str(input("inserte operación"))
entrada2 = float(input("inserte segundo valor"))
print (calculadora(entrada1,operacion,entrada2))

It works for numbers but wenn I want to sum numbers like 3B, it gives me a ValueError as it is coded as string.它适用于数字,但我想对像 3B 这样的数字求和,它给了我一个 ValueError,因为它被编码为字符串。

Could someone help me or say me how could I do it to sum such numbers please?有人可以帮助我或告诉我我怎么能把这些数字相加吗?

The easiest way to convert a base-12 encoded string to int is via int(string, 12) .将 base-12 编码字符串转换为 int 的最简单方法是通过int(string, 12) The reverse is not quite as easy, because Python doesn't seem to have a built-in way to do that.反过来就不是那么容易了,因为 Python 似乎没有内置的方法来做到这一点。 You can use format specifiers for binary, octal, and hex, but not arbitrary bases.您可以将格式说明符用于二进制、八进制和十六进制,但不能用于任意基数。

You can get a reversed list of digits using divmod() , which does division with remainder.您可以使用divmod()获得反转的数字列表,该列表使用余数进行除法。

def to_digits(x, base):
     while x > 0:
         x, rem = divmod(x, base)
         yield rem

But to round-trip, we want a string.但是为了往返,我们需要一个字符串。 Convert the int to a character (using a string as a lookup table), and join them into a string, then use a negatively-stepped slice to reverse it.将 int 转换为字符(使用字符串作为查找表),并将它们连接成一个字符串,然后使用负步进切片来反转它。

def to_base_12(num):
    return ''.join('0123456789AB'[d] for d in to_digits(num, 12))[::-1]

With a longer lookup table, you could generalize this into higher bases.使用更长的查找表,您可以将其概括为更高的基数。

Strings are already sequences, but if you want to convert that back to a list, you can just call list() on it.字符串已经是序列,但是如果你想把它转换回一个列表,你可以调用list()就可以了。 The inverse is that ''.join() method you just saw.相反的是你刚刚看到的''.join()方法。

Now that you can convert your base-12 representation to Python int objects and back, you can simply add them with + .现在您可以将 base-12 表示转换为 Python int 对象并返回,您只需使用+添加它们即可。

Here is slight variation on the excellent answer from gilch that handles negative numbers and zero.这是gilch处理负数和零的优秀答案的细微变化。

def convert_to_string(num, base):
    '''
    Convert integer (num) to base less than base 37.
    '''
    alpha = string.digits + string.ascii_lowercase

    assert isinstance(num, int)
    assert isinstance(base, int)
    assert 1 < base <= len(alpha)
    if num == 0:
        return '0'

    def to_digits(num, base, alpha):
        '''
        Generator to convert digits in reverse order.
        '''
        while num > 0:
            num, rem = divmod(num, base)
            yield alpha[rem]

    sign = ''
    if num < 0:
        num, sign = -num, '-'
    return sign + ''.join(d for d in reversed(tuple(to_digits(num, base, alpha))))


def convert_from_string(num, base):
    '''
    Convert string in base X to integer.
    '''
    return int(str(num), base)


def test():
    '''
    Test conversions.
    '''
    assert convert_to_string(0, 2) == '0'
    assert convert_to_string(4, 2) == '100'
    assert convert_to_string(23, 12) == '1b'
    assert convert_to_string(-6, 2) == '-110'
    assert convert_from_string('1b', 12) == 23
    assert convert_from_string('-110', 2) == -6

You can add them directly if you convert the A and B to 11 and 12. Then as each number is now a list of digits one can add them the same way that an ALU does.如果将 A 和 B 转换为 11 和 12,则可以直接添加它们。然后,由于每个数字现在都是数字列表,因此可以像 ALU 一样添加它们。 See the section on addition of integers in any text on computer arithmetic.请参阅有关计算机算术的任何文本中有关整数加法的部分。

def add(A, B):

    result = []
    carry = 0
    for a, b in reversed(zip(A,B)):
        carry, digit = divmod(a + b + carry, 12)
        result.append(digit)

    if carry:
        result.append(carry)

    result.reverse()
    return result


>>> add([4,3],[6,11])
[11, 2]
>>> add([5,3],[6,11])
[1, 0, 2]

The lists are MSD first.这些列表首先是 MSD。 The double reversing is not needed if the lists were in LSD first.如果列表首先在 LSD 中,则不需要双重反转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM