Solved

has12 challenge

Posted on 2016-08-03
13
77 Views
Last Modified: 2016-08-05
Hi,

I am working on below challenge
http://codingbat.com/prob/p169260

Psedo code description of approach :
1. loop though given array
2. check where is 1
3. check where is 2
4. if 2place is somewhere further down than by 1 return true
5 else return false
I wrote my code as below
public boolean has12(int[] nums) {
  int onePlace=0;
  int twoPlace=0;
  for(int i=0;i<nums.length;i++){
    if(nums[i]==1){
      onePlace=i;
      
    }
     if(nums[i]==2){
      twoPlace=i;
      
    }
  }
  return (twoPlace>onePlace);
}

Open in new window





I am passing all tests

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

Expert Comment

by:phoffric
ID: 41741758
>> return true if there is a 1 in the array with a 2 somewhere later in the array

By hand, what should the answer be for:
5 1 6 2 7 7 8 1 0 9

Does your program agree with your by hand answer?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41741874
First of all, there is a test missing.

What does your function return, when you're having the arrays [ 2 ] or [ 0, 2 ] ?

So you need to handle that.

Also the code path is not optimal. Your test function describes an implicit termination criteria for the loop.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742465
actually my code passes all tests.
public boolean has12(int[] nums) {
  int onePlace=0;
  int twoPlace=0;
  for(int i=0;i<nums.length;i++){
    if(nums[i]==1){
      onePlace=i;
      
    }
     if(nums[i]==2){
      twoPlace=i;
      
    }
  }
  return (twoPlace>onePlace);
}

Open in new window

Expected      Run            
has12([1, 3, 2]) → true      true      OK      
has12([3, 1, 2]) → true      true      OK      
has12([3, 1, 4, 5, 2]) → true      true      OK      
has12([3, 1, 4, 5, 6]) → false      false      OK      
has12([3, 1, 4, 1, 6, 2]) → true      true      OK      
has12([2, 1, 4, 1, 6, 2]) → true      true      OK      
has12([2, 1, 4, 1, 6]) → false      false      OK      
has12([3, 5, 9]) → false      false      OK      
has12([3, 5, 1]) → false      false      OK      
has12([3, 2, 1]) → false      false      OK      
has12([1, 2]) → true      true      OK      
has12([1, 1]) → false      false      OK      
has12([1]) → false      false      OK      
has12([]) → false      false      OK      
other tests
OK      
i think i am good on this unless you want to make some suggestions on improvement, corrections?
By hand, what should the answer be for:
5 1 6 2 7 7 8 1 0 9//yes

Does your program agree with your by hand answer?// let me start eclipse in debug mode and double check it
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 41742602
>> By hand, what should the answer be for:  5 1 6 2 7 7 8 1 0 9//yes

>>  Does your program agree with your by hand answer?// let me start eclipse in debug mode and double check it
OK. Reason I gave you this case is that your program should produce a different result. Also, for this small program, it is useful to walk through the program by hand to see what result you come up with. Then walk through in the debugger. If you do that, you get three results, not all the same. Which one(s) is/are correct?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41742605
It is not necessarily true that the code tester is running through all the edge (corner) cases.
Only thing that counts is a description of the problem (including their example cases which we have seen sometimes helps to clarify ambiguities).
0
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 41742668
i think i am good on this unless you want to make some suggestions on improvement, corrections?

In terms of TDD? Yes. When all tests are green, then its okay. But only for a black box test.

As we have your source we can do a white box test and feed it with specially crafted samples. Like phoffric's [ 1, 2, 1] or my [ 2 ] and [ 0, 2 ].  Have you evaluate [ 2 ] and [ 0, 2] by hand?

You have asked for further enhancements:
Well passing this tests correctly is such an enhancement.
Another one is the given termination criteria.
And last but not least there is a simple restructuring possible to minimize the number of if-tests.

How to improve my design, approach, code? please advise

Think about our points. Correct those cases above. Ask again when the rest is unclear.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742760
Another one is the given termination criteria.
And last but not least there is a simple restructuring possible to minimize the number of if-tests.
sure. how to achieve above?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41742799
Well, I've crafted my test interval for a certain purpose.

Solving this is step 1. Cause it will give you the necessary hint for the other two points.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742837
other two points.

which points? i was not clear?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41743022
Sure. But do you have already a solution which gives the correct result for [ 2 ] and [ 0, 2 ]?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41743267
>> By hand, what should the answer be for:  5 1 6 2 7 7 8 1 0 9//yes

>>  Does your program agree with your by hand answer?// let me start eclipse in debug mode and double check it
OK. Reason I gave you this case is that your program should produce a different result. Also, for this small program, it is useful to walk through the program by hand to see what result you come up with. Then walk through in the debugger. If you do that, you get three results, not all the same. Which one(s) is/are correct?



Given an array of ints, return true if there is (not like all 1's to say 5 1 6 2 7 7 8 1 0 9 false due to bolded 1 )a 1 in the array with a 2 somewhere later in the array.

has12([1, 3, 2]) → true
has12([3, 1, 2]) → true
has12([3, 1, 4, 5, 2]) → true
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41743643
>> Given an array of ints, return true if there is (not like all 1's to say 5 1 6 2 7 7 8 1 0 9 false due to bolded 1 )a 1 in the array with a 2 somewhere later in the array.

I didn't understand your comment.

You answered true for the manual, by hand, check. I agree since there is a 1 in the array followed by a 2. (But I suppose that one could try to interpret the question as:
  • if there is more than one 1 in the array than every 1 must be followed by a 2; or
  • if there is more than one 1 in the array than at least the last 1 must be followed by a 2; or
  • other interpretations

But I agree with your "by hand" answer.
In that case, your posted program does not give True because the last 1 is not followed by a 2.
Can you post your own solution so that your new program agrees with your "by hand" result?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41743799
Have changed your code?

public boolean has12(int[] nums) {
    for (int i = 0; i < nums.length - 1; i++) {
        if (nums[i] == 1) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == 2) {
                    return true;
                }
            }
        }
    }
    return false;
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 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.

749 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