Solved

isEverywhere  challenge

Posted on 2016-08-03
19
106 Views
Last Modified: 2016-08-07
Hi,

I am working on below challenge

http://codingbat.com/prob/p110222

Psedo code description of approach :
1. loop through array
2.check every adjacent values has given val
3. if yes return true
4. if no return false





I am not passing all tests
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      

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 2
  • +1
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 41742289
Their tests are wrong. e.g.

isEverywhere([2, 1, 2, 1, 1, 2], 2) → false	true	X	

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41742432
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;
}

Open in new window


is my code is correct? any modifications i can do to this. I am having hard time with edge cases? any tips to be 100% right on edge cases all the time?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41742755
Their tests are wrong. e.g.

How so? That array has two 1's next to each other, so it should fail.

is my code is correct?

Looks like you're checking to see if both elements equal the value. Is that what you want?
0
ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

 
LVL 7

Author Comment

by:gudii9
ID: 41742770
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?

that is what i understood from above challenge. i could be wrong?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41742779
Think carefully about when that if statement will return true and when it will return false. Will it return true if you pass it [1, 1]? How about [1, 2]?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41742785
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
Amusingly the problem is much easier when you don't take the path i did ;)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742830
Will it return true if you pass it [1, 1]? How about [1, 2]?

i have not get this. method needs two arguments right? what is second argument?

  isEverywhere([1, 1],1)  is true
How about isEverywhere([1, 2], 1)  is false
How about isEverywhere([1, 2], 2) also false?
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 41742840
Here's one of the tests:

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.)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742875
it is again looks like edge case issue
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742880
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.)

i stretching further in my thinking in terms of logic around this
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742885
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;
}

Open in new window

above fails below when i replace || with &&


Correct for more than half the tests

Your progress graph for this problem


i am confused mainly in 2 things

1. edge cases
2. usage of || and && since some experts used && for OR description of challenge and vice versa. How to know what to use?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41742909
"Return true if, for every pair, condition X is true." is logically equivalent to "Return false if, for any pair, condition X is false." When you're designing your code, you should use whichever is easier. In this case, you tried it the first way and couldn't get it to work, so it might be time to try it the second way.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41743191
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." 

Open in new window



above corollary approach failing below tests
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      
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41743204
Once again, break it down. Take this test and figure out why your code is failing:

isEverywhere([3, 1], 3) → true
0
 
LVL 7

Author Comment

by:gudii9
ID: 41745904
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." 

Open in new window


i fixded above test by taking 100% sure approach which is if 3 is there in {3,1} then it is true 100% of the time irrespective of following pair of elements



if no target value in {3.1} we cannot say 100% as false since following pair of values may have target value so that is bad approach in this case(which happened to be my original approach)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41745906
isEverywhere([1, 2, 1, 3], 2) → false      true      X

i was not clear why above is expected false.

i thought it should be true as target int 2 is present in first pair

similarly i though below should be true

isEverywhere([1, 2, 1, 3, 4], 1) → false      true      X
0
 
LVL 7

Author Comment

by:gudii9
ID: 41745908
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false

i thought above 2 also true as per below explanation of challenge
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.
0
 
LVL 4

Accepted Solution

by:
Hammadh Abdul Rahman earned 250 total points
ID: 41745977
I guess there is a mistake in the question.

"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."

It should actually be "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 elements has that value. Return true if the given value is everywhere in the array."

I have underlined the pair that causes the condition to fail.
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false
0
 
LVL 7

Author Comment

by:gudii9
ID: 41746403
I got it.

corollary or back ward reverse approach works.

psuedocode description of loic is


1. iterate each element
2. check first element and second elemt as not equal to target element then return isEverywhere false 100% sure case
3. else return true case.

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;
}

Open in new window


above pass all tests
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question