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

Python 如何对数字的数字重新排序并插入数字5以获得最大可能的绝对值,python,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.:

请告知我如何对数字进行重新排序,并在结果中添加数字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))
def溶液(N):
a=[]#数字列表,例如int(123)
而N!=0:
v=N%10#最后一位为div余数,例如:123%10=3
N=int(N/10)#使用整数除法删除最后一个数字:123/10=12.3;int(12.3)=12
a=[v]+a#连接两个列表:新创建的带有一个元素的列表(v=3)和列表a
#因此,数字将按自然顺序=>[1,2,3]
如果len(a)==0:#需要创建包含一个元素[0]的列表,作为忽略0之前的循环
a=[0]
插入=错误
对于范围(0,len(a))内的i:i=0,1,2;len=3
如果a[i]<5:
#a[从:到排他性]例如:[1,2,3][0:2]=>[1,2]。1的索引为0,2的索引为1,不包括索引2
a=a[0:i]+[5]+a[i:]
插入=真
打破
如果未插入:
a=a+[5]
N=0#将数字从数字、数字列表重建为整数
对于范围(0,len(a))中的i:
N=N*10+a[i]#0+1;1 * 10 + 2; 12 * 10 + 3 = 123
返回N
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(“解决方案:,解决方案(0))

在这里,我通过使用一些内置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(9672)
97652

下面这句话会起作用吗

x=-34278
无需插入=5
res=int(“”.join(排序(list(str(abs(x)))+[str(no-to-insert)],reverse=True)))

如果x如果您需要插入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))
def溶液(N):
否定=错误
如果N<0:
负=真
N=N*-1#等于N 5):
a=a[0:i]+[5]+a[i:]
插入=真
打破
如果未插入:
a=a+[5]
N=0
对于范围(0,len(a))中的i:
N=N*10+a[i]
如果没有:
N=N*-1
返回N
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(“溶液:,溶液(N))
Java解决方案

 public int solution(int N) {
            int digit = 5;
            if (N == 0) return digit * 10;
            int neg = N/Math.abs(N);
            N = Math.abs(N);
            int n = N;
            int ctr = 0;
            while (n > 0){
                ctr++;
                n = n / 10;
            }
            int pos = 1;
            int maxVal = Integer.MIN_VALUE;
            for (int i=0;i<=ctr;i++){
                int newVal = ((N/pos) * (pos*10)) + (digit*pos) + (N%pos);
                if (newVal * neg > maxVal){
                    maxVal = newVal*neg;
                }
                pos = pos * 10;
            }
            return maxVal;
        }
public int解决方案(int N){
整数位数=5;
如果(N==0)返回数字*10;
int neg=N/Math.abs(N);
N=数学abs(N);
int n=n;
int ctr=0;
而(n>0){
ctr++;
n=n/10;
}
int pos=1;
int maxVal=整数.MIN_值;
对于(int i=0;i maxVal){
maxVal=newVal*neg;
}
pos=pos*10;
}
返回maxVal;
}

谢谢@AVI AGARWAL,如果在正数和负数的情况下,我们只需要在输入数的末尾加上5(而不是最大值),该怎么办?欢迎。如果需要插入5并使输出数成为负数和正数的最大值,请在给定代码中删除a.sort()和a.reverse()(不是替换/转换当前的一组数字),那么这可能是一个解决方案:`用java解决这个问题:-
 public int solution(int N) {
            int digit = 5;
            if (N == 0) return digit * 10;
            int neg = N/Math.abs(N);
            N = Math.abs(N);
            int n = N;
            int ctr = 0;
            while (n > 0){
                ctr++;
                n = n / 10;
            }
            int pos = 1;
            int maxVal = Integer.MIN_VALUE;
            for (int i=0;i<=ctr;i++){
                int newVal = ((N/pos) * (pos*10)) + (digit*pos) + (N%pos);
                if (newVal * neg > maxVal){
                    maxVal = newVal*neg;
                }
                pos = pos * 10;
            }
            return maxVal;
        }