作者fourdollars (四元)
看板Programming
标题Re: [问题] 1-9位数不重复印出来 (Python)
时间Sun Nov 27 00:23:19 2016
这是我目前找出来 Python 3 上面执行速度最快的方法
不过还是比 Perl 的版本要慢一点点,最快的还是 C 大概是 Perl 的十倍快
#!/usr/bin/env python3
import sys
power = int(sys.argv[1])
class Num(object):
def __init__(self, power):
self.upper = 10 ** power
def __iter__(self):
for i in range(1, self.upper):
num = str(i)
if len(set(num)) == len(num):
yield(i)
num = Num(power)
numbers = [str(i) for i in num]
print(', '.join(numbers))
※ 引述《mikemagic88 (Mikemagic88)》之铭言:
: 使用者输入1 印1-9
: 使用者输入2 印1-98 (11, 22, 33等重复的不印)
: 使用者输入3 印1-987 (121, 988, 667等有重复的不印)
--
Debian 正体中文社群
https://plus.google.com/communities/106482636420342329539
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.227.56.142
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Programming/M.1480177402.A.6A6.html
※ 编辑: fourdollars (1.163.117.89), 11/28/2016 19:22:53