Python Challenge - Comparative Weights

This community-built FAQ covers the “Comparative Weights” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Comparative Weights

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I keep getting only 4/5 tests passed, i just can’t figure out what is missing on my code, for me it works 100%.
Can anyone please point out what i am missing?

def scale_of_truth_n(n): c = 0 while n != 1: n = n//2 c += 1 return c scale_of_truth_n(3)
def scale_of_truth_n(n): if n < 1: return "Error" else: count = 0 while n != 1: while n % 3 != 0: n += 1 n /= 3 count += 1 return count print(scale_of_truth_n(120))

This code passed all the tests

if you put in 0 you would get an endless loop thats probably why. But i think your method is wrong, for 81 as an example you split it into 3 groups, 27 now, then again, 9 now, then again,3 now and finally once more to get the one. thats 4 times whereas your code would say 8.
read the prompt again to see why its 3 groups not 2


import math def scale_of_truth_n(n): if n < 2: return 0 return math.ceil(math.log(n, 3)) print(scale_of_truth_n(3))

Can’t be too creative here, just handing in my own solution which is basically identical to what @text9831554157 's most recently posted for the reasons @text9831554157 posted before.


1 Like

yeah this one the math is really the solution

Could someone please explain why I have to use log with a base of 3 instead of base 2? It seemed more logical to me to divide the number of N by 2 until I reached a setting in which I would have only 1 ball on either side of the scale. Consequently, the number of weighs would be math.ceil(math.log(n,2)).



log base 2 works perfectly fine too, it’s just not the fastest solution. The trick with this challenge is:

  • From your n balls, form three piles of equal size.
  • Put pile 1 on the one side of your balance scale, pile 2 on the other side, and pile 3 aside.
    • If the scales tilt to either side, you know that the heavier ball is on that side.
    • If the scales remain balanced, you know that the heavier ball has to be in your third pile
  • With the identified pile: repeat until there’s only one ball left in your identified pile

This way you can cut the remaining number of balls in thirds with every weighing, hence the log base 3.


def scale_of_truth_n(n):
  from math import floor, log
  if n==1: return 0
    x = floor(log(n-1,3))+1
    return 1
  return x

def scale_of_truth_n(n): if n <= 1: return 0 else: c = 1 while n >= 3: if n % 3 != 0: n -= 1 if n % 3 != 0: n-= 1 n /= 3 c += 1 return c print(scale_of_truth_n(10))

This is basically the implementation of the log3 without actually using it…
If you keep finding yourself at 4/5 test passed, one possibility may be that you didn’t account for cases like n = 9 where it is actually optimal to form group of threes which allows for a maximum of two weight comparisons while the log2 algorithm needs three.

import math def scale_of_truth_n(n): if n < 2: return 0 return math.ceil(math.log(n, 3)) print(scale_of_truth_n(3))
def scale_of_truth_n(n): count = 0 while n > 3 ** count: count += 1 return count
def scale_of_truth_n(n): count,exponent = 2,1 while True: if n<count: return exponent-1 count += 2*(3**(exponent-1)) exponent+=1 print(scale_of_truth_n(4))

You put the balls in 3 equal piles (n//3), 2 of which will be weighed, one won’t.
If the weighed piles are unequal, you know the ball is one of the weighed piles.
Otherwise, it is in the unweighed piles.
If there is one extra ball it goes in the unweighed pile making it (n//3 +1) balls.
If there are 2 extra balls one goes to each of the weighed piles making each (n//3 +1) balls.
In either of those situations, we weigh it once, or – add 1 to the recursive call of (n//3 + 1).
If the initial (n) is divisible by 3, then we just add 1 to the recursive call of (n//3)

def scale_of_truth_n(n): if n <= 1: return 0 if n % 3 != 0: return scale_of_truth_n(n//3 + 1) + 1 else: return scale_of_truth_n(n//3) + 1 print(scale_of_truth_n(10))