diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-11-07 00:25:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-07 00:25:29 +0000 |
| commit | c33126467c964286dc8ce4feb44e773eadd8c8a5 (patch) | |
| tree | f05ec07074fdfe9d5541de8e1cb77c6bcbf90491 /challenge-294 | |
| parent | 53bc01550d6d5cf0917d1a535fccc9e8bbb46530 (diff) | |
| parent | 63e7d78e0733d9e535fef3df99b0256157128e5a (diff) | |
| download | perlweeklychallenge-club-c33126467c964286dc8ce4feb44e773eadd8c8a5.tar.gz perlweeklychallenge-club-c33126467c964286dc8ce4feb44e773eadd8c8a5.tar.bz2 perlweeklychallenge-club-c33126467c964286dc8ce4feb44e773eadd8c8a5.zip | |
Merge pull request #11132 from deadmarshal/TWC294
TWC294
Diffstat (limited to 'challenge-294')
21 files changed, 405 insertions, 0 deletions
diff --git a/challenge-294/deadmarshal/blog.txt b/challenge-294/deadmarshal/blog.txt new file mode 100644 index 0000000000..5425d1299e --- /dev/null +++ b/challenge-294/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2024/11/twc294.html diff --git a/challenge-294/deadmarshal/cpp/ch-1.cpp b/challenge-294/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..143d708679 --- /dev/null +++ b/challenge-294/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,33 @@ +#include<iostream> +#include<vector> +#include<unordered_set> + +template<typename T> +int consecutive_sequence(const std::vector<T> &vec) +{ + std::unordered_set<T> s{}; + int res = 0; + for(const auto &e : vec) s.insert(e); + for(const auto &e : vec) + { + if(s.find(e-1) == s.end()) + { + int j = e; + while(s.find(j) != s.end()) j++; + res = std::max(res,j-e); + } + } + return res == 1 ? -1 : res; +} + +int main() +{ + std::vector<int> vec1{10,4,20,1,3,2}, + vec2{0,6,1,8,5,2,4,3,0,7}, + vec3{10,30,20}; + std::cout << consecutive_sequence<int>(vec1) << '\n' + << consecutive_sequence<int>(vec2) << '\n' + << consecutive_sequence<int>(vec3) << '\n'; + return 0; +} + diff --git a/challenge-294/deadmarshal/cpp/ch-2.cpp b/challenge-294/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..49ac902f9f --- /dev/null +++ b/challenge-294/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,21 @@ +#include<iostream> +#include<vector> +#include<algorithm> + +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{1,2,3},vec2{2,1,3},vec3{3,1,2}; + std::next_permutation(vec1.begin(),vec1.end()); + std::next_permutation(vec2.begin(),vec2.end()); + std::next_permutation(vec3.begin(),vec3.end()); + std::cout << vec1 << vec2 << vec3; +} + diff --git a/challenge-294/deadmarshal/d/ch1.d b/challenge-294/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..2af686aa70 --- /dev/null +++ b/challenge-294/deadmarshal/d/ch1.d @@ -0,0 +1,25 @@ +import std.stdio:writeln; +import std.algorithm:max; + +int consecutive_sequence(int[] arr) +{ + int res = 0; + int[int] s; + foreach(e;arr) s[e] = 1; + foreach(e;arr){ + if(e-1 !in s){ + int j = e; + while(j in s) j++; + res = max(res,j - e); + } + } + return res == 1 ? -1 : res; +} + +void main() +{ + writeln(consecutive_sequence([10,4,20,1,3,2])); + writeln(consecutive_sequence([0,6,1,8,5,2,4,3,0,7])); + writeln(consecutive_sequence([10,30,20])); +} + diff --git a/challenge-294/deadmarshal/d/ch2.d b/challenge-294/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..d7a0da761e --- /dev/null +++ b/challenge-294/deadmarshal/d/ch2.d @@ -0,0 +1,16 @@ +import std.stdio:writeln; +import std.algorithm:nextPermutation; + +void main() +{ + int[] arr1 = [1,2,3]; + int[] arr2 = [2,1,3]; + int[] arr3 = [3,1,2]; + nextPermutation(arr1); + nextPermutation(arr2); + nextPermutation(arr3); + writeln(arr1); + writeln(arr2); + writeln(arr3); +} + diff --git a/challenge-294/deadmarshal/java/Ch1.java b/challenge-294/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..459ca23550 --- /dev/null +++ b/challenge-294/deadmarshal/java/Ch1.java @@ -0,0 +1,26 @@ +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(consecutive_sequence(new int[]{10, 4, 20, 1, 3, 2})); + System.out.println( + consecutive_sequence(new int[]{0, 6, 1, 8, 5, 2, 4, 3, 0, 7})); + System.out.println(consecutive_sequence(new int[]{10, 30, 20})); + } + + private static int consecutive_sequence(int[] arr) { + Set<Integer> s = Arrays.stream(arr).boxed().collect(Collectors.toSet()); + int res = 0, j; + for (int e : arr) { + if (!s.contains(e - 1)) { + j = e; + while (s.contains(j)) j++; + res = Math.max(res, j - e); + } + } + return res == 1 ? -1 : res; + } +} + diff --git a/challenge-294/deadmarshal/java/Ch2.java b/challenge-294/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..b9d0e4f41a --- /dev/null +++ b/challenge-294/deadmarshal/java/Ch2.java @@ -0,0 +1,36 @@ +import java.util.Arrays; +import java.util.Collections; + +public class Ch2 { + public static void main(String[] args) { + int[] arr1 = new int[]{1, 2, 3}; + int[] arr2 = new int[]{2, 1, 3}; + int[] arr3 = new int[]{3, 1, 2}; + next_permutation(arr1); + next_permutation(arr2); + next_permutation(arr3); + System.out.println(Arrays.toString(arr1)); + System.out.println(Arrays.toString(arr2)); + System.out.println(Arrays.toString(arr3)); + } + + private static void next_permutation(int[] arr) { + int p = -1; + for (int i = arr.length - 2; i >= 0; --i) + if (arr[i] < arr[i + 1]) { + p = i; + break; + } + if (p == -1) Collections.reverse(Arrays.asList(arr)); + for (int i = arr.length - 1; i >= p; --i) + if (arr[i] > arr[p]) { + int t = arr[i]; + arr[i] = arr[p]; + arr[p] = t; + break; + } + Collections.reverse(Arrays.asList(Arrays.copyOfRange(arr, p + 1, + arr.length - 1))); + } +} + diff --git a/challenge-294/deadmarshal/lua/ch-1.lua b/challenge-294/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..d67f70ade4 --- /dev/null +++ b/challenge-294/deadmarshal/lua/ch-1.lua @@ -0,0 +1,20 @@ +#!/usr/bin/env lua + +local function consecutive_sequence(t) + assert(type(t) == 'table','t must be a table!') + local s,res = {},0 + for i=1,#t do s[t[i]] = 1 end + for i=1,#t do + if not s[t[i]-1] then + local j = t[i] + while s[j] do j = j + 1 end + res = math.max(res,j - t[i]) + end + end + return res == 1 and -1 or res +end + +print(consecutive_sequence{10,4,20,1,3,2}) +print(consecutive_sequence{0,6,1,8,5,2,4,3,0,7}) +print(consecutive_sequence{10,30,20}) + diff --git a/challenge-294/deadmarshal/lua/ch-2.lua b/challenge-294/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..42eb9894b9 --- /dev/null +++ b/challenge-294/deadmarshal/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/usr/bin/env lua + +local function reverse(t) + assert(type(t) == 'table','t must be a table!') + for i=1,#t//2 do + t[i],t[#t-i+1] = t[#t-i+1],t[i] + end + return t +end + +local function next_permutation(t) + assert(type(t) == 'table','t must be a table!') + local p = -1 + for i=#t-1,1,-1 do + if t[i] < t[i+1] then p = i break end + end + if p == -1 then return reverse(t) end + for i=#t,p+1,-1 do + if t[i] > t[p] then + t[i],t[p] = t[p],t[i] + break + end + end + return t +end + +print(table.unpack(next_permutation{1,2,3})) +print(table.unpack(next_permutation{2,1,3})) +print(table.unpack(next_permutation{3,1,2})) + diff --git a/challenge-294/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-294/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..8797d88b11 --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,30 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,IntSetDef; + +VAR + A1 := ARRAY[0..5] OF INTEGER{10,4,20,1,3,2}; + A2 := ARRAY[0..9] OF INTEGER{0,6,1,8,5,2,4,3,0,7}; + A3 := ARRAY[0..2] OF INTEGER{10,30,20}; + +PROCEDURE ConsecutiveSequence(VAR A:ARRAY OF INTEGER):INTEGER = + VAR + Res,J:INTEGER; + S:IntSetDef.T := NEW(IntSetDef.T).init(NUMBER(A)).fromArray(A); + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + IF NOT S.member(A[I]-1) THEN + J := A[I]; + WHILE S.member(J) DO INC(J) END; + Res := MAX(Res,J - A[I]) + END; + END; + IF Res = 1 THEN RETURN -1 ELSE RETURN Res END + END ConsecutiveSequence; + +BEGIN + SIO.PutInt(ConsecutiveSequence(A1)); SIO.Nl(); + SIO.PutInt(ConsecutiveSequence(A2)); SIO.Nl(); + SIO.PutInt(ConsecutiveSequence(A3)); SIO.Nl() +END Ch1. + diff --git a/challenge-294/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-294/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..3b84d84e70 --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,6 @@ +import("libm3") +import("libsio") +import("set") +implementation("Ch1") +program("Ch1") + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-294/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..600518d43b --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,52 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,IntSwap; + +VAR + A1 := ARRAY[0..2] OF INTEGER{1,2,3}; + A2 := ARRAY[0..2] OF INTEGER{2,1,3}; + A3 := ARRAY[0..2] OF INTEGER{3,1,2}; + +PROCEDURE ReverseArray(VAR A:ARRAY OF INTEGER) = + VAR + Start := FIRST(A); + End := LAST(A); + BEGIN + WHILE Start < End DO + IntSwap.Swap(A[Start],A[End]); + INC(Start); + DEC(End) + END; + END ReverseArray; + +PROCEDURE NextPermutation(VAR A:ARRAY OF INTEGER) = + VAR P:INTEGER := -1; + BEGIN + FOR I := LAST(A)-1 TO FIRST(A) BY -1 DO + IF A[I] < A[I+1] THEN P := I; EXIT END + END; + IF P = -1 THEN ReverseArray(A) END; + FOR I := LAST(A) TO P BY -1 DO + IF A[I] > A[P] THEN IntSwap.Swap(A[I],A[P]); EXIT END + END; + ReverseArray(SUBARRAY(A,P+1,LAST(A)-1)) + END NextPermutation; + +PROCEDURE PrintArray(READONLY A:ARRAY OF INTEGER) = + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + SIO.PutInt(A[I]); + SIO.PutChar(' ') + END; + SIO.Nl() + END PrintArray; + +BEGIN + NextPermutation(A1); + NextPermutation(A2); + NextPermutation(A3); + PrintArray(A1); + PrintArray(A2); + PrintArray(A3) +END Ch2. + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/Swap.ig b/challenge-294/deadmarshal/modula-3/ch2/src/Swap.ig new file mode 100644 index 0000000000..664a9d586e --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/Swap.ig @@ -0,0 +1,6 @@ +GENERIC INTERFACE Swap(Elem); + +PROCEDURE Swap(VAR A,B:Elem.T); + +END Swap. + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/Swap.mg b/challenge-294/deadmarshal/modula-3/ch2/src/Swap.mg new file mode 100644 index 0000000000..8515ce3999 --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/Swap.mg @@ -0,0 +1,13 @@ +GENERIC MODULE Swap(Elem); + +PROCEDURE Swap(VAR A,B:Elem.T) = + VAR Temp:Elem.T := A; + BEGIN + A := B; + B := Temp + END Swap; + +BEGIN + +END Swap. + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-294/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..ea00487cb5 --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,7 @@ +import("libm3") +import("libsio") +generic_module("Swap") +module("specializations/IntSwap") +implementation("Ch2") +program("ch2") + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.i3 b/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.i3 new file mode 100644 index 0000000000..2a166cce4e --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.i3 @@ -0,0 +1,2 @@ +INTERFACE IntSwap = Swap(Integer) END IntSwap. + diff --git a/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.m3 b/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.m3 new file mode 100644 index 0000000000..ee25ebc7f1 --- /dev/null +++ b/challenge-294/deadmarshal/modula-3/ch2/src/specializations/IntSwap.m3 @@ -0,0 +1,2 @@ +MODULE IntSwap = Swap(Integer) END IntSwap. + diff --git a/challenge-294/deadmarshal/perl/ch-1.pl b/challenge-294/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..f98238bdda --- /dev/null +++ b/challenge-294/deadmarshal/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(max); +use Set::Scalar; + +sub consecutive_sequence{ + my ($arr) = @_; + my $s = Set::Scalar->new(@$arr); + my $res = 0; + foreach my $i(0..$#$arr){ + unless($s->has($arr->[$i]-1)){ + my $j = $arr->[$i]; + $j++ while($s->has($j)); + $res = max($res,$j - $arr->[$i]) + } + } + $res == 1 ? -1 : $res +} + +printf "%d\n",consecutive_sequence([10,4,20,1,3,2]); +printf "%d\n",consecutive_sequence([0,6,1,8,5,2,4,3,0,7]); +printf "%d\n",consecutive_sequence([10,30,20]); + diff --git a/challenge-294/deadmarshal/perl/ch-2.pl b/challenge-294/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..f7678ffcb8 --- /dev/null +++ b/challenge-294/deadmarshal/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub next_permutation{ + my ($arr) = @_; + my $p = -1; + for(my $i = $#$arr-1; $i >= 0; --$i){ + do{$p = $i; last}if($arr->[$i] < $arr->[$i+1]) + } + return reverse @$arr if $p == -1; + for(my $i = $#$arr; $i >= $p+1; --$i){ + if($arr->[$i] > $arr->[$p]){ + ($arr->[$i],$arr->[$p]) = ($arr->[$p],$arr->[$i]); + last + } + } + @$arr +} + +printf "(%s)\n",join ', ',next_permutation([1,2,3]); +printf "(%s)\n",join ', ',next_permutation([2,1,3]); +printf "(%s)\n",join ', ',next_permutation([3,1,2]); + diff --git a/challenge-294/deadmarshal/raku/ch-1.raku b/challenge-294/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..c1b8b65f03 --- /dev/null +++ b/challenge-294/deadmarshal/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +sub consecutive-sequence(@arr) +{ + my $s = @arr.Set; + my $res = 0; + for 0..@arr.end -> $i { + unless @arr[$i]-1 ∈ $s { + my $j = @arr[$i]; + $j++ while $j ∈ $s; + $res = max($res,$j - @arr[$i]) + } + } + $res == 1 ?? -1 !! $res +} + +say consecutive-sequence([10,4,20,1,3,2]); +say consecutive-sequence([0,6,1,8,5,2,4,3,0,7]); +say consecutive-sequence([10,30,20]); + diff --git a/challenge-294/deadmarshal/raku/ch-2.raku b/challenge-294/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..f3dfd42e12 --- /dev/null +++ b/challenge-294/deadmarshal/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +sub next-permutation(@arr) +{ + @arr.permutations[1] +} + +say next-permutation([1,2,3]); +say next-permutation([2,1,3]); +say next-permutation([3,1,2]); + |
