Skip to content
Snippets Groups Projects
Commit 71df0952 authored by Radu-Andrei Coanda's avatar Radu-Andrei Coanda
Browse files

klausur

parent 8ea3c42d
No related branches found
No related tags found
No related merge requests found
File added
File added
File added
File added
minim :: [Int] -> Int
minim [x] = x
minim (x : xs) = if x < m then x else m
where m = minim xs
levenshtein :: String -> String -> Int
levenshtein [] ys = length ys
levenshtein xs [] = length xs
levenshtein (x:xs) (y:ys)
| x == y = levenshtein xs ys
| otherwise = 1 + minim
[ levenshtein (x:xs) ys -- y einfuegen
, levenshtein xs (y:ys) -- x loeschen
, levenshtein xs ys-- x zu y aendern
]
import java.util.List;
import java.util.LinkedList;
public class Baum {
private int wert;
private Baum links, rechts;
public Baum(Baum l, int wert, Baum r) {
this.links = l;
this.wert = wert;
this.rechts = r;
}
public int hoehe() {
int l = 0;
if (this.links != null)
l = this.links.hoehe();
int r = 0;
if (this.rechts != null)
r = this.rechts.hoehe();
if (l > r)
return l + 1;
else
return r + 1;
}
public void einfuegen(int x) {
Baum prev = null;
Baum cur = this;
while (cur != null) {
prev = cur;
if (x <= cur.wert)
cur = cur.links;
else
cur = cur.rechts;
}
Baum neu = new Baum(null, x, null);
if (x <= prev.wert)
prev.links = neu;
else
prev.rechts = neu;
}
public List<Integer> toList() {
List<Integer> res = new LinkedList<>();
this.toList(res);
return res;
}
private void toList(List<Integer> res) {
if (this.links != null)
this.links.toList(res);
res.add(this.wert);
if (this.rechts != null)
this.rechts.toList(res);
}
public <T> T apply(BaumFolder<T> f) {
T l;
if (this.links != null) {
l = this.links.apply(f);
} else {
l = f.handleNull();
}
T r;
if (this.rechts != null) {
r = this.rechts.apply(f);
} else {
r = f.handleNull();
}
return f.handleBaum(l, this.wert, r);
}
}
\ No newline at end of file
public interface BaumFolder<T> {
T handleNull();
T handleBaum(T l, int w, T r);
}
class BaumInc implements BaumFolder<Baum> {
public Baum handleNull() {
return null;
}
public Baum handleBaum(Baum links, int wert, Baum rechts) {
return new Baum(links, wert + 1, rechts);
}
}
class BaumSum implements BaumFolder<Integer> {
public Integer handleNull() {
return 0;
}
public Integer handleBaum(Integer l, int w, Integer r) {
return l + w + r;
}
}
public class BaumTest {
public static void main(String[] args) {
Baum l = new Baum(null, 24, null);
Baum r = new Baum(null, 66, null);
Baum testTree = new Baum(l, 43, r);
// Height
System.out.println(testTree.hoehe());
// Insert
testTree.einfuegen(34);
testTree.einfuegen(100);
testTree.einfuegen(0);
System.out.println(testTree.hoehe());
// toList
System.out.println(testTree.toList());
}
}
\ No newline at end of file
% Part (a) % Part (a)
% Test Aufruf: increment(node(leaf(s(0)),s(s(0)),leaf(0)),Res). % Test Aufruf: increment(node(leaf(s(0)),s(s(0)),leaf(0)),Res).
increment(leaf(X), leaf(s(X))).
increment(node(L, V, R), node( ResL, s(V), ResR )) :- increment(L, ResL), increment(R, ResR).
% Part (b) % Part (b)
% Test Aufruf: append([a,b,c],[d,e],Res). % Test Aufruf: append([a,b,c],[d,e],Res).
append([], YS, YS).
append([X|XS], YS, [X|Res]) :- append(XS, YS, Res).
% Part (c) % Part (c)
% Test Aufruf: inorder(node(leaf(s(0)),s(s(0)),node(leaf(s(0)),0,leaf(s(s(s(0)))))), Res). % Test Aufruf: inorder(node(leaf(s(0)),s(s(0)),node(leaf(s(0)),0,leaf(s(s(s(0)))))), Res).
inorder(leaf(X), [X]).
inorder(node(L, V, R), Res) :- inorder(L, ResL), inorder(R, ResR), append(ResL, [V|ResR], Res).
\ No newline at end of file
% prime(N) ist genau dann wahr, wenn N > 1 und N eine Primzahl ist. % prime(N) ist genau dann wahr, wenn N > 1 und N eine Primzahl ist.
prime(N) :- N > 1, X is N div 2, nodivisors(N, X).
% nodivisors(N, X) ist genau dann wahr, wenn N keine Teiler zwischen X und 2 hat. % nodivisors(N, X) ist genau dann wahr, wenn N keine Teiler zwischen X und 2 hat.
nodivisors(_, 1).
nodivisors(N, X) :- notdivisor(N, X), Y is X - 1, nodivisors(N, Y).
% notdivisor (N , X ) ist wahr gdw . N nicht durch X teilbar ist . % notdivisor (N , X ) ist wahr gdw . N nicht durch X teilbar ist .
notdivisor(N, X) :- Y is N mod X, Y > 0.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment