Skip to content

Instantly share code, notes, and snippets.

@3355844
Created January 24, 2016 16:27
Show Gist options
  • Save 3355844/95657718c94c1349b7f1 to your computer and use it in GitHub Desktop.
Save 3355844/95657718c94c1349b7f1 to your computer and use it in GitHub Desktop.
package HomeWorkSecond;
/**
* Created by Andrey on 10.01.2016.
*/
public class ArraysTask1FrequentElemFinder {
public static void main(String[] args) {
int[] array = {1, 2, 1, 0, 3};
System.out.print(findFrequentElem(array));
}
public static int findFrequentElem(int[] array) {
int result = 0;
int maxIndex = 0;
for (int i : array) {
int count = 0;
for (int j : array) {
if (i == j) {
count++;
}
if (maxIndex < count) {
maxIndex = count;
result = i;
}
}
}
return result;
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 11.01.2016.
*/
public class ArraysTask2CyclicShifter {
public static void main(String[] args) {
int[] array = {0, 1, 2, 3, 4};
int shiftPosition = 1;
cyclicShift(array, shiftPosition);
}
public static void cyclicShift(int[] array, int shiftPozition) {
int[] reloadArray = new int[array.length];
int reload = array.length - 1;
while (shiftPozition < 0) {
shiftPozition += array.length;
}
while (shiftPozition > reload) {
shiftPozition -= array.length;
}
reload = shiftPozition;
for (int i : array) {
if (reload > array.length - 1) reload = 0;
reloadArray[reload] = i;
reload++;
}
for (int j : reloadArray) {
System.out.print(j + ", ");
}
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 12.01.2016.
*/
public class ArraysTask3SpiralCreator {
public static void main(String[] args) {
int rows = 7;
int cols = 8;
int[][] array = createSpiral(rows, cols);
}
public static int[][] createSpiral(int rows, int cols) {
checker(rows, cols);
int[][] array = new int[rows][cols];
madeSpiralArray(array);
printArray(array);
return array;
}
public static void madeSpiralArray(int[][] array) {
int indexCols = 0;
int indexRows = 0;
int lengthRows = array.length - 1;
int lengthCols = array[0].length - 1;
int digitResult = ((lengthCols + 1) * (lengthRows + 1));
int modulus = 0;
int count = 1;
while (count < digitResult) {
for (int i = indexCols + modulus; i < lengthCols - modulus; i++) {
array[indexRows + modulus][i] = count;
count++;
}
for (int i = indexRows + modulus; i < lengthRows - modulus; i++) {
array[i][lengthCols - modulus] = count;
count++;
}
for (int i = lengthCols - modulus; i > indexCols + modulus; i--) {
array[lengthRows - modulus][i] = count;
count++;
}
for (int i = lengthRows - modulus; i > indexRows + modulus; i--) {
array[i][indexCols + modulus] = count;
count++;
}
if (lengthRows % 2 == 0 && lengthCols % 2 == 0 && lengthCols == lengthRows)
array[lengthRows / 2][lengthRows / 2] = digitResult;
modulus++;
}
return;
}
public static void checker(int rows, int cols) {
if (rows <= 0 || cols <= 0) throw new IllegalArgumentException(" rows or cols is less then one ");
}
public static void printArray(int[][] array) {
for (int[] i : array) {
for (int j : i) {
System.out.print(j + "\t");
}
System.out.println();
}
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 15.01.2016.
*/
public class ArraysTask4PascalTriangleCreator {
public static void main(String[] args) {
int n = 2;
createPascalTriangle(n);
}
public static int[][] createPascalTriangle(int n) {
check(n);
int rows = n + 1;
int[][] triangle = new int[rows][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < i + 1; j++) {
if (j == 0) {
triangle[i][j] = 1;
System.out.print(triangle[i][j] + "\t");
} else {
triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
System.out.print(triangle[i][j] + "\t");
}
}
System.out.println();
}
return triangle;
}
public static void check(int n) {
if (n < 0) throw new IllegalArgumentException(" Your numb is less then zero :");
}
}
package HomeWorkSecond;
/**
* Created by Andrey on 19.01.2016.
*/
public class ArraysTask5MatrixPacking {
public static void main(String[] args) {
int[][] array = {{1, 2, 3}, {0, 0, 0}, {4, 5, 6}};
packMatrix(array);
}
public static int[][] packMatrix(int[][] array) {
int[][]packMatrix;
int indexRows = indexRowsFinder(array);
int indexCols = indexColsFinder(array);
packMatrix = new int[indexRows][indexCols];
packMatrix = arrayToPackMatrixWriter(array, indexCols, packMatrix);
print(packMatrix);
return array;
}
public static int [][] arrayToPackMatrixWriter(int[][] array, int indexCols, int[][] packMatrix) {
int cols = 0;
int rows = 0;
for (int i = 0; i < array[0].length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i][j] > 0) {
packMatrix[rows][cols] = array[i][j];
cols++;
cols = checkCols(indexCols, cols);
rows = rowPlus(cols, rows);
}
}
}return packMatrix;
}
public static void print(int[][] array) {
for (int[] i : array) {
for (int j : i) {
System.out.print(j + "\t");
}
System.out.println();
}
}
public static int rowPlus(int cols, int rows) {
if (cols == 0) {
rows++;
} return rows;
}
public static int checkCols(int indexCols, int cols) {
if (cols == indexCols) {
cols = 0;
}
return cols;
}
public static int indexColsFinder(int[][] array) {
int indexCols = 0;
for (int i = 0; i < array[0].length; i++) {
int count = 0;
for (int j = 0; j < array.length; j++) {
count = indexMaker(array[i][j], count);
}
indexCols = count;
}
return indexCols;
}
public static int indexRowsFinder(int[][] array) {
int indexRows = 0;
for (int i = 0; i < array[0].length; i++) {
int indexCols = 0;
for (int j = 0; j < array.length; j++) {
indexCols = indexMaker(array[i][j], indexCols);
}
indexRows = indexMaker(indexCols, indexRows);
}
return indexRows;
}
public static int indexMaker(int i, int index) {
if (i > 0) index++;
return index;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment