gudii9
asked on
has23
Hi,
i am trying
http://codingbat.com/prob/p171022
i failed one test
how to improve and fix my above code
please advise
i am trying
http://codingbat.com/prob/p171022
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3)
return true;
else
return false;
}
return false;
}
i failed one test
how to improve and fix my above code
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The important thing here gudii, is to know how and where you went wrong. Are you clear on that?
ASKER
Your code fails because you break out of the loop at the first false result (thus returning false)
why it does that?
| should check other value also right(i am not using short cut OR like || which breaks as soon as it see first false?)
please advise
ASKER
[quote][quote]public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3)
return true;
if(nums[i]==3 | nums[i]==2)
return true;
else
return false;
}
return false;
}[/quote][/quote]
i thought above code should pass all but failed. i wonder why. please advise
ASKER
return nums[0]==2||nums[0]==3||nums[1]==2||nums[1]==3;
i like this solutiin. How to write using for loop and int i etc to take make it more universal solution similar to my approach. please advise
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3) return true;
}
return false;
}
to take make it more universal solution similar to my approach. please adviseBut the problem domain is specific and the problem is not really reusable so universality, while a good aspiration often, is probably irrelevant here
Perhaps gudii9 now wants to talk about a different problem.
Maybe one that is similar to http://codingbat.com/prob/p171022 but without the array length 2 specification.
In which case, it may be helpful if we could get a clear description of the new problem.
If we do now want to talk about a different problem perhaps it help to avoid confusion about which question is being answered if the different problem is asked in a separate question.
Maybe one that is similar to http://codingbat.com/prob/p171022 but without the array length 2 specification.
In which case, it may be helpful if we could get a clear description of the new problem.
If we do now want to talk about a different problem perhaps it help to avoid confusion about which question is being answered if the different problem is asked in a separate question.
ASKER
How my non working below solution
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3)
return true;
else
return false;
}
return false;
}
different from working solution
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3) return true;
}
return false;
}
i see my solution has extra return false; apart from that everything looks same to me. please advise
ASKER
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3)
return true;
}
else
return false;
// return false;
}
above gives below error
Compile problems:
Error: else
^^^^
Syntax error on token "else", delete this token
why i cannot write like
else
return false;
please advise
delete this token
public boolean has23(int[] nums) {
int len=nums.length;
for(int i=0;i<=len-1;i++)
{
if(nums[i]==2 | nums[i]==3)
return true;
}
//else
return false;
// return false;
}
why i cannot write likeelse is part of if...else
ASKER
so i can write if without else but i cannot write a method with if and then without return after if block right?
Is my understanding is correct?
if i write a method with if (with return ) then else(with return) and then without return at the end of the method still the compilation error is thrown by compiler right?
Is my understanding is correct?
if i write a method with if (with return ) then else(with return) and then without return at the end of the method still the compilation error is thrown by compiler right?
else is part of if...else
there is no such thing as for...else in java
there is no such thing as for...else in java
ASKER
public int[] front11(int[] a, int[] b) {
int len1=a.length;
int len2=b.length;
int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
if(len1>=1&len2>=1){
c[0]=a[0];
c[1]=b[0];
// return c;
}
if(len1>=1&len2==0){
c[0]=a[0];
//c[1]=0;
// return c;
}
if(len1==0&len2>=1){
// c[0]=0;
c[0]=b[0];
//return c;
}
else{
return c;
}
//return c;
}
gives error
Compile problems:
Error: public int[] front11(int[] a, int[] b) {
^^^^^^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int[]
Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.
see Example Code to help with compile problems
when i write as below
public int[] front11(int[] a, int[] b) {
int len1=a.length;
int len2=b.length;
int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
if(len1>=1&len2>=1){
c[0]=a[0];
c[1]=b[0];
// return c;
}
if(len1>=1&len2==0){
c[0]=a[0];
//c[1]=0;
// return c;
}
if(len1==0&len2>=1){
// c[0]=0;
c[0]=b[0];
//return c;
}
else{
return c;
}
return c;
}
compiler is happy eventhough both else return and last return statement of method does same job in case of challenge(http://codingbat.com/prob/p128270) similar to this one
It may be better to discus this in
https://www.experts-exchange.com/questions/28691071/front11-challenge.html
but you could write as
https://www.experts-exchange.com/questions/28691071/front11-challenge.html
but you could write as
public int[] front11(int[] a, int[] b) {
int len1=a.length;
int len2=b.length;
int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
if(len1>=1&len2>=1){
c[0]=a[0];
c[1]=b[0];
// return c;
}
if(len1>=1&len2==0){
c[0]=a[0];
//c[1]=0;
// return c;
}
if(len1==0&len2>=1){
// c[0]=0;
c[0]=b[0];
return c;
}
else{
return c;
}
// return c;
}
You can do this :
or this :
but not this :
public boolean thingy(){
if(true){return true;}
else return false;
}
or this :
public static boolean thingy(){
if(true){return true;}
//else return false;
return false;
}
but not this :
public static boolean thingy(){
if(true){return true;}
//else return false;
}
And . . . I don't know what the others think about this, but IMHO you should ALWAYS frame if . . . else statements like this :
if( x ) { . . . ;}
else{ . . . ;}
USING curly braces EVERY TIME, even if you only use the if part.
if( x ) { . . . ;}
else{ . . . ;}
USING curly braces EVERY TIME, even if you only use the if part.
I agree with krakatoa about the curly braces. I don't even like framing return values on the same line as the Boolean evaluation
if (true) {
return true;
} else {
return false;
}
if (true) {
return true;
} else {
return false;
}
USING curly braces EVERY TIME would be my personal preference.
I even tend to treat ){ and }else{ as two tokens, even though java treats them as five.
I even tend to treat ){ and }else{ as two tokens, even though java treats them as five.
I don't even like framing return values on the same line as the Boolean evaluation
Yes, agreed. Good point. . . . and I admit to that being a bad habit of mine when I do stuff quickly, as here on EE. But it should not be in a real piece of code that way.
Ditto agreement with ozo's too.
ASKER
i got it about formatting. Also i got
You can do this :
public boolean thingy(){
if(true){return true;}
else return false;
or this :
public static boolean thingy(){
if(true){return true;}
//else return false;
return false;
}
but not this :
public static boolean thingy(){
if(true){return true;}
//else return false;
}
ASKER
public boolean thingy(){
if(true)
{
return true;
}
else return false;
}
above and below codes are technically same right? (even though way we write with or without else block?) please advise
public static boolean thingy(){
if(true)
{
return true;
}
//else return false;
return false;
}
if(true)
{
return true;
}
else return false;
}
above and below codes are technically same right? (even though way we write with or without else block?) please advise
public static boolean thingy(){
if(true)
{
return true;
}
//else return false;
return false;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... again, you can read my comment here, because hard-wired reassignment of array values to new ones is fine for tiny arrays, but when dealing with larger arrays, it makes no sense to write verbatim code.