Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import static java.util.stream.Collectors.toList;
- import static java.util.stream.IntStream.of;
- public class FirstSteps {
- public static void main(String[] args) {
- System.out.println("Hello world!");
- }
- public int sum(int x, int y) {
- return x + y;
- }
- public int mul(int x, int y) {
- return x * y;
- }
- public int div(int x, int y) {
- return x / y;
- }
- public int mod(int x, int y) {
- return x % y;
- }
- public boolean isEqual(int x, int y) {
- return x == y;
- }
- public boolean isGreater(int x, int y) {
- return x > y;
- }
- public boolean isInsideRect(int xLeft, int yTop, int xRight, int yBottom, int x, int y) {
- return (x >= xLeft) && (y >= yTop) && (x <= xRight) && (y <= yBottom);
- }
- public int sum(int[] array) {
- return array.length > 0 ?
- of(array)
- .sum() : 0;
- }
- public int mul(int[] array) {
- return array.length > 0 ?
- of(array)
- .reduce((firstMultiplier, secondMultiplier) -> firstMultiplier * secondMultiplier)
- .getAsInt() : 0;
- }
- public int min(int[] array) {
- return array.length > 0 ?
- of(array)
- .summaryStatistics()
- .getMin() : Integer.MAX_VALUE;
- }
- public int max(int[] array) {
- return array.length > 0 ?
- of(array)
- .summaryStatistics()
- .getMax(): Integer.MIN_VALUE;
- }
- public double average(int [] array) {
- return array.length > 0 ?
- of(array)
- .summaryStatistics()
- .getAverage(): 0;
- }
- public boolean isSortedDescendant(int[] array) {
- if (array.length > 0) {
- int[] sortedDescendantArray = Arrays.copyOf(array,array.length);
- Arrays.sort(sortedDescendantArray);
- for (int index = 0; index < sortedDescendantArray.length / 2; index++) {
- int temp = sortedDescendantArray[index];
- sortedDescendantArray[index] = sortedDescendantArray[sortedDescendantArray.length - 1 - index];
- sortedDescendantArray[sortedDescendantArray.length - 1 - index] = temp;
- }
- for (int index = 0; index < sortedDescendantArray.length - 1; index++) {
- if (!isGreater(sortedDescendantArray[index], sortedDescendantArray[index + 1])) {
- return false;
- }
- }
- return Arrays.equals(sortedDescendantArray, array);
- }
- return true;
- }
- public void cube(int[] array) {
- for (int index = 0; index < array.length; index++) {
- array[index] = (int) Math.pow(array[index], 3);
- }
- }
- public boolean find(int[]array, int value) {
- return of(array)
- .boxed()
- .collect(toList()).contains(value);
- }
- public void reverse(int[]array) {
- for (int currentIndex = 0; currentIndex < array.length / 2; currentIndex++) {
- int tempElement = array[currentIndex];
- array[currentIndex] = array[array.length - 1 - currentIndex];
- array[array.length - 1 - currentIndex] = tempElement;
- }
- }
- public boolean isPalindrome(int[] array) {
- if (array.length == 0) {
- return true;
- }
- int[] reverseArray = Arrays.copyOf(array, array.length);
- reverse(reverseArray);
- return Arrays.equals(array, reverseArray);
- }
- public int sum(int[][] matrix) {
- int sum = 0;
- for (int[] line: matrix) {
- sum += of(line)
- .sum();
- }
- return sum;
- }
- public int max(int[][] matrix) {
- if (Arrays.deepEquals(matrix, new int[matrix.length][matrix[0].length])) {
- return Integer.MIN_VALUE;
- }
- int max = matrix[0][0];
- for (int[] line: matrix) {
- max = Math.max(max, max(line));
- }
- return max;
- }
- public int diagonalMax(int[][] matrix) {
- if (Arrays.deepEquals(matrix, new int[matrix.length][matrix[0].length])) {
- return Integer.MIN_VALUE;
- }
- int max = matrix[0][0];
- for (int lineIndex = 0, columnIndex = 0; lineIndex < matrix.length && columnIndex < matrix[lineIndex].length; lineIndex++, columnIndex++) {
- max = Math.max(max, matrix[lineIndex][columnIndex]);
- }
- return max;
- }
- public boolean isSortedDescendant(int[][] matrix) {
- for (int[] line: matrix) {
- if (!isSortedDescendant(line)) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement