Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.ArrayList; import java.util.List; public class Zigzag { public static List zigzag(List array) { int height = array.size() - 1;

image


import java.util.ArrayList;
import java.util.List; 

public class Zigzag {

  public static List zigzag(List> array) {
     int height = array.size() - 1;
     int width = array.get(0).size() - 1;
     List result = new ArrayList<>();
     int row = 0, col = 0;
     // Your code below:
     // boolean goingDown = true;  // <- feel free to use this variable (or delete it)

     return result;
  }

  // Feel free to use this method -- it's intended to simplify your code in
  // the zigzag method. Otherwise, write your own, or delete it if you'd like.
  public static boolean outOfBounds(int row, int col, int height, int width) {
     return row < 0 || row > height || col < 0 || col > width;
  }

  // These are the test-cases to help you debug your code.
  // (Some test-cases are commented out. Feel free to uncomment them.)
  public static void main(String[] args) {

     List> table;
     List result;

     List row1, row2, row3, row4, row5, row6;  // in test cases, these will be table rows

     /*------------- START TEST 1 ---------------*/
     row1 = new ArrayList<>(List.of(1, 3, 4, 7));
     row2 = new ArrayList<>(List.of(2, 5, 6, 8));
     table = new ArrayList<>(List.of(row1, row2));
     result = zigzag(table);
     System.out.println("Test 1 result: \n\t " + result + "\n"); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]

     /*------------- START TEST 2 ---------------*/
     row1 = new ArrayList<>(List.of(1, 3, 4, 10));
     row2 = new ArrayList<>(List.of(2, 5, 9, 11));
     row3 = new ArrayList<>(List.of(6, 8, 12, 15));
     row4 = new ArrayList<>(List.of(7, 13, 14, 16));
     table = new ArrayList<>(List.of(row1, row2, row3, row4));
     result = zigzag(table);
     System.out.println("Test 2 result: \n\t " + result + "\n");  // should print [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

     /*------------- START TEST 3 ---------------*/
     row1 = new ArrayList<>(List.of(1)); // [1]
     table = new ArrayList<>(List.of(row1));
     result = zigzag(table);
     System.out.println("Test 3 result: \n\t " + result + "\n");  // should print [1]

     /*------------- START TEST 4 ---------------*/
     row1 = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7));
     table = new ArrayList<>(List.of(row1));
     result = zigzag(table);
     System.out.println("Test 4 result: \n\t " + result + "\n");  // should print [1, 2, 3, 4, 5, 6, 7]

     /*------------- START TEST 5 ---------------*/
     row1 = new ArrayList<>(List.of(1));
     row2 = new ArrayList<>(List.of(2));
     row3 = new ArrayList<>(List.of(3));
     row4 = new ArrayList<>(List.of(4));
     table = new ArrayList<>(List.of(row1, row2, row3, row4));
     result = zigzag(table);
     System.out.println("Test 5 result: \n\t " + result + "\n");  // should print [1, 2, 3, 4]
//
//      /*------------- START TEST 6 ---------------*/
//      row1 = new ArrayList<>(List.of(1, 3));
//      row2 = new ArrayList<>(List.of(2, 4));
//      row3 = new ArrayList<>(List.of(5, 7));
//      row4 = new ArrayList<>(List.of(6, 8));
//      row5 = new ArrayList<>(List.of(9, 10));
//      table = new ArrayList<>(List.of(row1, row2, row3, row4, row5));
//      result = zigzag(table);
//      System.out.println("Test 6 result: \n\t " + result + "\n");  // should print [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//
//      /*------------- START TEST 7 ---------------*/
//      row1 = new ArrayList<>(List.of(1, 21, -3, 4, 15, 6, -7, 88, 9));
//      row2 = new ArrayList<>(List.of(10, 11, 112, 12, 20, -1, -2, -3, -4));
//      row3 = new ArrayList<>(List.of(6, 8, 113, 19, 21, 0, 0, 0, 0));
//      row4 = new ArrayList<>(List.of(7, 2, 18, 22, -27, 12, 32, -11, 66));
//      row5 = new ArrayList<>(List.of(15, 17, 23, 226, 28, -28, -226, -23, -17));
//      row6 = new ArrayList<>(List.of(16, 24, 27, 299, 30, 29, 32, 31, 88));
//      table = new ArrayList<>(List.of(row1, row2, row3, row4, row5, row6));
//      result = zigzag(table);
//      System.out.println("Test 7 result: \n\t " + result + "\n");  // should print [1, 10, 21, -3, 11, 6, 7, 8, 112, 4,
//                                                                   //               15, 12, 113, 2, 15, 16, 17, 18, 19,
//                                                                   //               20, 6, -7, -1, 21, 22, 23, 24, 27,
//                                                                   //               226, -27, 0, -2, 88, 9, -3, 0, 12,
//                                                                   //               28, 299, 30, -28, 32, 0, -4, 0, -11,
//                                                                   //               -226, 29, 32, -23, 66, -17, 31, 88]

  }
}

The Zigzag Problem Write a method that takes a m x n two-dimensional array (that can be square-shaped when n = m) and returns a one-dimensional array of all the elements in the array in zigzag order. Zigzag starts at the top left corner of the two-dimensional array, goes down by one element and continues in a zigzag manner all the way to the bottom right corner. 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions

Question

Calculate the missing value.

Answered: 1 week ago