diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-07 10:58:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-07 10:58:53 +0100 |
| commit | 22b39e684a80293624ef1e66f56fa087a07325b3 (patch) | |
| tree | cbe684b023e245674884ef190fe95a6c2ada82d4 | |
| parent | ebe06169032e1093c84e0feb68a41622a2f84efc (diff) | |
| parent | c90ab7553881540e48f85c7d57f51d71dafb12cb (diff) | |
| download | perlweeklychallenge-club-22b39e684a80293624ef1e66f56fa087a07325b3.tar.gz perlweeklychallenge-club-22b39e684a80293624ef1e66f56fa087a07325b3.tar.bz2 perlweeklychallenge-club-22b39e684a80293624ef1e66f56fa087a07325b3.zip | |
Merge pull request #12807 from deadmarshal/TWC342
TWC342
| -rw-r--r-- | challenge-342/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/java/Ch1.java | 32 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/java/Ch2.java | 23 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/modula-3/Ch1/src/Ch1.m3 | 42 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/modula-3/Ch1/src/m3makefile | 4 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/modula-3/Ch2/src/Ch2.m3 | 28 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/modula-3/Ch2/src/m3makefile | 4 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-342/deadmarshal/perl/ch-2.pl | 26 |
9 files changed, 190 insertions, 0 deletions
diff --git a/challenge-342/deadmarshal/blog.txt b/challenge-342/deadmarshal/blog.txt new file mode 100644 index 0000000000..759501b4a0 --- /dev/null +++ b/challenge-342/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/10/twc342.html diff --git a/challenge-342/deadmarshal/java/Ch1.java b/challenge-342/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..9ec571cbdf --- /dev/null +++ b/challenge-342/deadmarshal/java/Ch1.java @@ -0,0 +1,32 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println(balance_string("a0b1c2")); + System.out.println(balance_string("abc12")); + System.out.println(balance_string("0a2b1c3")); + System.out.println(balance_string("1a23")); + System.out.println(balance_string("ab123")); + } + + private static String balance_string(String str) { + StringBuilder a = new StringBuilder(); + StringBuilder b = new StringBuilder(); + for(char c : str.toCharArray()) { + if(Character.isDigit(c)) a.append(c); + else b.append(c); + } + int m = a.length(), n = b.length(); + if(Math.abs(m-n) > 1) return ""; + StringBuilder res = new StringBuilder(); + for(int i = 0; i < Math.min(m,n); ++i) { + if(m > n) { + res.append(a.charAt(i)).append(b.charAt(i)); + } else { + res.append(b.charAt(i)).append(a.charAt(i)); + } + } + if(m > n) res.append(a.charAt(m-1)); + if(m < n) res.append(b.charAt(n-1)); + return res.toString(); + } +} + diff --git a/challenge-342/deadmarshal/java/Ch2.java b/challenge-342/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..e91b5f837e --- /dev/null +++ b/challenge-342/deadmarshal/java/Ch2.java @@ -0,0 +1,23 @@ +public class Ch2 { + public static void main(String[] args) { + System.out.println(max_score("0011")); + System.out.println(max_score("0000")); + System.out.println(max_score("1111")); + System.out.println(max_score("0101")); + System.out.println(max_score("011101")); + } + + private static int max_score(String str) { + int res = 0; + for(int i = 1; i < str.length(); ++i) { + int temp = 0; + for(int j = 0; j < i; ++j) + if(str.charAt(j) == '0') ++temp; + for(int j = i; j < str.length(); ++j) + if(str.charAt(j) == '1') ++temp; + res = Math.max(res,temp); + } + return res; + } +} + diff --git a/challenge-342/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-342/deadmarshal/modula-3/Ch1/src/Ch1.m3 new file mode 100644 index 0000000000..6e13122280 --- /dev/null +++ b/challenge-342/deadmarshal/modula-3/Ch1/src/Ch1.m3 @@ -0,0 +1,42 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,ASCII; +FROM Text IMPORT FromChar,GetChar,Length; + +PROCEDURE BalanceString(READONLY Str:TEXT):TEXT = + VAR + A,B,Res:TEXT := ""; + M,N:CARDINAL; + BEGIN + FOR I := 0 TO Length(Str)-1 DO + WITH C = GetChar(Str,I) DO + IF C IN ASCII.Digits THEN + A := A & FromChar(C) + ELSE + B := B & FromChar(C) + END + END + END; + M := Length(A); + N := Length(B); + IF ABS(M-N) > 1 THEN RETURN "" END; + FOR I := 0 TO MIN(M,N)-1 DO + IF M > N THEN + Res := Res & FromChar(GetChar(A,I)) & FromChar(GetChar(B,I)) + ELSE + Res := Res & FromChar(GetChar(B,I)) & FromChar(GetChar(A,I)) + END + END; + IF M > N THEN Res := Res & FromChar(GetChar(A,M-1)) END; + IF M < N THEN Res := Res & FromChar(GetChar(B,N-1)) END; + RETURN Res + END BalanceString; + +BEGIN + SIO.PutText(BalanceString("a0b1c2")); SIO.Nl(); + SIO.PutText(BalanceString("abc12")); SIO.Nl(); + SIO.PutText(BalanceString("0a2b1c3")); SIO.Nl(); + SIO.PutText(BalanceString("1a23")); SIO.Nl(); + SIO.PutText(BalanceString("ab123")); SIO.Nl() +END Ch1. + diff --git a/challenge-342/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-342/deadmarshal/modula-3/Ch1/src/m3makefile new file mode 100644 index 0000000000..643b33d043 --- /dev/null +++ b/challenge-342/deadmarshal/modula-3/Ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch1") +program("Ch1") diff --git a/challenge-342/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-342/deadmarshal/modula-3/Ch2/src/Ch2.m3 new file mode 100644 index 0000000000..a5141d140a --- /dev/null +++ b/challenge-342/deadmarshal/modula-3/Ch2/src/Ch2.m3 @@ -0,0 +1,28 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text,Word; + +PROCEDURE MaxScore(READONLY Str:TEXT):CARDINAL = + VAR L,R,Res:CARDINAL; + BEGIN + FOR I := 0 TO Text.Length(Str)-1 DO + IF Text.GetChar(Str,I) = '1' THEN INC(R) END + END; + FOR I := 0 TO Text.Length(Str)-2 DO + WITH C = ORD(Text.GetChar(Str,I)) - ORD('0') DO + INC(L,Word.Xor(C,1)); + DEC(R,C); + Res := MAX(Res,L+R) + END + END; + RETURN Res + END MaxScore; + +BEGIN + SIO.PutInt(MaxScore("0011")); SIO.Nl(); + SIO.PutInt(MaxScore("0000")); SIO.Nl(); + SIO.PutInt(MaxScore("1111")); SIO.Nl(); + SIO.PutInt(MaxScore("0101")); SIO.Nl(); + SIO.PutInt(MaxScore("011101")); SIO.Nl() +END Ch2. + diff --git a/challenge-342/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-342/deadmarshal/modula-3/Ch2/src/m3makefile new file mode 100644 index 0000000000..78802242fe --- /dev/null +++ b/challenge-342/deadmarshal/modula-3/Ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("Ch2") diff --git a/challenge-342/deadmarshal/perl/ch-1.pl b/challenge-342/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..aaf045873a --- /dev/null +++ b/challenge-342/deadmarshal/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(min); + +sub balance_string{ + my @chars = split '',$_[0]; + my @ds = grep{/\d/} @chars; + my @ls = grep{/\D/} @chars; + my ($m,$n) = (scalar @ds,scalar @ls); + return '' if abs($m-$n) > 1; + my $res; + foreach my $i(0..min($m,$n)-1){ + if($m > $n) { + $res .= $ds[$i] . $ls[$i] + } else { + $res .= $ds[$i] . $ls[$i] + } + } + $res .= $ds[$m-1] if($m > $n); + $res .= $ls[$n-1] if($m < $n); + $res +} + +printf "%s\n",balance_string('a0b1c2'); +printf "%s\n",balance_string('abc12'); +printf "%s\n",balance_string('0a2b1c3'); +printf "%s\n",balance_string('1a23'); +printf "%s\n",balance_string('ab123') + diff --git a/challenge-342/deadmarshal/perl/ch-2.pl b/challenge-342/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..0d25f1e3a1 --- /dev/null +++ b/challenge-342/deadmarshal/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(max); + +sub max_score{ + my $res = 0; + foreach my $i(1..(length $_[0])-1) { + my $temp = 0; + foreach my $j(0..$i-1) { + $temp++ if (substr $_[0],$j,1) eq '0' + } + foreach my $j($i..(length $_[0])-1) { + $temp++ if (substr $_[0],$j,1) eq '1' + } + $res = max $res,$temp; + } + $res +} + +printf "%d\n",max_score('0011'); +printf "%d\n",max_score('0000'); +printf "%d\n",max_score('1111'); +printf "%d\n",max_score('0101'); +printf "%d\n",max_score('011101') + |
