首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提高itertools.permutations性能

提高itertools.permutations性能
EN

Stack Overflow用户
提问于 2020-05-11 07:01:33
回答 2查看 190关注 0票数 1

我正在完成一个问题,其中我创建了一个函数,该函数接受一个正整数,并返回下一个更大的数字,该数字可以通过重新排列其数字来形成。例如: 12 --> 21513 --> 531,12435 --> 12453,9817121211 --> 9817122111。

我一次又一次地重新编译我的代码,以提高性能,但最终还是停了下来,我不能再快了。有人有什么建议吗?大部分时间都花在itertools.permutations线路上。

代码语言:javascript
复制
def next_bigger(n):
    num = str(n)
    num1 = set(int(x) for x in str(num))
    if num == num[0] *len(num):
        return -1
    #full_set = set(num)
    lis = set(int(''.join(nums)) for nums in itertools.permutations(num, len(num)))   
    lis = sorted(lis)
    try:
        return int(lis[lis.index(n)+1])
    except Exception:
        return -1

链接到问题:https://www.codewars.com/kata/55983863da40caa2c900004e/train/python

EN

回答 2

Stack Overflow用户

发布于 2020-05-11 09:01:22

如果你正在寻找更好的性能“时间复杂度”,方法将是找到算法的“关键”。在这种情况下,你应该问问自己,创建下一个更大的数字意味着什么?答案很简单,只需在两个相邻数字之间进行交换即可。代码应该是这样的。

代码语言:javascript
复制
def next_bigger(n):
    num_string = list(str(n))
    for i in range(1, len(num_string)):
        if i == len(num_string):
            return -1

        #find two the two numbers one bigger than the other with the minimun order
        if num_string[-i] > num_string[-i-1]:

            compare_reference = num_string[-i]
            index_reference = -i

            #check if the current number is smaller than any of the tail 
            for k, current in enumerate(num_string[-i:]):
                if num_string[-i-1] < current and current < compare_reference:
                    compare_reference = current
                    index_reference = -i+k

            #interchange the locations:
            num_string[index_reference] = num_string[-i-1]
            num_string[-i-1] = compare_reference

            #check if the tail is larger than one digit
            if i > 1:
                #order the rest of the vector to create the smaller number (ordering it).
                lower_part_ordered = sort_ascendant(num_string[-i:])
            else:
                lower_part_ordered = [num_string[-i]]


            # create a string from the list
            return int("".join(num_string[:-i] + lower_part_ordered))        

    # no match found means a number like 65311
    return -1
票数 1
EN

Stack Overflow用户

发布于 2020-05-11 19:37:22

虽然这不是提高置换函数本身性能的方法,但这是我发现的提高代码性能的方法。非常感谢所有提供帮助的人!

代码语言:javascript
复制
def next_bigger(n):
    num_string = list(str(n))
    a = []
    for i in range(1, len(num_string)):
        if i == len(num_string):
            return -1
        p = int(num_string[-i])
        q = int (num_string[-(i+1)])
        if p > q:
            a.append(num_string[:-(i+1)])
            lis = list(num_string[-(i+1):])
        if len(lis) > 1:
            lis2 = list(set(lis))
            lis2.sort()
            qindex = lis2.index(str(q))
            first = lis2[qindex+1]
            a[0].append(first)
            lis.remove(first)
            lis.sort()
        for j in range (len(lis)):
            a[0].append(lis[j])
        return int("".join(a[0]))
    return -1
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61719835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档