如何对数字的数字重新排序并插入数字5以获得最大可能的绝对值

2024-04-20 11:45:31 发布

您现在位置:Python中文网/ 问答频道 /正文

请告知我如何对数字进行重新排序,并在结果中添加数字5,以使其绝对值最高

例如,如果输入为578,则预期结果为8755。否则,如果输入为负-483,则输出预期为-8543

我已经设法使其仅适用于正数,但是,我还需要使其适用于负数:

def solution(N):
    a = [] # list of digits, e.g. int(123)
    while N != 0:
        v = N % 10  # last digit as div remainder, e.g.: 123 % 10 = 3
        N = int(N / 10)  # remove last digit using integer division: 123 / 10 = 12.3; int(12.3) = 12
        a = [v] + a  # concatenate two lists: newly created list with one element (v = 3) and list a 
# as a result the digits will be in natural order => [1,2,3]
    if len(a) == 0:   # need to create list with one element [0] as the cycle before ignores 0
        a = [0]
    inserted = False
    for i in range(0, len(a)):   # i = 0, 1, 2; len = 3
        if a[i] < 5:
            # a[from:to exclusive] e.g.: [1, 2, 3][0:2] => [1, 2]. index of 1 is 0, index of 2 is 1, index 2 is excluded
            a = a[0:i] + [5] + a[i:]
            inserted = True
            break
    if not inserted:
        a = a + [5]
    N = 0  # reconstruct number from digits, list of digits to int
    for i in range(0, len(a)):
        N = N * 10 + a[i]  # 0 + 1; 1 * 10 + 2; 12 * 10 + 3 = 123
    return N 

if __name__ == ‘__main__’:
    print(“Solution:”, solution(0))

Tags: oftoinindexlenifisas
3条回答

下面这句话会起作用吗

x=-34278
no_to_insert=5
res=int(''.join(sorted(list(str(abs(x)))+[str(no_to_insert)], reverse=True)))
if x<0:
    res=-res

输出:

-875432

在这里,我通过使用一些内置python方法进行了一些重大更改:

def solution(N):

    sign = False   #to determine the sign of N (positive or negative )
    if N < 0:
       sign  = True
       N= N * -1   # as N<0 we make it positive

    a = []
    while N != 0:
        v = N % 10
        N = int(N / 10)
        a = [v] + a

    a.append(5)  # in built method to add an element at the end of the list 
    a.sort()     # in built method to sort the list (ascending order)
    a.reverse()  # in build method to reverse the order of list (make it descending order)

    N = 0
    for i in range(0, len(a)):
        N = N * 10 + a[i]

    if sign:    # convert negative integers back to negative 
       N = N * -1 

    return N

样本输出:

负片

solution(-2859)
-98552

肯定的

solution(9672)
97652

如果需要插入5并使输出数成为负数和正数的最大数(并且不具备不替换或转换输入数字集的条件),则这可能是一种解决方案:

def solution(N):

    negative = False

    if N < 0:
        negative = True
        N = N * -1   # as N<0 we make it positive

    a = []
    while N != 0:
        v = N % 10
        N = int(N / 10)
        a = [v] + a

    if len(a) == 0:
        a = [0]

    inserted = False
    for i in range(0, len(a)):
        if (not negative and a[i] < 5) or (negative and a[i] > 5):
            a = a[0:i] + [5] + a [i:]
            inserted = True
            break

    if not inserted:
        a = a + [5]

    N = 0
    for i in range(0, len(a)):
        N = N * 10 + a[i]

    if negative:
       N = N * -1

    return N

if __name__ == '__main__':
    print("Solution:", solution(N))

相关问题 更多 >