From a09ecf8eee4013cad8b8bb1edb43b6db0a87d3e1 Mon Sep 17 00:00:00 2001 From: Ali Date: Mon, 27 Oct 2025 13:52:19 +0330 Subject: TWC345 --- challenge-345/deadmarshal/blog.txt | 1 + challenge-345/deadmarshal/go/ch1.go | 23 ++++++++++ challenge-345/deadmarshal/go/ch2.go | 30 +++++++++++++ challenge-345/deadmarshal/java/Ch1.java | 21 ++++++++++ challenge-345/deadmarshal/java/Ch2.java | 28 +++++++++++++ challenge-345/deadmarshal/modula-3/Ch1/src/Ch1.m3 | 43 +++++++++++++++++++ .../deadmarshal/modula-3/Ch1/src/m3makefile | 4 ++ challenge-345/deadmarshal/modula-3/Ch2/src/Ch2.m3 | 49 ++++++++++++++++++++++ .../deadmarshal/modula-3/Ch2/src/m3makefile | 4 ++ challenge-345/deadmarshal/perl/ch-1.pl | 22 ++++++++++ challenge-345/deadmarshal/perl/ch-2.pl | 28 +++++++++++++ 11 files changed, 253 insertions(+) create mode 100644 challenge-345/deadmarshal/blog.txt create mode 100644 challenge-345/deadmarshal/go/ch1.go create mode 100644 challenge-345/deadmarshal/go/ch2.go create mode 100644 challenge-345/deadmarshal/java/Ch1.java create mode 100644 challenge-345/deadmarshal/java/Ch2.java create mode 100644 challenge-345/deadmarshal/modula-3/Ch1/src/Ch1.m3 create mode 100644 challenge-345/deadmarshal/modula-3/Ch1/src/m3makefile create mode 100644 challenge-345/deadmarshal/modula-3/Ch2/src/Ch2.m3 create mode 100644 challenge-345/deadmarshal/modula-3/Ch2/src/m3makefile create mode 100644 challenge-345/deadmarshal/perl/ch-1.pl create mode 100644 challenge-345/deadmarshal/perl/ch-2.pl diff --git a/challenge-345/deadmarshal/blog.txt b/challenge-345/deadmarshal/blog.txt new file mode 100644 index 0000000000..eeaa7e644f --- /dev/null +++ b/challenge-345/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/10/twc345.html diff --git a/challenge-345/deadmarshal/go/ch1.go b/challenge-345/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..8d055ab731 --- /dev/null +++ b/challenge-345/deadmarshal/go/ch1.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" +) + +func peakPositions(arr []int) []int { + res := []int{} + for i := 1; i < len(arr)-1; i++ { + if arr[i-1] < arr[i] && arr[i+1] < arr[i] { + res = append(res, i) + } + } + return res +} + +func main() { + fmt.Println(peakPositions([]int{1, 3, 2})) + fmt.Println(peakPositions([]int{2, 4, 6, 5, 3})) + fmt.Println(peakPositions([]int{1, 2, 3, 2, 4, 1})) + fmt.Println(peakPositions([]int{5, 3, 1})) + fmt.Println(peakPositions([]int{1, 5, 1, 5, 1, 5, 1})) +} diff --git a/challenge-345/deadmarshal/go/ch2.go b/challenge-345/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..dae2c9a781 --- /dev/null +++ b/challenge-345/deadmarshal/go/ch2.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" +) + +func lastVisitor(arr []int) []int { + res, seen := []int{}, []int{} + k := 0 + for _, e := range arr { + if e != -1 { + seen = append(seen, e) + k = len(seen) + } else if k == 0 { + res = append(res, -1) + } else { + k-- + res = append(res, seen[k]) + } + } + return res +} + +func main() { + fmt.Println(lastVisitor([]int{5, -1, -1})) + fmt.Println(lastVisitor([]int{3, 7, -1, -1, -1})) + fmt.Println(lastVisitor([]int{2, -1, 4, -1, -1})) + fmt.Println(lastVisitor([]int{10, 20, -1, 30, -1, -1})) + fmt.Println(lastVisitor([]int{-1, -1, 5, -1})) +} diff --git a/challenge-345/deadmarshal/java/Ch1.java b/challenge-345/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..d3012c5f90 --- /dev/null +++ b/challenge-345/deadmarshal/java/Ch1.java @@ -0,0 +1,21 @@ +import java.util.List; +import java.util.ArrayList; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(peak_positions(new int[]{1,3,2})); + System.out.println(peak_positions(new int[]{2,4,6,5,3})); + System.out.println(peak_positions(new int[]{1,2,3,2,4,1})); + System.out.println(peak_positions(new int[]{5,3,1})); + System.out.println(peak_positions(new int[]{1,5,1,5,1,5,1})); + } + + private static List peak_positions(int[] arr) { + List res = new ArrayList<>(); + for(int i = 1; i < arr.length-1; ++i) { + if(arr[i-1] < arr[i] && arr[i+1] < arr[i]) res.add(i); + } + return res; + } +} + diff --git a/challenge-345/deadmarshal/java/Ch2.java b/challenge-345/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..25c25ff98a --- /dev/null +++ b/challenge-345/deadmarshal/java/Ch2.java @@ -0,0 +1,28 @@ +import java.util.List; +import java.util.LinkedList; +import java.util.ArrayList; + +public class Ch2 { + public static void main(String[] args) { + System.out.println(last_visitor(new int[]{5,-1,-1})); + System.out.println(last_visitor(new int[]{3,7,-1,-1,-1})); + System.out.println(last_visitor(new int[]{2,-1,4,-1,-1})); + System.out.println(last_visitor(new int[]{10,20,-1,30,-1,-1})); + System.out.println(last_visitor(new int[]{-1,-1,5,-1})); + } + + private static List last_visitor(int[] arr) { + List res = new LinkedList(); + List seen = new ArrayList(); + int k = 0; + for(int i =0; i < arr.length; ++i) { + if(arr[i] != -1) { + seen.add(arr[i]); + k = seen.size(); + } + else if(k == 0) res.add(-1); + else res.add(seen.get(--k)); + } + return res; + } +} diff --git a/challenge-345/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-345/deadmarshal/modula-3/Ch1/src/Ch1.m3 new file mode 100644 index 0000000000..473ff745be --- /dev/null +++ b/challenge-345/deadmarshal/modula-3/Ch1/src/Ch1.m3 @@ -0,0 +1,43 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,IntSeq; + +VAR + A1 := ARRAY[0..2] OF INTEGER{1,3,2}; + A2 := ARRAY[0..4] OF INTEGER{2,4,6,5,3}; + A3 := ARRAY[0..5] OF INTEGER{1,2,3,2,4,1}; + A4 := ARRAY[0..2] OF INTEGER{5,3,1}; + A5 := ARRAY[0..6] OF INTEGER{1,5,1,5,1,5,1}; + +PROCEDURE PeakPositions(VAR A:ARRAY OF INTEGER):IntSeq.T = + VAR + S := NEW(IntSeq.T).init(NUMBER(A)); + I:INTEGER := 1; + BEGIN + IF NUMBER(A) < 3 THEN RETURN NIL END; + WHILE I < NUMBER(A)-1 DO + IF A[I-1] < A[I] AND A[I+1] < A[I] THEN S.addhi(I) END; + INC(I); + END; + IF S.size() = 0 THEN S.addhi(0) END; + RETURN S + END PeakPositions; + +PROCEDURE PrintSeq(READONLY S:IntSeq.T) = + BEGIN + IF S = NIL OR S.size() = 0 THEN SIO.PutText("[]\n"); RETURN END; + FOR I := 0 TO S.size()-1 DO + SIO.PutInt(S.get(I)); + SIO.PutChar(' ') + END; + SIO.Nl() + END PrintSeq; + +BEGIN + PrintSeq(PeakPositions(A1)); + PrintSeq(PeakPositions(A2)); + PrintSeq(PeakPositions(A3)); + PrintSeq(PeakPositions(A4)); + PrintSeq(PeakPositions(A5)) +END Ch1. + diff --git a/challenge-345/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-345/deadmarshal/modula-3/Ch1/src/m3makefile new file mode 100644 index 0000000000..643b33d043 --- /dev/null +++ b/challenge-345/deadmarshal/modula-3/Ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch1") +program("Ch1") diff --git a/challenge-345/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-345/deadmarshal/modula-3/Ch2/src/Ch2.m3 new file mode 100644 index 0000000000..2cd2cb5560 --- /dev/null +++ b/challenge-345/deadmarshal/modula-3/Ch2/src/Ch2.m3 @@ -0,0 +1,49 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,IntSeq; + +VAR + A1 := ARRAY[0..2] OF INTEGER{5,-1,-1}; + A2 := ARRAY[0..4] OF INTEGER{3,7,-1,-1,-1}; + A3 := ARRAY[0..4] OF INTEGER{2,-1,4,-1,-1}; + A4 := ARRAY[0..5] OF INTEGER{10,20,-1,30,-1,-1}; + A5 := ARRAY[0..3] OF INTEGER{-1,-1,5,-1}; + +PROCEDURE LastVisitor(VAR A:ARRAY OF INTEGER):IntSeq.T = + VAR + Res := NEW(IntSeq.T).init(NUMBER(A)); + Seen := NEW(IntSeq.T).init(NUMBER(A)); + K := 0; + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + IF A[I] # -1 THEN + Seen.addhi(A[I]); + K := Seen.size() + ELSIF K = 0 THEN + Res.addhi(-1) + ELSE + DEC(K); + Res.addhi(Seen.get(K)) + END + END; + RETURN Res + END LastVisitor; + +PROCEDURE PrintSeq(READONLY S:IntSeq.T) = + BEGIN + IF S = NIL OR S.size() = 0 THEN SIO.PutText("[]\n"); RETURN END; + FOR I := 0 TO S.size()-1 DO + SIO.PutInt(S.get(I)); + SIO.PutChar(' ') + END; + SIO.Nl() + END PrintSeq; + +BEGIN + PrintSeq(LastVisitor(A1)); + PrintSeq(LastVisitor(A2)); + PrintSeq(LastVisitor(A3)); + PrintSeq(LastVisitor(A4)); + PrintSeq(LastVisitor(A5)) +END Ch2. + diff --git a/challenge-345/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-345/deadmarshal/modula-3/Ch2/src/m3makefile new file mode 100644 index 0000000000..78802242fe --- /dev/null +++ b/challenge-345/deadmarshal/modula-3/Ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("Ch2") diff --git a/challenge-345/deadmarshal/perl/ch-1.pl b/challenge-345/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..a6c90d204a --- /dev/null +++ b/challenge-345/deadmarshal/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; + +sub peak_positions{ + my ($arr) = @_; + my @res; + foreach my $i(1..$#$arr) { + if($arr->[$i-1] < $arr->[$i] && $arr->[$i+1] < $arr->[$i]) { + push @res,$i + } + } + @res +} + +show peak_positions([1,3,2]); +show peak_positions([2,4,6,5,3]); +show peak_positions([1,2,3,2,4,1]); +show peak_positions([5,3,1]); +show peak_positions([1,5,1,5,1,5,1]); + diff --git a/challenge-345/deadmarshal/perl/ch-2.pl b/challenge-345/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..18aeb76c47 --- /dev/null +++ b/challenge-345/deadmarshal/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; + +sub last_visitor{ + my ($arr) = @_; + my (@res,@seen); + my $k = 0; + foreach my $i(0..$#$arr) { + if($arr->[$i] != -1) { + push @seen,$arr->[$i]; + $k = @seen + } elsif ($k == 0) { + push @res,-1 + } else { + push @res,$seen[--$k] + } + } + @res +} + +show last_visitor([5,-1,-1]); +show last_visitor([3,7,-1,-1,-1]); +show last_visitor([2,-1,4,-1,-1]); +show last_visitor([10,20,-1,30,-1,-1]); +show last_visitor([-1,-1,5,-1]) + -- cgit