rian bron

Ranch Hand
+ Follow
since Oct 14, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by rian bron

Piet Souris wrote:I would start by writing a method 'int removeLastNode()' and 'int removeFirstNode()' in  your IntListTwo class. The return value is the value of the removed node.



yes, i know this one , but i am struggling to write those 2 methods and basically those 2 methods are the answer plus some things around it but basically this is that answer.
can you show me how to write on of them and i will try to write the other one?

thank you
5 years ago
Hello guys, i have a question about linked list i got for homework and i need your help:

i got these 2 classes:





now, i need to write a method in the IntListTwo class that gets an average int from the user, and returns true if there is a sub-list that it's average is equals to the given average. and false if not.
the method should look like this :



so i thought about the algorithm:
1. i will calculate the sum of all the list + count the number of numbers in the list.
2. i will calculate the average of the list ( sum/count)
3. if the average is bigger than the given average, i will subtract the last element from the sum and subtract 1 from the counting and take the last element of the list one step back.
3. if the average is smaller than the given average, i will subtract the first element from the sum and subtract 1 from the counting and take the first element of the list one step forward.

By the way- the list is sorted!
Now, when i have the algorithm im still struggling to write it,
would you help me ?
Thank you !
5 years ago

salvin francis wrote:instead of using if, use else-if since your cases are exclusive. I think there is some more problems with the counts anyways.
When using recursion, I think its best to print all variables to see what is happening in each call.


you are right, but my task is to print the number of results there are , for example for num=4, i need to get 3 because there are 3 results possible: 1 1 2, 1 2 1 , 2 1 1
5 years ago

salvin francis wrote:The example I showed you for only for x2,x3 for x1=1. It gets 3 matches. Similarly you can loop for x1=2,3,etc..

Lets forget recursion, can you implement this using a simple for loop ?



Ok! i figured it out how to write the same method with recursion, but i now have my last problem, the method i wrote gives me all the results compositions possible , but the method does not stop running, i think it is  a problem with the base case but i don't know what:



Do you know what is the problem?
5 years ago

salvin francis wrote:The example I showed you for only for x2,x3 for x1=1. It gets 3 matches. Similarly you can loop for x1=2,3,etc..

Lets forget recursion, can you implement this using a simple for loop ?



yes, i would do it like this:

5 years ago

salvin francis wrote:I am unsure why you are counting till 10, but here's my approach:

First of all, we need to validate num. Since you are allowing x1,x2,x3 to be a min of 1 and a max of 9, num can only lie between 3 and 27.

The simplest brute force to check a number (say 5) would be to loop between 1 to 3. (i.e. 1 till num-2)




Do you see a pattern emerging above ?



you are basically right, but the thing is the i need to get all the possible compositions for the number.
for example if you chose 5, i need to get 6 as an answer:
1 1 3
1 2 2
1 3 1
2 1 2
2 2 1
3 1 1
It doesn't only need to find the possible compositions from left to right, it needs to find all compositions from right to left too.
But my problem is with the returning int, i get a wrong int every time i run this method, i get 1 every time instead of the correct number of results possible.
can you help me with that?
5 years ago

salvin francis wrote:

rian bron wrote:...Yes the number must me between 1 to 9 (x>=1&&x<=9)



Great, Now that we know that, I hope there are no other restrictions such as repeating values for x1,x2,x3.
As I asked before, can you work out in plain english and not in java code, how you would solve this problem ?



Lets say i am working right now on X,  so as a start x,y,z=1.
and i will add up 1 each round until x will be equals to 10, when this will happen I will set x=1 and start add up 1 to y each round until y=10. when this will happen i will set y=1 and add up 1 each round to z.
when z will be equals to 10, i will return the counting variable?


Is this method is correct?
5 years ago

salvin francis wrote:

rian bron wrote:... 1-9 with a recursion call instead of copying it ...


Why is 0 not considered ?
e.g. if num=5, then 0+0+5 =5

Are there any other restrictions on the value of x1, x2, x3 ?


Yes the number must me between 1 to 9 (x>=1&&x<=9)
5 years ago

salvin francis wrote:Are negative numbers allowed ?
Then I think there could be infinite possibilities.

Instead of trying to solve this directly in Java code, can you work it out in plain english, how would you manually solve this problem.
e.g. Lets take num=5
what are the possibilities of x1, x2, x3 that add up to 5 and how did you come up with that ?


Sorry i forgot to mentioned that negative numbers are not allowed,
and I tried to do what you said, and this is the result i got into.
The main problem is that the returned int is always staying 1 and does not represent the real answer.
It is probably because i don't insert the counting in the correct place, but i tried few places and still didn't get it right.
Can you help me with that?
5 years ago
Hello guys, i am stuck a few days already on my problem my school gave me:
The question is :
"Write a recursive method without using any loops, that will  count all the number of possible compositions of x1+x2+x3=num."
i tried to do it with the following code:


I have 2 problems:
1.The method needs to return  an int that represents the number of results possible, but of some reason the returned is always 1, why is that?

2. lines 15-32 makes the code really go longer than it should, is there a  way to get all over the number 1-9 with a recursion call instead of copying it over and over again?
Thank you Very much!
5 years ago
/**
   * actions method calculates the minimum actions neede to reach from x to y (x<y) with
   * only using the operators +1,*2.
   * @param x -an integer from the user.
   * @param y - an integer form the user.
   * return - an integer number which represents the minimum number to reach from x to y.
   */
   public static int actions(int x,int y)
   {
       // checks if x reached to y or if the given values are vaild
       if(x==y)
           return 0;
           // checks if y is an odd number
       else if (y%2==1)
       return 1+actions(x,y-1);//calls actions method with x and even y,adds 1 to the total counting
       //checks if y/2 is still bigger than or equals to x
       if(x<=y/2)
       return 1+actions(x,y/2);// calls actions method with x and y/2, adds 1 to the total counting
       else
       return 1+actions(x,y-1);// calls actionsmethod with x and y-1, adds 1 to the total counting
   }
5 years ago
you were right guys, the problem was with the brackets,
the right solution is :


thank you all for help
5 years ago

Paul Clapham wrote:Well, first of all your code is very hard to read because you use the accessor methods instead of the instance variables. If I rewrite your code to remove that idea (and to remove the complicated method of squaring a number, and to remove unnecessary brackets) then it looks like this:



When I compare that to the quadratic formula I can see at least two differences, probably three. And that's before considering the possibility of complex numbers as the solution.


well, your answer is indeed easier to read, but still if i insert a=1,b=2,c=-3 the answer i get according to this code is x1=8,x2=-6 versa the correct answer is x1=-3,x2=1
and i don't get why the answer is incorrect although the algorithm seems to be right
5 years ago
Hello guys,  i tried to write the formula of Quadratic equation in  java, but for some reason it seems that i wrote it right but the answer is wrong.
the code i wrote :

the algorithm is at lines 20,21
could you please tell me what i did wrong?
Thank you

5 years ago