[英]Generate numbers with strictly decreasing digits
I am trying to generate numbers of maximum length n whose all digits are strictly decreasing.我正在尝试生成所有数字都严格减少的最大长度 n 的数字。
If n = 5
, then numbers would be :如果
n = 5
,那么数字将是:
54321
5432
543
54
5421
542
541
5321
532
53
521
52
51
5
4321
432
43
431
421
42
41
4
...
Any hints on how to proceed?有关如何进行的任何提示?
Thank you in advance先感谢您
Edit - My current code looks like this and I am not getting all the results.编辑 - 我当前的代码看起来像这样,我没有得到所有的结果。
def func(arr):
for x in arr:
token=x[-1]
k=int(token)-1
while k>1:
for i in range(1,k):
f.write(x)
for j in range(k,i,-1):
f.write(str(j))
k-=1
f = open ("demo.txt", "w")
arr = ["987"]
for i in range(986,321,-1):
tens = i
units = i%10
tens = int(tens)/10
hundreds = int(tens)/10
tens%=10
hundreds%=10
if tens >= hundreds or units>= tens:
continue
arr.append(str(i))
func(arr)
f.close()
Here is a recursive generator function:这是一个递归生成器函数:
def nums(n):
if n:
yield from (int(f"{n}{num}") for num in nums(n-1))
yield n
yield from nums(n-1)
[*nums(1)]
# [1]
[*nums(2)]
# [21, 2, 1]
[*nums(3)]
# [321, 32, 31, 3, 21, 2, 1]
[*nums(4)]
# [4321, 432, 431, 43, 421, 42, 41, 4, 321, 32, 31, 3, 21, 2, 1]
Or using the itertools.combinations
approach:或者使用
itertools.combinations
方法:
from itertools import combinations
def nums(n):
for l in range(n, 0, -1):
for c in combinations(range(n, 0, -1), l):
yield int("".join(map(str, c)))
[*nums(2)]
# [21, 2, 1]
[*nums(3)]
# [321, 32, 31, 21, 3, 2, 1]
[*nums(4)]
# [4321, 432, 431, 421, 321, 43, 42, 41, 32, 31, 21, 4, 3, 2, 1]
all digits are strictly decreasing.
所有数字都严格递减。
this code generate descending digits series with custom lenght:此代码生成具有自定义长度的降序数字系列:
import itertools
def generate(lenght):
arr = [range(10)] * lenght
for digits_tuple in itertools.product(*arr):
if all(current_item > next_item for current_item, next_item in zip(digits_tuple, digits_tuple[1:])):
print(digits_tuple)
generate(5)
If you want the length to be 5 or less:如果您希望长度为 5 或更小:
for i in range(5):
generate(i+1)
The question is a bit vague, but you can change this part of code to get your goal:这个问题有点含糊,但您可以更改这部分代码来实现您的目标:
all(current_item > next_item for current_item, next_item in zip(digits_tuple, digits_tuple[1:])):
It is actually not complicated, you can generate combinations of numbers with itertools.combinations
, then apply a factor with zip
and take the product per item and sum
.它实际上并不复杂,您可以使用
itertools.combinations
生成数字组合,然后使用zip
应用一个因子并获取每个项目的乘积和sum
。
For example [1,2,4,5] with factors [1,10,100,1000] would give 1+20+400+5000 = 5421.例如 [1,2,4,5] 与因子 [1,10,100,1000] 将给出 1+20+400+5000 = 5421。
The trick is that combinations
always produces the combinations ina defined order (first to last), so combinations([1,2,3], 2)
would give [(1, 2), (1, 3), (2, 3)]
诀窍是
combinations
总是按定义的顺序(从头到尾)产生组合,因此combinations([1,2,3], 2)
将给出[(1, 2), (1, 3), (2, 3)]
from math import prod
n = 5
factors = [10**i for i in range(n)]
[sum(map(prod, zip(factors, c)))
for i in range(n,0,-1)
for c in combinations(range(1,n+1), i)]
output:输出:
[54321,
4321,
5321,
5421,
5431,
5432,
321,
421,
521,
431,
531,
541,
432,
532,
542,
543,
21,
31,
41,
51,
32,
42,
52,
43,
53,
54,
1,
2,
3,
4,
5]
If you need a strict order of the output, use the following (slightly less efficient):如果您需要严格的输出顺序,请使用以下命令(效率稍低):
[sum(map(prod, zip(factors, c)))
for i in range(n,0,-1)
for c in list(combinations(range(1,n+1), i))[::-1]]
output:输出:
[54321,
5432,
5431,
5421,
5321,
4321,
543,
542,
532,
432,
541,
531,
431,
521,
421,
321,
54,
53,
43,
52,
42,
32,
51,
41,
31,
21,
5,
4,
3,
2,
1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.