Du befindest dich hier: FSI Informatik » Prüfungsfragen und Altklausuren » Prüfungen im Bachelor-Studium (1. - 5. Semester) » pfp » Lösungsversuch (Übersicht)
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige ÜberarbeitungVorherige ÜberarbeitungNächste Überarbeitung | Vorherige Überarbeitung | ||
pruefungen:bachelor:pfp:loesungws14 [24.07.2015 09:58] – awkward_Silence | pruefungen:bachelor:pfp:loesungws14 [21.07.2018 19:41] (aktuell) – evren | ||
---|---|---|---|
Zeile 35: | Zeile 35: | ||
^ ^ Ausgabe (MyThread1) ^ Ausgabe (MyThread2) ^ | ^ ^ Ausgabe (MyThread1) ^ Ausgabe (MyThread2) ^ | ||
| Möglichkeit 1 | T1 = done. | T2 = done. | | | Möglichkeit 1 | T1 = done. | T2 = done. | | ||
- | | ... | T1 = done. | | | + | | ... | T1 = done. | T2 = | |
Begründung: | Begründung: | ||
value ist nicht volatile, durch Sichbarkeitsprobleme kann es hier zu einem Deadlock kommen, nämlich gerade dann wenn MyThread2 nichts von der Änderung in MyThread1 mitbekommt. | value ist nicht volatile, durch Sichbarkeitsprobleme kann es hier zu einem Deadlock kommen, nämlich gerade dann wenn MyThread2 nichts von der Änderung in MyThread1 mitbekommt. | ||
Zeile 44: | Zeile 44: | ||
**b)** | **b)** | ||
- | Keine Zyklen, aus denen man nicht mehr heraus kommt! | + | Wenn irgendwann einmal wieder jede Transition (t1 - t4) schalten/ |
- | EDIT: finde obige Formulierung etwas ungenau/ | + | Beispiele für ein **nicht** lebendiges Petri-Netz:\\ |
+ | - Eine Belegung [x, y, z] in einem Erreichbarkeitsgraphen aus dem keine Pfeile gehen (Deadlock)\\ | ||
+ | - Zyklus in dem nicht alle möglichen Belegungen erreicht werden (Lifelock)\\ | ||
**c)** | **c)** | ||
Zeile 103: | Zeile 105: | ||
} | } | ||
// add values of count to globalCounts 3 | // add values of count to globalCounts 3 | ||
- | for (int i = begin; i < end; i++) { | + | for (int i = 0; i < 256; i++) { |
- | globalCounts.addAndGet(i, | + | globalCounts.addAndGet(i, |
} | } | ||
barrier.await(); | barrier.await(); | ||
Zeile 112: | Zeile 114: | ||
} | } | ||
</ | </ | ||
+ | \\ | ||
+ | (ich glaube im oberen Code ist ein barrier.await() zu viel) \\ | ||
+ | Alternativlösung: | ||
+ | 1: | ||
+ | <code java> | ||
+ | this.barrier = new CyclicBarrier(threads + 1); | ||
+ | </ | ||
+ | \\ | ||
+ | 2: | ||
+ | <code java> | ||
+ | for (int i = 0; i < thread; i++) { | ||
+ | int end = start + step; | ||
+ | if (end >= array.length) end = array.length - 1; | ||
+ | |||
+ | (new CountingThread(start, | ||
+ | |||
+ | start = end + 1; | ||
+ | if (i != array.length - 1 && start >= array.length) { | ||
+ | threads = i; | ||
+ | barrier = new CyclicBarrier(i + 1); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | 3: | ||
+ | <code java> | ||
+ | for (int i = 0; i < count.length; | ||
+ | </ | ||
+ | \\ | ||
+ | 4 (nur oberes Feld): | ||
+ | <code java> | ||
+ | barrier.await(); | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
==== Aufgabe 5 (Scala - Listenoperationen) ==== | ==== Aufgabe 5 (Scala - Listenoperationen) ==== | ||
**a)** | **a)** | ||
- | < | + | < |
def flatten : List[(Char, Char)] => List[Char] = { | def flatten : List[(Char, Char)] => List[Char] = { | ||
case Nil => List() | case Nil => List() | ||
case t :: ts => List(t._1, t._2) ::: flatten(ts) | case t :: ts => List(t._1, t._2) ::: flatten(ts) | ||
} | } | ||
+ | </ | ||
+ | |||
+ | Alternativlösung: | ||
+ | <code scala> | ||
+ | def flatten : List[(Char, Char)] => List[Char] = { | ||
+ | case Nil => Nil | ||
+ | case (a, b) :: tailList => a :: List(b) ::: flatten(tailList) | ||
+ | } | ||
</ | </ | ||
**b)** | **b)** | ||
- | < | + | < |
def countPar : List[Char] => List[(Char, Int)] = cs => { | def countPar : List[Char] => List[(Char, Int)] = cs => { | ||
for (c <- cs) yield (c, cs.par.filter(_ == c).size) | for (c <- cs) yield (c, cs.par.filter(_ == c).size) | ||
Zeile 130: | Zeile 175: | ||
**c)** | **c)** | ||
- | < | + | < |
def distinct : List[(Char, Int)] => List[(Char, Int)] = cs => { | def distinct : List[(Char, Int)] => List[(Char, Int)] = cs => { | ||
cs.foldLeft(List[(Char, | cs.foldLeft(List[(Char, | ||
Zeile 138: | Zeile 183: | ||
==== Aufgabe 6 (Scala - Lauflängenkodierung) ==== | ==== Aufgabe 6 (Scala - Lauflängenkodierung) ==== | ||
**a)** | **a)** | ||
- | < | + | < |
- | def decodeTupel: ((Char, Int)) => Stream[Char] = { | + | def decodeTuple: ((Char, Int)) => Stream[Char] = { |
- | case (c, 1) => Stream(c) | + | case (c, 1) => Stream(c) |
- | case (c, k) => c #:: decodeTupel(c, k - 1) | + | case (c, k) => c #:: decodeTuple(c, k - 1) |
- | } | + | } |
</ | </ | ||
**b)** | **b)** | ||
- | < | + | Geht für '' |
- | def decode: Stream[(Char, | + | < |
- | case null => Stream() | + | def decode: Stream[(Char, |
- | case ts => decodeTupel(ts.head) #::: decode(ts.tail) | + | case null => Stream() |
- | } | + | case ts => decodeTuple(ts.head) #::: decode(ts.tail) |
+ | } | ||
+ | </ | ||
+ | Alternativlösungen: | ||
+ | <code scala> | ||
+ | def decode: Stream[(Char, | ||
+ | | ||
+ | case ts => decodeTuple(ts.head) #::: decode(ts.tail) | ||
+ | } | ||
+ | </ | ||
+ | <code scala> | ||
+ | def decode: Stream[(Char, | ||
+ | case Stream.Empty => Stream.Empty | ||
+ | case first #:: tailList => decodeTuple(first) #::: decode(tailList) | ||
+ | } | ||
</ | </ |