aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/jeongoon/elm/src/Ch2.elm
blob: 4a0f1c3768c52d10036d40dc421684aff0541fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Ch2 exposing (getFNR)
import String exposing (join, split)
import Dict
import List exposing (head, tail)
import Maybe exposing (withDefault)

getFNR : String -> Maybe String
getFNR newString =
    let chrs   = split "" newString
        counts x dic = case (Dict.get x dic) of
                         Nothing -> 0
                         Just  n -> n
        doFind dic rfnr cs =
            case (head cs) of
                Nothing ->
                    if List.length rfnr == 0 then Nothing
                    else Just (join "" (List.reverse rfnr))
                Just  k ->
                    case (counts k dic) of
                             0 -> doFind (Dict.insert k 1 dic) (k :: rfnr)
                                      (withDefault [] (tail cs))
                             m -> doFind (Dict.insert k (m+1) dic)
                                      ((findPrevious
                                            (Dict.insert k (m+1) dic) rfnr)
                                                :: rfnr)
                                      (withDefault [] (tail cs))
        findPrevious dic nrs =
            case (head nrs) of
                Nothing -> "#"
                Just  y ->
                    case (Dict.get y dic) of
                        Nothing -> y
                        Just  u -> if u == 1 then y
                                   else findPrevious dic
                                       (withDefault [] (tail nrs))

    in
        doFind Dict.empty [] chrs