gudii9
asked on
271 challenge
Hi
I am trying below challenge
http://codingbat.com/prob/p167430
I was not sure on how to proceed. please advise
I am trying below challenge
http://codingbat.com/prob/p167430
I was not sure on how to proceed. please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
return true if the array contains any of
2, 7, -1
or
2, 7, 0
or
2, 7, 1
or
2, 7, 2
or
2, 7, 3
I have to check in sequence and in pattern right. please advise
1, 6, 0 also valid right?I believe so
ASKER
please advise on how should i start thinking about this challenge and proceed?
ASKER
Please advise
I think you have got to try a few moves and ideas first.
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j]==7 && nums[j]==1)
return true;
}
}
return false;
}
i wrote like above. passing in some test cases and failing in others
Expected Run
has271({1, 2, 7, 1}) → true false X
has271({1, 2, 8, 1}) → false false OK
has271({2, 7, 1}) → true false X
has271({3, 8, 2}) → true false X
has271({2, 7, 3}) → true false X
has271({2, 7, 4}) → false false OK
has271({2, 7, -1}) → true false X
has271({2, 7, -2}) → false false OK
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
has271({2, 7, 5, 10, 1}) → false false OK
has271({2, 7, -2, 4, 10, 2}) → false false OK
please advise
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1can never be true?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
i see your point now. I improved as below my success rate in test cases but not 100% yet. Please advise
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}
}
return false;
}
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false false OK
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true false X
has271({2, 7, 3}) → true false X
has271({2, 7, 4}) → false false OK
has271({2, 7, -1}) → true false X
has271({2, 7, -2}) → false false OK
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
has271({2, 7, 5, 10, 1}) → false false OK
has271({2, 7, -2, 4, 10, 2}) → false false OK
ASKER
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
I wonder how above two test cases supposed to be true according to below code challenge description
Given an array of ints, return true if it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
has271({1, 2, 7, 1}) → true
has271({1, 2, 8, 1}) → false
has271({2, 7, 1}) → true
5 and -2 are never 'near differ by 2' from 1 right?
please advise
has271({2, 7, 5, 10, 4}) → true false X
a value (5), followed by the value plus 5 (10), followed by the value minus 1 (4).
has271({2, 7, -2, 4, 9, 3}) → true false X
a value (4), followed by the value plus 5 (9), followed by the value minus 1 (3).
a value (5), followed by the value plus 5 (10), followed by the value minus 1 (4).
has271({2, 7, -2, 4, 9, 3}) → true false X
a value (4), followed by the value plus 5 (9), followed by the value minus 1 (3).
Yeah - but that's not all there is to the story.
The final "value" you have to aim at, can be within a tolerance of 2 from "the value". You need to think about how you do that.
The final "value" you have to aim at, can be within a tolerance of 2 from "the value". You need to think about how you do that.
ASKER
still thinking. Seems bit challenging example
OK so what you need to think about is that you are given a set of ints. It doesn't matter what they are.
You are looking for a pattern somewhere in these ints. The search - or "thinking" - you have to do and are asked to carry out is :
* You are "told" that the first int is the "pattern setter" let's call it. They have used the word "value" actually.
* You are also "told" that you are looking for a "pattern" involving just 3 of the ints you are given. You might be given more than 3 ints, you might be given fewer than 3, and you could be given exactly 3 ints. So however many ints you are given, you must ensure that you look at all of them (the entire [] ) to see if 3 of them follow the pattern that you have been asked to find. For this to happen, you obviously need to have in the back of your mind that the use of a loop could well come into the solution.
* THE "PATTERN" (the task is) "are there 3 consecutive ints like this : 1) pattern-setter (first) value, followed by 2) pattern-setter PLUS 5, followed by (in the third position) 3) a value which is NOT MORE THAN 2 different from the pattern-setter after 1 has been taken away from the pattern-setter"?
If your ints are for example 8,13,5 then would the pattern have been found in these ints or not???
Well - 8 is our pattern-setter, so that is a given (we accept that no matter what else we are thinking about). Right, so we know that we are asked to discover whether the next int is equal to pattern-setter plus 5. Is it? Yes it is, it's 13. 8 + 5 = 13. Right, now we need to know whether the 3rd int is equal to the pattern-setter minus 1 (which is 8-1 =7) and then ALSO BE not more than 2 away from 7. So is 5 more than two away from 7? No it IS 2 away from 7, so it satisfies our third criterion. Now we have evaluated 3 ints and found that they satisfy the question, we can return true. We found the pattern.
8,13,5 was ok. 8,13,6 would have been too. 8,13,7 would have been also. Can you see the meta here now? If so, go back to the code and match it up with the above explanatory steps, and go over it until you see it flow.
You are looking for a pattern somewhere in these ints. The search - or "thinking" - you have to do and are asked to carry out is :
* You are "told" that the first int is the "pattern setter" let's call it. They have used the word "value" actually.
* You are also "told" that you are looking for a "pattern" involving just 3 of the ints you are given. You might be given more than 3 ints, you might be given fewer than 3, and you could be given exactly 3 ints. So however many ints you are given, you must ensure that you look at all of them (the entire [] ) to see if 3 of them follow the pattern that you have been asked to find. For this to happen, you obviously need to have in the back of your mind that the use of a loop could well come into the solution.
* THE "PATTERN" (the task is) "are there 3 consecutive ints like this : 1) pattern-setter (first) value, followed by 2) pattern-setter PLUS 5, followed by (in the third position) 3) a value which is NOT MORE THAN 2 different from the pattern-setter after 1 has been taken away from the pattern-setter"?
If your ints are for example 8,13,5 then would the pattern have been found in these ints or not???
Well - 8 is our pattern-setter, so that is a given (we accept that no matter what else we are thinking about). Right, so we know that we are asked to discover whether the next int is equal to pattern-setter plus 5. Is it? Yes it is, it's 13. 8 + 5 = 13. Right, now we need to know whether the 3rd int is equal to the pattern-setter minus 1 (which is 8-1 =7) and then ALSO BE not more than 2 away from 7. So is 5 more than two away from 7? No it IS 2 away from 7, so it satisfies our third criterion. Now we have evaluated 3 ints and found that they satisfy the question, we can return true. We found the pattern.
8,13,5 was ok. 8,13,6 would have been too. 8,13,7 would have been also. Can you see the meta here now? If so, go back to the code and match it up with the above explanatory steps, and go over it until you see it flow.
ASKER
i need to come up some example based on above notes
OK, so this part should be really for you alone, as you have said several times in the past that you could see the ideas behind the code more easily in direct English, rather than in straight code. So now is your chance to think up a good problem ( or Googl for one) - and then try to give thought to a coding solution for it, given what you know.
ASKER
this challenge is going over my head. I will close this and try later.
For this challenge, you probably shouldn't worry too much about using more cases than you need to.
ASKER
sure. Let me rethink again and again and come up with solution
ASKER
Can you see why
nums[j]==2 && nums[j]==7 && nums[j]==1
can never be true?
above is never true because at same index of array 3 numbers cannot be there.
My solution as below is true for some cases but false for some other
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1)
return true;
}
}
return false;
}
please advise
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false false OK
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true false X
has271({2, 7, 3}) → true false X
has271({2, 7, 4}) → false false OK
has271({2, 7, -1}) → true false X
has271({2, 7, -2}) → false false OK
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
has271({2, 7, 5, 10, 1}) → false false OK
has271({2, 7, -2, 4, 10, 2}) → false
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
return true;
}
}
}
return false;
}
i updated my code and passed more tests as below
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false false OK
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true false X
has271({2, 7, 3}) → true true OK
has271({2, 7, 4}) → false false OK
has271({2, 7, -1}) → true true OK
has271({2, 7, -2}) → false false OK
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
has271({2, 7, 5, 10, 1}) → false false OK
has271({2, 7, -2, 4, 10, 2}) → false false OK
stil failing on 4 test cases. please advise
Do you understand why those 4 test cases should return true?
If so, can you add conditions to handle them?
If so, can you add conditions to handle them?
So I wasted the time and effort of my comment at ID: 40444324 - which you have not read at all apparently.
Instead, you have hard-wired your code to look at "271". I said in my above comment, that IT DOES NOT MATTER what those 3 ints are - they COULD BE ANYTHING - "0,1,2", "7,9,11" . . . you name it.
Re-read my quoted comment, and think again.
---------
And . . . the last code you posted looks NOTHING LIKE the code skeleton I posted at : ID: 40438936
Instead, you have hard-wired your code to look at "271". I said in my above comment, that IT DOES NOT MATTER what those 3 ints are - they COULD BE ANYTHING - "0,1,2", "7,9,11" . . . you name it.
Re-read my quoted comment, and think again.
---------
And . . . the last code you posted looks NOTHING LIKE the code skeleton I posted at : ID: 40438936
ASKER
sure i will try
You are looking for the relationship between 3 consecutive ints.
That relationship HAS to be :
1st int = anything
2nd int = 1st int plus 5
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.
If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
That relationship HAS to be :
1st int = anything
2nd int = 1st int plus 5
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.
If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
ASKER
has271({3, 8, 2}) → true false X
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X
i am failing above four.3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 3.you mean it should be as below right
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.
ASKER
1st int = anythingabove is clear
2nd int = 1st int plus 5
Below is not clear. I am trying to re read again and again to understand it
If you are given more than 3 ints, then move along the array one place (leaving the first int out of it) and include the next int in the triplet. This is where you need the for loop.
ASKER
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*has271({3, 8, 2}) → true false X
has271({4, 5, 3, 8, 0}) → true false X
has271({2, 7, 5, 10, 4}) → true false X
has271({2, 7, -2, 4, 9, 3}) → true false X*/
has271({3, 8, 2});
}
public static boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;j++)
{
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==1){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==2){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==3){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==0){
return true;
}
if(nums[j]==2 && nums[j+1]==7 && nums[j+2]==-1){
return true;
}
}
}
return false;
}
}
i am getting error. please advies
you mean it should be as below right
3rd int = 1st int -1 give or take 2 (so if 1st int is "4", third int can only be : 3, 2 or 1 (going down from 4), or 4 or 5 going upwards from 4.
No. Going upwards from 3. Because 3 would be the "expected" value - because that's 4-1, which is 3. So 3 is in a sense the "correct" value BUT the rules say that you can deviate by 2 or less from that "correct" value, so you can have 3+1 (4) or 3+2 (5) going up.
Going down - if your 1st int is 4 - then the "correct" third int SHOULD be 3, but it COULD also be 2 or 1.
Look, whatever code you decide to write, you simply :
* get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.
* get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.
ASKER
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]={1,8,2};
System.out.println("value is--->"+has271(arr));
}
public static boolean has271(int[] nums) {
/** get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[1]==nums[0]+5 && nums[2]==nums[0]-1);
j=j+2;
return true;
}
}
return false;
}
}
i wrote as aboe to check second and third value of the array based on first. I see i am getting true no matter what i pass as belowvalue is--->true
Once i make above working i will think about adding additionally
or is 2 away from the first int-1 IN EITHER DIRECTION.please advise
ASKER
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false true X
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true true OK
has271({2, 7, 3}) → true true OK
has271({2, 7, 4}) → false true X
has271({2, 7, -1}) → true true OK
has271({2, 7, -2}) → false true X
has271({4, 5, 3, 8, 0}) → true true OK
has271({2, 7, 5, 10, 4}) → true true OK
has271({2, 7, -2, 4, 9, 3}) → true true OK
has271({2, 7, 5, 10, 1}) → false true X
has271({2, 7, -2, 4, 10, 2}) → false true X
i am failing some test cases as above
ASKER
public class Test34 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]={1,8,2};
System.out.println("value is--->"+has271(arr));
}
public static boolean has271(int[] nums) {
/** get the first int
* see if the next int is equal to the first int +5 and if it is then :
* see if the third int is equal to the first int minus 1, or is 2 away from the first int-1 IN EITHER DIRECTION.
and if you are given more than 3 ints, then put the above into a for loop, and move along the array.*/
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[1]==nums[0]+5 && (nums[2]==nums[0]-1||nums[2]==nums[0]-2||nums[2]==nums[0]+1||nums[2]==nums[0]+2));
j=j+2;
return true;
}
}
return false;
}
}
i updated my code as above still failing 5 test cases
I feel i am getting close
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false true X
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true true OK
has271({2, 7, 3}) → true true OK
has271({2, 7, 4}) → false true X
has271({2, 7, -1}) → true true OK
has271({2, 7, -2}) → false true X
has271({4, 5, 3, 8, 0}) → true true OK
has271({2, 7, 5, 10, 4}) → true true OK
has271({2, 7, -2, 4, 9, 3}) → true true OK
has271({2, 7, 5, 10, 1}) → false true X
has271({2, 7, -2, 4, 10, 2}) → false true X
please advise
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
if(i>=3){
for(int j=0;j<i-1;)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
j=j+2;
return true;
}
}
return false;
}
i updated further still above 5 test cases failing. please advise
You are not looking at the skeleton code I posted still. Why are you not asking questions about that? Why are you making statements like this >if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||n ums[j+2]== nums[j]-2| |nums[j+2] ==nums[j]+ 1||nums[j+ 2]==nums[j ]+2));
You are given 3 ints.
1st int is . .......................... ... 103
Right.
2nd int is .......................... .... 109
Is 103 + 5 = to 109 ? No. So that means the test already flunked. Return false.
How about .......... 1st int = 90.
2nd int = 95.
Is 90 + 5 = to 95??????????????????????. Answer = YES, it is. Right. So that's one "if statement out of the way!"
3rd int = 92
Is 92 = to (95-1), give or take 2????????????????????????? ?????????? ?????????? ?????????? ?????????? ????
YES!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! ! IT IS !!!!!!!!!!!!!!!!!!!!!!!!!! !!
2nd if statisfied. WE HAVE A SATISFACTORY int triplet here!. Return TRUE!
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
Now . . . . the places where you see 90, 95 and 92 above :
PUT ANY INTEGERS YOU LIKE, AND GO THROUGH THE STEPS JUST LIKE WE WENT THROUGH THEM ABOVE - EXACTLY THE SAME LOGIC, BUT USING YOUR NEW NUMBERS. SO WHEN YOU ADD 5, YOU ADD 5 TO THE FIRST NUMBER. WHEN YOU SEE -1, YOU TAKE ONE AWAY FROM YOUR FIRST INT. WHEN YOU DECIDE WHETHER THE 3RD INT YOU CHOSE IS OK, ASK YOUSELF IF IT IS NOT MORE THAN 2 DIFFERENT FROM THE FIRST INT-1.
1st int is . ..........................
Right.
2nd int is ..........................
Is 103 + 5 = to 109 ? No. So that means the test already flunked. Return false.
How about .......... 1st int = 90.
2nd int = 95.
Is 90 + 5 = to 95??????????????????????. Answer = YES, it is. Right. So that's one "if statement out of the way!"
3rd int = 92
Is 92 = to (95-1), give or take 2?????????????????????????
YES!!!!!!!!!!!!!!!!!!!!!!!
2nd if statisfied. WE HAVE A SATISFACTORY int triplet here!. Return TRUE!
--------------------------
Now . . . . the places where you see 90, 95 and 92 above :
PUT ANY INTEGERS YOU LIKE, AND GO THROUGH THE STEPS JUST LIKE WE WENT THROUGH THEM ABOVE - EXACTLY THE SAME LOGIC, BUT USING YOUR NEW NUMBERS. SO WHEN YOU ADD 5, YOU ADD 5 TO THE FIRST NUMBER. WHEN YOU SEE -1, YOU TAKE ONE AWAY FROM YOUR FIRST INT. WHEN YOU DECIDE WHETHER THE 3RD INT YOU CHOSE IS OK, ASK YOUSELF IF IT IS NOT MORE THAN 2 DIFFERENT FROM THE FIRST INT-1.
ASKER
let me try.
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]== nums[j]-2| |nums[j+2] ==nums[j]+ 1||nums[j+ 2]==nums[j ]+2));
j=j+2;
return true;
}
return false;
}
i modifiede as above. passed few more test cases but still not 100% correct
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false true X
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true true OK
has271({2, 7, 3}) → true true OK
has271({2, 7, 4}) → false true X
has271({2, 7, -1}) → true true OK
has271({2, 7, -2}) → false true X
has271({4, 5, 3, 8, 0}) → true true OK
has271({2, 7, 5, 10, 4}) → true true OK
has271({2, 7, -2, 4, 9, 3}) → true true OK
has271({2, 7, 5, 10, 1}) → false true X
has271({2, 7, -2, 4, 10, 2}) → false true X
Sorry that i am not able to completely follow on this challenge(since it seems bit stretch for me). Please advise how to fix and improve my code
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]+1||nums[j+2]==nums[j]+2));
return true;
}
return false;
}
i modified furthwer now only 4 test cases failing instead of 5 compared to earlier comment
ASKER
has271({1, 2, 8, 1}) → false true X
has271({2, 7, 5, 10, 1}) → false true X
has271({2, 7, -2, 4, 10, 2}) → false true X
has271({2, 7, 4}) → false true X
({1, 2, 8, 1}) so . . . 1 + 5 = 6, so this one fails immediately. 2 + 5 = 7, so this fails because you are looking for an 8 there, and now there are not enough other ints left to test 3 in a row. So this set should return false.
({2, 7, 5, 10, 1}) . . . so 2 + 5 =7, so we are ok so far. But 5 is more than 2 away from 2-1 (which is 1), so that's a fail. So now 7 +5 is 12, not 10, so that's a fail. Then finally, 5 +5 IS 10, but again 5-1 (which is 4) is more than 2 away from 1, so the whole thing fails again to false.
has271({2, 7, -2, 4, 10, 2}) .... so 2+5 = 7, but 2-1 (which is 1) is not 2 away from -2, so fail. Now 7+5 isn't -2, so another fail. Now -2 +5 isn't 4, so another fail. Finally 4+5 isn't 10, so there's another fail, and no need to even look at the last 2 in that sequence.
has271({2, 7, 4}) . . . so 2+5 is 7, so that's ok, and so is 2-1 (which is 1) 2 or less away from 4? No, so another fail to false.
({2, 7, 5, 10, 1}) . . . so 2 + 5 =7, so we are ok so far. But 5 is more than 2 away from 2-1 (which is 1), so that's a fail. So now 7 +5 is 12, not 10, so that's a fail. Then finally, 5 +5 IS 10, but again 5-1 (which is 4) is more than 2 away from 1, so the whole thing fails again to false.
has271({2, 7, -2, 4, 10, 2}) .... so 2+5 = 7, but 2-1 (which is 1) is not 2 away from -2, so fail. Now 7+5 isn't -2, so another fail. Now -2 +5 isn't 4, so another fail. Finally 4+5 isn't 10, so there's another fail, and no need to even look at the last 2 in that sequence.
has271({2, 7, 4}) . . . so 2+5 is 7, so that's ok, and so is 2-1 (which is 1) 2 or less away from 4? No, so another fail to false.
Here is some idiosyncratic pseudo-code which may help you :
if ( 2nd int = 1st int plus 5 ) {we are ok so far; now test the third int; is the 3rd int AT MOST 2 different from the 1st int-1? If it is ( return true; );}
{else return false;}
Keep doing this until you can't test 3 sequential ints from the series you are given because you've already moved along the series too far.
if ( 2nd int = 1st int plus 5 ) {we are ok so far; now test the third int; is the 3rd int AT MOST 2 different from the 1st int-1? If it is ( return true; );}
{else return false;}
Keep doing this until you can't test 3 sequential ints from the series you are given because you've already moved along the series too far.
ASKER
is the 3rd int AT MOST 2 different from the 1st int-1? If it is ( return true; );}
i mistakenly thought as below instead till now
is the 3rd int AT MOST 2 different from the 1st int? If it is ( return true; );}
Given an array of ints, return true if it contains a 2, 7, 1 pattern -- a value, followed by the value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
ASKER
public boolean has271(int[] nums) {
int i=nums.length;
for(int j=0;j<i-2;j++)
{
if(nums[j+1]==nums[j]+5 &&(nums[j+2]==nums[j]-1||nums[j+2]==nums[j]-2||nums[j+2]==nums[j]-3||nums[j+2]==nums[j]||nums[j+2]==nums[j]+1));
return true;
}
return false;
}
i wrote as above still 5 failing
The reason your code is failing is because you have failed to understand the question.
I am not sure that any more help will do you any good here. The task is simple.
You get a series of ints.
The SECOND int has to be EXACTLY equal to the first int + 5.
If that test is passed, then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.
I am not sure that any more help will do you any good here. The task is simple.
You get a series of ints.
The SECOND int has to be EXACTLY equal to the first int + 5.
If that test is passed, then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.
In this series of ints:
1,2,32,33,45,766,12,2,7,1
only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :
1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
1,2,32,33,45,766,12,2,7,1
only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :
1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
. . . . so given the same set of ints as I just used above, with one change :
1,2,32,33,45,766,12,2,7,-1
would that series return true or false??
Don't write code - just look at the series with your eyes.
1,2,32,33,45,766,12,2,7,-1
would that series return true or false??
Don't write code - just look at the series with your eyes.
ASKER
public boolean has271(int[] nums) {
if(nums.length>2){
for(int i=0;i<nums.length-1;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i]-nums[i+2])<=3))
{
return true;
}
}
}
return false;
}
i kind of modified and failing in 2 instead
Expected Run
has271({1, 2, 7, 1}) → true true OK
has271({1, 2, 8, 1}) → false false OK
has271({2, 7, 1}) → true true OK
has271({3, 8, 2}) → true true OK
has271({2, 7, 3}) → true true OK
has271({2, 7, 4}) → false true X
has271({2, 7, -1}) → true true OK
has271({2, 7, -2}) → false false OK
has271({4, 5, 3, 8, 0}) → true true OK
has271({2, 7, 5, 10, 4}) → true true OK
has271({2, 7, -2, 4, 9, 3}) → true true OK
has271({2, 7, 5, 10, 1}) → false true X
has271({2, 7, -2, 4, 10, 2}) → false false OK
ASKER
The SECOND int has to be EXACTLY equal to the first int + 5.100% clear
then the third int has to be equal to the first int -1, OR equal to the first int -1,GIVE OR TAKE 2.still clear 50%
ASKER
In this series of ints:100% clear
1,2,32,33,45,766,12,2,7,1
only the last 3 ints will pass the test and return true. There are 8, repeat 8 sets of possible 3 ints you have to evaluate :
1,2,32
2,32,33
32,33,45
33,45,766
45,766,12
766,12,2
12,2,7
and
2,7,1.
ASKER
. . . . so given the same set of ints as I just used above, with one change :
1,2,32,33,45,766,12,2,7,-1
would that series return true or false??
based on the challenge it should be true
followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.above highlighted one is in 2,7,1 or the one italicized above?
basically 3rd=2nd plus/minus atmost 2(or may be 3) right?
>>basically 3rd=2nd plus/minus atmost 2(or may be 3) right? << NO!
2nd = 1st +5. End of story.
3rd = (1st-1) give or take 2.
2nd = 1st +5. End of story.
3rd = (1st-1) give or take 2.
>>based on the challenge it should be true<<
Correct.
Correct.
ASKER
3rd = (1st-1) give or take 2.
i got it
public boolean has271(int[] nums) {
if(nums.length>2){
for(int i=0;i<nums.length-2;i++)
{
if((nums[i+1]==(nums[i]+5))&& (Math.abs(nums[i+2]-(nums[i]-1))<=2))
{
return true;
}
}
}
return false;
}
above passed fine
if i think pattern numbers as x y z
y=x+5
z=(x-1) plus or minus 2
followed by the value minus 1. Additionally the 271 counts even if the "1" differs by 2 or less from the correct value.
above highlighted one is in 2,7,1 or the one italicized above?
I am having trouble getting my head around what you are asking here. But let me try :
"followed by the value minus 1" means : an int in the 3rd position which is equal to the 1st int -1.
" Additionally the 271 counts even if the "1" differs by 2 or less from the correct value" means : the int in the position that the 1 is in, in the 271 above, should return true EVEN IF the value in that position (always the third position of the 3 ints) is "OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int" - in other words, the 2 in the 271 above.
That sounds about right now.
See how much shorter your code is now you understand the challenge. :) Well done.
See how much shorter your code is now you understand the challenge. :) Well done.
ASKER
int in the position that the 1 is in
challenge should have mentioned as above instead of saying "1"
"OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int"
i was thinking above should be as below instead
"OUT" by as much as 2 from the correct value, and by "correct value", codingbat means "the 1st int - 1"
please correct me if i am wrong
Right - I made a mistake, which in my defence is hardly surprising amidst all this narrative.
So
codingbat means "the 1st int" - in other words, the 2 in the 271 above.
should indeed read :
codingbat means "the 1st int" - in other words, the 2 in the 271 above - BUT MINUS ONE IOW, whatever the first int is, but minus 1.
Anyway, you understood it all already, so don't start overthinking things now. Your own xyz explanation was what did the trick for you I think.
So
codingbat means "the 1st int" - in other words, the 2 in the 271 above.
should indeed read :
codingbat means "the 1st int" - in other words, the 2 in the 271 above - BUT MINUS ONE IOW, whatever the first int is, but minus 1.
Anyway, you understood it all already, so don't start overthinking things now. Your own xyz explanation was what did the trick for you I think.
ASKER
trick is experts advise.
ASKER