Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Ex06_StringMatrixRotation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int degrees = getDegrees(scanner);
- List<List<String>> matrix = new ArrayList<>();
- fillTheMatrix(scanner, matrix);
- int size = matrix.size();
- String[][] matrix90i270 = new String[matrix.get(0).size()][size];
- String[][] matrix0i180 = new String[size][matrix.get(0).size()];
- if (degrees / 4 == 90 || degrees / 8 == 90) {
- //matrix0i180;
- } else if (degrees == 90 || degrees / 5 == 90 || degrees / 9 == 90) {
- // matrix90i270;
- rotate90and450and900degrees(matrix, matrix90i270);
- //for (int col = 0; col < matrix.get(0).size(); col++) {
- // for (int row = 0; row < matrix.size(); row++) {
- // String curr = matrix.get(matrix.size()-1-row).get(col);
- //
- //
- // matrix90i270[col][row]= curr;
- // }
- printMatrix(matrix90i270);
- } else if (degrees / 3 == 90 || degrees / 7 == 90) {
- // matrix90i270;
- for (int col = 0; col < matrix.get(0).size(); col++) {
- for (int row = 0; row < matrix.size(); row++) {
- //String curr = matrix.get(matrix.size()-1-row).get(col);
- //int innerIndex = matrix.get(0).size()-1-row;
- String curr = matrix.get(0).get(size()-1);
- // matrix.get(matrix.size() - 1 - row).get(col);
- matrix90i270[col][row]= curr;
- }
- }
- printMatrix(matrix90i270);
- }
- }
- public static void printMatrix(String[][] matrix) {
- for (String[] currArray : matrix) {
- System.out.println(String.join("", currArray));
- }
- }
- public static void rotate90and450and900degrees(List<List<String>> matrix, String[][] matrix90i270) {
- for (int col = 0; col < matrix.get(0).size(); col++) {
- for (int row = 0; row < matrix.size(); row++) {
- String curr = matrix.get(matrix.size() - 1 - row).get(col);
- // int length1 = matrix90i270.length;
- // int length2 = matrix0i180.length;
- matrix90i270[col][row] = curr;
- }
- }
- }
- public static void fillTheMatrix(Scanner scanner, List<List<String>> matrix) {
- String input = scanner.nextLine();
- int maxChars = 0;
- while (!input.equals("END")) {
- List<String> arr = Arrays.stream(input.split("")).collect(Collectors.toList());
- matrix.add(arr);
- int countChars = arr.size();
- if (countChars > maxChars) {
- maxChars = countChars;
- }
- input = scanner.nextLine();
- }
- for (List<String> currList : matrix) {
- for (int i = currList.size(); i < maxChars; i++) {
- currList.add(" ");
- }
- }
- }
- public static int getDegrees(Scanner scanner) {
- String input = scanner.nextLine();
- String regex = "(?<rotate>[A-za-z]*)\\((?<degrees>[0-90-9]*?)\\)";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- int degrees = 0;
- while (matcher.find()) {
- String result = matcher.group("degrees");
- degrees = Integer.parseInt(result);
- }
- return degrees;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement