aboutsummaryrefslogtreecommitdiff
path: root/challenge-307/simon-dueck/fsharp/ch-2.fsx
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-307/simon-dueck/fsharp/ch-2.fsx')
-rw-r--r--challenge-307/simon-dueck/fsharp/ch-2.fsx21
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-307/simon-dueck/fsharp/ch-2.fsx b/challenge-307/simon-dueck/fsharp/ch-2.fsx
new file mode 100644
index 0000000000..7c96e86624
--- /dev/null
+++ b/challenge-307/simon-dueck/fsharp/ch-2.fsx
@@ -0,0 +1,21 @@
+(*
+ You are given a list of words, @words.
+ Write a script to find any two consecutive words and if they are anagrams,
+ drop the first word and keep the second.
+ You continue this until there is no more anagrams in the given list and return the count of final list.
+*)
+
+let is_anagram (a: string) (b: string) =
+ Seq.length (set [ yield! a; yield! b]) = a.Length
+
+let find_anagrams words =
+ let rec loop words =
+ match words with
+ | x::y::xs when is_anagram x y -> loop (x::xs)
+ | x::xs -> x::(loop xs)
+ | x -> x
+ in words |> loop |> Seq.length
+
+let words = ["acca"; "dog"; "god"; "perl"; "repl"]
+
+find_anagrams words \ No newline at end of file