ProgrammingVery hardProduct company OA

What is printed?

What is printed?
def bucket(x, w):
    return x // w, x % w, int(x / w), -(-x // w)
print(bucket(-7, 3), bucket(7, -3))
  1. A. (-3, 2, -2, -2) (-2, 1, -2, -3)
  2. B. (-2, -1, -2, -3) (-2, 1, -2, -3)
  3. C. (-3, 2, -2, -2) (-3, -2, -2, -2)
  4. D. (-3, 2, -2, -3) (-3, -2, -2, -2)

Correct answer: C. (-3, 2, -2, -2) (-3, -2, -2, -2)

// floors toward −∞ and % takes the sign of the divisor so that x == (x // w) * w + x % w. −7 // 3 = −3 with remainder 2; int(−2.33) truncates to −2; −(7 // 3) = −2 is the ceiling idiom. For 7 and −3: 7 // −3 = −3, 7 % −3 = −2 (sign of −3), int(−2.33) = −2 and −(−7 // −3) = −(2) = −2. The (−2, −1, …) options are C-style truncating division.

More Python Programming questions

Exams that ask this topic