Assessment 4_6_sample

Instructions

This assessment is designed to be completed in 10 minutes or less.

Copy the template below and get all the test cases to pass. Hard-coding answers will not receieve credit; your solution must work for valid arrays.

The problem

Imagine that the user provides a two dimensional list. Return a one dimensional list that is the flattened version of that list. For example, if the user enters [[3, 1], [2, 4, 3], [], [1]], your code would return the array [3, 1, 2, 4, 3, 1].

You will have access to the cleanResult method here as in the previous assignments, if needed.

Please do not use any Java code or libraries that we have not covered in class; this defeats the purpose of the assessment and will not receive credit.

Code Template and Test cases

import java.util.Arrays;

public class Assess4_6_Sample{

  public static int[] flatten(int[][] arr){
    int[] result = new int[2];
    return result;
  }

  public static int[] cleanResult(int[] array){
    return Arrays.stream(array).filter(num -> num != 0).toArray();    
  }

  public static void main(String[] args){

    int [][] arr1 = {{}};
    int [] result1 = {};
    System.out.println("test1: " + Arrays.equals(cleanResult(flatten(arr1)), result1));
    int [][] arr2 = {{1}};
    int [] result2 = {1};
    System.out.println("test2: " + Arrays.equals(cleanResult(flatten(arr2)), result2));
    int [][] arr3 = {{1, 2}};
    int [] result3 = {1, 2};
    System.out.println("test3: " + Arrays.equals(cleanResult(flatten(arr3)), result3));
    int [][] arr4 = {{4, 6}, {1, 2}};
    int [] result4 = {4, 6, 1, 2};
    System.out.println("test4: " + Arrays.equals(cleanResult(flatten(arr4)), result4));
    int [][] arr5 = {{4, 6}, {1, 2, 3}};
    int [] result5 = {4, 6, 1, 2, 3};
    System.out.println("test5: " + Arrays.equals(cleanResult(flatten(arr5)), result5));
    int [][] arr6 = {{4, 6}, {1, 2, 3}, {4}};
    int [] result6 = {4, 6, 1, 2, 3, 4};
    System.out.println("test6: " + Arrays.equals(cleanResult(flatten(arr6)), result6));
    int [][] arr7 = {{4, 6}, {}, {1, 2, 3}, {4}};
    int [] result7 = {4, 6, 1, 2, 3, 4};
    System.out.println("test7: " + Arrays.equals(cleanResult(flatten(arr7)), result7));
    int [][] arr8 = {{}, {4, 6}, {}, {1, 2, 3}, {4}};
    int [] result8 = {4, 6, 1, 2, 3, 4};
    System.out.println("test8: " + Arrays.equals(cleanResult(flatten(arr8)), result8));
  }

}