Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

second 6 is 7 challenge

Posted on 2014-11-09
13
Medium Priority
?
120 Views
Last Modified: 2014-11-16
Hi,

i am  trying below challenge

http://codingbat.com/prob/p110019

I have not understood on problem statement when they say like
count instances where the second "6" is actually a 7.

i wrote as below

public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==nums[i+1])
  count++;
  }
  return count;
}

Open in new window


some test cases are failing as below
Expected      Run            
array667({6, 6, 2}) → 1      1      OK         
array667({6, 6, 2, 6}) → 1      1      OK         
array667({6, 7, 2, 6}) → 1      0      X         
array667({6, 6, 2, 6, 7}) → 2      1      X         
array667({1, 6, 3}) → 0      0      OK         
array667({6, 1}) → 0      0      OK         
array667({}) → 0      0      OK         
array667({3, 6, 7, 6}) → 1      0      X         
array667({3, 6, 6, 7}) → 2      1      X         
array667({6, 3, 6, 6}) → 1      1      OK         
array667({6, 7, 6, 6}) → 2      1      X         
array667({1, 2, 3, 5, 6}) → 0      0      OK         
array667({1, 2, 3, 6, 6}) → 1      1      OK         

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
  • 5
  • 4
  • 4
13 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 40431925
Count the number of times that 6, 6 appears, plus the number of times that 6, 7 appears
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40432049
Your code detects whenever there are 2 identical numbers appearing one after another in the array.

So your code would detect
6,6   (which is correct)
but also
3,3  (which is not)

Also the instructions say you need to find pairs like this:
6,6
or
6,7

So you want code something like this:
public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==6 && nums[i+1] == 7)
  count++;
  }
  return count;
}

Open in new window

which would detect one of those pairs - the 6,7 combination.

So you need to add code to make it also recognize the 6,6 combination.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435959
public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==nums[i+1]|| nums[i]==6&&nums[i+1]==7)
  count++;
  }
  return count;
}

Open in new window

i wrote as above . It pass all the tests.
does it looks ok?
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 84

Expert Comment

by:ozo
ID: 40436525
Although it passes all the tests at the challenge link, the count would be different from the solution given at the challenge link if it was tested with
array667({1,1})
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40436563
Yeah - like ozo pointed out, you don't really want this

if (nums[i]==nums[i+1] ... )

Open in new window


in the test as that's just checking if two numbers in the sequence are identical.

The problem is asking for specifically checking for 6s and 7s, not any two numbers.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437907
, you don't really want this

By removing that as below

public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==6&&nums[i+1]==7)
  count++;
  }
  return count;
}

Open in new window


i am failing test cases as below
Expected	Run		
array667({6, 6, 2}) → 1	0	X	    
array667({6, 6, 2, 6}) → 1	0	X	    
array667({6, 7, 2, 6}) → 1	1	OK	    
array667({6, 6, 2, 6, 7}) → 2	1	X	    
array667({1, 6, 3}) → 0	0	OK	    
array667({6, 1}) → 0	0	OK	    
array667({}) → 0	0	OK	    
array667({3, 6, 7, 6}) → 1	1	OK	    
array667({3, 6, 6, 7}) → 2	1	X	    
array667({6, 3, 6, 6}) → 1	0	X	    
array667({6, 7, 6, 6}) → 2	1	X	    
array667({1, 2, 3, 5, 6}) → 0	0	OK	    
array667({1, 2, 3, 6, 6}) → 1	0	X	    

Open in new window


Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437910
return the number of times that two 6's are next to each other

how to incorporate above in to if condition . please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40438908
Can you think of a condition that is true when two 6's are next to each other?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441094
 if(nums[i]==6&&nums[i+1]==7)

Open in new window



as of now i am able to check 6 and 7 are adjacent. Since i am using 6 in this condition not sure how to check if another 6 also adjacent. Not able to think. Also i wonder why thinking is hard sometimes. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441115
Perhaps after trying http://codingbat.com/prob/p121853
you can then come back to http://codingbat.com/prob/p110019 with a new idea.
0
 
LVL 28

Accepted Solution

by:
dpearson earned 1000 total points
ID: 40441656
Can you see what this tests for?

  if(nums[i]==6 && (nums[i+1] == 6 || nums[i+1] == 7))

Open in new window


And how we can use it in the whole method like this?

public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==6 && (nums[i+1] == 6 || nums[i+1] == 7))
  count++;
  }
  return count;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40442647
public int array667(int[] nums) {
int count=0;
  for(int i=0;i<nums.length-1;i++)
  {
  if(nums[i]==6 && (nums[i+1] == 6 || nums[i+1] == 7))
  count++;
  }
  return count;
}

Open in new window


i like above solution where we can check same index for 2 different values 6 as well as 7. This is new perspective opened for me.
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40445992
This is new perspective opened for me.
Excellent.  That's the whole point of expert's exchange :)

Glad you've got this now.

Doug
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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