diff options
| author | simon-dueck <126712673+simon-dueck@users.noreply.github.com> | 2025-02-06 10:15:57 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-06 10:15:57 -0600 |
| commit | 7e91d0b239355ba31d9b96851e0773a5d424860c (patch) | |
| tree | 5fd0850bbe32942f8030ee4afbb1fda5ef38cebd | |
| parent | ea528b3a4f9f371f274a0c9c3012f49cb5e2bed0 (diff) | |
| download | perlweeklychallenge-club-7e91d0b239355ba31d9b96851e0773a5d424860c.tar.gz perlweeklychallenge-club-7e91d0b239355ba31d9b96851e0773a5d424860c.tar.bz2 perlweeklychallenge-club-7e91d0b239355ba31d9b96851e0773a5d424860c.zip | |
Added solutions to week 307 in F#
| -rw-r--r-- | challenge-307/simon-dueck/fsharp/ch-1.fsx | 16 | ||||
| -rw-r--r-- | challenge-307/simon-dueck/fsharp/ch-2.fsx | 21 |
2 files changed, 37 insertions, 0 deletions
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 |
