gudii9
asked on
fix23 challenge
Hi,
i am trying below challenge
http://codingbat.com/prob/p120347
i wrote as below
i am failing below
how to improve and fix my code. please advise
i am trying below challenge
http://codingbat.com/prob/p120347
i wrote as below
public int[] fix23(int[] nums) {
int numsNew[]=new int[3];
numsNew=nums;
numsNew[1]=0;
if(nums[1]==nums[2])
return numsNew;
else
return nums;
}
i am failing below
xpected Run
fix23({1, 2, 3}) → {1, 2, 0} {1, 0, 3} X
fix23({2, 3, 5}) → {2, 0, 5} {2, 0, 5} OK
fix23({1, 2, 1}) → {1, 2, 1} {1, 0, 1} X
fix23({3, 2, 1}) → {3, 2, 1} {3, 0, 1} X
fix23({2, 2, 3}) → {2, 2, 0} {2, 0, 3} X
fix23({2, 3, 3}) → {2, 0, 3} {2, 0, 3} OK
other tests
how to improve and fix my 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.
ASKER
public int[] fix23(int[] nums) {
int numsNew[]=new int[3];
int len =nums.length;
numsNew=nums;
for(int i=0;i<=len-2;i++)
{
if((nums[i]==2)&(nums[i+1]==3))
{
numsNew[i+1]=0;
}
}
return numsNew;
}
i wrote as below and passed all tests.
Expected Run
fix23({1, 2, 3}) → {1, 2, 0} {1, 2, 0} OK
fix23({2, 3, 5}) → {2, 0, 5} {2, 0, 5} OK
fix23({1, 2, 1}) → {1, 2, 1} {1, 2, 1} OK
fix23({3, 2, 1}) → {3, 2, 1} {3, 2, 1} OK
fix23({2, 2, 3}) → {2, 2, 0} {2, 2, 0} OK
fix23({2, 3, 3}) → {2, 0, 3} {2, 0, 3} OK
other tests
OK
Does below supposed to pass?
fix23({3, 2, 4}) → {0, 2, 4} {0,2, 4} OK
Does below supposed to pass?
No.
ASKER
No.Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.
is it because 2 followed 3 means always 3 should come afgter 2 not otherway(3 should come before 2 still that is following right??sorry english is not my first language??)
http://www.learnersdictionary.com/definition/follow
to come after
ASKER
1 a: to go or come after or behind (someone or something)
i saw the link it says after or behind both??
please advise
In this context:
{2 , 3}
^ ^
before after
^ ^
front back
mean the same thing
ASKER
got it. So 2 should come before then 3 should come after (that is like saying 2 in the array immediately followed by a 3
that is why below came like this without replacing 3 with 0
fix23({3, 2, 1}) → {3, 2, 1} {3, 2, 1}
)
now i take follow means after to make it easy
that is why below came like this without replacing 3 with 0
fix23({3, 2, 1}) → {3, 2, 1} {3, 2, 1}
)
now i take follow means after to make it easy
if( nums[i-1]==2&&nums[ i ]==3 ){ nums[ i ]=0; }
}
return nums;