aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-215/Week 215/simon-dueck/README1
-rw-r--r--challenge-215/Week 215/simon-dueck/fsharp/ch-1.fsx27
-rw-r--r--challenge-215/Week 215/simon-dueck/fsharp/ch-2.fsx26
-rw-r--r--challenge-216/simon-dueck/README1
-rw-r--r--challenge-216/simon-dueck/fsharp/ch-1.fsx36
-rw-r--r--challenge-217/simon-dueck/README1
-rw-r--r--challenge-217/simon-dueck/fsharp/ch-1.fsx14
7 files changed, 106 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
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)
diff --git a/challenge-217/simon-dueck/README b/challenge-217/simon-dueck/README
index e69de29bb2..1356b2f16f 100644
--- a/challenge-217/simon-dueck/README
+++ b/challenge-217/simon-dueck/README
@@ -0,0 +1 @@
+Solution by Simon Dueck \ No newline at end of file
diff --git a/challenge-217/simon-dueck/fsharp/ch-1.fsx b/challenge-217/simon-dueck/fsharp/ch-1.fsx
new file mode 100644
index 0000000000..2b64595b10
--- /dev/null
+++ b/challenge-217/simon-dueck/fsharp/ch-1.fsx
@@ -0,0 +1,14 @@
+(*
+ You are given a n x n matrix where n >= 2.
+ Write a script to find 3rd smallest element in the sorted matrix.
+*)
+
+let sort_matrix (matrix: int list list): int =
+ let rec loop matrix =
+ match matrix with
+ | x::xs -> x @ loop xs
+ | [] -> []
+ in let sorted = loop matrix |> List.sort
+ sorted[2]
+
+printfn $"{sort_matrix [[1;2;3];[2;3;4];[6;7;8];]}" \ No newline at end of file