gpt4 book ai didi

python - python中的基本转换

转载 作者:太空宇宙 更新时间:2023-11-04 10:45:40 24 4
gpt4 key购买 nike

我需要帮助来理解这个问题。请不要发布答案,只发布解决问题的方法。

Assign 10 to the variable base. Assign the set {0,1,2,3,4,5,6,7,8,9} to the variable digits. Now write an expression using a comprehension and base and digits whose value is the set of all at-most- three-digit numbers. Your expression should work for any base. For example, if you instead assign 2 to base and assign {0,1} to digits, the value of your expression should be {0,1,2,3,4,5,6,7} because this is the set of numbers that, base two, have at most three digits.

我尝试了这个表达式,但我无法解决以 2 为基数的问题。

base = 10
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{(x*(base**2))+(y*(base**1))+(z*(base**0)) for x in digits for y in digits for z in digits if (x*(base**2))+(y*(base**1))+(z*(base**0))>((y*(base**1))+(z*(base**0)))}

最佳答案

...whose value is the set of all at-most- three-digit numbers.

添加了强调。您需要包含值 000 - 099(对于 base = 10)。

看起来你已经差不多搞定了。您的理解不需要过滤器。想一想您将从 for x in digits for y in digits for z in digits 对于 digits 的不同值中得到多少结果。它应该是正确数量的值。

关于python - python中的基本转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17534989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com