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 () 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 () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
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))
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.
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)).
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))
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.