将pental转换为十进制,十进制转换为pental,添加基数为5的数字,并使用def函数将基数为5的数字相乘

问题描述 投票:0回答:1

如何将基数为5的数字转换为十进制数,十进制数转换为基数为5的数字,添加基数为5的数字,并使用def函数将基数为5的数字相乘:

  • gumatj_to_decimal(a),将Gumatj数转换为十进制数
  • decimal_to_gumatj(a),将十进制数转换为Gumatj
  • gumatj_add(a, b),增加2个Gumatj数字
  • qazxsw poi,乘以2个Gumatj数字
gumatj_multiply(a, b)

样本I / O:

choice = input ("Choose test:\n")
action = choice[:1]
print ("calling function")

if action == 'g' or action == 'd':

    num = int(choice[2:])

    if action == 'g':
      answer = gumatj.gumatj_to_decimal (num)
    else:
      answer = gumatj.decimal_to_gumatj (num)

 elif action == 'a' or action == 'm':

    num1, num2 = map (int, choice[2:].split(" "))

    if action == 'a':
      answer = gumatj.gumatj_add (num1, num2)
    else:
      answer = gumatj.gumatj_multiply (num1, num2)

 print ("called function")
 print (answer)

样本I / O:

Choose test: 
d 12 
calling function 
called function 
22 

样本I / O:

Choose test: 
g 22 
calling function 
called function 
12 

样本I / O:

Choose test:
a 12 14 
calling function 
called function 
31 
python-3.x algorithm
1个回答
0
投票

试试这段代码:

Choose test: 
m 3 4 
calling function 
called function 
22

示例输出:

import numpy


def gumatj_to_decimal(num):
    return int(str(num), 5)


def decimal_to_gumatj(num):
    return numpy.base_repr(num, 5)


def gumatj_add(num1, num2):
    return decimal_to_gumatj(gumatj_to_decimal(num1) + gumatj_to_decimal(num2))


def gumatj_multiply(num1, num2):
    return decimal_to_gumatj(gumatj_to_decimal(num1) * gumatj_to_decimal(num2))


choice = input("Choose test:\n")
action = choice[:1]
print("calling function")

if action == 'g' or action == 'd':

    num = int(choice[2:])

    if action == 'g':
        answer = gumatj_to_decimal(num)
    else:
        answer = decimal_to_gumatj(num)

elif action == 'a' or action == 'm':

    num1, num2 = map(int, choice[2:].split(" "))

    if action == 'a':
        answer = gumatj_add(num1, num2)
    else:
        answer = gumatj_multiply(num1, num2)

print("called function")
print(answer)

说明:

  • 将基数5转换为十进制:

Python Choose test: d 12 calling function called function 22 Choose test: g 22 calling function called function 12 Choose test: a 12 14 calling function called function 31 Choose test: m 3 4 calling function called function 22 可以将任何基数的字符串转换为整数:

int
  • 将十进制数转换为基数5:

Numpy的def gumatj_to_decimal(num): return int(str(num), 5) 可以做到这一点:

base_repr
  • 添加两个基数为5的数字:

首先将它们转换为十进制,添加它们并将它们转换回基数5:

def decimal_to_gumatj(num):
    return numpy.base_repr(num, 5)
  • 乘以两个基数为5的数字:

首先将它们转换为十进制,乘以,将它们转换回基数5:

def gumatj_add(num1, num2):
    return decimal_to_gumatj(gumatj_to_decimal(num1) + gumatj_to_decimal(num2))
© www.soinside.com 2019 - 2024. All rights reserved.