aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-10 09:23:35 +0100
committerGitHub <noreply@github.com>2025-06-10 09:23:35 +0100
commit7ad8fdb1ba3d67e7d46ff5ed9527c200ab5dbc0e (patch)
tree42245290f03bb21105d07b4ce5153c1c6b602cf4
parentf9a0da9465d4b6745ede068698d1d59125a8a7ab (diff)
parent8f7edcdf6fd5d1ecc837c050d7ca3298827fb4ac (diff)
downloadperlweeklychallenge-club-7ad8fdb1ba3d67e7d46ff5ed9527c200ab5dbc0e.tar.gz
perlweeklychallenge-club-7ad8fdb1ba3d67e7d46ff5ed9527c200ab5dbc0e.tar.bz2
perlweeklychallenge-club-7ad8fdb1ba3d67e7d46ff5ed9527c200ab5dbc0e.zip
Merge pull request #12159 from deadmarshal/TWC325
TWC325
-rw-r--r--challenge-325/deadmarshal/blog.txt1
-rw-r--r--challenge-325/deadmarshal/erlang/ch1.erl11
-rw-r--r--challenge-325/deadmarshal/erlang/ch2.erl16
-rw-r--r--challenge-325/deadmarshal/go/ch1.go24
-rw-r--r--challenge-325/deadmarshal/go/ch2.go24
-rw-r--r--challenge-325/deadmarshal/go/stack.go44
-rw-r--r--challenge-325/deadmarshal/java/Ch1.java20
-rw-r--r--challenge-325/deadmarshal/java/Ch2.java21
-rw-r--r--challenge-325/deadmarshal/modula-3/Ch1/src/Ch1.m328
-rw-r--r--challenge-325/deadmarshal/modula-3/Ch1/src/m3makefile4
-rw-r--r--challenge-325/deadmarshal/modula-3/Ch2/src/Ch2.m338
-rw-r--r--challenge-325/deadmarshal/modula-3/Ch2/src/m3makefile4
-rw-r--r--challenge-325/deadmarshal/perl/ch-1.pl13
-rw-r--r--challenge-325/deadmarshal/perl/ch-2.pl23
14 files changed, 271 insertions, 0 deletions
diff --git a/challenge-325/deadmarshal/blog.txt b/challenge-325/deadmarshal/blog.txt
new file mode 100644
index 0000000000..1ff486148b
--- /dev/null
+++ b/challenge-325/deadmarshal/blog.txt
@@ -0,0 +1 @@
+https://deadmarshal.blogspot.com/2025/06/twc325.html
diff --git a/challenge-325/deadmarshal/erlang/ch1.erl b/challenge-325/deadmarshal/erlang/ch1.erl
new file mode 100644
index 0000000000..b2465dae07
--- /dev/null
+++ b/challenge-325/deadmarshal/erlang/ch1.erl
@@ -0,0 +1,11 @@
+-module(ch1).
+-export([consecutive_one/1]).
+
+consecutive_one(L) ->
+ {Count,Res} = lists:foldl(fun(1,{Count,Res}) -> {Count+1,Res};
+ (0,{Count,Res}) -> {0,max(Count,Res)}
+ end,
+ {0,0},
+ L),
+ max(Count,Res).
+
diff --git a/challenge-325/deadmarshal/erlang/ch2.erl b/challenge-325/deadmarshal/erlang/ch2.erl
new file mode 100644
index 0000000000..491b8c5b46
--- /dev/null
+++ b/challenge-325/deadmarshal/erlang/ch2.erl
@@ -0,0 +1,16 @@
+-module(ch2).
+-export([final_price/1]).
+
+-spec final_price(L) -> R when
+ L :: [T],
+ R :: [T],
+ T :: integer().
+final_price(Prices) ->
+ T = lists:foldr(fun do_fold/2,{[],[0]},Prices),
+ element(1,T).
+
+do_fold(X,{Ans,[Y|Stack]}) when Y > X ->
+ do_fold(X,{Ans,Stack});
+do_fold(X,{Ans,[Y|Stack]}) ->
+ {[X-Y|Ans],[X,Y|Stack]}.
+
diff --git a/challenge-325/deadmarshal/go/ch1.go b/challenge-325/deadmarshal/go/ch1.go
new file mode 100644
index 0000000000..af4cd5e863
--- /dev/null
+++ b/challenge-325/deadmarshal/go/ch1.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+ "fmt"
+)
+
+func consecutiveOne(arr []int) int {
+ count, res := 0, 0
+ for _, e := range arr {
+ if e == 1 {
+ count++
+ } else {
+ res = max(res, count)
+ count = 0
+ }
+ }
+ return max(count, res)
+}
+
+func main() {
+ fmt.Println(consecutiveOne([]int{0, 1, 1, 0, 1, 1, 1}))
+ fmt.Println(consecutiveOne([]int{0, 0, 0, 0}))
+ fmt.Println(consecutiveOne([]int{1, 0, 1, 0, 1, 1}))
+}
diff --git a/challenge-325/deadmarshal/go/ch2.go b/challenge-325/deadmarshal/go/ch2.go
new file mode 100644
index 0000000000..bb4a4481f7
--- /dev/null
+++ b/challenge-325/deadmarshal/go/ch2.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+ "fmt"
+)
+
+func finalPrice(prices []int) []int {
+ var stk Stack[int]
+ for i, _ := range prices {
+ p, _ := stk.Peek()
+ for !stk.IsEmpty() && prices[p] >= prices[i] {
+ popped, _ := stk.Pop()
+ prices[popped] -= prices[i]
+ }
+ stk.Push(i)
+ }
+ return prices
+}
+
+func main() {
+ fmt.Println(finalPrice([]int{8, 4, 6, 2, 3}))
+ fmt.Println(finalPrice([]int{1, 2, 3, 4, 5}))
+ fmt.Println(finalPrice([]int{7, 1, 1, 5}))
+}
diff --git a/challenge-325/deadmarshal/go/stack.go b/challenge-325/deadmarshal/go/stack.go
new file mode 100644
index 0000000000..8890e88d1b
--- /dev/null
+++ b/challenge-325/deadmarshal/go/stack.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ "errors"
+)
+
+var ErrStackEmpty = errors.New("ErrStackEmpty")
+
+type Node[T comparable] struct {
+ data T
+ next *Node[T]
+}
+
+// Stack using singly linked list:
+type Stack[T comparable] struct {
+ head *Node[T]
+}
+
+func (stack Stack[T]) IsEmpty() bool {
+ return stack.head == nil
+}
+
+func (stack *Stack[T]) Push(data T) {
+ stack.head = &Node[T]{data: data, next: stack.head}
+}
+
+func (stack *Stack[T]) Pop() (T, error) {
+ var res T
+ if stack.IsEmpty() {
+ return res, ErrStackEmpty
+ }
+ res = stack.head.data // save the data
+ stack.head = stack.head.next // move the head one node forward
+ return res, nil
+}
+
+func (stack Stack[T]) Peek() (T, error) {
+ var res T
+ if stack.IsEmpty() {
+ return res, ErrStackEmpty
+ }
+ res = stack.head.data
+ return res, nil
+}
diff --git a/challenge-325/deadmarshal/java/Ch1.java b/challenge-325/deadmarshal/java/Ch1.java
new file mode 100644
index 0000000000..f1f27e332c
--- /dev/null
+++ b/challenge-325/deadmarshal/java/Ch1.java
@@ -0,0 +1,20 @@
+public class Ch1 {
+ public static void main(String[] args) {
+ System.out.println(consecutive_one(new int[]{0, 1, 1, 0, 1, 1, 1}));
+ System.out.println(consecutive_one(new int[]{0, 0, 0, 0}));
+ System.out.println(consecutive_one(new int[]{1, 0, 1, 0, 1, 1}));
+ }
+
+ private static int consecutive_one(int[] arr) {
+ int count = 0, res = 0;
+ for (int e : arr) {
+ if (e == 1) count++;
+ else {
+ res = Math.max(res, count);
+ count = 0;
+ }
+ }
+ return Math.max(count, res);
+ }
+}
+
diff --git a/challenge-325/deadmarshal/java/Ch2.java b/challenge-325/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..353b6f6506
--- /dev/null
+++ b/challenge-325/deadmarshal/java/Ch2.java
@@ -0,0 +1,21 @@
+import java.util.Arrays;
+import java.util.Stack;
+
+public class Ch2 {
+ public static void main(String[] args) {
+ System.out.println(Arrays.toString(final_price(new int[]{8, 4, 6, 2, 3})));
+ System.out.println(Arrays.toString(final_price(new int[]{1, 2, 3, 4, 5})));
+ System.out.println(Arrays.toString(final_price(new int[]{7, 1, 1, 5})));
+ }
+
+ private static int[] final_price(int[] arr) {
+ Stack<Integer> stack = new Stack<>();
+ for (int i = 0; i < arr.length; ++i) {
+ while (!stack.isEmpty() && arr[stack.peek()] >= arr[i])
+ arr[stack.pop()] -= arr[i];
+ stack.push(i);
+ }
+ return arr;
+ }
+}
+
diff --git a/challenge-325/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-325/deadmarshal/modula-3/Ch1/src/Ch1.m3
new file mode 100644
index 0000000000..922f4ba9fb
--- /dev/null
+++ b/challenge-325/deadmarshal/modula-3/Ch1/src/Ch1.m3
@@ -0,0 +1,28 @@
+MODULE Ch1 EXPORTS Main;
+
+IMPORT SIO;
+
+VAR
+ A1 := ARRAY[0..6] OF INTEGER{0,1,1,0,1,1,1};
+ A2 := ARRAY[0..3] OF INTEGER{0,0,0,0};
+ A3 := ARRAY[0..5] OF INTEGER{1,0,1,0,1,1};
+
+PROCEDURE ConsecutiveOne(VAR A:ARRAY OF INTEGER):CARDINAL =
+ VAR Count,Res:CARDINAL;
+ BEGIN
+ FOR I := FIRST(A) TO LAST(A) DO
+ IF A[I] = 1 THEN INC(Count)
+ ELSE
+ Res := MAX(Res,Count);
+ Count := 0
+ END
+ END;
+ RETURN MAX(Count,Res)
+ END ConsecutiveOne;
+
+BEGIN
+ SIO.PutInt(ConsecutiveOne(A1)); SIO.Nl();
+ SIO.PutInt(ConsecutiveOne(A2)); SIO.Nl();
+ SIO.PutInt(ConsecutiveOne(A3)); SIO.Nl()
+END Ch1.
+
diff --git a/challenge-325/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-325/deadmarshal/modula-3/Ch1/src/m3makefile
new file mode 100644
index 0000000000..643b33d043
--- /dev/null
+++ b/challenge-325/deadmarshal/modula-3/Ch1/src/m3makefile
@@ -0,0 +1,4 @@
+import("libm3")
+import("libsio")
+implementation("Ch1")
+program("Ch1")
diff --git a/challenge-325/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-325/deadmarshal/modula-3/Ch2/src/Ch2.m3
new file mode 100644
index 0000000000..873fe6b6e0
--- /dev/null
+++ b/challenge-325/deadmarshal/modula-3/Ch2/src/Ch2.m3
@@ -0,0 +1,38 @@
+MODULE Ch2 EXPORTS Main;
+
+IMPORT SIO,IntSeq;
+
+VAR
+ A1 := ARRAY[0..4] OF INTEGER{8,4,6,2,3};
+ A2 := ARRAY[0..4] OF INTEGER{1,2,3,4,5};
+ A3 := ARRAY[0..3] OF INTEGER{7,1,1,5};
+
+PROCEDURE FinalPrice(VAR A:ARRAY OF INTEGER) =
+ VAR S := NEW(IntSeq.T).init(NUMBER(A));
+ BEGIN
+ FOR I := FIRST(A) TO LAST(A) DO
+ WHILE S.size() # 0 AND A[S.gethi()] >= A[I] DO
+ DEC(A[S.remhi()],A[I])
+ END;
+ S.addhi(I)
+ END;
+ END FinalPrice;
+
+PROCEDURE PrintArray(VAR 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
+ FinalPrice(A1);
+ FinalPrice(A2);
+ FinalPrice(A3);
+ PrintArray(A1);
+ PrintArray(A2);
+ PrintArray(A3)
+END Ch2.
+
diff --git a/challenge-325/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-325/deadmarshal/modula-3/Ch2/src/m3makefile
new file mode 100644
index 0000000000..78802242fe
--- /dev/null
+++ b/challenge-325/deadmarshal/modula-3/Ch2/src/m3makefile
@@ -0,0 +1,4 @@
+import("libm3")
+import("libsio")
+implementation("Ch2")
+program("Ch2")
diff --git a/challenge-325/deadmarshal/perl/ch-1.pl b/challenge-325/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..80f4a56848
--- /dev/null
+++ b/challenge-325/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util qw(max);
+
+sub consecutive_one{
+ max 0,map length,split /[^1]+/,join '',@{$_[0]}
+}
+
+printf "%d\n",consecutive_one([0,1,1,0,1,1,1]);
+printf "%d\n",consecutive_one([0,0,0,0]);
+printf "%d\n",consecutive_one([1,0,1,0,1,1]);
+
diff --git a/challenge-325/deadmarshal/perl/ch-2.pl b/challenge-325/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..381251aada
--- /dev/null
+++ b/challenge-325/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Data::Show;
+
+sub final_price {
+ my ($p) = @_;
+ my @stack;
+ my @res = @$p;
+ foreach my $i(0..$#$p){
+ while(@stack && ($p->[$stack[-1]] >= $p->[$i])) {
+ my $j = pop @stack;
+ $res[$j] = $p->[$j] - $p->[$i]
+ }
+ push @stack,$i
+ }
+ @res
+}
+
+print show final_price([8,4,6,2,3]);
+print show final_price([1,2,3,4,5]);
+print show final_price([7,1,1,5]);
+