diff options
| author | simon-dueck <126712673+simon-dueck@users.noreply.github.com> | 2023-05-21 00:58:56 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 00:58:56 -0500 |
| commit | 0d6822c17fa4b88bcadee5c2791929305fae7e5a (patch) | |
| tree | 271a7d12e83c5a0f712991f615ac63020521d8c8 | |
| parent | 0b993c6051fdc996c88df66264653351958af75c (diff) | |
| download | perlweeklychallenge-club-0d6822c17fa4b88bcadee5c2791929305fae7e5a.tar.gz perlweeklychallenge-club-0d6822c17fa4b88bcadee5c2791929305fae7e5a.tar.bz2 perlweeklychallenge-club-0d6822c17fa4b88bcadee5c2791929305fae7e5a.zip | |
Added solution for challenge #1 in F#
| -rw-r--r-- | challenge-216/simon-dueck/README | 1 | ||||
| -rw-r--r-- | challenge-216/simon-dueck/fsharp/ch-1.fsx | 36 |
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-216/simon-dueck/README b/challenge-216/simon-dueck/README index e69de29bb2..1356b2f16f 100644 --- a/challenge-216/simon-dueck/README +++ b/challenge-216/simon-dueck/README @@ -0,0 +1 @@ +Solution by Simon Dueck
\ No newline at end of file diff --git a/challenge-216/simon-dueck/fsharp/ch-1.fsx b/challenge-216/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..6a777fa28d --- /dev/null +++ b/challenge-216/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,36 @@ +(*
+ You are given a list of words and a random registration number.
+ Write a script to find all the words in the given list that has every letter in the given registration number.
+*)
+
+let word_contains_letter (word: string) (l: char): bool =
+ if l >= 'a' && l <= 'z' then
+ List.contains (char ((int l) - 32)) (Seq.toList word) // to uppercase
+ elif l >= 'A' && l <= 'Z' then
+ List.contains l (Seq.toList word)
+ else true // l is not a letter
+
+let rec word_contains_all_letters (word: string) (l: char list): bool =
+ match l with
+ | x::xs when word_contains_letter word x -> word_contains_all_letters word xs
+ | [] -> true
+ | _ -> false
+
+let contains_all (words: string list) (plate: string): string list =
+ let rec loop (words:string list) (plate: char list) =
+ match words with
+ | word::xs when word_contains_all_letters (word.ToUpper()) plate -> word :: (loop xs plate)
+ | _::xs -> loop xs plate
+ | [] -> []
+ loop words (plate |> Seq.toList)
+
+let words_a = ["abc"; "abcd"; "bcd"]
+let plate_a = "AB1 2CD"
+let words_b = ["job"; "james"; "bjorg"]
+let plate_b = "007 JB"
+let words_c = ["crack"; "road"; "rac"]
+let plate_c = "C7 RA2"
+
+printfn "%A %s -> %A" words_a plate_a (contains_all words_a plate_a)
+printfn "%A %s -> %A" words_b plate_b (contains_all words_b plate_b)
+printfn "%A %s -> %A" words_c plate_c (contains_all words_c plate_c)
|
