diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-07 14:10:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-07 14:10:36 +0000 |
| commit | ddc19ed5a251eb1b6123591a8594b4779eff7d38 (patch) | |
| tree | 846f24b891e13e420a72f6e5b5d9048f6bdee961 | |
| parent | b24ead8163415d30dc65e98fef3599f306f5ef35 (diff) | |
| parent | 39d4a76d5b352976cbb0916e410ead016380d346 (diff) | |
| download | perlweeklychallenge-club-ddc19ed5a251eb1b6123591a8594b4779eff7d38.tar.gz perlweeklychallenge-club-ddc19ed5a251eb1b6123591a8594b4779eff7d38.tar.bz2 perlweeklychallenge-club-ddc19ed5a251eb1b6123591a8594b4779eff7d38.zip | |
Merge pull request #9354 from deadmarshal/TWC250_extra
Extra solutions for TWC250
| -rw-r--r-- | challenge-250/deadmarshal/cl/ch1.lisp | 11 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/cl/ch2.lisp | 11 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/haskell/ch1.hs | 17 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/haskell/ch2.hs | 13 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/java/Ch1.java | 19 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/java/Ch2.java | 33 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/ocaml/ch1.ml | 17 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/ocaml/ch2.ml | 25 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/python/ch1.py | 11 | ||||
| -rw-r--r-- | challenge-250/deadmarshal/python/ch2.py | 12 |
10 files changed, 169 insertions, 0 deletions
diff --git a/challenge-250/deadmarshal/cl/ch1.lisp b/challenge-250/deadmarshal/cl/ch1.lisp new file mode 100644 index 0000000000..bc44d4888b --- /dev/null +++ b/challenge-250/deadmarshal/cl/ch1.lisp @@ -0,0 +1,11 @@ +(defun smallest-index (list) + (loop :for i :from 0 :below (list-length list) + :do (when (= (mod i 10) (nth i list)) + (return-from smallest-index i))) + -1) + +(progn + (print (smallest-index '(0 1 2))) + (print (smallest-index '(4 3 2 1))) + (print (smallest-index '(1 2 3 4 5 6 7 8 9 0)))) + diff --git a/challenge-250/deadmarshal/cl/ch2.lisp b/challenge-250/deadmarshal/cl/ch2.lisp new file mode 100644 index 0000000000..26b6a6048b --- /dev/null +++ b/challenge-250/deadmarshal/cl/ch2.lisp @@ -0,0 +1,11 @@ +(defun alphanumeric-string-value (list) + (apply #'max (mapcar + #'(lambda (x) (if (every #'(lambda (c) (digit-char-p c)) x) + (parse-integer x) + (length x))) + list))) + +(progn + (print (alphanumeric-string-value '("perl" "2" "000" "python" "r4ku"))) + (print (alphanumeric-string-value '("001" "1" "000" "0001")))) + diff --git a/challenge-250/deadmarshal/haskell/ch1.hs b/challenge-250/deadmarshal/haskell/ch1.hs new file mode 100644 index 0000000000..f5cf72a15e --- /dev/null +++ b/challenge-250/deadmarshal/haskell/ch1.hs @@ -0,0 +1,17 @@ +module Ch1 where + +smallestIndex :: Integral a => [a] -> a +smallestIndex xs = + aux $ zip [0..] xs + where + aux [] = -1 + aux ((i,e):tl) + | mod i 10 == e = i + | otherwise = aux tl + +main :: IO () +main = do + print $ smallestIndex [0,1,2] + print $ smallestIndex [4,3,2,1] + print $ smallestIndex [1,2,3,4,5,6,7,8,9,0] + diff --git a/challenge-250/deadmarshal/haskell/ch2.hs b/challenge-250/deadmarshal/haskell/ch2.hs new file mode 100644 index 0000000000..d10a85d7c2 --- /dev/null +++ b/challenge-250/deadmarshal/haskell/ch2.hs @@ -0,0 +1,13 @@ +module Ch2 where + +import Data.Char (isDigit) + +alphanumericStringValue :: [String] -> Int +alphanumericStringValue xs = + maximum $ map (\x -> if all isDigit x then read x :: Int else length x) xs + +main :: IO () +main = do + print $ alphanumericStringValue ["perl","2","000","python","r4ku"] + print $ alphanumericStringValue ["001","1","000","0001"] + diff --git a/challenge-250/deadmarshal/java/Ch1.java b/challenge-250/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..2a9612e4b9 --- /dev/null +++ b/challenge-250/deadmarshal/java/Ch1.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; +import java.util.List; + +public class Ch1 { + public static void main(String[] args) { + ArrayList<Integer> list1 = new ArrayList<>(List.of(0,1,2)); + ArrayList<Integer> list2 = new ArrayList<>(List.of(4,3,2,1)); + ArrayList<Integer> list3 = new ArrayList<>(List.of(1,2,3,4,5,6,7,8,9,0)); + System.out.println(smallest_index(list1)); + System.out.println(smallest_index(list2)); + System.out.println(smallest_index(list3)); + } + + private static int smallest_index(List<Integer> list) { + for(int i = 0; i < list.size(); ++i) if(i % 10 == list.get(i)) return i; + return -1; + } +} + diff --git a/challenge-250/deadmarshal/java/Ch2.java b/challenge-250/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..a5a286d6b2 --- /dev/null +++ b/challenge-250/deadmarshal/java/Ch2.java @@ -0,0 +1,33 @@ +import java.util.ArrayList; +import java.util.List; +import java.lang.Integer; + +public class Ch2 { + public static void main(String[] args) { + ArrayList<String> list1 = + new ArrayList<>(List.of("perl","2","000","python","r4ku")); + ArrayList<String> list2 = + new ArrayList<>(List.of("001","1","000","0001")); + System.out.println(alphanumeric_string_value(list1)); + System.out.println(alphanumeric_string_value(list2)); + } + + private static boolean is_numeric(String str) { + try { + Integer.parseInt(str); + return true; + } catch(NumberFormatException e) { + return false; + } + } + + private static int alphanumeric_string_value(List<String> list) { + int max = 0; + for(var e : list){ + int n = is_numeric(e) ? Integer.parseInt(e) : e.length(); + if(n > max) max = n; + } + return max; + } +} + diff --git a/challenge-250/deadmarshal/ocaml/ch1.ml b/challenge-250/deadmarshal/ocaml/ch1.ml new file mode 100644 index 0000000000..4063f359b7 --- /dev/null +++ b/challenge-250/deadmarshal/ocaml/ch1.ml @@ -0,0 +1,17 @@ +let smallest_index lst = + let rec range l h = + if l = h then [] else l :: range (succ l) h + in + let rec aux = function + | [] -> -1 + | (i,e) :: tl -> + if i mod 10 == e then i else aux tl + in + List.combine (range 0 (List.length lst)) lst |> aux + +let _ = + let open Printf in + [0;1;2] |> smallest_index |> printf "%d\n"; + [4;3;2;1] |> smallest_index |> printf "%d\n"; + [1;2;3;4;5;6;7;8;9;0] |> smallest_index |> printf "%d\n"; + diff --git a/challenge-250/deadmarshal/ocaml/ch2.ml b/challenge-250/deadmarshal/ocaml/ch2.ml new file mode 100644 index 0000000000..05e2a83c77 --- /dev/null +++ b/challenge-250/deadmarshal/ocaml/ch2.ml @@ -0,0 +1,25 @@ +let explode s = + let rec exp i l = + if i < 0 then l else exp (i - 1) (s.[i] :: l) in + exp (String.length s - 1) [] + +let alphanumeric_string_value xs = + let is_digit = function '0'..'9' -> true | _ -> false in + let rec transform = function + | [] -> [] + | hd :: tl -> + if (List.for_all is_digit (explode hd)) then + (int_of_string hd) :: transform tl + else String.length hd :: transform tl + in + List.fold_left max Int.min_int (transform xs) + +let _ = + let open Printf in + ["perl";"2";"000";"python";"r4ku"] + |> alphanumeric_string_value + |> printf "%d\n"; + ["001";"1";"000";"0001"] + |> alphanumeric_string_value + |> printf "%d\n" + diff --git a/challenge-250/deadmarshal/python/ch1.py b/challenge-250/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..ffe8ecb00a --- /dev/null +++ b/challenge-250/deadmarshal/python/ch1.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +def smallest_index(arr): + for i in range(0,len(arr)): + if i % 10 == arr[i]: return i + return -1 + +print(smallest_index([0,1,2])) +print(smallest_index([4,3,2,1])) +print(smallest_index([1,2,3,4,5,6,7,8,9,0])) + diff --git a/challenge-250/deadmarshal/python/ch2.py b/challenge-250/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..4a98713b00 --- /dev/null +++ b/challenge-250/deadmarshal/python/ch2.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +def alphanumeric_string_value(arr): + ret = [] + for e in arr: + if e.isdigit(): ret.append(int(e)) + else: ret.append(len(e)) + return max(ret) + +print(alphanumeric_string_value(['perl','2','000','python','r4ku'])) +print(alphanumeric_string_value(['001','1','000','0001'])) + |
