我无法理解代码的功能。

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

I am unable to understand the function of code

问题

def something(number):
    return number % 3 == 0

def nothing(number):
    return number % 5 == 0

def anything(number):
    total = 0
    for i in range(number):
        if something(i) or nothing(i):
            total += i
    output = (total // 5) // 33
    return output

#应该预测anything(1000)的输出结果
英文:
def something(number):
  return number%3==0
def nothing(number):
  return number%5==0
def anything(number):
  total=0
  for i in range(number):
     if something or nothing:
         total+=i
  output=(total//5)//33
  return output

#the output should be predicted for anything(1000)

I couldn't understand the flow of function here.

答案1

得分: 1

如约翰所提到的,没有括号和参数,'something' 和 'nothing' 在 if 语句中被视为真值。您没有调用这些函数。像这样应该可以工作:

def anything(number):
  total = 0
  for i in range(number):
    if something(i) or nothing(i):
      total += i
  output = (total // 5) // 33
  return output
英文:

As John mentioned, without the parenthesis and arguments 'something' and 'nothing' are considered truthy values in the if statement. You're not calling the functions. Something like this should work:

def anything(number):
  total = 0
  for i in range(number)
    if something(i) or nothing(i):
      total += i
  output = (total // 5) // 33
  return output

答案2

得分: 0

以下是翻译好的部分:

在这个答案中,我正在分析原问题中的代码。正如John Gordon和sstab所指出的,这段代码很不寻常,因为函数没有被调用,所以if ...:语句总是为真。因此,该代码等价于:

def anything(number):
  total=0
  for i in range(number):
     if True:
         total+=i
  output=(total//5)//33
  return output
print(anything(1000))

total中,它计算了0到999(包括0和999)之间整数的总和:

  • 整数总和:0 + ... + 999 =(0 + 999)* 1000 / 2 = 499500。

  • output =(499500 // 5)// 33 = 3027。

确实,当运行时,它打印出3027。

英文:

In this answer I'm analyzing the code in the original question. As noted by John Gordon and sstab, it is unusual, because the functions are not called, so the if ...: is always true. Thus the code is equivalent to:

def anything(number):
  total=0
  for i in range(number):
     if True:
         total+=i
  output=(total//5)//33
  return output
print(anything(1000))

In total, it computes the sum of integers in 0 .. 999 (both inclusive):

  • Sum of integers: 0 + ... + 999 = (0 + 999) * 1000 / 2 = 499500.

  • output = (499500 // 5) // 33 = 3027.

And, indeed, it prints 3027 when run.

答案3

得分: 0

我认为正确的代码部分如下:

if something(i) or nothing(i):

而不是

if something or nothing:

代码可以解释如下:

def something(number):
  return number%3==0

def nothing(number):
  return number%5==0

def anything(number):
  total=0
  for i in range(number):
     if something(i) or nothing(i):
         total+=i
  output = (total//5)//33
  return output

anything(1000)
# 使用 number == 1000 运行 anything(1000)
# total == 0
# 执行循环 0 到 999
# - 当 i == 0 时的循环:
# -- 运行 something(0) -> 返回 0 mod 3 == 0 -> 返回 0 == 0 -> 返回 true
# -- 运行 nothing(0) -> 返回 0 mod 5 == 0 -> 返回 0 == 0 -> 返回 true
# -- 如果 true or true -> 如果 true
# --- total = 0 + 1 -> 1
# - 当 i == 1 时的循环:
# -- 运行 something(1) -> 返回 1 mod 3 == 0 -> 返回 1 == 0 -> 返回 false
# -- 运行 nothing(1) -> 返回 1 mod 5 == 0 -> 返回 1 == 0 -> 返回 false
# -- 如果 false or false -> 如果 false -> 不执行 "total+=i"
# .
# .
# .
# - 当 i == 999 时的循环:
# -- 运行 something(999) -> 返回 999 mod 3 == 0 -> 返回 1 == 0 -> 返回 false
# -- 运行 nothing(999) -> 返回 999 mod 5 == 0 -> 返回 4 == 0 -> 返回 false
# -- 如果 false or false -> 如果 false -> 不执行 "total+=i"
# total == 233168
# output = (total//5)//33 -> (233168//5)//33 -> 46633//33 -> 1413
# 最终,anything(1000) 返回 1413

所以 anything(1000) 函数的结果应该是 1413

英文:

I assume that part of the correct code is like this:

if something(i) or nothing(i):

instead of

if something or nothing:

The code can be explained below:

def something(number):
  return number%3==0

def nothing(number):
  return number%5==0

def anything(number):
  total=0
  for i in range(number):
     if something(i) or nothing(i):
         total+=i
  output = (total//5)//33
  return output

anything(1000)
# run anything(1000) with number == 1000
# total == 0
# do loop 0 to 999
# - loop with i == 0:
# -- run something(0) -> return 0 mod 3 == 0 -> return 0 == 0 -> return true
# -- run nothing(0) -> return 0 mod 5 == 0 -> return 0 == 0 -> return true
# -- if true or true --> if true
# --- total = 0 + 1 -> 1
# - loop with i == 1:
# -- run something(1) -> return 1 mod 3 == 0 -> return 1 == 0 -> return false
# -- run nothing(1) -> return 1 mod 5 == 0 -> return 1 == 0 -> return false
# -- if false or false --> if false -> dont run "total+=i"
# .
# .
# .
# - loop with i == 999:
# -- run something(999) -> return 999 mod 3 == 0 -> return 1 == 0 -> return false
# -- run nothing(999) -> return 999 mod 5 == 0 -> return 4 == 0 -> return false
# -- if false or false --> if false -> dont run "total+=i"
# total == 233168
# output = (total//5)//33 -> (233168//5)//33 -> 46633//33 -> 1413
# finally, anything(1000) return 1413

So the result of anything(1000) function should be 1413

答案4

得分: -1

以下是代码的翻译部分:

def something(number):
  return number % 3 == 0

def nothing(number):
  return number % 5 == 0

def anything(number):
  total = 0
  for i in range(number):
    if something(i) or nothing(i):
      total += i
  output = (total // 5) // 33
  return output

print(anything(1000))

total中,它计算了在0到999(包括0和999)之间可以被3或5整除的整数的和。

  • 可以被3整除的整数之和:0 + 3 + ... + 999 = (0 + 999) * 334 / 2 = 166833。

  • 可以被5整除的整数之和:0 + 5 + ... + 995 = (0 + 995) * 200 / 2 = 99500。

  • 可以被15整除的整数之和:0 + 15 + ... + 990 = (0 + 990) * 67 / 2 = 33165。

  • total = 166833 + 99500 - 33165 = 233168。

  • output = (233168 // 5) // 33 = 1413

当运行时,的确会打印出1413。

英文:

I'm analyzing this program, which is the code in the question, except for the if line, which is changed according to the comment by Johh Gordon and the answer by sstab:

def something(number):
  return number%3==0
def nothing(number):
  return number%5==0
def anything(number):
  total=0
  for i in range(number):
     if something(i) or nothing(i):
         total+=i
  output=(total//5)//33
  return output
print(anything(1000))

In total, it computes the sum of integers in 0 .. 999 (both inclusive) which are divisible by 3 or divisible by 5.

  • Sum of integers divisible by 3: 0 + 3 + ... + 999 = (0 + 999) * 334 / 2 = 166833.

  • Sum of integers divisible by 5: 0 + 5 + ... + 995 = (0 + 995) * 200 / 2 = 99500.

  • Sum of integers divisible by 15: 0 + 15 + ... + 990 = (0 + 990) * 67 / 2 = 33165.

  • total = 166833 + 99500 - 33165 = 233168.

  • output = (233168 // 5) // 33 = 1413.

And, indeed, it prints 1413 when run.

huangapple
  • 本文由 发表于 2023年3月7日 11:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657678.html
匿名

发表评论

匿名网友

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

确定