We help IT Professionals succeed at work.

make2 challenge

247 Views
Last Modified: 2015-07-13
Hi,

I am trying below challenge

make2
http://codingbat.com/prob/p143461

i have not understood the requirement.
Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}
please advise
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};

Author

Commented:
elements from a followed by the elements from b:
{4, 5}, {1, 2, 3} → {4, 5,1, 2, 3}
{4}, {1, 2, 3})→ {4, 1,  2, 3}
{}, {1, 2} → {1, 2}
as much as will fit in in a new array length 2:
{4, 5,1, 2, 3 } → {4, 5}
{4, 1,  2, 3}→ {4, 1}
{1, 2} → {1, 2}

this is clear

Author

Commented:
public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
  d=a+b;
  int[] e=new int[2];
  e={d[0],d[1]}
  return e;
  
  }

Open in new window


i tried like this but not working

Author

Commented:
public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
 // d=a+b;
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window


i tried as above and got below error. Please advise on how to fix it.

What is the best way to add two array so that i can get its first two elements from resultant array?

Author

Commented:
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};

i di dnot understand above solution. why we are only checking a>0 and a>1 etc not b>0 or b>1 etc.

Please advise
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Since we are guaranteed that "there will be 2 or more elements available between the 2 arrays.",
we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b[0] or b[1-a.length] are valid

Author

Commented:
there will be 2 or more elements available between the 2 arrays

what it mean by above sentence. I am not clear on that. please advise

when i try as below getting below error

public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
 // d=a+b;
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window


Compile problems:


Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined


see Example Code to help with compile problems



Also cannot we add one array with other array simply without converting to List and then add as below?


 // d=a+b;//why this is not possible some waht like this to resolve this challenge
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));


please advise

Author

Commented:
public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
 // d=a+b;
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window


i thought this solution should work 100% but i got below error

Compile problems:


Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined


see Example Code to help with compile problems

is it beacuase codingbat site wont support ArrayList?

Please advise

Author

Commented:
b[1-a.length]};

Open in new window


i am not clear on above. Why are we dependent on a withing b array. please advise

Author

Commented:
we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b[0] or b[1-a.length] are valid

what it means by negation of guards?

please advise

Author

Commented:
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};
  

Open in new window


how above solution is working when a length is 0. please advise
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
make2({}, {1, 2}) → {1, 2}
when a length is 0
a.length>0 is not true, so a.length>0?a[0]:b[0] is b[0]
a.length>1 is not true, so a.length>1?a[1]:b[1-a.length] is b[1-a.length] == b[1-0] == b[1]
so we return new int[]{b[0],b[1]} as required

Author

Commented:
b[1-a.length]

i never got this idea. Is there is any alternate, simple approach to resolve this challenge. Please advise

public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
 // d=a+b;
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window

i thought above solution should work 100% but i got below error

Compile problems:


Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined


see Example Code to help with compile problems


is it beacuase codingbat site wont support ArrayList? cannot i use arraylist in that site? please advise
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
public int[] make2(int[] a, int[] b) {
  List<Integer> list=new ArrayList<Integer>(a.length+b.length);
  for( int i:a ){
       list.add(i);
  }
  for( int i:b ){
       list.add(i);
  }
  int[] e=new int[2];
  e[0]=list.get(0);
  e[1]=list.get(1);
  return e;
}

Author

Commented:
  public int[] make2(int[] a, int[] b) {
  List<Integer> list=new ArrayList<Integer>(a.length+b.length);
  for( int i:a ){
       list.add(i);
  }
  for( int i:b ){
       list.add(i);
  }
  int[] e=new int[2];
  e[0]=list.get(0);
  e[1]=list.get(1);
  return e;
}

Open in new window


i like this solution close to what i am thinking with ArrayList approach. But i still do not understand why i fail with below code

public int[] make2(int[] a, int[] b) {
  //int[] c=new int[2];
  int[] d=new int[2];
 // d=a+b;
     List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }
  
  
  

Open in new window


i get below error
Compile problems:


Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined


see Example Code to help with compile problems

i wonder why error messages are not so obviously clear to me but looks some gibberish.
what they meant constroctor not defined?

How to fix my code so that i can make use of Arrays.asList.

please advise

Author

Commented:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestArrayEx {
	public static void main(String[] args) {
		int a[] = { 4, 5 };
		int b[] = { 1, 2, 3 };

		/*
		 * Integer c[]= toObject(a); Integer d[]=toObject(b);
		 */

		System.out.println("-->" + make2(a, b)[0] + "-->" + make2(a, b)[1]);

	}

	public static int[] make2(int[] a, int[] b) {

		Integer c[] = toObject(a);
		Integer d[] = toObject(b);
		List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));
		list.addAll(Arrays.asList(d));

		/*
		 * int[] result = new int[IntegerArray.length]; for (int i = 0; i <
		 * IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
		 * return result;
		 */

		// Integer list2[] = new Integer[arrlist.size()];
		// list2 = arrlist.toArray(list2);

		/*
		 * int[] result = new int[list.length]; for (int i = 0; i <
		 * IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
		 * return result;
		 */
		Integer list2[] = new Integer[list.size()];
		list2 = list.toArray(list2);

		int[] e = new int[2];
		e[0] = list2[0];
		e[1] = list2[1];
		return e;

	}

	public static Integer[] toObject(int[] intArray) {

		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;
	}

}

Open in new window


above worked. Not sure right way of doing or not. Please advise.

I still cannot run in codingbat as i have to move toObject method inside though

Author

Commented:
public int[] make2(int[] a, int[] b) {
	Integer c[] = toObject(a);
		Integer d[] = toObject(b);
		List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));
		list.addAll(Arrays.asList(d));		
		Integer list2[] = new Integer[list.size()];
		list2 = list.toArray(list2);

		int[] e = new int[2];
		e[0] = list2[0];
		e[1] = list2[1];
		return e;
	}

	public Integer[] toObject(int[] intArray) {
		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;
	}

  
  
  
  
  
  
  

Open in new window


i refactored as above and passed all tests. Please advise if above approach is correct?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.