From b1caccfe266c25cd9fdec359a9b4af0d8259f2b9 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Fri, 15 Oct 2021 19:30:29 +0200 Subject: [PATCH] Add UE1 A.2 --- Dockerfile | 4 ++++ code/Aufgabe1.hs | 58 ++++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6dd274a..5720b94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,7 @@ FROM haskell:buster RUN apt-get update && apt-get install -y haskell-platform + +WORKDIR /opt + +CMD ["ghci"] diff --git a/code/Aufgabe1.hs b/code/Aufgabe1.hs index fd2314e..52b8407 100644 --- a/code/Aufgabe1.hs +++ b/code/Aufgabe1.hs @@ -16,9 +16,6 @@ type Zerlegungszeugen = [Zerlegungszeuge] -- Aufgabe A.1 - -ist_tzr :: Zeichenreihe -> Teilzeichenreihe -> IstTeilzeichenreihe - {- Solution: - if both strings are the same, True @@ -29,26 +26,49 @@ ist_tzr :: Zeichenreihe -> Teilzeichenreihe -> IstTeilzeichenreihe If it is not, the first string will get smaller than the second eventually and the function will return False -} -ist_tzr x y - | y == x = True - | y == [] = True - | length y > length x = False +ist_tzr :: Zeichenreihe -> Teilzeichenreihe -> IstTeilzeichenreihe + +ist_tzr z t + | t == z = True + | t == [] = True + | length t > length z = False | otherwise = - if head x == head y then - ist_tzr (tail x) (tail y) + if head z == head t then + ist_tzr (tail z) (tail t) else - ist_tzr (tail x) y - - + ist_tzr (tail z) t -- Aufgabe A.2 - ---tzr_zeuge :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeuge - -{- Knapp, aber gut nachvollziehbar geht tzr_zeuge folgendermassen vor: - ... +{- + Solution: + - the base cases are as follows: + - both strings are empty, return a triple with empty strings + - the second string is empty, return a triple with empty strings as first two elements and the whole first string as a third. + The problem description states that the split doesn't matter, so we take this because it is easy and cheap to do :) + - the second string is not a subset of the first string, return the "error triple" + - for handling the other cases: + - we find the position of the last subset string in the first string + - we take all of the characters until this position - this is the first element of the triple + - we then take all of the characters after this position summed with the length of the second string - this is the third element of the triple -} +tzr_zeuge :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeuge +tzr_zeuge [] [] = ("", "", "") +tzr_zeuge z [] = ("", "", z) +tzr_zeuge z t + | not (ist_tzr z t) = ("", t ++ t, "") + | otherwise = + let pos = (find_tzr_position z t 0) - 1 + pre = take (pos) z + suf = drop (pos + (length t)) z + in (pre, t, suf) + +-- find the index of the last occurrence of t in z +find_tzr_position :: Zeichenreihe -> Teilzeichenreihe -> Nat0 -> Nat0 +find_tzr_position (z:zs) t n + | not (ist_tzr (z:zs) t) = n + | otherwise = find_tzr_position zs t (n + 1) + -- Aufgabe A.3 --tzr_zeugen :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeugen @@ -65,7 +85,3 @@ ist_tzr x y {- Knapp, aber gut nachvollziehbar geht wieOft folgendermassen vor: ... -} - - - -