From 7e91d0b239355ba31d9b96851e0773a5d424860c Mon Sep 17 00:00:00 2001 From: simon-dueck <126712673+simon-dueck@users.noreply.github.com> Date: Thu, 6 Feb 2025 10:15:57 -0600 Subject: Added solutions to week 307 in F# --- challenge-307/simon-dueck/fsharp/ch-1.fsx | 16 ++++++++++++++++ challenge-307/simon-dueck/fsharp/ch-2.fsx | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-307/simon-dueck/fsharp/ch-1.fsx create mode 100644 challenge-307/simon-dueck/fsharp/ch-2.fsx diff --git a/challenge-307/simon-dueck/fsharp/ch-1.fsx b/challenge-307/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..9fb284f9e2 --- /dev/null +++ b/challenge-307/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,16 @@ +(* + You are given an array of integers, @ints. + Write a script to re-arrange the given array in an increasing order + and return the indices where it differs from the original array. +*) + +let check_order arr = + let rec loop i a b = + if Seq.isEmpty a then [] + elif Seq.head a = Seq.head b then i :: (loop (i + 1) (Seq.tail a) (Seq.tail b)) + else loop (i + 1) (Seq.tail a) (Seq.tail b) + in loop 0 arr (Seq.sort arr) + +let ints = [ for i in 0..15 do let r = new System.Random() in yield r.NextDouble() * 10. |> int ] + +check_order ints \ No newline at end of file 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 -- cgit