Step Number from 10 to N using python

后端 未结 4 1655
春和景丽
春和景丽 2021-01-21 18:04

The Program must accept an integer N. The program must print all the stepping number from 10 to N, if there is no such number present the program should print -1 as

4条回答
  •  余生分开走
    2021-01-21 18:29

    from itertools import product
    from itertools import accumulate
    import time
    
    def main(n):
        result=set()
        for length in range(2,7):
            dxss=list(product([-1,1],repeat=length-1))
            for initial in range(1,10):
                for dxs in dxss:
                    ans=list(accumulate(dxs,initial=initial))
                    if all([(y in range(0,10)) for y in ans]):
                         result.add(int("".join(map(str,ans))))
        sorted_lst=sorted(result)
        return [x for x in sorted_lst if x

    time=0.0020003318786621094[s]

    [10,12,21,...989898]

    Step number definition is "(nth digit - n-1th digit)= 1 or -1".

    N is 10 < N < 10**7.Therefore, I must decide 1st number(1or2or..9) and 6 dx constructed by 1 or -1.

提交回复
热议问题