diff options
| author | simon-dueck <126712673+simon-dueck@users.noreply.github.com> | 2023-05-21 00:59:39 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 00:59:39 -0500 |
| commit | cedfb3790fc11f7b41651249df99e83e709d471f (patch) | |
| tree | 628f8175dad7c189bcafef567a746804f239c828 | |
| parent | 0d6822c17fa4b88bcadee5c2791929305fae7e5a (diff) | |
| download | perlweeklychallenge-club-cedfb3790fc11f7b41651249df99e83e709d471f.tar.gz perlweeklychallenge-club-cedfb3790fc11f7b41651249df99e83e709d471f.tar.bz2 perlweeklychallenge-club-cedfb3790fc11f7b41651249df99e83e709d471f.zip | |
Added solutions in F#
| -rw-r--r-- | challenge-215/Week 215/simon-dueck/README | 1 | ||||
| -rw-r--r-- | challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx | 27 | ||||
| -rw-r--r-- | challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx | 26 |
3 files changed, 54 insertions, 0 deletions
diff --git a/challenge-215/Week 215/simon-dueck/README b/challenge-215/Week 215/simon-dueck/README new file mode 100644 index 0000000000..1356b2f16f --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck
\ No newline at end of file diff --git a/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx b/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..1367346b2f --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,27 @@ +(*
+ You are given a list of words (alphabetic characters only) of same size.
+
+ Write a script to remove all words not sorted alphabetically and print
+ the number of words in the list that are not alphabetically sorted.
+*)
+
+let word_is_not_sorted (word: string): bool =
+ let rec loop (letters: char list): bool =
+ match letters with
+ | x::y::xs when x < y -> loop (y::xs)
+ | [x] -> false
+ | _ -> true
+ in loop (Seq.toList word)
+
+let count_unsorted (words: string list): int =
+ List.filter word_is_not_sorted words |> List.length
+
+
+let words_a = ["abc"; "xyz"; "tsu"]
+printfn $"{count_unsorted words_a}"
+
+let words_b = ["rat"; "cab"; "dad"]
+printfn $"{count_unsorted words_b}"
+
+let words_c = ["x"; "y"; "z"]
+printfn $"{count_unsorted words_c}"
diff --git a/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx b/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx new file mode 100644 index 0000000000..4c3ca66711 --- /dev/null +++ b/challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx @@ -0,0 +1,26 @@ +(*
+ You are given a list of numbers having just 0 and 1. You are also given placement count (>=1).
+
+ Write a script to find out if it is possible to replace 0 with 1 in the given list.
+ The only condition is that you can only replace when there is no 1 on either side.
+ Print 1 if it is possible otherwise 0.
+*)
+
+let rec replace_zero (arr: int list) (count: int): int =
+ match arr with
+ | _ when count = 0 -> 1 // all are placed
+ | 0::0::xs -> replace_zero xs (count - 1) // can replace; skip next index as it will not be replaceable
+ | [0] -> replace_zero [] (count - 1) // end of the list is zero and replacable
+ | 1::0::xs -> replace_zero xs count // current and next index are not replaceable
+ | _::xs -> replace_zero xs count // current index is not replaceable but next potentially is or is a one
+ | _ -> 0 // end of list with count not reaching zero
+
+let numbers_a = [1; 0; 0; 0; 1]
+let numbers_b = [1; 0; 0; 0; 1]
+let numbers_c = [1; 0; 0; 0; 0; 0; 0; 0; 1]
+let numbers_d = [1; 0; 0; 0; 1; 0; 0; 1]
+
+printfn $"{replace_zero numbers_a 1}"
+printfn $"{replace_zero numbers_b 2}"
+printfn $"{replace_zero numbers_c 3}"
+printfn $"{replace_zero numbers_d 2}"
\ No newline at end of file |
