Pascal Schärli 04.12.2020
Die Programmieraufgaben lohnen sich wirklich schon unter dem Semester zu lösen, da es sehr schwierig ist, sich die Routine welche man dadurch bekommt in ein paar Wochen Lernphase anzueignen.
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
public ArrayList<T> sort(ArrayList<T> items) {
return sortRec(items, 0, items.size());
}
private ArrayList<T> sortRec(ArrayList<T> items, int begin, int end) {
if (begin == end) {
return new ArrayList<T>();
}
if (begin + 1 == end) {
ArrayList<T> result = new ArrayList<T>();
result.add(items.get(begin));
return result;
}
int middle = begin + (end - begin) / 2;
ArrayList<T> left = sortRec(items, begin, middle);
ArrayList<T> right = sortRec(items, middle, end);
// [...]
Pascal Schärli 04.12.2020
// [...]
int leftIdx = 0;
int rightIdx = 0;
ArrayList<T> sorted = new ArrayList<T>(end - begin);
while (true) {
if (leftIdx == left.size()) {
sorted.addAll(right.subList(rightIdx, right.size()));
return sorted;
}
if (rightIdx == right.size()) {
sorted.addAll(left.subList(leftIdx, left.size()));
return sorted;
}
if (left.get(leftIdx).compareTo(right.get(rightIdx)) < 0) {
sorted.add(left.get(leftIdx));
leftIdx += 1;
} else {
sorted.add(right.get(rightIdx));
rightIdx += 1;
}
}
}
Pascal Schärli 04.12.2020
moves = 2^4-1;
counter = 0;
while (counter < moves)
make available move between tower 1 and tower 2
make available move between tower 1 and tower 3
make available move between tower 2 and tower 3
increment counter by 3
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
private static int f1(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
out++;
}
return out;
}
Pascal Schärli 04.12.2020
private static int f2(int n) {
int out = 0;
for(int i = 0; i * 5 < n; i++) {
out++;
}
return out;
}
Pascal Schärli 04.12.2020
private static int f3(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
out++;
}
}
return out;
}
Pascal Schärli 04.12.2020
private static int f4(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < 1000; j++){
out++;
}
}
return out;
}
Pascal Schärli 04.12.2020
private static int f5(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
out++;
}
}
return out;
}
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
// Fragment 1
for (int i=1; i<n; i++) a++;
// Fragment 2
for (int i=0; i<2*n; i++) a++;
for (int j=0; j<n; j++) a++;
// Fragment 3
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
a++;
// Fragment 4
for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
a++;
// Fragment 5
while(n >= 1) n = n/2;
// Fragment 6
for (int i=0; i<n; i++)
for (int j=0; j<n*n; j++)
for (int k=0; k<j; k++)
a++;
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020
Position pos1plus2 = pos1.add(pos2);
possibleMoves = new ArrayList<Position>(8);
possibleMoves.add(new Position(1, 2));
possibleMoves.add(new Position(2, 1));
// … usw
Pascal Schärli 04.12.2020
Aufgabe 1: getReachableSet
visit(Positoin pos, int maxDepth, int depth, ArrayList<Position> visited)
Pascal Schärli 04.12.2020
Aufgabe 2: completePath
Pascal Schärli 04.12.2020
Pascal Schärli 04.12.2020