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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
advertisement module in core php | 4 | 94 | |
eclipse formatting | 6 | 43 | |
Not needed | 13 | 58 | |
Java - sorting a list of objects where the properties of the objects can change during the sort | 7 | 31 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
12 Experts available now in Live!