diff options
Diffstat (limited to 'challenge-321')
61 files changed, 1535 insertions, 0 deletions
diff --git a/challenge-321/0rir/raku/ch-1.raku b/challenge-321/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..f7a84bd09d --- /dev/null +++ b/challenge-321/0rir/raku/ch-1.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +Task 1: Distinct Average Submitted by: Mohammad Sajid Anwar + [edited] +You are given an array of numbers with even length. + +Write a script to return the count of distinct average. The average is +calculated, by repeatedly, removing the minimum and the maximum, then average of the two. See notes to examples. + +Example 1 +Input: @nums = (1, 2, 4, 3, 5, 6) +Output: 1 + +Step 1: Min = 1, Max = 6, Avg = 3.5 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 1. + +Example 2 +Input: @nums = (0, 2, 4, 8, 3, 5) +Output: 2 + +Example 3 +Input: @nums = (7, 3, 1, 0, 5, 9) +Output: 2 + +=end comment + +my @Test = + # in exp + (1, 2, 4, 3, 5, 6), 1, + (0, 2, 4, 8, 3, 5), 2, + (7, 3, 1, 0, 5, 9), 2, + (1,1), 1, + (2,1), 1, +; +plan @Test ÷ 2; + +sub task( $a is copy -->Int) { + $a = $a.sort; + my \i = $a.end div 2; + ( [Z+] $a[0..i], + $a[$a.end … i+1] + ).unique.elems; +} + +for @Test -> @in, $exp, { + is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()"; +} +done-testing; + +my @num = (7, 3, 1, 0, 5, 9); + +say "\nInput: @nums = @num.raku()\nOutput: &task( @num)"; + diff --git a/challenge-321/0rir/raku/ch-2.raku b/challenge-321/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..9d0e8856dc --- /dev/null +++ b/challenge-321/0rir/raku/ch-2.raku @@ -0,0 +1,106 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +321-Task 2: Backspace Compare Submitted by: Mohammad Sajid Anwar +You are given two strings containing zero or more #. + +Write a script to return true if the two given strings are same by treating # as backspace. + + +Example 1 +Input: $str1 = "ab#c" + $str2 = "ad#c" +Output: true + +For first string, we remove "b" as it is followed by "#". +For second string, we remove "d" as it is followed by "#". +In the end both strings became the same. + +Example 2 +Input: $str1 = "ab##" + $str2 = "a#b#" +Output: true + +Example 3 +Input: $str1 = "a#b" + $str2 = "c" +Output: false +=end comment + +my @Test = + "ab*c", "ad*c", True, + "ab**", "a*b*", True, + "***ab**", "**a*b*", True, + "ab**", "***a*b*", True, + + "a*b", "c", False, + "*a*b", "c", False, + "*a*b", "***c", False, +; + +my @Test-BS = + "***c", "c", + "ab*c", "ac", + "ab**", "", + "a*b*", "", + "***ab**", "", + "***ab****", "", + "*a*b*", "", + "ab**", "", + "ab****", "", + "a*b", "b", + "c", "c", + "*a*b", "b", + '*', '', +; +plan @Test ÷ 3 + @Test-BS ÷ 2; + +my $BS-CHAR = '*'; # Using '*' to avoid Test::proclaim's munging. + +# task -- return the 'cmp' of two Strs after processing "backspace" +# characters. Leading "backspace"s are deleteable. +sub task( $a is copy, $b is copy, :$bs = $BS-CHAR -->Bool) { + my @bs; + for $a, $b -> $word { + @bs.push: apply-bs( $word); + } + return @bs[0] eq @bs[1]; +} + +# apply-bs +# Process "backspace" characters in an Array repping a string. +sub apply-bs( $w, :$bs = $BS-CHAR -->Str) { + my @w = $w.comb; + my $k = -1; + my @ret; + + while $k < @w.end { + ++$k; + when @w[$k] ne $bs { + @ret.push: @w[$k]; + } + when @w[$k] eq $bs and @ret.elems > 0 { + @ret.pop; + } + # nought to do: @w[$k] eq $bs and @ret ~~ Empty + } + @ret.join; +} +for @Test-BS -> $in, $exp { + is apply-bs( $in), $exp, "$exp <- " ~ $in.raku(); +} + +for @Test -> $a, $b, $exp { + is task($a, $b ), $exp, "$exp <- @$a[] ∘∘ @$b[]"; +} +done-testing; + +$BS-CHAR = '#'; +my $str1 = "ab#xy##c#####a#b#abcdefg######ce###acb#"; +my $str2 = "ad#c"; + +say "\nInput: \$str1 = $str1\n \$str2 = $str2\nOutput: ", + task( $str1, $str2); diff --git a/challenge-321/ash/raku/ch-1.raku b/challenge-321/ash/raku/ch-1.raku new file mode 100644 index 0000000000..7e7cbb3642 --- /dev/null +++ b/challenge-321/ash/raku/ch-1.raku @@ -0,0 +1,20 @@ +# Task 1 of the Weekly Challenge 321 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/#TASK1 + +my @nums = 1, 2, 4, 3, 5, 6; # 1 +# my @nums = 0, 2, 4, 8, 3, 5; # 2 +# my @nums = 7, 3, 1, 0, 5, 9; # 2 + +my @avg = gather { + while (@nums) { + my $min = @nums.min; + my $max = @nums.max; + + my $avg = ($min + $max) / 2; + take $avg; + + @nums.=grep: * ∉ (@nums.min, @nums.max); + } +} + +say @avg.unique.elems; diff --git a/challenge-321/ash/raku/ch-2.raku b/challenge-321/ash/raku/ch-2.raku new file mode 100644 index 0000000000..904586b279 --- /dev/null +++ b/challenge-321/ash/raku/ch-2.raku @@ -0,0 +1,19 @@ +# Task 2 of the Weekly Challenge 321 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/#TASK2 + +my $str1 = 'ab#c'; +my $str2 = 'ad#c'; # True + +# my $str1 = 'ab##'; +# my $str2 = 'a#b#'; # True + +# my $str1 = 'a#b'; +# my $str2 = 'c'; # False + +say process($str1) eq process($str2); + +sub process($s is copy) { + $s ~~ s/.\#// while $s ~~ /.\#/; + + return $s; +} diff --git a/challenge-321/conor-hoekstra/ch-1.bqn b/challenge-321/conor-hoekstra/ch-1.bqn new file mode 100644 index 0000000000..a67f8afb62 --- /dev/null +++ b/challenge-321/conor-hoekstra/ch-1.bqn @@ -0,0 +1,15 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/321-1.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +DistinctAverage ← { ≠⍷+˝⌽⌾⊏2‿∘⥊∧𝕩 } # Explicit +DistinctAverage2 ← ≠·⍷·+˝·⌽⌾⊏2‿∘⥊∧ # Tacit + +# Tests +u.UnitTest (DistinctAverage ⟨1, 2, 4, 3, 5, 6⟩) ≡ 1 +u.UnitTest (DistinctAverage ⟨0, 2, 4, 8, 3, 5⟩) ≡ 2 +u.UnitTest (DistinctAverage ⟨7, 3, 1, 0, 5, 9⟩) ≡ 2 +u.UnitTest (DistinctAverage2 ⟨1, 2, 4, 3, 5, 6⟩) ≡ 1 +u.UnitTest (DistinctAverage2 ⟨0, 2, 4, 8, 3, 5⟩) ≡ 2 +u.UnitTest (DistinctAverage2 ⟨7, 3, 1, 0, 5, 9⟩) ≡ 2 diff --git a/challenge-321/conor-hoekstra/ch-2.bqn b/challenge-321/conor-hoekstra/ch-2.bqn new file mode 100644 index 0000000000..8a98af3d4a --- /dev/null +++ b/challenge-321/conor-hoekstra/ch-2.bqn @@ -0,0 +1,12 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/321-2.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +Norm ← 0>·(⊢⌈+)`⌾⌽·-⟜¬'#'=⊢ +BackspaceCompare ← ≡○Norm + +# Tests +u.UnitTest ("ad#c" BackspaceCompare "ab#c") ≡ 1 +u.UnitTest ("ab##" BackspaceCompare "a#b#") ≡ 1 +u.UnitTest ( "a#b" BackspaceCompare "c" ) ≡ 0 diff --git a/challenge-321/deadmarshal/blog.txt b/challenge-321/deadmarshal/blog.txt new file mode 100644 index 0000000000..08bc2ea63b --- /dev/null +++ b/challenge-321/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/05/twc321.html diff --git a/challenge-321/deadmarshal/erlang/ch1.erl b/challenge-321/deadmarshal/erlang/ch1.erl new file mode 100644 index 0000000000..e283ab5d1e --- /dev/null +++ b/challenge-321/deadmarshal/erlang/ch1.erl @@ -0,0 +1,13 @@ +-module(ch1). +-author("Ali Moradi"). +-export([distinct_average/1]). + +distinct_average(L) -> + L2 = lists:sort(L), + L3 = pair_min_max(L2), + L4 = lists:map(fun({A, B}) -> A + B end, L3), + sets:size(sets:from_list(L4)). + +pair_min_max(L) -> + {Left, Right} = lists:split(length(L) div 2, L), + lists:zip(Left, lists:reverse(Right)). diff --git a/challenge-321/deadmarshal/erlang/ch2.erl b/challenge-321/deadmarshal/erlang/ch2.erl new file mode 100644 index 0000000000..9dba2298db --- /dev/null +++ b/challenge-321/deadmarshal/erlang/ch2.erl @@ -0,0 +1,16 @@ +-module(ch2). +-author("Ali Moradi"). +-export([backspace_compare/2]). + +backspace_compare(S1, S2) -> + process_string(S1) =:= process_string(S2). + +process_string(S) -> + process_string(S, []). + +process_string([], Acc) -> lists:reverse(Acc); +process_string([$# | T], [_ | AccT]) -> % remove one character from Acc + process_string(T, AccT); +process_string([C | T], Acc) -> % normal character + process_string(T, [C | Acc]). + diff --git a/challenge-321/deadmarshal/java/Ch1.java b/challenge-321/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..bf51dc88cb --- /dev/null +++ b/challenge-321/deadmarshal/java/Ch1.java @@ -0,0 +1,20 @@ +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(distinct_average(new int[]{1, 2, 4, 3, 5, 6})); + System.out.println(distinct_average(new int[]{0, 2, 4, 8, 3, 5})); + System.out.println(distinct_average(new int[]{7, 3, 1, 0, 5, 9})); + } + + private static int distinct_average(int[] arr) { + Arrays.sort(arr); + Set<Integer> s = new HashSet<>(); + for (int i = 0; i < arr.length / 2; ++i) + s.add(arr[i] + arr[arr.length - i - 1]); + return s.size(); + } +} + diff --git a/challenge-321/deadmarshal/java/Ch2.java b/challenge-321/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..8d92e9bc6e --- /dev/null +++ b/challenge-321/deadmarshal/java/Ch2.java @@ -0,0 +1,41 @@ +public class Ch2 { + public static void main(String[] args) { + System.out.println(backspace_compare("ab#c", "ad#c")); + System.out.println(backspace_compare("ab##", "a#b#")); + System.out.println(backspace_compare("a#b", "c")); + } + + private static boolean backspace_compare(String s1, String s2) { + int i = s1.length() - 1, j = s2.length() - 1; + int skip1 = 0, skip2 = 0; + for (; i >= 0 || j >= 0; --i, --j) { + while (i >= 0) { + if (s1.charAt(i) == '#') { + ++skip1; + --i; + } else if (skip1 > 0) { + --skip1; + --i; + } else { + break; + } + } + while (j >= 0) { + if (s2.charAt(j) == '#') { + ++skip2; + --j; + } else if (skip2 > 0) { + --skip2; + --j; + } else { + break; + } + } + if (i >= 0 && j >= 0) { + if (s1.charAt(i) != s2.charAt(j)) return false; + } else if (i >= 0 || j >= 0) return false; + } + return true; + } +} + diff --git a/challenge-321/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-321/deadmarshal/modula-3/Ch1/src/Ch1.m3 new file mode 100644 index 0000000000..77c3e8efbf --- /dev/null +++ b/challenge-321/deadmarshal/modula-3/Ch1/src/Ch1.m3 @@ -0,0 +1,25 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,IntSetDef,IntArraySort; + +VAR + A1 := ARRAY[0..5] OF INTEGER{1,2,4,3,5,6}; + A2 := ARRAY[0..5] OF INTEGER{0,2,4,8,3,5}; + A3 := ARRAY[0..5] OF INTEGER{7,3,1,0,5,9}; + +PROCEDURE DistinctAverage(VAR A:ARRAY OF INTEGER):INTEGER = + VAR S := NEW(IntSetDef.T).init(); + BEGIN + IntArraySort.Sort(A); + FOR I := FIRST(A) TO NUMBER(A) DIV 2 DO + EVAL S.insert(A[I] + A[LAST(A)-I]) + END; + RETURN S.size() + END DistinctAverage; + +BEGIN + SIO.PutInt(DistinctAverage(A1)); SIO.Nl(); + SIO.PutInt(DistinctAverage(A2)); SIO.Nl(); + SIO.PutInt(DistinctAverage(A3)); SIO.Nl() +END Ch1. + diff --git a/challenge-321/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-321/deadmarshal/modula-3/Ch1/src/m3makefile new file mode 100644 index 0000000000..66a81a47c4 --- /dev/null +++ b/challenge-321/deadmarshal/modula-3/Ch1/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +import("set") +implementation("Ch1") +program("Ch1") diff --git a/challenge-321/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-321/deadmarshal/modula-3/Ch2/src/Ch2.m3 new file mode 100644 index 0000000000..958e968a7d --- /dev/null +++ b/challenge-321/deadmarshal/modula-3/Ch2/src/Ch2.m3 @@ -0,0 +1,49 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text; + +PROCEDURE BackspaceCompare(READONLY S1,S2:TEXT):BOOLEAN = + VAR + I := Text.Length(S1)-1; + J := Text.Length(S2)-1; + Skip1,Skip2:CARDINAL; + BEGIN + WHILE I >= 0 OR J >= 0 DO + WHILE I >= 0 DO + IF Text.GetChar(S1,I) = '#' THEN + INC(Skip1); + DEC(I) + ELSIF Skip1 > 0 THEN + DEC(Skip1); + DEC(I) + ELSE EXIT + END + END; + WHILE J >= 0 DO + IF Text.GetChar(S2,J) = '#' THEN + INC(Skip2); + DEC(J); + ELSIF Skip2 > 0 THEN + DEC(Skip2); + DEC(J) + ELSE EXIT + END + END; + IF I >= 0 AND J >= 0 THEN + IF Text.GetChar(S1,I) # Text.GetChar(S2,J) THEN + RETURN FALSE + END; + ELSIF I >= 0 OR J >= 0 THEN RETURN FALSE + END; + DEC(I); + DEC(J); + END; + RETURN TRUE + END BackspaceCompare; + +BEGIN + SIO.PutBool(BackspaceCompare("ab#c","ad#c")); SIO.Nl(); + SIO.PutBool(BackspaceCompare("ab##","a#b#")); SIO.Nl(); + SIO.PutBool(BackspaceCompare("a#b","c")); SIO.Nl() +END Ch2. + diff --git a/challenge-321/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-321/deadmarshal/modula-3/Ch2/src/m3makefile new file mode 100644 index 0000000000..78802242fe --- /dev/null +++ b/challenge-321/deadmarshal/modula-3/Ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("Ch2") diff --git a/challenge-321/deadmarshal/perl/ch-1.pl b/challenge-321/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..b4e9fe6016 --- /dev/null +++ b/challenge-321/deadmarshal/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub distinct_average{ + my @arr = sort{$a <=> $b} @{$_[0]}; + my %h; + undef $h{($arr[$_] + $arr[$#arr-$_]) / 2} foreach 0..@arr/2; + scalar keys %h +} + +printf "%d\n",distinct_average([1,2,4,3,5,6]); +printf "%d\n",distinct_average([0,2,4,8,3,5]); +printf "%d\n",distinct_average([7,3,1,0,5,9]); + diff --git a/challenge-321/deadmarshal/perl/ch-2.pl b/challenge-321/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..17260287f1 --- /dev/null +++ b/challenge-321/deadmarshal/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub backspace_compare{ + my ($s1,$s2) = @_; + for($s1,$s2){ + 1 while s/[^#]#// + } + $s1 eq $s2 +} + +printf "%d\n",backspace_compare('ab#c','ad#c'); +printf "%d\n",backspace_compare('ab##','a#b#'); +printf "%d\n",backspace_compare('a#b','c'); + diff --git a/challenge-321/e-choroba/perl/ch-1.pl b/challenge-321/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6cbf0aa7da --- /dev/null +++ b/challenge-321/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub distinct_average(@nums) { + @nums = s |
