Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
code di ia
type QuestionId = string type QuestionDictionary = Map<QuestionId, string> type AnswerSet = Map<string, string> type AnswerDictionary = Map<QuestionId, AnswerSet> let print (questionDict : QuestionDictionary) (answerDict : AnswerDictionary) = let helperDict = answerDict |> Map.toList |> List.groupBy snd |> List.map (fun (answerSet, oldList) -> answerSet, oldList |> List.map fst |> Set.ofList) |> Map.ofList let printQuestion questionId questionLabel = printfn "QUESTION [%s]: %s" questionId questionLabel match answerDict |> Map.tryFind questionId with | Some answerSet -> let relatedQuestionIds = helperDict |> Map.find answerSet for id in relatedQuestionIds do if questionId <> id then let label = questionDict |> Map.find id printfn "/ [%s]: %s" id label let answerArr = answerSet |> Map.toArray answerArr |> Array.iteri (fun index (id, label) -> printf "\t[%s]: %s" id label printfn (if index = answerArr.Length - 1 then "." else "")) relatedQuestionIds | None -> Set.empty questionDict |> Map.fold (fun printedQuestionIds questionId questionLabel -> if printedQuestionIds |> Set.contains questionId then printedQuestionIds else printQuestion questionId questionLabel |> Set.union printedQuestionIds) Set.empty |> ignore // Test let questionDict : QuestionDictionary = [ "Q1", "Ban ten gi?" "Q2", "Gioi tinh?" "Q3.1", "Ngay di ia may lan?" "Q3.2", "Ngay du may lan?" ] |> Map.ofList let answerDict : AnswerDictionary = [ "Q2", [ ("1", "Nam"); ("2", "Nu") ] |> Map.ofList "Q3.1", [ ("1", "Mot lan"); ("2", "Hai lan"); ("3", "Ngan lan") ] |> Map.ofList "Q3.2", [ ("1", "Mot lan"); ("2", "Hai lan"); ("3", "Ngan lan") ] |> Map.ofList ] |> Map.ofList print questionDict answerDict
run
|
edit
|
history
|
help
0
Map.except
Recursion in F#
KR#2
Highly Modular FizzBuzz
Calculate distance between two GPS latitude-longitude points.
Average Square Error
Musical Note Frequencies (Equal temperament)
Sending an email
Yet another Fibonacci
The wonders of F#