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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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!

Hello!
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

image

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.

Cheers

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)).

Thanks!

Hey,

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.

Cheers

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

print(scale_of_truth_n(3))