Skip to content

Instantly share code, notes, and snippets.

@Brutt
Created September 6, 2018 22:19
Show Gist options
  • Save Brutt/fcc4dbdb3a91e480b0f6fc9c8d591e21 to your computer and use it in GitHub Desktop.
Save Brutt/fcc4dbdb3a91e480b0f6fc9c8d591e21 to your computer and use it in GitHub Desktop.
class Part1 {
public int max(int a, int b, boolean showParams){
if (showParams){
System.out.print("(" + a + ", " + b + "): ");
}
return a > b ? a : b;
}
public int max(int a, int b, int c, boolean showParams){
if (showParams){
System.out.print("(" + a + ", " + b + ", " + c + "): ");
}
return max(a, max(b, c, false), false);
}
public int max(int a, int b, int c, int d, int e, boolean showParams){
if (showParams){
System.out.print("(" + a + ", " + b +", " + c +", " + d + ", " + e + "): ");
}
return max(a, b, max(c, d, e, false), false);
}
public int factorial(int a, boolean showParams){
if (showParams){
System.out.print("(" + a + "): ");
}
if (a <= 2){
return a;
}
return a * factorial(a-1, false);
}
/* https://en.wikipedia.org/wiki/Leap_year :
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
*/
public boolean isLeapYear(int year, boolean showParams){
if (showParams){
System.out.print("(" + year + "): ");
}
boolean result = false;
if (year > 0) {
if (year % 400 == 0) {
result = true;
} else if (year % 100 == 0) {
result = false;
} else if (year % 4 == 0) {
result = true;
}
}
return result;
}
public void fibonacci(int n, boolean showParams){
if (showParams){
System.out.print("(" + n + "): ");
}
int prevNum = 1;
if (prevNum <= n){
System.out.print(prevNum + ", ");
}else{
return;
}
int currentNum = 2;
if (currentNum <= n){
System.out.print(currentNum + ", ");
}else{
return;
}
for(int i=3; i<=n; i++) {
if (i==n){
System.out.print(currentNum + prevNum);
}else {
System.out.print(currentNum + prevNum + ", ");
}
currentNum += prevNum;
prevNum = currentNum - prevNum;
}
}
}
class Part2 {
public String convertCharArrayToString(char[] charArray){
StringBuilder resultString = new StringBuilder();
for(int i=0;i<charArray.length;i++){
resultString.append(String.valueOf(charArray[i]));
}
return resultString.toString();
}
public int getIndexForward(int[] intArray, int lookupNumber){
int index = -1;
for(int i=0;i<intArray.length;i++){
if (intArray[i] == lookupNumber){
index = i;
break;
}
}
return index;
}
public int getIndexBackward(int[] intArray, int lookupNumber){
int index = -1;
for(int i=intArray.length-1;i>0;i--){
if (intArray[i] == lookupNumber){
index = i;
break;
}
}
return index;
}
public void checkDivisionByNumber(int[] intArray, int divider){
for(int i=0;i<intArray.length;i++){
if (intArray[i] % divider == 0){
System.out.print(intArray[i] + ",");
}
}
}
public void printIntArray(int[] arrayInt){
System.out.print("[");
for (int i = 0; i < arrayInt.length; i++) {
if (i == arrayInt.length - 1){
System.out.print(arrayInt[i] + "]");
}else {
System.out.print(arrayInt[i] + ", ");
}
}
}
public int [] sortByAscending(int[] arrayInt, boolean showParams){
if(showParams){
printIntArray(arrayInt);
}
int[] arrayResult = arrayInt;
for (int i = 0; i < arrayResult.length - 1; i++) {
for (int j = i; j < arrayResult.length; j++) {
if (arrayResult[i] > arrayResult[j]){
arrayResult[i] += arrayResult[j];
arrayResult[j] = arrayResult[i] - arrayResult[j];
arrayResult[i] -= arrayResult[j];
}
}
}
return arrayResult;
}
public boolean isDuplicates(byte[] arrayByte){
boolean result = false;
boolean[] bufferArray = new boolean[256];
for (int i = 0; i < arrayByte.length; i++) {
if (!bufferArray[arrayByte[i] + 128]){
bufferArray[arrayByte[i] + 128] = true;
}else{
result = true;
break;
}
}
return result;
}
public boolean checkSequenceInArray(char[] firstArray, char[] secondArray){
boolean result = false;
int startIndex = 0;
int endIndex = secondArray.length;
boolean bufferResult;
while(endIndex <= firstArray.length){
bufferResult = true;
for (int i = startIndex; i < endIndex; i++) {
if (firstArray[i] != secondArray[i - startIndex]){
bufferResult = false;
break;
}
}
if(bufferResult){
result = true;
break;
}
startIndex++;
endIndex++;
}
return result;
}
public String[] getStringsWhichContainString(String[] stringArray, String string) {
String[] stringArrayBuffer = new String[stringArray.length];
int index = 0;
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i].contains(string)){
stringArrayBuffer[index] = stringArray[i];
index++;
}
}
String[] stringArrayResult = new String[index];
System.arraycopy(stringArrayBuffer,0,stringArrayResult,0,index);
return stringArrayResult;
}
}
class NumberConverter {
public static String intToString(int number){
StringBuilder result = new StringBuilder();
if (number < 0){
result.append('-');
number = number * -1;
}
int lengthOfNumbers = 0;
int[] patternsDivider = {1,10,100,1000,10_000,100_000,1_000_000,10_000_000,100_000_000,1_000_000_000};
for (int i = 0; i < patternsDivider.length; i++) {
if (number / patternsDivider[i] < 10) {
lengthOfNumbers = i + 1;
break;
}
}
int bufNum = number;
int prevNum = number;
for (int i = lengthOfNumbers; i > 0; i--) {
bufNum = (int)(bufNum / patternsDivider[i-1]);
result.append(bufNum);
bufNum = prevNum - bufNum * patternsDivider[i-1];
prevNum = bufNum;
}
return result.toString();
}
}
class Runner {
public static void main(String[] args) {
Part1 part1 = new Part1();
System.out.print("The maximum of two:");
System.out.println(part1.max(1,2, true));
System.out.print("The maximum of three:");
System.out.println(part1.max(7,2,3, true));
System.out.print("The maximum of five:");
System.out.println(part1.max(7,2,3,33,2, true));
System.out.print("Factorial:");
System.out.println(part1.factorial(5, true));
System.out.print("Is leap year:");
System.out.println(part1.isLeapYear(2401, true));
System.out.print("Fibonacci:");
part1.fibonacci(10, true);
System.out.println();
Part2 part2 = new Part2();
System.out.println("Char array to String: " + part2.convertCharArrayToString(new char[]{'a','b','c'}));
System.out.println("Forward: " + part2.getIndexForward(new int[]{1,2,333,4,5}, 333));
System.out.println("Backward: " + part2.getIndexBackward(new int[]{1,2,333,4,5}, 333));
System.out.print("Divider: ");
part2.checkDivisionByNumber(new int[]{1,2,333,4,51}, 3);
int[] arrayInt = part2.sortByAscending(new int[]{33,11,2,5,7,2,4,-5}, true);
System.out.print(" => ");
part2.printIntArray(arrayInt);
System.out.println();
System.out.println("Duplicates: " + part2.isDuplicates(new byte[]{-128,127,1,5,10,1}));
System.out.println("Sequence in char array: "
+ part2.checkSequenceInArray(new char[]{'a','a','b','c','d','e','b','c','d','e'},
new char[]{'a','a','c'}));
System.out.print("Check String in String array: ");
String[] stringArray = part2.getStringsWhichContainString(new String[]{"q55c4we","asd2","zcx2", "sd2c"},"d2");
for (int i = 0; i < stringArray.length; i++) {
System.out.print(stringArray[i] + ", ");
}
System.out.println();
/*part3*/
System.out.println(NumberConverter.intToString(-22_147));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment