isEverywhere([2, 1, 2, 1, 1, 2], 2) → false true X
Expected Run
isEverywhere([1, 2, 1, 3], 1) → true true OK
isEverywhere([1, 2, 1, 3], 2) → false true X
isEverywhere([1, 2, 1, 3, 4], 1) → false true X
isEverywhere([2, 1, 2, 1], 1) → true true OK
isEverywhere([2, 1, 2, 1], 2) → true true OK
isEverywhere([2, 1, 2, 3, 1], 2) → false true X
isEverywhere([3, 1], 3) → true true OK
isEverywhere([3, 1], 2) → false false OK
isEverywhere([3], 1) → true false X
isEverywhere([], 1) → true false X
isEverywhere([1, 2, 1, 2, 3, 2, 5], 2) → true true OK
isEverywhere([1, 2, 1, 1, 1, 2], 2) → false true X
isEverywhere([2, 1, 2, 1, 1, 2], 2) → false true X
isEverywhere([2, 1, 2, 2, 2, 1, 1, 2], 2) → false true X
isEverywhere([2, 1, 2, 2, 2, 1, 2, 1], 2) → true true OK
isEverywhere([2, 1, 2, 1, 2], 2) → true true OK
other tests
X
public boolean isEverywhere(int[] nums, int val) {
boolean result = false;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] == val && nums[i+1] == val)
result = true;
}
return result;
}
Their tests are wrong. e.g.
is my code is correct?
We'll say that a value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is that value. Return true if the given value is everywhere in the array.
isEverywhere([1, 2, 1, 3], 1) → true
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false
Is that what you want?
How so? That array has two 1's next to each other, so it should fail.Ah - i think i was misinterpreting the question. I thought they meant distinct pairs with no overlaps
Will it return true if you pass it [1, 1]? How about [1, 2]?
isEverywhere([3, 1], 3) → true
isEverywhere([3, 1], 3) → true
What will your if statement do in this situation?
(This is what I mean, by the way, when I suggest that you break down the problem into small pieces. This is a test you're failing; focus on understanding why.)
public boolean isEverywhere(int[] nums, int val) {
boolean result = false;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] == val || nums[i+1] == val)
result = true;
}
return result;
}
above fails below when i replace || with &&
Correct for more than half the tests
Your progress graph for this problem
public boolean isEverywhere(int[] nums, int val) {
boolean result = true;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] != val || nums[i+1] != val)
result = false;
}
return result;
}
//"Return true if, for every pair, condition X is true." is logically
//equivalent to "Return false if, for any pair, condition X is false."
Expected Run
isEverywhere([1, 2, 1, 3], 1) → true false X
isEverywhere([1, 2, 1, 3], 2) → false false OK
isEverywhere([1, 2, 1, 3, 4], 1) → false false OK
isEverywhere([2, 1, 2, 1], 1) → true false X
isEverywhere([2, 1, 2, 1], 2) → true false X
isEverywhere([2, 1, 2, 3, 1], 2) → false false OK
isEverywhere([3, 1], 3) → true false X
isEverywhere([3, 1], 2) → false false OK
isEverywhere([3], 1) → true true OK
isEverywhere([], 1) → true true OK
isEverywhere([1, 2, 1, 2, 3, 2, 5], 2) → true false X
isEverywhere([1, 2, 1, 1, 1, 2], 2) → false false OK
isEverywhere([2, 1, 2, 1, 1, 2], 2) → false false OK
isEverywhere([2, 1, 2, 2, 2, 1, 1, 2], 2) → false false OK
isEverywhere([2, 1, 2, 2, 2, 1, 2, 1], 2) → true false X
isEverywhere([2, 1, 2, 1, 2], 2) → true false X
other tests
X
isEverywhere([3, 1], 3) → true
public boolean isEverywhere(int[] nums, int val) {
boolean result = false;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] == val || nums[i+1] == val)
result = true;
i=i+1;
}
return result;
}
//"Return true if, for every pair, condition X is true." is logically
//equivalent to "Return false if, for any pair, condition X is false."
isEverywhere([1, 2, 1, 3], 2) → false true X
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false
We'll say that a value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is that value. Return true if the given value is everywhere in the array.
public boolean isEverywhere(int[] nums, int val) {
boolean result = true;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] != val && nums[i+1] != val)
result = false;
}
return result;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
github account with ecipse | 1 | 42 | |
Problem to page | 4 | 70 | |
How to remove numeric and alpha from an alphanumeric field? | 5 | 70 | |
Advice on Xojo as a development tool over VB. | 4 | 31 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
14 Experts available now in Live!