diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-02 11:01:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-02 11:01:21 +0000 |
| commit | 62dc1a7c12fb3bc19a572c7bfb1404705847917c (patch) | |
| tree | 899e51c0b6177de82871a0a7b873b056df4418a0 | |
| parent | abe50443fc73cffd819a05ab7733c4c192797d36 (diff) | |
| parent | 0c702901ccc86d908ce0bb77de5698f555b3c958 (diff) | |
| download | perlweeklychallenge-club-62dc1a7c12fb3bc19a572c7bfb1404705847917c.tar.gz perlweeklychallenge-club-62dc1a7c12fb3bc19a572c7bfb1404705847917c.tar.bz2 perlweeklychallenge-club-62dc1a7c12fb3bc19a572c7bfb1404705847917c.zip | |
Merge pull request #7019 from deadmarshal/challenge189
challenge189
24 files changed, 813 insertions, 0 deletions
diff --git a/challenge-189/deadmarshal/c/ch-1.c b/challenge-189/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..5e5ce7d7ce --- /dev/null +++ b/challenge-189/deadmarshal/c/ch-1.c @@ -0,0 +1,36 @@ +#include<stdio.h> +#include<stdlib.h> + +int compare_chars(const void *a, const void *b) +{ + char arg1 = *(const char*)a; + char arg2 = *(const char*)b; + return(arg1 > arg2) - (arg1 < arg2); +} + +char greater_character(char *arr, size_t sz, char target) +{ + qsort((void*)arr, sz, sizeof(char), compare_chars); + for(size_t i = 0; i < sz; ++i) + { + if(arr[i] > target) return arr[i]; + } + return target; +} + +int main() +{ + char a1[4] = {'e','m','u','g'}; + char a2[4] = {'d','c','e','f'}; + char a3[3] = {'j','a','r'}; + char a4[4] = {'d','c','a','f'}; + char a5[4] = {'t','g','a','l'}; + size_t sz1 = 4, sz2 = 3; + printf("%c\n", greater_character(a1, sz1, 'b')); + printf("%c\n", greater_character(a2, sz1, 'a')); + printf("%c\n", greater_character(a3, sz2, 'o')); + printf("%c\n", greater_character(a4, sz1, 'a')); + printf("%c\n", greater_character(a5, sz1, 'v')); + return 0; +} + diff --git a/challenge-189/deadmarshal/c/ch-2.c b/challenge-189/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..27923a41b0 --- /dev/null +++ b/challenge-189/deadmarshal/c/ch-2.c @@ -0,0 +1,51 @@ +#include<stdio.h> + +void array_degree(int *arr, size_t sz) +{ + int left[10] = {0}, count[10] = {0}; + int x = 0, min = 0, max = 0, index = 0; + + for(size_t i = 0; i < sz; ++i) + { + x = arr[i]; + if(count[x] == 0) + { + left[x] = i; + count[x] = 1; + } + else count[x]++; + + if(count[x] > max) + { + max = count[x]; + min = i - left[x] + 1; + index = left[x]; + } + else if((count[x] == max) && (i - left[x] + 1 < min)) + { + min = i - left[x] + 1; + index = left[x]; + } + } + + for(size_t i = index; i < (index+min); ++i) + printf("%d ", arr[i]); + puts(""); +} + +int main(void) +{ + int a1[4] = {1,3,3,2}; + int a2[4] = {1,2,1,3}; + int a3[5] = {1,3,2,1,2}; + int a4[5] = {1,1,2,3,2}; + int a5[5] = {2,1,2,1,1}; + size_t sz1 = 4, sz2 = 5; + array_degree(a1, sz1); + array_degree(a2, sz1); + array_degree(a3, sz2); + array_degree(a4, sz2); + array_degree(a5, sz2); + return 0; +} + diff --git a/challenge-189/deadmarshal/cpp/ch-1.cpp b/challenge-189/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..46aa62cb47 --- /dev/null +++ b/challenge-189/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,23 @@ +#include<iostream> +#include<algorithm> + +char greater_character(std::vector<char> vec, char target) +{ + std::sort(vec.begin(), vec.end()); + for(std::size_t i = 0; i < vec.size(); ++i) + { + if(vec[i] > target) return vec[i]; + } + return target; +} + +int main() +{ + std::cout << greater_character({'e','m','u','g'}, 'b') << '\n'; + std::cout << greater_character({'d','c','e','f'}, 'a') << '\n'; + std::cout << greater_character({'j','a','r'}, 'o') << '\n'; + std::cout << greater_character({'d','c','a','f'}, 'a') << '\n'; + std::cout << greater_character({'t','g','a','l'}, 'v') << '\n'; + return 0; +} + diff --git a/challenge-189/deadmarshal/cpp/ch-2.cpp b/challenge-189/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..ced6366898 --- /dev/null +++ b/challenge-189/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,51 @@ +#include<iostream> +#include<vector> +#include<unordered_map> + +void array_degree(const std::vector<int>& vec) +{ + std::unordered_map<int,int> left{},count{}; + std::size_t x{},min{},max{},index{}; + for(std::size_t i = 0; i < vec.size(); ++i) + { + x = vec[i]; + if(!count.contains(x)) + { + left[x] = i; + count[x] = 1; + } + else count[x]++; + + if(count[x] > max) + { + max = count[x]; + min = i - left[x] + 1; + index = left[x]; + } + else if((count[x] == max) && (i - left[x] + 1 < min)) + { + min = i - left[x] + 1; + index = left[x]; + } + } + + for(std::size_t i = index; i < index+min; ++i) + std::cout << vec[i] << ' '; + std::cout << '\n'; +} + +int main() +{ + std::vector<int> + vec1{1,3,3,2}, + vec2{1,2,1,3}, + vec3{1,3,2,1,2}, + vec4{1,1,2,3,2}, + vec5{2,1,2,1,1}; + array_degree(vec1); + array_degree(vec2); + array_degree(vec3); + array_degree(vec4); + array_degree(vec5); + return 0; +} diff --git a/challenge-189/deadmarshal/d/ch1.d b/challenge-189/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..7c91795a4f --- /dev/null +++ b/challenge-189/deadmarshal/d/ch1.d @@ -0,0 +1,19 @@ +import std.stdio:writeln; +import std.algorithm:sort; + +dchar greater_character(dchar[] arr, dchar target) +{ + arr.sort(); + for(size_t i = 0; i < arr.length; ++i) + if(arr[i] > target) return arr[i]; + return target; +} + +void main() +{ + writeln(greater_character(['e','m','u','g'], 'b')); + writeln(greater_character(['d','c','e','f'], 'a')); + writeln(greater_character(['j','a','r'], 'o')); + writeln(greater_character(['d','c','a','f'], 'a')); + writeln(greater_character(['t','g','a','l'], 'v')); +} diff --git a/challenge-189/deadmarshal/d/ch2.d b/challenge-189/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..6fc863027e --- /dev/null +++ b/challenge-189/deadmarshal/d/ch2.d @@ -0,0 +1,47 @@ +import std.stdio:write,writeln; + +void array_degree(const ref int[] arr) +{ + int[int] left,count; + int x,min,max,index; + for(int i = 0; i < arr.length; ++i) + { + x = arr[i]; + if(x !in count) + { + left[x] = i; + count[x] = 1; + } + else count[x]++; + + if(count[x] > max) + { + max = count[x]; + min = i - left[x] + 1; + index = left[x]; + } + else if((count[x] == max) && (i - left[x] + 1 < min)) + { + min = i - left[x] + 1; + index = left[x]; + } + } + for(int i = index; i < index+min; ++i) + write(arr[i], ' '); + writeln; +} + +void main() +{ + int[] arr1 = [1,3,3,2]; + int[] arr2 = [1,2,1,3]; + int[] arr3 = [1,3,2,1,2]; + int[] arr4 = [1,1,2,3,2]; + int[] arr5 = [2,1,2,1,1]; + array_degree(arr1); + array_degree(arr2); + array_degree(arr3); + array_degree(arr4); + array_degree(arr5); +} + diff --git a/challenge-189/deadmarshal/lua/ch-1.lua b/challenge-189/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..6cbb08d73f --- /dev/null +++ b/challenge-189/deadmarshal/lua/ch-1.lua @@ -0,0 +1,15 @@ +local function greater_character(t,target) + assert(type(t) == 'table' and type(target) == 'string', + 't and target must be a table and a string respectively!') + table.sort(t) + for i=1, #t do + if t[i] > target then return t[i] end + end + return target +end + +print(greater_character({'e','m','u','g'}, 'b')) +print(greater_character({'d','c','e','f'}, 'a')) +print(greater_character({'j','a','r'}, 'o')) +print(greater_character({'d','c','a','f'}, 'a')) +print(greater_character({'t','g','a','l'}, 'v')) diff --git a/challenge-189/deadmarshal/lua/ch-2.lua b/challenge-189/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..87b9a882e2 --- /dev/null +++ b/challenge-189/deadmarshal/lua/ch-2.lua @@ -0,0 +1,31 @@ +local function array_degree(t) + local left,count = {},{} + local index,max,min = 0,0,0 + for i=1, #t do + if not count[t[i]] then + left[t[i]] = i + count[t[i]] = 1 + else + count[t[i]] = count[t[i]] + 1 + end + if count[t[i]] > max then + max = count[t[i]] + min = i - left[t[i]] + index = left[t[i]] + elseif(count[t[i]] == max) and (i - left[t[i]] < min) then + min = i - left[t[i]] + index = left[t[i]] + end + end + for i=index, index+min do + io.write(t[i], ' ') + end + print() +end + +array_degree({1,3,3,2}) +array_degree({1,2,1,3}) +array_degree({1,3,2,1,2}) +array_degree({1,1,2,3,2}) +array_degree({2,1,2,1,1}) + diff --git a/challenge-189/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-189/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..17249fc595 --- /dev/null +++ b/challenge-189/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,51 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..3] OF CHAR := ARRAY OF CHAR{'e','m','u','g'}; + A2:ARRAY[0..3] OF CHAR := ARRAY OF CHAR{'d','c','e','f'}; + A3:ARRAY[0..2] OF CHAR := ARRAY OF CHAR{'j','a','r'}; + A4:ARRAY[0..3] OF CHAR := ARRAY OF CHAR{'d','c','a','f'}; + A5:ARRAY[0..3] OF CHAR := ARRAY OF CHAR{'t','g','a','l'}; + +PROCEDURE QuickSort(VAR A:ARRAY OF CHAR;Left,Right:INTEGER) = + VAR + I,J:INTEGER; + Pivot,Temp:CHAR; + BEGIN + I := Left; + J := Right; + Pivot := A[(Left + Right) DIV 2]; + REPEAT + WHILE ORD(Pivot) > ORD(A[I]) DO INC(I) END; + WHILE ORD(Pivot) < ORD(A[J]) 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) END; + IF I < Right THEN QuickSort(A, I, Right) END; + END QuickSort; + +PROCEDURE GreaterCharacter(VAR A:ARRAY OF CHAR;Target:CHAR):CHAR = + BEGIN + QuickSort(A, 0, LAST(A)); + FOR I := FIRST(A) TO LAST(A) DO + IF A[I] > Target THEN RETURN A[I] END; + END; + RETURN Target; + END GreaterCharacter; + +BEGIN + IO.PutChar(GreaterCharacter(A1,'b')); IO.Put("\n"); + IO.PutChar(GreaterCharacter(A2,'a')); IO.Put("\n"); + IO.PutChar(GreaterCharacter(A3,'o')); IO.Put("\n"); + IO.PutChar(GreaterCharacter(A4,'a')); IO.Put("\n"); + IO.PutChar(GreaterCharacter(A5,'v')); IO.Put("\n"); +END Ch1. + diff --git a/challenge-189/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-189/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..8a6bc1e4f8 --- /dev/null +++ b/challenge-189/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,3 @@ +import("libm3") +implementation("Ch1") +program("ch1") diff --git a/challenge-189/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-189/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..f89b9f525a --- /dev/null +++ b/challenge-189/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,50 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{1,3,3,2}; + A2:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{1,2,1,3}; + A3:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{1,3,2,1,2}; + A4:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{1,1,2,3,2}; + A5:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{2,1,2,1,1}; + +PROCEDURE ArrayDegree(VAR Arr:ARRAY OF INTEGER) = + VAR + Left,Count:ARRAY[0..9] OF INTEGER; + X,Min,Max,Index:INTEGER := 0; + BEGIN + FOR I := FIRST(Arr) TO LAST(Arr) DO + X := Arr[I]; + IF Count[X] = 0 THEN + Left[X] := I; + Count[X] := 1; + ELSE + Count[X] := Count[X] + 1; + END; + + IF Count[X] > Max THEN + Max := Count[X]; + Min := I - Left[X] + 1; + Index := Left[X]; + ELSIF((Count[X] = Max) AND (I - Left[X] + 1 < Min)) THEN + Min := I - Left[X] + 1; + Index := Left[X]; + END; + END; + + FOR I := Index TO Index+Min-1 DO + IO.PutInt(Arr[I]); + IO.PutChar(' '); + END; + IO.Put("\n"); + END ArrayDegree; + +BEGIN + ArrayDegree(A1); + ArrayDegree(A2); + ArrayDegree(A3); + ArrayDegree(A4); + ArrayDegree(A5); +END Ch2. + diff --git a/challenge-189/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-189/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..4bfbb7373d --- /dev/null +++ b/challenge-189/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,3 @@ +import("libm3") +implementation("Ch2") +program("ch2") diff --git a/challenge-189/deadmarshal/nim/ch1.nim b/challenge-189/deadmarshal/nim/ch1.nim new file mode 100644 index 0000000000..0e31eb49b5 --- /dev/null +++ b/challenge-189/deadmarshal/nim/ch1.nim @@ -0,0 +1,20 @@ +import std/algorithm + +proc greaterCharacter(s:var seq[char];target:char):char = + s.sort() + for i in 0..<s.len: + if s[i] > target: return s[i] + return target + +var + s1 = @['e','m','u','g'] + s2 = @['d','c','e','f'] + s3 = @['j','a','r'] + s4 = @['d','c','a','f'] + s5 = @['t','g','a','l'] + +echo greater_character(s1,'b') +echo greater_character(s2,'a') +echo greater_character(s3,'o') +echo greater_character(s4,'a') +echo greater_character(s5,'v') diff --git a/challenge-189/deadmarshal/nim/ch2.nim b/challenge-189/deadmarshal/nim/ch2.nim new file mode 100644 index 0000000000..4e1d9f540d --- /dev/null +++ b/challenge-189/deadmarshal/nim/ch2.nim @@ -0,0 +1,32 @@ +import std/tables + +proc arrayDegree(s:seq[int]) = + var + left = initTable[int,int]() + count = initTable[int,int]() + mx,mn,index,x:int + + for i in 0..<s.len: + x = s[i] + if x notin count: + left[x] = i + count[x] = 1 + else: count[x] += 1 + + if count[x] > mx: + mx = count[x] + mn = i - left[x] + 1 + index = left[x] + elif count[x] == mx and i - left[x] + 1 < mn: + mn = i - left[x] + 1 + index = left[x] + + for i in index..<index+mn: + stdout.write(s[i], ' ') + echo "" + +arrayDegree(@[1,3,3,2]); +arrayDegree(@[1,2,1,3]); +arrayDegree(@[1,3,2,1,2]); +arrayDegree(@[1,1,2,3,2]); +arrayDegree(@[2,1,2,1,1]); diff --git a/challenge-189/deadmarshal/oberon/Ch1.Mod b/challenge-189/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..7094b73832 --- /dev/null +++ b/challenge-189/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,60 @@ +MODULE Ch1; + + IMPORT Out; + + VAR + A1,A2,A4,A5:ARRAY 4 OF CHAR; + A3:ARRAY 3 OF CHAR; + + PROCEDURE Init(); + BEGIN + A1[0] := 'e'; A1[1] := 'm'; A1[2] := 'u'; A1[3] := 'g'; + A2[0] := 'd'; A2[1] := 'c'; A2[2] := 'e'; A2[3] := 'f'; + A3[0] := 'j'; A3[1] := 'a'; A3[2] := 'r'; + A4[0] := 'd'; A4[1] := 'c'; A4[2] := 'a'; A4[3] := 'f'; + A5[0] := 't'; A5[1] := 'g'; A5[2] := 'a'; A5[3] := 'l'; + END Init; + + PROCEDURE QuickSort(VAR A:ARRAY OF CHAR;Left,Right:LONGINT); + VAR + I,J:LONGINT; + Pivot,Temp:CHAR; + BEGIN + I := Left; + J := Right; + Pivot := A[(Left + Right) DIV 2]; + REPEAT + WHILE ORD(Pivot) > ORD(A[I]) DO INC(I) END; + WHILE ORD(Pivot) < ORD(A[J]) 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) END; + IF I < Right THEN QuickSort(A, I, Right) END; + END QuickSort; + + PROCEDURE GreaterCharacter(VAR A:ARRAY OF CHAR;Target:CHAR):CHAR; + VAR + I:LONGINT; + BEGIN + QuickSort(A, 0, LEN(A)-1); + FOR I := 0 TO LEN(A)-1 DO + IF A[I] > Target THEN RETURN A[I] END + END; + RETURN Target; + END GreaterCharacter; + +BEGIN + Init; + Out.Char(GreaterCharacter(A1,'b')); Out.Ln; + Out.Char(GreaterCharacter(A2,'a')); Out.Ln; + Out.Char(GreaterCharacter(A3,'o')); Out.Ln; + Out.Char(GreaterCharacter(A4,'a')); Out.Ln; + Out.Char(GreaterCharacter(A5,'v')); Out.Ln; +END Ch1. + diff --git a/challenge-189/deadmarshal/oberon/Ch2.Mod b/challenge-189/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..a5b4f901e3 --- /dev/null +++ b/challenge-189/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,59 @@ +MODULE Ch2; + + IMPORT Out; + + VAR + A1,A2:ARRAY 4 OF INTEGER; + A3,A4,A5:ARRAY 5 OF INTEGER; + + PROCEDURE Init(); + BEGIN + A1[0] := 1; A1[1] := 3; A1[2] := 3; A1[3] := 2; + A2[0] := 1; A2[1] := 2; A2[2] := 1; A2[3] := 3; + A3[0] := 1; A3[1] := 3; A3[2] := 2; A3[3] := 1; A3[4] := 2; + A4[0] := 1; A4[1] := 1; A4[2] := 2; A4[3] := 3; A4[4] := 2; + A5[0] := 2; A5[1] := 1; A5[2] := 2; A5[3] := 1; A5[4] := 1; + END Init; + + PROCEDURE ArrayDegree(VAR Arr:ARRAY OF INTEGER); + VAR + Left,Count:ARRAY 10 OF INTEGER; + I,X,Min,Max,Index:LONGINT; + BEGIN + Min := 0; Max := 0; Index := 0; + FOR I := 0 TO LEN(Left)-1 DO Left[I] := 0 END; + FOR I := 0 TO LEN(Count)-1 DO Count[I] := 0 END; + FOR I := 0 TO LEN(Arr)-1 DO + X := Arr[I]; + IF Count[X] = 0 THEN + Left[X] := SHORT(I); + Count[X] := 1; + ELSE + Count[X] := Count[X] + 1; + END; + IF Count[X] > Max THEN + Max := Count[X]; + Min := I - Left[X] + 1; + Index := Left[X]; + ELSIF((Count[X] = Max) & (I - Left[X] + 1 < Min)) THEN + Min := I - Left[X] + 1; + Index := Left[X]; + END; + END; + + FOR I := Index TO (Index+Min)-1 DO + Out.Int(Arr[I],0); + Out.Char(' '); + END; + Out.Ln; + END ArrayDegree; + +BEGIN + Init; + ArrayDegree(A1); + ArrayDegree(A2); + ArrayDegree(A3); + ArrayDegree(A4); + ArrayDegree(A5); +END Ch2. + diff --git a/challenge-189/deadmarshal/pascal/ch1.pas b/challenge-189/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..48a5e2a622 --- /dev/null +++ b/challenge-189/deadmarshal/pascal/ch1.pas @@ -0,0 +1,55 @@ +program Ch1; + +{$mode objfpc} +uses + SysUtils; + +var + A1:array[0..3] of Char = ('e','m','u','g'); + A2:array[0..3] of Char = ('d','c','e','f'); + A3:array[0..2] of Char = ('j','a','r'); + A4:array[0..3] of Char = ('d','c','a','f'); + A5:array[0..3] of Char = ('t','g','a','l'); + +procedure QuickSort(var A:array of Char;Left,Right:Integer); +var + I,J:Integer; + Pivot,Temp:Char; +begin + I := Left; + J := Right; + Pivot := A[(Left + Right) div 2]; + repeat + while Ord(Pivot) > Ord(A[I]) do Inc(I); + while Ord(Pivot) < Ord(A[J]) do Dec(J); + if I <= J then + begin + 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); + if I < Right then QuickSort(A, I, Right); +end; + +function GreaterCharacter(var A:array of Char;Target:Char):Char; +var + I:Integer; +begin + QuickSort(A, Low(A), High(A)); + for I := Low(A) to High(A) do + if A[I] > Target then Exit(A[I]); + Result := Target; +end; + +begin + WriteLn(GreaterCharacter(A1,'b')); + WriteLn(GreaterCharacter(A2,'a')); + WriteLn(GreaterCharacter(A3,'o')); + WriteLn(GreaterCharacter(A4,'a')); + WriteLn(GreaterCharacter(A5,'v')); +end. + diff --git a/challenge-189/deadmarshal/pascal/ch2.pas b/challenge-189/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..4c0bc2d6df --- /dev/null +++ b/challenge-189/deadmarshal/pascal/ch2.pas @@ -0,0 +1,51 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils,Types; + +procedure ArrayDegree(constref Arr:TIntegerDynArray); +var + Left,Count:array[0..9] of Integer; + I,X,Min,Max,Index:Integer; +begin + Min := 0; Max := 0; Index := 0; + FillDWord(Left,Length(Left),0); + FillDWord(Count,Length(Count),0); + for I := Low(Arr) to High(Arr) do + begin + X := Arr[I]; + if Count[X] = 0 then + begin + Left[X] := I; + Count[X] := 1; + end + else Count[X] := Count[X] + 1; + + if Count[X] > Max then + begin + Max := Count[X]; + Min := I - Left[X] + 1; + Index := Left[X]; + end + else if((Count[X] = Max) and (I - Left[X] + 1 < Min)) then + begin + Min := I - Left[X] + 1; + Index := Left[X]; + end; + end; + + for I := Index to Pred(Index + Min) do + Write(Arr[I], ' '); + WriteLn; +end; + +begin + ArrayDegree([1,3,3,2]); + ArrayDegree([1,2,1,3]); + ArrayDegree([1,3,2,1,2]); + ArrayDegree([1,1,2,3,2]); + ArrayDegree([2,1,2,1,1]); +end. + diff --git a/challenge-189/deadmarshal/perl/ch-1.pl b/challenge-189/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..117460f10d --- /dev/null +++ b/challenge-189/deadmarshal/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub greater_character{ + my ($arr, $target) = @_; + @$arr = sort {$a cmp $b} @$arr; + for(my $i = 0; $i < @$arr; ++$i){ + return $arr->[$i] if $arr->[$i] gt $target; + } + return $target; +} + +print greater_character([qw/e m u g/], 'b'), "\n"; +print greater_character([qw/d c e f/], 'a'), "\n"; +print greater_character([qw/j a r/], 'o'), "\n"; +print greater_character([qw/d c a f/], 'a'), "\n"; +print greater_character([qw/t g a l/], 'v'), "\n"; + diff --git a/challenge-189/deadmarshal/perl/ch-2.pl b/challenge-189/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..adc8cd6cdd --- /dev/null +++ b/challenge-189/deadmarshal/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub array_degree{ + my ($arr) = @_; + my (%left,%count); + my ($max,$min,$index) = (0) x 3; + for(my $i = 0; $i < @$arr; ++$i){ + my $x = $arr->[$i]; + if(!$count{$x}){ + $left{$x} = $i; + $count{$x} = 1; + } + else{ + $count{$x}++; + } + if($count{$x} > $max){ + $max = $count{$x}; + $min = $i - $left{$x} + 1; + $index = $left{$x}; + } + elsif(($count{$x} == $max) && + ($i - $left{$x} + 1 < $min)){ + $min = $i - $left{$x} + 1; + $index = $left{$x}; + } + } + foreach($index..$index+$min-1){ + print $arr->[$_], ' '; + } + print "\n"; +} + +array_degree([1,3,3,2]); +array_degree([1,2,1,3]); +array_degree([1,3,2,1,2]); +array_degree([1,1,2,3,2]); +array_degree([2,1,2,1,1]); + diff --git a/challenge-189/deadmarshal/python/ch1.py b/challenge-189/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..daa9b0b8d5 --- /dev/null +++ b/challenge-189/deadmarshal/python/ch1.py @@ -0,0 +1,11 @@ +def greater_character(arr, target): + arr.sort() + for i in range(0,len(arr)): + if arr[i] > target: return arr[i] + return target + +print(greater_character(['e','m','u','g'], 'b')) +print(greater_character(['d','c','e','f'], 'a')) +print(greater_character(['j','a','r'], 'o')) +print(greater_character(['d', 'c', 'a', 'f'], 'a')) +print(greater_character(['t', 'g', 'a', 'l'], 'v')) diff --git a/challenge-189/deadmarshal/python/ch2.py b/challenge-189/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..89e0fd752c --- /dev/null +++ b/challenge-189/deadmarshal/python/ch2.py @@ -0,0 +1,28 @@ +def array_degree(arr): + left,count = {},{} + mx,mn,index = 0,0,0 + for i in range(0,len(arr)): + x = arr[i] + if x not in count: + left[x] = i + count[x] = 1 + else: count[x] += 1 + + if count[x] > mx: + mx = count[x] + mn = i - left[x] + 1 + index = left[x] + elif count[x] == mx and i - left[x] + 1 < mn: + mn = i - left[x] + 1 + index = left[x] + + for i in range(index,index+mn): + print(arr[i], end=' ') + print() + +array_degree([1,3,3,2]) +array_degree([1,2,1,3]) +array_degree([1,3,2,1,2]) +array_degree([1,1,2,3,2]) +array_degree([2,1,2,1,1]) + diff --git a/challenge-189/deadmarshal/raku/ch-1.raku b/challenge-189/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..638ecc5b55 --- /dev/null +++ b/challenge-189/deadmarshal/raku/ch-1.raku @@ -0,0 +1,16 @@ +sub greater-character(@arr, $target) +{ + @arr = @arr.sort({.Str}); + for 0..^@arr.elems -> $i + { + return @arr[$i] if @arr[$i] gt $target; + } + return $target; +} + +say greater-character([qw/e m u g/], 'b'); +say greater-character([qw/d c e f/], 'a'); +say greater-character([qw/j a r/], 'o'); +say greater-character([qw/d c a f/], 'a'); +say greater-character([qw/t g a l/], 'v'); + diff --git a/challenge-189/deadmarshal/raku/ch-2.raku b/challenge-189/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..ddbff502f6 --- /dev/null +++ b/challenge-189/deadmarshal/raku/ch-2.raku @@ -0,0 +1,42 @@ +sub array-degree(@arr) +{ + my (%left,%count); + my ($max,$min,$index) = (0) x 3; + for 0..^@arr.elems -> $i + { + my $x = @arr[$i]; + if %count{$x}:!exists + { + %left{$x} = $i; + %count{$x} = 1; + } + else + { + %count{$x}++; + } + if (%count{$x} > $max) + { + $max = %count{$x}; + $min = $i - %left{$x} + 1; + $index = %left{$x}; + } + elsif ((%count{$x} == $max) && + ($i - %left{$x} + 1 < $min)) + { + $min = $i - %left{$x} + 1; + $index = %left{$x}; + } + } + for $index..^$index+$min -> $i + { + print @arr[$i], ' '; + } + print "\n"; +} + +array-degree([1,3,3,2]); +array-degree([1,2,1,3]); +array-degree([1,3,2,1,2]); +array-degree([1,1,2,3,2]); +array-degree([2,1,2,1,1]); + |
