diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-07 11:09:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-07 11:09:24 +0100 |
| commit | 7f84027690a734f2844fbf91e5a9758e530c7ae2 (patch) | |
| tree | efc5377747dced16e137ad424285aea0af0dfcb1 | |
| parent | 6b0142bc81d43f4eb36c6a8ccd5172663de8e501 (diff) | |
| parent | 3264283de3c8790e0921cb790793ae771cc3879c (diff) | |
| download | perlweeklychallenge-club-7f84027690a734f2844fbf91e5a9758e530c7ae2.tar.gz perlweeklychallenge-club-7f84027690a734f2844fbf91e5a9758e530c7ae2.tar.bz2 perlweeklychallenge-club-7f84027690a734f2844fbf91e5a9758e530c7ae2.zip | |
Merge pull request #12809 from Firedrake/rogerbw-challenge-342
RogerBW solutions for challenge no. 342
| -rwxr-xr-x | challenge-342/roger-bell-west/perl/ch-1.pl | 46 | ||||
| -rwxr-xr-x | challenge-342/roger-bell-west/perl/ch-2.pl | 32 | ||||
| -rw-r--r-- | challenge-342/roger-bell-west/postscript/ch-1.ps | 199 | ||||
| -rw-r--r-- | challenge-342/roger-bell-west/postscript/ch-2.ps | 84 | ||||
| -rwxr-xr-x | challenge-342/roger-bell-west/rust/ch-1.rs | 59 | ||||
| -rwxr-xr-x | challenge-342/roger-bell-west/rust/ch-2.rs | 44 | ||||
| -rw-r--r-- | challenge-342/roger-bell-west/tests.json | 48 |
7 files changed, 512 insertions, 0 deletions
diff --git a/challenge-342/roger-bell-west/perl/ch-1.pl b/challenge-342/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..9ad31aae11 --- /dev/null +++ b/challenge-342/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(balancestring('0a1b2c'), '0a1b2c', 'example 1'); +is(balancestring('abc12'), 'a1b2c', 'example 2'); +is(balancestring('0a1b2c3'), '0a1b2c3', 'example 3'); +is(balancestring('1a23'), '', 'example 4'); +is(balancestring('ab123'), '1a2b3', 'example 5'); + +sub balancestring($a) { + my @letters; + my @digits; + foreach my $c (split '', $a) { + if ($c =~ /[A-Z]/i) { + push @letters, $c, + } elsif ($c =~ /[0-9]/) { + push @digits, $c; + } + } + if (scalar @digits > scalar @letters + 1 || + scalar @letters > scalar @digits + 1) { + return ""; + } + @digits = sort @digits; + @letters = sort @letters; + my @a = @digits; + my @b = @letters; + if (scalar @letters == scalar @digits + 1) { + @a = @letters; + @b = @digits; + } + my @out; + foreach my $i (0 .. scalar @b - 1) { + push @out, $a[$i]; + push @out, $b[$i]; + } + if (scalar @a > scalar @b) { + push @out,$a[scalar @a - 1]; + } + join('', @out); +} diff --git a/challenge-342/roger-bell-west/perl/ch-2.pl b/challenge-342/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..87189fdc64 --- /dev/null +++ b/challenge-342/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(maxscore('0011'), 4, 'example 1'); +is(maxscore('0000'), 3, 'example 2'); +is(maxscore('1111'), 3, 'example 3'); +is(maxscore('0101'), 3, 'example 4'); +is(maxscore('011101'), 5, 'example 5'); + +use List::Util qw(max); +sub maxscore($a) { + my $zeroestoleft = 0; + my $onestoright = length($a); + my $maxscore = 0; + my @sa = split('', $a); + while (my ($i, $c) = each @sa) { + if ($c eq '0') { + $zeroestoleft++; + } elsif ($c eq '1') { + $onestoright--; + } + if ($i + 1 < length($a)) { + $maxscore = max($maxscore, $zeroestoleft + $onestoright); + } + } + $maxscore - $onestoright; +} diff --git a/challenge-342/roger-bell-west/postscript/ch-1.ps b/challenge-342/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..245f7fc6f4 --- /dev/null +++ b/challenge-342/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,199 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/a2s { + 2 dict begin + /i exch def + i length dup string /o exch def + 1 sub 0 exch 1 exch { + dup i 3 -1 roll get o 3 1 roll put + } for + o + end +} bind def + +/c.isdigit { + dup 48 ge exch 57 le and +} bind def + +/c.islower { + dup 97 ge exch 122 le and +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/balancestring { + 0 dict begin + /letters 0 array def + /digits 0 array def + s2a { + /c exch def + c c.islower { + /letters letters c apush.right def + } if + c c.isdigit { + /digits digits c apush.right def + } if + } forall + digits length letters length sub abs 1 gt { + () + } { + /digits digits quicksort def + /letters letters quicksort def + /a digits def + /b letters def + letters length digits length 1 add eq { + /a letters def + /b digits def + } if + [ + 0 1 b length 1 sub { + /i exch def + a i get + b i get + } for + a length b length gt { + a a length 1 sub get + } if + ] a2s + } ifelse + end +} bind def + +(balancestring) test.start +(0a1b2c) balancestring (0a1b2c) eq test +(abc12) balancestring (a1b2c) eq test +(0a1b2c3) balancestring (0a1b2c3) eq test +(1a23) balancestring () eq test +(ab123) balancestring (1a2b3) eq test +test.end diff --git a/challenge-342/roger-bell-west/postscript/ch-2.ps b/challenge-342/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..29a6930473 --- /dev/null +++ b/challenge-342/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,84 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/s2a { + [ exch { } forall ] +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/maxscore { + 0 dict begin + /a exch def + /zeroestoleft 0 def + /onestoright a length def + /maxscore 0 def + a s2a enumerate.array { + aload pop + /c exch def + /i exch def + c 48 eq { + /zeroestoleft zeroestoleft 1 add def + } if + c 49 eq { + /onestoright onestoright 1 sub def + } if + i 1 add a length lt { + /maxscore maxscore zeroestoleft onestoright add max def + } if + } forall + maxscore onestoright sub + end +} bind def + +(maxscore) test.start +(0011) maxscore 4 eq test +(0000) maxscore 3 eq test +(1111) maxscore 3 eq test +(0101) maxscore 3 eq test +(011101) maxscore 5 eq test +test.end diff --git a/challenge-342/roger-bell-west/rust/ch-1.rs b/challenge-342/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..747ea91039 --- /dev/null +++ b/challenge-342/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,59 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(balancestring("a0b1c2"), "0a1b2c"); +} + +#[test] +fn test_ex2() { + assert_eq!(balancestring("abc12"), "a1b2c"); +} + +#[test] +fn test_ex3() { + assert_eq!(balancestring("0a1b2c3"), "0a1b2c3"); +} + +#[test] +fn test_ex4() { + assert_eq!(balancestring("1a23"), ""); +} + +#[test] +fn test_ex5() { + assert_eq!(balancestring("ab123"), "1a2b3"); +} + +fn balancestring(a: &str) -> String { + let mut letters = Vec::new(); + let mut digits = Vec::new(); + for c in a.chars() { + match c { + 'a'..='z' => letters.push(c), + '0'..='9' => digits.push(c), + _ => (), + }; + } + if digits.len() > letters.len() + 1 || letters.len() > digits.len() + 1 { + return "".to_string(); + } + digits.sort_unstable(); + letters.sort_unstable(); + let mut a = digits.clone(); + let mut b = letters.clone(); + if letters.len() == digits.len() + 1 { + a = letters; + b = digits; + } + let mut out = String::new(); + for i in 0..b.len() { + out.push(a[i]); + out.push(b[i]); + } + if a.len() > b.len() { + out.push(a[a.len() - 1]); + } + out +} diff --git a/challenge-342/roger-bell-west/rust/ch-2.rs b/challenge-342/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..68e4c44f94 --- /dev/null +++ b/challenge-342/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,44 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(maxscore("0011"), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(maxscore("0000"), 3); +} + +#[test] +fn test_ex3() { + assert_eq!(maxscore("1111"), 3); +} + +#[test] +fn test_ex4() { + assert_eq!(maxscore("0101"), 3); +} + +#[test] +fn test_ex5() { + assert_eq!(maxscore("011101"), 5); +} + +fn maxscore(a: &str) -> usize { + let mut zeroestoleft = 0; + let mut onestoright = a.len(); + let mut maxscore = 0; + for (i, c) in a.chars().enumerate() { + match c { + '0' => zeroestoleft += 1, + '1' => onestoright -= 1, + _ => () + }; + if i + 1 < a.len() { + maxscore = std::cmp::max(maxscore, zeroestoleft + onestoright); + } + } + maxscore - onestoright +} diff --git a/challenge-342/roger-bell-west/tests.json b/challenge-342/roger-bell-west/tests.json new file mode 100644 index 0000000000..2483940527 --- /dev/null +++ b/challenge-342/roger-bell-west/tests.json @@ -0,0 +1,48 @@ +{ + "ch-1" : [ + { + "function" : "balancestring", + "arguments" : "0a1b2c", + "result" : "0a1b2c" + }, + { + "arguments" : "abc12", + "result" : "a1b2c" + }, + { + "arguments" : "0a1b2c3", + "result" : "0a1b2c3" + }, + { + "arguments" : "1a23", + "result" : "" + }, + { + "arguments" : "ab123", + "result" : "1a2b3" + } + ], + "ch-2" : [ + { + "function" : "maxscore", + "arguments" : "0011", + "result" : 4 + }, + { + "arguments" : "0000", + "result" : 3 + }, + { + "arguments" : "1111", + "result" : 3 + }, + { + "arguments" : "0101", + "result" : 3 + }, + { + "arguments" : "011101", + "result" : 5 + } + ] +} |
