Compare commits
No commits in common. "519bc165dd0d6d34ff97b9113c42e40ac8ec5c36" and "44d88bb3e7d5250531473c037ac63a9a0030615f" have entirely different histories.
519bc165dd
...
44d88bb3e7
@ -2,6 +2,7 @@ package Uebungen.Uebung5;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.lang.Character;
|
import java.lang.Character;
|
||||||
public class Aufgabe2 {
|
public class Aufgabe2 {
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
// initiate scanner for input
|
// initiate scanner for input
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
@ -22,11 +23,13 @@ public class Aufgabe2 {
|
|||||||
sort(charAmountArr,usedCharArr);
|
sort(charAmountArr,usedCharArr);
|
||||||
histogramm(20,usedCharArr, charAmountArr);
|
histogramm(20,usedCharArr, charAmountArr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static char[] normalize(char[] workerArray){
|
public static char[] normalize(char[] workerArray){
|
||||||
for(int i=0; i<workerArray.length;i++)
|
for(int i=0; i<workerArray.length;i++)
|
||||||
workerArray[i]=Character.toLowerCase(workerArray[i]);
|
workerArray[i]=Character.toLowerCase(workerArray[i]);
|
||||||
return workerArray;
|
return workerArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void histogramm(int width, char[] availableChars, int[] charAmount){
|
public static void histogramm(int width, char[] availableChars, int[] charAmount){
|
||||||
int max = maximum(charAmount);
|
int max = maximum(charAmount);
|
||||||
for(int i=0;i<availableChars.length;i++) {
|
for(int i=0;i<availableChars.length;i++) {
|
||||||
@ -37,6 +40,7 @@ public class Aufgabe2 {
|
|||||||
for(int j=0;j<charAmount[i];j++) System.out.print("#");
|
for(int j=0;j<charAmount[i];j++) System.out.print("#");
|
||||||
System.out.println("x"+charAmount[i]);
|
System.out.println("x"+charAmount[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public static int scale(int value, int oldMax, int newMax){
|
public static int scale(int value, int oldMax, int newMax){
|
||||||
double calc = (double) value/oldMax*newMax;
|
double calc = (double) value/oldMax*newMax;
|
||||||
@ -94,6 +98,7 @@ public class Aufgabe2 {
|
|||||||
targetArray[targetPosition] = currChar; // add the current char
|
targetArray[targetPosition] = currChar; // add the current char
|
||||||
targetPosition++; // we added a new char, so we need to increment for the next one
|
targetPosition++; // we added a new char, so we need to increment for the next one
|
||||||
}
|
}
|
||||||
|
|
||||||
return targetArray;
|
return targetArray;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
package Uebungen.Uebung5;
|
|
||||||
|
|
||||||
public class Aufgabe3 {
|
|
||||||
public static void main(String[] args){
|
|
||||||
int[] myArray = {1,2,3};
|
|
||||||
outputArray(myArray);
|
|
||||||
myArray = anfuegen(myArray, 4);
|
|
||||||
outputArray(myArray);
|
|
||||||
}
|
|
||||||
//
|
|
||||||
public static int[] anfuegen(int[] inputArray, int addedInt){
|
|
||||||
int[] outputArray = new int[inputArray.length+1];
|
|
||||||
for(int i=0;i<inputArray.length;i++)
|
|
||||||
outputArray[i] = inputArray[i];
|
|
||||||
outputArray[inputArray.length]=addedInt;
|
|
||||||
return outputArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void outputArray(int[] inputArray){
|
|
||||||
System.out.print("[");
|
|
||||||
for(int i=0;i<inputArray.length;i++)
|
|
||||||
if(inputArray.length-1==i)
|
|
||||||
System.out.print(inputArray[i]);
|
|
||||||
else
|
|
||||||
System.out.print(inputArray[i]+" ,");
|
|
||||||
System.out.println("]");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
package Uebungen.Uebung5;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Arrays;
|
|
||||||
public class Aufgabe4 {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int timeout = 150; // milliseconds
|
|
||||||
int[] testValues = {2,34,134,4,53,11,87,142,742,115,00,411,61,33,23456,2346,12,6234562,4562,4562,56245,62456,2456,24562,1,1345,13451,435,1345,1345,134,51345,134513,451,345};
|
|
||||||
Arrays.sort(testValues);
|
|
||||||
for (int value:testValues){
|
|
||||||
int result = binarySearch(testValues,value, timeout);
|
|
||||||
System.out.println("Found: "+testValues[result]+" at index " + result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static int binarySearch(int[] inputArray, int value,int timeout){
|
|
||||||
System.out.println("Searching for " + value);
|
|
||||||
printArray(inputArray);
|
|
||||||
int res=-1;
|
|
||||||
int rangeMax=inputArray.length-1, rangeMin=0,center,modulo;
|
|
||||||
long lastdate=0;
|
|
||||||
while(true){
|
|
||||||
Date date = new Date();
|
|
||||||
long deltaT = date.getTime()-lastdate;
|
|
||||||
if(deltaT>timeout) {
|
|
||||||
lastdate = date.getTime();
|
|
||||||
modulo = (rangeMax - rangeMin) % 2;
|
|
||||||
center = rangeMin + (rangeMax - rangeMin) / 2 + modulo;
|
|
||||||
infoGraphic( value, center, rangeMin, rangeMax, modulo, inputArray);
|
|
||||||
if (value == inputArray[center]) {
|
|
||||||
res = center;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (value == rangeMax){
|
|
||||||
res = rangeMax;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (value == rangeMin){
|
|
||||||
res = rangeMin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (value < inputArray[center])
|
|
||||||
rangeMax = center;
|
|
||||||
else if (value > inputArray[center])
|
|
||||||
rangeMin = center;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
public static void printArray(int[] inputArray){
|
|
||||||
int maxlength=maxLength(inputArray);
|
|
||||||
System.out.print("I|");
|
|
||||||
for(int i=0; i<inputArray.length;i++){
|
|
||||||
System.out.print(fixedLengthString(Integer.toString(i), maxlength));
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
System.out.print("V|");
|
|
||||||
for(int i=0; i<inputArray.length;i++){
|
|
||||||
System.out.print(fixedLengthString(Integer.toString(inputArray[i]), maxlength));
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
public static void infoGraphic(int value, int center, int min, int max, int modulo, int[] array){
|
|
||||||
int maxlength=maxLength(array);
|
|
||||||
System.out.print("P|");
|
|
||||||
for(int i=0; i<array.length;i++){
|
|
||||||
if(i==center)
|
|
||||||
System.out.print(fixedLengthString("C", maxlength, false));
|
|
||||||
else if(i==max)
|
|
||||||
System.out.print(fixedLengthString("<", maxlength, false));
|
|
||||||
else if(i==min)
|
|
||||||
System.out.print(fixedLengthString(">", maxlength, false));
|
|
||||||
else
|
|
||||||
System.out.print(fixedLengthString(" ", maxlength, false));
|
|
||||||
}
|
|
||||||
System.out.println(" MOD: "+modulo);
|
|
||||||
}
|
|
||||||
public static String fixedLengthString(String string, int length){
|
|
||||||
return fixedLengthString( string, length, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String fixedLengthString(String string, int length,boolean brackets) {
|
|
||||||
if(brackets) return String.format("%1$"+length+ "s|", string);
|
|
||||||
else return String.format("%1$"+(length+1)+ "s", string);
|
|
||||||
|
|
||||||
}
|
|
||||||
public static int maxLength(int[] inputArray){
|
|
||||||
int res=0;
|
|
||||||
for(int number:inputArray){
|
|
||||||
int length = (int) (Math.log10(number) + 1);
|
|
||||||
if(length>res)
|
|
||||||
res=length;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user