对于我的CS课程中的作业,我必须使用不超过2行代码打印小于1000的所有3(1,3,9,27等)的幂。我知道我可以使用
for x in range(7):
print(3**x)
因为我知道3 ^ 6是最后一次给出低于1000的结果但是我想知道是否有办法有条件地检查3 ^ x是否低于1000,然后打印仍然只使用2行代码。我可能只是在思考它,但我想确保自己的信息。
答案 0 :(得分:3)
通常在python中我们更喜欢许多行可以提供最可读的代码。
因此,限制像这样的代码行是一个奇怪的要求。猜测一下,你的导师可能一直在寻找关于如何预先计算迭代范围的数学见解:
>>> for i in range(1 + int(math.log(1000,3))):
... print(3**i)
...
1
3
9
27
81
243
729
答案 1 :(得分:1)
如果允许使用分号:
> explain analyze select service_name, state, res_id, count(*) from bookings group by rollup(service_name, state, res_id);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
GroupAggregate (cost=43069.12..45216.05 rows=4161 width=24) (actual time=1027.341..1120.675 rows=428 loops=1)
Group Key: service_name, state, res_id
Group Key: service_name, state
Group Key: service_name
Group Key: ()
-> Sort (cost=43069.12..43490.18 rows=168426 width=24) (actual time=1027.301..1070.321 rows=168426 loops=1)
Sort Key: service_name, state, res_id
Sort Method: external merge Disk: 5728kB
-> Seq Scan on bookings (cost=0.00..28448.26 rows=168426 width=24) (actual time=0.079..147.619 rows=168426 loops=1)
Planning time: 0.118 ms
Execution time: 1122.557 ms
(11 rows)
> explain analyze select service_name, state, res_id, count(*) from bookings group by service_name, state, res_id
UNION ALL select service_name, state, NULL, count(*) from bookings group by service_name, state
UNION ALL select service_name, NULL, NULL, count(*) from bookings group by service_name
UNION ALL select NULL, NULL, NULL, count(*) from bookings;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Append (cost=30132.52..118086.91 rows=4161 width=32) (actual time=208.986..706.347 rows=428 loops=1)
-> HashAggregate (cost=30132.52..30172.12 rows=3960 width=24) (actual time=208.986..209.078 rows=305 loops=1)
Group Key: bookings.service_name, bookings.state, bookings.res_id
-> Seq Scan on bookings (cost=0.00..28448.26 rows=168426 width=24) (actual time=0.022..97.637 rows=168426 loops=1)
-> HashAggregate (cost=29711.45..29713.25 rows=180 width=20) (actual time=195.851..195.879 rows=96 loops=1)
Group Key: bookings_1.service_name, bookings_1.state
-> Seq Scan on bookings bookings_1 (cost=0.00..28448.26 rows=168426 width=20) (actual time=0.029..95.588 rows=168426 loops=1)
-> HashAggregate (cost=29290.39..29290.59 rows=20 width=11) (actual time=181.955..181.960 rows=26 loops=1)
Group Key: bookings_2.service_name
-> Seq Scan on bookings bookings_2 (cost=0.00..28448.26 rows=168426 width=11) (actual time=0.030..97.047 rows=168426 loops=1)
-> Aggregate (cost=28869.32..28869.33 rows=1 width=0) (actual time=119.332..119.332 rows=1 loops=1)
-> Seq Scan on bookings bookings_3 (cost=0.00..28448.26 rows=168426 width=0) (actual time=0.039..93.508 rows=168426 loops=1)
Planning time: 0.373 ms
Execution time: 706.558 ms
(14 rows)
答案 2 :(得分:1)
>>> from itertools import takewhile, count
>>> print(*map(lambda x: 3**x, takewhile(lambda x: 3**x < 1000, count(0))), sep='\n')
1
3
9
27
81
243
729
答案 3 :(得分:1)
您可能已经发现6是使用日志的最低允许指数。这是一个单线解决方案:
import math
def printer(exp_num, target_num):
for i in range(int(math.log(target_num, exp_num))+1): print exp_num**i
printer(3, 1000)
答案 4 :(得分:1)
itertools.takewhile
是表达您所要求的正确方法,但它取决于必须添加一行的模块。
>>> import itertools
>>> print(list(itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count()))))
[1, 3, 9, 27, 81, 243, 729]
以更加理智的3线格式表达......
>>> import itertools
>>> for x in itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count())):
... print(x)
...
1
3
9
27
81
243
729
让我们从最里面开始解释这里发生了什么。每个步骤都基于前一步。
itertools.count()
是一个永久生成数字1, 2, 3, 4, ...
的生成器。这就是你表达range(infinity)
的方式。
(3**x for x in itertools.count())
是一个永久生成数字1, 3, 9, 27, ...
的生成器。
itertools.takewhile(lambda x: x < 1000, (3**x for x in itertools.count()))
是一个生成数字1, 3, 9, 27, ...
永远而x < 1000
为真的生成器。
之后,只需打印生成的数字即可。
The itertools module是Python非常重要的一部分,我建议一般学习它,因为它解决了很多像这样的问题。
答案 5 :(得分:0)
一个带有列表理解的班轮
print([3 ** x for x in range(7) if 3 ** x < 1000])
解决凯文的评论并试图坚持两行代码
for i in range(1000):
print(3 ** i) if (3 ** i) < 1000 else i
我不喜欢其他声明
答案 6 :(得分:0)
可能,如果你可以弄清楚如何使用ternary operator。 This answer may help,但我最好的猜测(我不在Python中工作)将是......
for x in range(10):
print(3**x) if (3**x) < 1000 else print()
答案 7 :(得分:0)
"prettyhtml": {
"printWidth": 300,
"singleQuote": false,
"wrapAttributes": false,
"sortAttributes": false
}