diff options
Diffstat (limited to 'challenge-238/deadmarshal')
29 files changed, 770 insertions, 0 deletions
diff --git a/challenge-238/deadmarshal/apl/ch1.dws b/challenge-238/deadmarshal/apl/ch1.dws Binary files differnew file mode 100644 index 0000000000..a400307b70 --- /dev/null +++ b/challenge-238/deadmarshal/apl/ch1.dws diff --git a/challenge-238/deadmarshal/blog.txt b/challenge-238/deadmarshal/blog.txt new file mode 100644 index 0000000000..e2dcc6c014 --- /dev/null +++ b/challenge-238/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2023/10/twc238.html diff --git a/challenge-238/deadmarshal/c/ch-1.c b/challenge-238/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..c256112a04 --- /dev/null +++ b/challenge-238/deadmarshal/c/ch-1.c @@ -0,0 +1,25 @@ +#include<stdio.h> + +void running_sum(int *arr,size_t sz) +{ + int sum = 0; + for(size_t i = 0; i < sz; ++i) + { + sum += arr[i]; + printf("%d ",sum); + } + puts(""); +} + +int main(void) +{ + int arr1[] = {1,2,3,4,5}; + int arr2[] = {1,1,1,1,1}; + int arr3[] = {0,-1,1,2}; + size_t sz1 = 5,sz2 = 5,sz3 = 4; + running_sum(arr1,sz1); + running_sum(arr2,sz2); + running_sum(arr3,sz3); + return 0; +} + diff --git a/challenge-238/deadmarshal/c/ch-2.c b/challenge-238/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..cdd8ee5086 --- /dev/null +++ b/challenge-238/deadmarshal/c/ch-2.c @@ -0,0 +1,49 @@ +#include<stdio.h> +#include<stdlib.h> + +int product(int n) +{ + int prod = 1; + while(n) + { + prod *= n % 10; + n /= 10; + } + return prod; +} + +int helper(int n) +{ + int sum = 0; + while(n >= 10) + { + sum++; + n = product(n); + } + return sum; +} + +int compare(const void *a, const void *b) +{ + int ha = helper(*(int*)a); + int hb = helper(*(int*)b); + return ha == hb ? *(int*)a - *(int*)b : ha - hb; +} + +void persistence_sort(int *arr,size_t sz) +{ + qsort(arr,sz,sizeof(*arr),compare); + for(size_t i = 0; i < sz; ++i) printf("%d ", arr[i]); + puts(""); +} + +int main(void) +{ + int arr1[] = {15,99,1,34}; + int arr2[] = {50,25,33,22}; + size_t sz1 = 4, sz2 = 4; + persistence_sort(arr1,sz1); + persistence_sort(arr2,sz2); + return 0; +} + diff --git a/challenge-238/deadmarshal/cl/ch1.lisp b/challenge-238/deadmarshal/cl/ch1.lisp new file mode 100644 index 0000000000..d394b05dde --- /dev/null +++ b/challenge-238/deadmarshal/cl/ch1.lisp @@ -0,0 +1,8 @@ +(defun running-sum (list) + (loop :for e :in list :sum e :into s :collect s)) + +(progn + (print (running-sum '(1 2 3 4 5))) + (print (running-sum '(1 1 1 1 1))) + (print (running-sum '(0 -1 1 2)))) + diff --git a/challenge-238/deadmarshal/cl/ch2.lisp b/challenge-238/deadmarshal/cl/ch2.lisp new file mode 100644 index 0000000000..c2d1ff22d7 --- /dev/null +++ b/challenge-238/deadmarshal/cl/ch2.lisp @@ -0,0 +1,20 @@ +(defun digits (n) + (map 'list #'digit-char-p (prin1-to-string n))) + +(defun helper (acc n) + (if (< n 10) + acc + (helper (1+ acc) (apply #'* (digits n))))) + +(defun persistence-sort (list) + (sort list #'(lambda (a b) + (let ((ha (helper 0 a)) + (hb (helper 0 b))) + (if (/= ha hb) + (< ha hb) + (< a b)))))) + +(progn + (print (persistence-sort '(15 99 1 34))) + (print (persistence-sort '(50 25 33 22)))) + diff --git a/challenge-238/deadmarshal/cpp/ch-1.cpp b/challenge-238/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..9d2ee4ab6d --- /dev/null +++ b/challenge-238/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,23 @@ +#include<iostream> +#include<vector> +#include<numeric> +#include<iterator> + +template<typename T> +void running_sum(const std::vector<T> &vec) +{ + std::vector<T> ret{}; + std::partial_sum(vec.cbegin(),vec.cend(), + std::ostream_iterator<T>(std::cout, " ")); + std::cout << '\n'; +} + +int main(void) +{ + std::vector<int> vec1{1,2,3,4,5},vec2{1,1,1,1,1},vec3{0,-1,1,2}; + running_sum<int>(vec1); + running_sum<int>(vec2); + running_sum<int>(vec3); + return 0; +} + diff --git a/challenge-238/deadmarshal/cpp/ch-2.cpp b/challenge-238/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..e2c00022aa --- /dev/null +++ b/challenge-238/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,55 @@ +#include<iostream> +#include<vector> +#include<algorithm> + +template<typename T> +T product(T n) +{ + T prod = 1; + while(n) + { + prod *= n % 10; + n /= 10; + } + return prod; +} + +template<typename T> +T helper(T n) +{ + T sum = 0; + while(n >= 10) + { + sum++; + n = product<T>(n); + } + return sum; +} + +template<typename T> +void persistence_sort(std::vector<T>& v) +{ + std::sort(v.begin(),v.end(),[](T a, T b){ + T ha = helper<T>(a), hb = helper<T>(b); + return ha == hb ? a < b : ha < hb; + }); +} + +template<typename T> +std::ostream &operator<<(std::ostream &os, + const std::vector<T>& vec) +{ + for(const auto &e : vec) os << e << ' '; + os << "\n"; + return os; +} + +int main() +{ + std::vector<int> vec1{15,99,1,34},vec2{50,25,33,22}; + persistence_sort(vec1); + persistence_sort(vec2); + std::cout << vec1 << vec2; + return 0; +} + diff --git a/challenge-238/deadmarshal/d/ch1.d b/challenge-238/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..93c2f14813 --- /dev/null +++ b/challenge-238/deadmarshal/d/ch1.d @@ -0,0 +1,23 @@ +import std.stdio:write,writeln; + +void running_sum(ref int[] arr) +{ + int sum = 0; + foreach(ref e;arr) + { + sum += e; + write(sum,' '); + } + writeln; +} + +void main() +{ + int[] arr1 = [1,2,3,4,5]; + int[] arr2 = [1,1,1,1,1]; + int[] arr3 = [0,-1,1,2]; + running_sum(arr1); + running_sum(arr2); + running_sum(arr3); +} + diff --git a/challenge-238/deadmarshal/d/ch2.d b/challenge-238/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..8d92f1cd07 --- /dev/null +++ b/challenge-238/deadmarshal/d/ch2.d @@ -0,0 +1,47 @@ +import std.stdio:writeln,write; +import std.algorithm:sort; + +int product(int n) +{ + int prod = 1; + while(n) + { + prod *= n % 10; + n /= 10; + } + return prod; +} + +int helper(int n) +{ + int sum = 0; + while(n >= 10) + { + sum++; + n = product(n); + } + return sum; +} + +bool compare(int a, int b) +{ + int ha = helper(a); + int hb = helper(b); + return ha == hb ? a < b : ha < hb; +} + +void persistence_sort(ref int[] arr) +{ + arr.sort!(compare); +} + +void main() +{ + int[] arr1 = [15,99,1,34]; + int[] arr2 = [50,25,33,22]; + persistence_sort(arr1); + persistence_sort(arr2); + writeln(arr1); + writeln(arr2); +} + diff --git a/challenge-238/deadmarshal/go/ch1.go b/challenge-238/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..a880735306 --- /dev/null +++ b/challenge-238/deadmarshal/go/ch1.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" +) + +func runningSum(arr []int) { + sum := 0 + for _, v := range arr { + sum += v + fmt.Printf("%v ", sum) + } + fmt.Println() +} + +func main() { + arr1 := []int{1, 2, 3, 4, 5} + arr2 := []int{1, 1, 1, 1, 1} + arr3 := []int{0, -1, 1, 2} + runningSum(arr1) + runningSum(arr2) + runningSum(arr3) +} diff --git a/challenge-238/deadmarshal/haskell/ch1.hs b/challenge-238/deadmarshal/haskell/ch1.hs new file mode 100644 index 0000000000..dbae0a4d3d --- /dev/null +++ b/challenge-238/deadmarshal/haskell/ch1.hs @@ -0,0 +1,8 @@ +module Ch1 where + +main :: IO () +main = do + print $ scanl1 (+) [1,2,3,4,5] + print $ scanl1 (+) [1,1,1,1,1] + print $ scanl1 (+) [0,-1,1,2] + diff --git a/challenge-238/deadmarshal/haskell/ch2.hs b/challenge-238/deadmarshal/haskell/ch2.hs new file mode 100644 index 0000000000..8da89471c0 --- /dev/null +++ b/challenge-238/deadmarshal/haskell/ch2.hs @@ -0,0 +1,26 @@ +module Ch2 where + +import Data.List + +digits :: Integral a => a -> [a] +digits 0 = [] +digits x = digits (div x 10) ++ [mod x 10] + +helper :: Integral a => a -> a -> a +helper acc n + | n < 10 = acc + | otherwise = + helper (acc+1) (product $ digits n) + +persistenceSort :: Integral a => [a] -> [a] +persistenceSort xs = + sortBy (\a b -> let ha = helper 0 a; hb = helper 0 b in + if ha /= hb then + compare ha hb + else compare a b) xs + +main :: IO () +main = do + print $ persistenceSort [15,99,1,34] + print $ persistenceSort [50,25,33,22] + diff --git a/challenge-238/deadmarshal/java/Ch1.java b/challenge-238/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..43b8f25e2b --- /dev/null +++ b/challenge-238/deadmarshal/java/Ch1.java @@ -0,0 +1,20 @@ +public class Ch1 { + public static void main(String[] args) { + int[] arr1 = {1,2,3,4,5}; + int[] arr2 = {1,1,1,1,1}; + int[] arr3 = {0,-1,1,2}; + running_sum(arr1); + running_sum(arr2); + running_sum(arr3); + } + + private static void running_sum(int[] arr) { + int sum = 0; + for(var e : arr){ + sum += e; + System.out.printf("%d ",sum); + } + System.out.println(); + } +} + diff --git a/challenge-238/deadmarshal/java/Ch2.java b/challenge-238/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..0a71d431a4 --- /dev/null +++ b/challenge-238/deadmarshal/java/Ch2.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class Ch2 { + public static void main(String[] args) { + ArrayList<Integer> list1 = new ArrayList<>(List.of(15,99,1,34)); + ArrayList<Integer> list2 = new ArrayList<>(List.of(50,25,33,22)); + persistence_sort(list1); + persistence_sort(list2); + System.out.println(list1); + System.out.println(list2); + } + + private static int product(int n) { + int prod = 1; + while(n > 0) + { + prod *= n % 10; + n /= 10; + } + return prod; + } + + private static int helper(int n) { + int sum = 0; + while(n >= 10) + { + sum++; + n = product(n); + } + return sum; + } + + private static void persistence_sort(List<Integer> list) { + list.sort(new Comparator<Integer>() { + @Override + public int compare(Integer a, Integer b) { + int ha = helper(a); + int hb = helper(b); + return ha == hb ? Integer.compare(a,b) : + Integer.compare(ha,hb); + }}); + } +} + diff --git a/challenge-238/deadmarshal/lua/ch-1.lua b/challenge-238/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..d0e626fcdb --- /dev/null +++ b/challenge-238/deadmarshal/lua/ch-1.lua @@ -0,0 +1,17 @@ +#!/usr/bin/env lua + +local function running_sum(t) + assert(type(t) == 'table','t must be a table!') + local ret = {} + local sum = 0 + for i=1,#t do + sum = sum + t[i] + ret[#ret+1] = sum + end + return ret +end + +print(table.unpack(running_sum{1,2,3,4,5})) +print(table.unpack(running_sum{1,1,1,1,1})) +print(table.unpack(running_sum{0,-1,1,2})) + diff --git a/challenge-238/deadmarshal/lua/ch-2.lua b/challenge-238/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..660235f0bb --- /dev/null +++ b/challenge-238/deadmarshal/lua/ch-2.lua @@ -0,0 +1,32 @@ +#!/usr/bin/env lua + +local function product(n) + assert(type(n) == 'number','n must be a number!') + local prod = 1 + while n > 0 do + prod = prod * (n % 10) + n = n // 10 + end + return prod +end + +local function helper(n) + assert(type(n) == 'number','n must be a number!') + local sum = 0 + repeat + sum = sum + 1 + n = product(n) + until n < 10 + return sum +end + +local function persistence_sort(t) + assert(type(t) == 'table','t must be a table!') + table.sort(t,function(a,b) return helper(a) < helper(b) or + a < b end) + print(table.unpack(t)) +end + +persistence_sort{15,99,1,34} +persistence_sort{50,25,33,22} + diff --git a/challenge-238/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-238/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..e487358900 --- /dev/null +++ b/challenge-238/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,26 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{1,2,3,4,5}; + A2:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{1,1,1,1,1}; + A3:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{0,-1,1,2}; + +PROCEDURE RunningSum(VAR Arr:ARRAY OF INTEGER) = + VAR Sum:INTEGER; + BEGIN + Sum := 0; + FOR I := FIRST(Arr) TO LAST(Arr) DO + INC(Sum,Arr[I]); + IO.PutInt(Sum); IO.Put(" ") + END; + IO.Put("\n"); + END RunningSum; + +BEGIN + RunningSum(A1); + RunningSum(A2); + RunningSum(A3); +END Ch1. + diff --git a/challenge-238/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-238/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..0ee72d695b --- /dev/null +++ b/challenge-238/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch1") +program("ch1") + diff --git a/challenge-238/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-238/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..b1c3583f94 --- /dev/null +++ b/challenge-238/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,78 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; + +TYPE + TProc = PROCEDURE(A,B:INTEGER):INTEGER; + +VAR + A1:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{15,99,1,34}; + A2:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{50,25,33,22}; + +PROCEDURE Helper(N:INTEGER):INTEGER = + VAR Sum:INTEGER := 0; + PROCEDURE Product(N:INTEGER):INTEGER = + VAR P:INTEGER; + BEGIN + P := 1; + WHILE N # 0 DO P := P * (N MOD 10); N := N DIV 10 END; + RETURN P + END Product; + BEGIN + REPEAT INC(Sum); N := Product(N) UNTIL N < 10; + RETURN Sum + END Helper; + +PROCEDURE Compare(A,B:INTEGER):INTEGER = + VAR Ha,Hb:INTEGER; + BEGIN + Ha := Helper(A); + Hb := Helper(B); + IF Ha # Hb THEN + IF Ha < Hb THEN RETURN -1 ELSE RETURN 1 END + END; + IF A < B THEN RETURN -1 + ELSIF A > B THEN RETURN 1 + ELSE RETURN 0 + END; + END Compare; + +PROCEDURE QuickSort(VAR A:ARRAY OF INTEGER;Left,Right:INTEGER;Comp:TProc) = + VAR + I,J,Pivot,Temp:INTEGER; + BEGIN + I := Left; J := Right; + Pivot := A[(Left + Right) DIV 2]; + REPEAT + WHILE Comp(Pivot,A[I]) = 1 DO INC(I) END; + WHILE Comp(Pivot,A[J]) = -1 DO DEC(J) END; + IF I <= J THEN + Temp := A[I]; + A[I] := A[J]; + A[J] := Temp; + INC(I); + DEC(J); + END; + UNTIL I > J; + IF Left < J THEN QuickSort(A,Left,J,Comp) END; + IF I < Right THEN QuickSort(A,I,Right,Comp) END; + END QuickSort; + +PROCEDURE PersistenceSort(VAR A:ARRAY OF INTEGER) = + BEGIN + QuickSort(A,FIRST(A),LAST(A),Compare); + END PersistenceSort; + +PROCEDURE PrintArray(VAR A:ARRAY OF INTEGER) = + BEGIN + FOR I := FIRST(A) TO LAST(A) DO IO.PutInt(A[I]); IO.Put(" ") END; + IO.Put("\n") + END PrintArray; + +BEGIN + PersistenceSort(A1); + PersistenceSort(A2); + PrintArray(A1); + PrintArray(A2); +END Ch2. + diff --git a/challenge-238/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-238/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..5c32bbc4bb --- /dev/null +++ b/challenge-238/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch2") +program("ch2") + diff --git a/challenge-238/deadmarshal/oberon/Ch1.Mod b/challenge-238/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..6b07ab39dd --- /dev/null +++ b/challenge-238/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,33 @@ +MODULE Ch1; + + IMPORT Out; + + VAR + A1,A2:ARRAY 5 OF LONGINT; + A3:ARRAY 4 OF LONGINT; + + PROCEDURE Init; + BEGIN + A1[0] := 1; A1[1] := 2; A1[2] := 3; A1[3] := 4; A1[4] := 5; + A2[0] := 1; A2[1] := 1; A2[2] := 1; A2[3] := 1; A2[4] := 1; + A3[0] := 0; A3[1] := -1; A3[2] := 1; A3[3] := 2; + END Init; + + PROCEDURE RunningSum(VAR arr:ARRAY OF LONGINT); + VAR sum,i:LONGINT; + BEGIN + sum := 0; + FOR i := 0 TO LEN(arr)-1 DO + INC(sum,arr[i]); + Out.Int(sum,0); Out.Char(' ') + END; + Out.Ln + END RunningSum; + +BEGIN + Init; + RunningSum(A1); + RunningSum(A2); + RunningSum(A3); +END Ch1. + diff --git a/challenge-238/deadmarshal/oberon/Ch2.Mod b/challenge-238/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..1c607c3d6d --- /dev/null +++ b/challenge-238/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,82 @@ +MODULE Ch2; + + IMPORT Out; + + TYPE + TProc = PROCEDURE(a,b:LONGINT):LONGINT; + + VAR + A1,A2:ARRAY 4 OF LONGINT; + + PROCEDURE Init; + BEGIN + A1[0] := 15; A1[1] := 99; A1[2] := 1; A1[3] := 34; + A2[0] := 50; A2[1] := 25; A2[2] := 33; A2[3] := 22; + END Init; + + PROCEDURE Helper(n:LONGINT):LONGINT; + VAR sum:LONGINT; + PROCEDURE Product(n:LONGINT):LONGINT; + VAR p:LONGINT; + BEGIN + p := 1; + WHILE n # 0 DO p := p * (n MOD 10); n := n DIV 10 END; + RETURN p + END Product; + BEGIN + sum := 0; + REPEAT INC(sum); n := Product(n) UNTIL n < 10; + RETURN sum + END Helper; + + PROCEDURE Compare(a,b:LONGINT):LONGINT; + VAR ha,hb:LONGINT; + BEGIN + ha := Helper(a); + hb := Helper(b); + IF ha # hb THEN + IF ha < hb THEN RETURN -1 ELSE RETURN 1 END + END; + IF a < b THEN RETURN -1 + ELSIF a > b THEN RETURN 1 + ELSE RETURN 0 + END; + END Compare; + + PROCEDURE QuickSort(VAR arr:ARRAY OF LONGINT;left,right:LONGINT;comp:TProc); + VAR + i,j,pivot,temp:LONGINT; + BEGIN + i := left; j := right; + pivot := arr[(left + right) DIV 2]; + REPEAT + WHILE comp(pivot,arr[i]) = 1 DO INC(i) END; + WHILE comp(pivot,arr[j]) = -1 DO DEC(j) END; + IF i <= j THEN + temp := arr[i]; arr[i] := arr[j]; arr[j] := temp; + INC(i); DEC(j); + END; + UNTIL i > j; + IF left < j THEN QuickSort(arr,left,j,comp) END; + IF i < right THEN QuickSort(arr,i,right,comp) END; + END QuickSort; + + PROCEDURE PrintArray(VAR arr:ARRAY OF LONGINT); + VAR i:LONGINT; + BEGIN + FOR i := 0 TO LEN(arr)-1 DO Out.Int(arr[i],0); Out.Char(' ') END; Out.Ln + END PrintArray; + + PROCEDURE PersistenceSort(VAR arr:ARRAY OF LONGINT); + BEGIN + QuickSort(arr,0,LEN(arr)-1,Compare); + END PersistenceSort; + +BEGIN + Init; + PersistenceSort(A1); + PersistenceSort(A2); + PrintArray(A1); + PrintArray(A2); +END Ch2. + diff --git a/challenge-238/deadmarshal/ocaml/ch1.ml b/challenge-238/deadmarshal/ocaml/ch1.ml new file mode 100644 index 0000000000..ef0bf39a87 --- /dev/null +++ b/challenge-238/deadmarshal/ocaml/ch1.ml @@ -0,0 +1,28 @@ +let running_sum lst = + let rec aux s acc lst = + match lst with + | [] -> acc + | hd :: tl -> + let temp = s + hd in + aux temp (temp :: acc) tl + in + List.rev (aux 0 [] lst) + +let _ = + let open Printf in + [1;2;3;4;5] + |> running_sum + |> List.map string_of_int + |> String.concat " " + |> printf "[%s]\n"; + [1;1;1;1;1] + |> running_sum + |> List.map string_of_int + |> String.concat " " + |> printf "[%s]\n"; + [0;-1;1;2] + |> running_sum + |> List.map string_of_int + |> String.concat " " + |> printf "[%s]\n"; + diff --git a/challenge-238/deadmarshal/ocaml/ch2.ml b/challenge-238/deadmarshal/ocaml/ch2.ml new file mode 100644 index 0000000000..b8207b66d9 --- /dev/null +++ b/challenge-238/deadmarshal/ocaml/ch2.ml @@ -0,0 +1,34 @@ +let rec digits (d:int) = + if d = 0 then [] else digits (d / 10) @ [d mod 10] + +let rec helper (acc:int) (n:int) = + let rec product prod lst = + match lst with + | [] -> prod + | hd :: tl -> + product (prod * hd) tl + in + if n < 10 then + acc + else + helper (succ acc) (product 1 (digits n)) + +let persistence_sort (lst:int list) = + List.sort (fun a b -> + let ha = helper 0 a and hb = helper 0 b in + if ha <> hb then + compare ha hb + else + compare a b) lst + +let _ = + let open Printf in + persistence_sort [15;99;1;34] + |> List.map string_of_int + |> String.concat " " + |> printf "[%s]\n"; + persistence_sort [50;25;33;22] + |> List.map string_of_int + |> String.concat " " + |> printf "[%s]\n"; + diff --git a/challenge-238/deadmarshal/perl/ch-1.pl b/challenge-238/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..6224938c1a --- /dev/null +++ b/challenge-238/deadmarshal/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub running_sum{ + my $sum = 0; + map{$sum += $_[0]->[$_]} 0..$#{$_[0]} +} + +printf "(%s)\n",join ',',running_sum([1,2,3,4,5]); +printf "(%s)\n",join ',',running_sum([1,1,1,1,1]); +printf "(%s)\n",join ',',running_sum([0,-1,1,2]); + diff --git a/challenge-238/deadmarshal/perl/ch-2.pl b/challenge-238/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..3f430e3ad2 --- /dev/null +++ b/challenge-238/deadmarshal/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(product); + +sub persistence_sort{ + my $helper = sub{ + my ($sum,$n) = (0,@_); + $n = do{$sum++; product split '',$n} until $n < 10; + $sum + }; + sort{$helper->($a) <=> $helper->($b) || $a <=> $b} @{$_[0]} +} + +printf "(%s)\n",join ',',persistence_sort([15,99,1,34]); +printf "(%s)\n",join ',',persistence_sort([50,25,33,22]); + diff --git a/challenge-238/deadmarshal/raku/ch-1.raku b/challenge-238/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..ce49199a2e --- /dev/null +++ b/challenge-238/deadmarshal/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +sub running-sum(@arr) +{ + [\+] @arr +} + +say running-sum([1,2,3,4,5]); +say running-sum([1,1,1,1,1]); +say running-sum([0,-1,1,2]); + diff --git a/challenge-238/deadmarshal/raku/ch-2.raku b/challenge-238/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..ffe6fe9004 --- /dev/null +++ b/challenge-238/deadmarshal/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +sub helper($n is copy) +{ + my $sum = 0; + $n = do {$sum++; [*] $n.comb} until $n < 10; + $sum +} + +sub persistence_sort(@arr) +{ + @arr.sort({helper($_), $_}); +} + +say persistence_sort([15,99,1,34]); +say persistence_sort([50,25,33,22]); + |
