Dividing two numbers and printing the result and adding one to the result if there is a remainder, without using if-statements or imports or function?

huangapple go评论57阅读模式
英文:

Dividing two numbers and printing the result and adding one to the result if there is a remainder, without using if-statements or imports or function?

问题

我尝试将数字在纯Python中分成组,不导入或使用if语句,以便将除法结果分成组并形成一个额外的组,使得200 / 99等于3,而7 / 3等于3,但8 / 4仍然等于2,4 / 2等于2等等... 我不能导入任何东西,所以它必须在纯Python中进行。

我尝试将用户输入的数字存储在变量中,然后进行除法,然后再加1。我还尝试使用//并加1,但我无法使其工作。

英文:

I'm trying to divide number into groups in plain python without importing or the use of if-statements, and get the division into groups + remainder forming one extra group so that 200 / 99 would be 3, And 7 / 3 would be 3, but that 8 / 4 would still be just 2, and 4 / 2 would be 2 etc.. I cannot import anything so it needs to be in plain python.

I tried storing the numbers from inputs from user into variables and dividing them, and then adding one. I also tried // and adding 1 but I cannot get it to work.

答案1

得分: 1

这是您提供的代码段的翻译:

a, b = 200, 99
result, remainder = a // b + int(bool(a % b)), a % b

这段代码通过执行整数除法 a // b 并计算余数 a % b 来计算结果。将整数值转换为布尔值时,0 对应 False,而其他值对应 True,将其再次转换为整数将给您要添加到结果的值。如果需要,可以再次计算余数来分配它。

正如用户 @markransom 所评论的那样,int() 转换甚至是不必要的,因为 bool 已经是整数类型:

>>> isinstance(True, int)
True

因此,这也可以工作(尽管可能被认为不够易读):

a, b = 200, 99
result, remainder = a // b + bool(a % b), a % b

如果您使用的是Python的现代版本,并且真的希望代码更短,这也可以工作:

result = a // b + bool(remainder := a % b)

这使用了河马运算符(walrus operator)在首次计算时分配了 remainder,避免了需要两次计算余数。

英文:

How about this:

a, b = 200, 99
result, remainder = a // b + int(bool(a % b)), a % b

This computes the result by performing an integer divide a // b and computing the remainder a % b. Converting an integer value to a boolean is False for 0 and True for any other value, and converting that back to an integer gives you the value you want to add to the result. The remainder is computed again to assign it, if you need it.

As user @markransom commented, the conversion to int() isn't even necessary, as bool already 'is' an integer type:

>>> isinstance(True, int)
True

So, this works (although it may be considered a bit less readable):

a, b = 200, 99
result, remainder = a // b + bool(a % b), a % b

If you're using a modern version of Python and really want it to be short, this also works:

result = a // b + bool(remainder := a % b)

This uses the walrus operator to assign the remainder when it is first computed, avoiding having to compute it twice as well.

答案2

得分: 1

Python的布尔操作会短路并返回最后一个被评估的值,您可以利用这一特性将非零余数转换为1,以便将其添加到商中。

def group_me(dividend, divisor):
    quotient, remainder = divmod(dividend, divisor)
    return quotient + (remainder and 1 or 0)

print(group_me(200, 99))
print(group_me(7, 3))
print(group_me(8, 4))

输出:

3
3
2

如果remainder非零,remainder and 1会短路并返回1。否则,or变成了0 or 0,保持其最后的值0

英文:

Python boolean operations short-circuit and return the last value evaluated and you can use that to convert a non-zero remainder to 1 for addition to a quotient

def group_me(dividend, divisor):
    quotient, remainder = divmod(dividend, divisor)
    return quotient + (remainder and 1 or 0)

print(group_me(200, 99))
print(group_me(7, 3))
print(group_me(8, 4))

Output

3
3
2

If remainder is non-zero, remainder and 1 short-circuits and returns 1. Otherwise, the or now becomes 0 or 0, which retains its last value 0.

答案3

得分: 0

你可以在数学上不使用if语句来实现条件判断:

n, d = 200, 99
x, y = n/d, n//d + 1
result = int((x%1 == 0) * x + (x%1 != 0) * y)

基本上,这是条件 * 答案1 + (1 - 条件) * 答案2。然后根据条件是1还是0切换到其中一个答案。

英文:

You could do an if statement mathematically without using if itself:

n, d = 200, 99
x, y = n/d, n//d + 1
result = int((x%1 == 0) * x + (x%1 != 0) * y)

Essentially it is condition * answer 1 + (1 - condition) * answer 2. Then it switch to whichever answer depending on condition being 1 or 0.

huangapple
  • 本文由 发表于 2023年2月10日 06:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定