Java Challenge - Comparative Weights

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

Top Discussions on the Java 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!

No curley braces thanks to unpleasant hacking:

  public static int scaleOfTruthN(int n) {
    int returnV = 0;
    
    for(float n2 = (float) n; n2>1; returnV++)
      if((n2/=3)>(int)n2)
        n2=(int)n2+1;

    return returnV;
  }

No if statement.

  public static int scaleOfTruthN(int n) {
    int returnV = 0;
    
    for(float n2 = (float) n; n2>1; returnV++)
      n2=((n2/=3)>(int)n2)?(int)n2+1:n2;

    return returnV;
  }

I am so sorry, it has to stop. No statement in the for-loop

  public static int scaleOfTruthN(int n) {

    int weighs = 0;
    
    for(float n2 = (float) n; n2>1; n2=((n2/=3)>(int)n2+(weighs++*0))?(int)n2+1:n2);

    return weighs;
  }

So that’s it, sorry for spamming this thread. But I think less then 2 lines of code including the return statement is not possible.

  public static int scaleOfTruthN(int n) {
    
  for(float n2 = (float) n +(n=0); n2>1; n2=((n2/=3)>(int)n2+(n++*0))?(int)n2+1:n2);

  return n;
 }

Hello! Can someone explain why am I getting a stackOverflowException with this code inside Codecadamy but when I run it in Eclipse, it works just fine? Thanks a lot!
public class ComparativeWeights {
static int x;
public static void main(String args) {
scaleOfTruthN(3);
}

public static int scaleOfTruthN(int n) {
n = n/2;
if (n==1) {
x = 1;
} else{
scaleOfTruthN(n);
x=x+1;
}
return x;
}
}

I am not enirely shure why you get the exception yet, but I saw that you never use the return value of the recursive called function. Also that global variable confuses me a bit. I think you use it instead of handling the return value, which is pretty confusing and as far as i know, bad practice.

To your issue, it looks like you line n = n / 2; never equals 1 for whatever reason. I would try something like if(Math.floor(n)>=1) in the line below to rule out some error sources. (Don’t forget to import the library).

At first glance i could imagine two possible reasons for the issue:

  1. Codecademy simulates Java with JS or something else and generates therefore weird behaviour sometimes.
  2. There is an issue in the code I did’t spot and the Eclipse Compiler corrected it for you.

Does it happen every time or just with 3 as starting value?

HI there and thanks a lot for the quick reply. I really did not know that it was bad practice not to use the return value of the recursive function. I first thought about the algorithm per se and then came up with the idea, that it just has to count how often the recursive function has been used until it gets to 1, so I was not interested in the result. So, the global variable was nothing else but a counter - and it had to be global for that matter (well at least that is what my still very basic Java-skills were telling me :wink:)
Oh, and my first code did not work, no matter which n I chose.

After I read your reply, I came up with a new idea! What do you think about this code? would this be better?
public class ComparativeWeights {
static int x;
public static void main(String args) {
System.out.println(scaleOfTruthN(69));
}

public static int scaleOfTruthN(int n) {
if(n < Integer.MIN_VALUE || n > Integer.MAX_VALUE){
System.err.println(“The value of n is outside the integer range.”);
}
double zahl = n;
double basis = 2;
double logarithmusBasis2 = Math.log(zahl) / Math.log(basis);
int result = (int) Math.floor(logarithmusBasis2);
return result;
}
}
This one works in the codecademy-workspace, but it passed only 4 out of 5 tests. :woman_shrugging:

Hi!

First, I learned programming several years ago but didn’t have the chance to test my skills in the professional world yet, so very expierienced programmers have probably a better insight.

Second, it’s not bad practice if you don’t use the return value. It’s bad practice to use a global variable. It’s easier for programming but use up unecessary memory in your programm and are usually just lazy developement. Instead of the global variable you could have used a counter inside the function. If it get’s to 1 it returnes 1 and in any other case it returnes the value of the nestet function +1. Like this

if(n==1)
return 1;
else
return scaleOfTruthN(n)+1;

Last, I am not profound in logarithm logic. Your algorithm probably didn’t cover some edge cases, or covers 4 of 5 tests by luck. I don’t know.

I just read the example from Math.log() in the mozilla JavaScript documentation which is exactly your code and i kind of get now what it does, but I don’t see why this algorithm should be the solution to this problem.

So i wrote a little program to test your algoritm against a correct one with the numbers 1-100 and your algorithm displays the correct solution 17 times and the wrong solution 82 times. So the 4 correct test are just lucky. When I test it with the number 1-1000 it still gets the correct solution 17 times and the wrong solution 982 times. It just works with the numbers (1,2,3,4,5,6,7,10,11,12,13,14,15,28,29,30,31). I like the creative approach. Your first attempt was closer to the correct solution though.

I think if you are just learning programming, it’s completely acceptable to write bad code that works. Best practice and clean coding can be learned later on. It’s good to learn things correct from the beginning, but these things change a bit over time like the importance of object oriented programming for example. When I look at my code from several years ago, I almost get an heart attack because it’s caotic and poorly written but it did the thing it should and I was very proud at that time. I then learned about clean code and focused more on that and now it sometimes looks machine generated but clean. The most imporand thing is to continue learning new things and to improve. That nevers end as far as I know.

Good luck and have fun with further coding! I am curious for your final solution.

Edit: I wrote this post while researching and testing, so you kind of see my thinking process. It might look a little confusing, but if you keep in mind that between each sentence is some researching and testing it makes more sense.

1 Like

Hi!
To be honest, I don’t know what to say regarding the amount of time you invested in my question. Thank you! And I did not find your answer confusing or difficult to follow - that was just fine.
Especially thanks for the code snippet: it was exactly what I wanted to program but I did not come up with the solution “return scaleOfTruthN(n)+1;”. that is why I used the global variable. I hope the more I practice the easier it will get to find such solutions.

Now both codes deliver the same results. I thought that was it, but after your post, I looked at my algorithm and googled it and found out that my algorithm was wrong. You do not divide by 2 but by 3 → 3 groups. Then I learned about Math.ceil and now I have a third version ( I however stuck with the logarithm as I found it prettier :joy:):

public class ComparativeWeights {
public static void main(String args) {
System.out.println(scaleOfTruthN(32));
}

	  public static int scaleOfTruthN(int n) {
		  double logarithmusBasis3 = Math.log(n) / Math.log(3);
		  int result = (int) Math.ceil(logarithmusBasis3);
		  return result;
	  }

}
→ All tests passed. Only, I am really frustrated with how much googling and help I needed to find the solution. Without a hint, I would have never accomplished this task.

1 Like