I am unable to understand the function of code

Question:

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.

Asked By: Ananya Tiwari

||

Answers:

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
Answered By: sstab

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.

Answered By: pts

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.

Answered By: pts

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

Answered By: Jordy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.