Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

array sum2 challenge

Hi,

i am trying  below challenge

http://codingbat.com/prob/p190968

i tried as below
public int sum2(int[] nums) {

int numsSum[]=new int[4];

if(nums.length>=2)
return nums[0]+nums[1];

//  else (nums.length==1)
 // return nums[0];
  return 0;
}

Open in new window



Expected	Run		
sum2({1, 2, 3}) → 3	3	OK	    
sum2({1, 1}) → 2	2	OK	    
sum2({1, 1, 1, 1}) → 2	2	OK	    
sum2({1, 2}) → 3	3	OK	    
sum2({1}) → 1	0	X	    
sum2({}) → 0	0	OK	    
sum2({4, 5, 6}) → 9	9	OK	    
sum2({4}) → 4	0	X	    
other tests
OK	    

Open in new window



i failed few tests. How to fix and improve my code. please advise
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

i wonder when i write like below


public int sum2(int[] nums) {

int numsSum[]=new int[4];

if(nums.length>=2)
return nums[0]+nums[1];

 else (nums.length==1)
  return nums[0];
 
 
 
}

why i got below error

Compile problems:


Error:      else (nums.length==1)
                          ^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement



what it means?
please advise
Avatar of gudii9

ASKER

public int sum2(int[] nums) {

int numsSum[]=new int[4];

if(nums.length>=2)
return nums[0]+nums[1];

 if(nums.length==1)
  return nums[0];
 
 return 0;
 
  
}

Open in new window


if i put both if and then return 0 at end i passed all test though. not sure if that is correct way of doing
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is how I would do the ternary option :

public int sum2(int[] nums) {

return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;


}

Open in new window

You could also do it this way -
return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]
return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);
Avatar of gudii9

ASKER

return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);






return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;



return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]


return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);

i have not clearly understood meaning of above 4 lines.

what it means by
nums[1]:0);

also what it mean by
nums[1;]

also as below
return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);(//if length less than 1 (true say 0 ) then return nums[0] which is fine if it false what is happening in nums[0]+(nums.length<2?0:nums[1]??
please advise
(nums.length>1?nums[1]:0);
is nums[1] when nums.length>1, otherwise, it is 0

nums[1;]
should have been
nums[1];
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);//// >0 should cover >1 case already in the front right???

return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;
// >1 should cover >0 case already in the front right???

The first is saying, if the length is greater than 0 return the first number, if not return 0,
then, if the length is greater than 1, plus the second number, else plus 0.

The second is saying, if the length is greater than 1, return the first number plus the second number, then, if the length is greater than 0, return the first number, else return 0.
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);//// >0 should cover >1 case already in the front right???
>0 covers the nums[0] part of the >1 case result


return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;
return nums.length>1?(
    nums[0]+nums[1] // this is the >1 case
):(
    nums.length>0?nums[0]:0   // this is the not >1 case
);
Avatar of gudii9

ASKER

nums.length>0?nums[0]:0   // this is the not >1 case

above can be writen as below??

   nums.length==0?nums[0]:0   // this is the not >1 case
No, but it can be written as
  nums.length==0?0:nums[0]
(because we know that nums.length is never < 0)
Avatar of gudii9

ASKER

public int sum2(int[] nums) {

return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);
 
  
}

Open in new window


above worked fine
Avatar of gudii9

ASKER

public int sum2(int[] nums) {

return (nums.length==0?0:nums[0])+(nums.length>=1?nums[1]:0);
 
  
}

Open in new window


why above code failing when i change from
(nums.length>1
to
(nums.length>=1

please advise

return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);

in above working solution where we are taking care when nums.length is 1 case?
Avatar of gudii9

ASKER

return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]


return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);

Open in new window


i am bit confused about above 2 lines.How to break down put some brackets around to clearly understand the flow of solution. please advise.

where can i see more nested ternary related examples to understand above lines of codes
>>why above code failing when i change from
 (nums.length>1
 to
 (nums.length>=1<<
Take a look at what this says
return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);
If the length is 0, return 0, else return the first number plus, if the length is 2 or more, the second number, else plus nothing.
and this says
return (nums.length==0?0:nums[0])+(nums.length>=1?nums[1]:0);
If the length is 0, return 0, else return the first number plus, if the length is 1 or more, the second number, else plus nothing. The problem is that if the length is 1, there is no second number and you will get an out of bounds index error.

BTW, you need to stop piling questions on top of each other too quickly so, when the experts respond, everyone knows which question they're referring to.
where can i see more nested ternary related examples to understand above lines of codes

In just about all the Codingbat questions you've asked.
return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);

in above working solution where we are taking care when nums.length is 1 case?
When nums.length == 1, nums.length==0 is false, so the first term becomes nums[0]
When nums.length == 1,  nums.length>0 is false, so the second term becomes 0
nums[0]+0 is the required result
//return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);
return
          nums.length<1
          ?
              0
          :
              (
                    nums[0]
                +
                    (nums.length<2?0:nums[1])
             )
          ;

//return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1];
return
        nums.length==0
        ?
           0
        :
           (
             nums.length==1
             ?
               nums[0]
             :
               (nums[0]+nums[1])
           )
         ;
Avatar of gudii9

ASKER

public int sum2(int[] nums) {


if (nums.length ==0){return 0;}
if(nums.length==1){return nums[0];}
return nums[0]+nums[1];


}
how this solution works when nums.length is 1. In this case it should return

return nums[0]+nums[1];

but we do not have nums[1] when nums.length is 1?

please advise
>>how this solution works when nums.length is 1. In this case it should return

 return nums[0]+nums[1];<<

No, it shouldn't, it should return just nums[0]
Avatar of gudii9

ASKER

//return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1];
return 
        nums.length==0
        ?
           0
        :
       [b]    ([/b]
             nums.length==1
             ?
               nums[0]
             :
               (nums[0]+nums[1])
       [b]    )[/b]
         ;

Open in new window


above highlighted brackets are crucial for my understanding. Now get picture with those brackets without them i lost originally.

How to put those brackets in below single line

return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums)[1]

can i write as above? But i am getting below error

public int sum2(int[] nums) {


return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums)[1];


Compile problems:


Error:      return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums)[1];
                                                      ^^^^^^^^^^^^
The operator + is undefined for the argument type(s) int, int[]


see Example Code to help with compile problems

please advise
return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums[1]);
gudii9, Just in case you didn't notice what ozo pointed out, the parenthesis needs to be outside the nums[1]) and not in between nums and the opening bracket.