diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2023-07-14 08:12:41 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2023-07-14 08:12:41 +0330 |
| commit | fcd352576e64dd7156578f626b5e91f9dbd41438 (patch) | |
| tree | 597ec55003091e4849ce00ae5be8574bb2fbd7d3 | |
| parent | f4d2ab78a5ea8cb69132cfee6c46fce3b98d2a10 (diff) | |
| download | perlweeklychallenge-club-fcd352576e64dd7156578f626b5e91f9dbd41438.tar.gz perlweeklychallenge-club-fcd352576e64dd7156578f626b5e91f9dbd41438.tar.bz2 perlweeklychallenge-club-fcd352576e64dd7156578f626b5e91f9dbd41438.zip | |
TWC225
22 files changed, 514 insertions, 0 deletions
diff --git a/challenge-225/deadmarshal/c/ch-1.c b/challenge-225/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..e7ab9338c5 --- /dev/null +++ b/challenge-225/deadmarshal/c/ch-1.c @@ -0,0 +1,30 @@ +#include<stdio.h> +#include<ctype.h> + +size_t max_words(const char **arr, size_t sz) +{ + size_t count = 0, max = 0; + for(size_t i = 0; i < sz; ++i) + { + for(size_t j = 0; arr[i][j] != '\0'; ++j) + if(isspace(arr[i][j])) count++; + if(max < count) max = count; + count = 0; + } + return max+1; +} + +int main(int argc, char **argv) +{ + const char *arr1[] = {"Perl and Raku belong to the same family.", + "I love perl.", + "The Perl and Raku Conference."}; + const char *arr2[] = {"The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."}; + const size_t sz = 3; + printf("%zu\n",max_words(arr1,sz)); + printf("%zu\n",max_words(arr2,sz)); + return 0; +} + diff --git a/challenge-225/deadmarshal/c/ch-2.c b/challenge-225/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..1d79b7e1c6 --- /dev/null +++ b/challenge-225/deadmarshal/c/ch-2.c @@ -0,0 +1,28 @@ +#include<stdio.h> +#include<stdlib.h> + +void left_right_sum_diff(int *arr, size_t sz) +{ + size_t left = 0,right = 0; + for(size_t i = 1; i < sz; ++i) right += arr[i]; + for(size_t i = 0; i < sz; ++i) + { + printf("%d ", abs(left - right)); + left += arr[i]; + right -= i < sz-1 ? arr[i+1] : 0; + } + puts(""); +} + +int main() +{ + int a1[] = {10,4,8,3}; + int a2[] = {1}; + int a3[] = {1,2,3,4,5}; + size_t sz1 = 4, sz2 = 1, sz3 = 5; + left_right_sum_diff(a1,sz1); + left_right_sum_diff(a2,sz2); + left_right_sum_diff(a3,sz3); + return 0; +} + diff --git a/challenge-225/deadmarshal/cpp/ch-1.cpp b/challenge-225/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..17fb182b49 --- /dev/null +++ b/challenge-225/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,31 @@ +#include<iostream> +#include<vector> +#include<cctype> + +std::size_t max_words(const std::vector<std::string> &vec) +{ + size_t count{},max{}; + for(const auto & s : vec) + { + for(const auto &c : s) if(isspace(c)) count++; + if(max < count) max = count; + count = 0; + } + return max+1; +} + +int main(int argc, char **argv) +{ + std::vector<std::string> + vec{"Perl and Raku belong to the same family.", + "I love perl.", + "The Perl and Raku Conference."}; + std::vector<std::string> + vec2{"The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."}; + std::cout << max_words(vec) << '\n'; + std::cout << max_words(vec2) << '\n'; + return 0; +} + diff --git a/challenge-225/deadmarshal/cpp/ch-2.cpp b/challenge-225/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..bf93226008 --- /dev/null +++ b/challenge-225/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include<iostream> +#include<vector> + +template<typename T> +void left_right_sum_diff(const std::vector<T> &vec) +{ + std::size_t left{},right{}; + for(std::size_t i = 1; i < vec.size(); ++i) right += vec.at(i); + for(std::size_t i = 0; i < vec.size(); ++i) + { + std::cout << abs(left - right) << ' '; + left += vec.at(i); + right -= i < vec.size()-1 ? vec.at(i+1) : 0; + } + std::cout << '\n'; +} + +int main() +{ + std::vector<int> vec1{10,4,8,3},vec2{1},vec3{1,2,3,4,5}; + left_right_sum_diff<int>(vec1); + left_right_sum_diff<int>(vec2); + left_right_sum_diff<int>(vec3); + return 0; +} + diff --git a/challenge-225/deadmarshal/d/ch1.d b/challenge-225/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..2fd8435b83 --- /dev/null +++ b/challenge-225/deadmarshal/d/ch1.d @@ -0,0 +1,27 @@ +import std.stdio:writeln; +import std.uni:isSpace; + +size_t max_words(const ref string[] arr) +{ + size_t count = 0,max = 0; + foreach(ref s; arr) + { + foreach(ref c;s) if(isSpace(c)) count++; + if(max < count) max = count; + count = 0; + } + return max+1; +} + +void main() +{ + string[] a1 = ["Perl and Raku belong to the same family.", + "I love perl.", + "The Perl and Raku Conference."]; + string[] a2 = ["The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."]; + writeln(max_words(a1)); + writeln(max_words(a2)); +} + diff --git a/challenge-225/deadmarshal/d/ch2.d b/challenge-225/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..7db21a7921 --- /dev/null +++ b/challenge-225/deadmarshal/d/ch2.d @@ -0,0 +1,26 @@ +import std.stdio:write,writeln; +import std.algorithm:sum; +import std.math:abs; + +void left_right_sum_diff(const ref int[] arr) +{ + int left = 0, right = arr[1..$].sum; + foreach(i; 0..arr.length) + { + write(abs(left - right), ' '); + left += arr[i]; + right -= i < arr.length-1 ? arr[i+1] : 0; + } + writeln; +} + +void main() +{ + int[] a1 = [10,4,8,3]; + int[] a2 = [1]; + int[] a3 = [1,2,3,4,5]; + left_right_sum_diff(a1); + left_right_sum_diff(a2); + left_right_sum_diff(a3); +} + diff --git a/challenge-225/deadmarshal/lua/ch-1.lua b/challenge-225/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..816c08fc29 --- /dev/null +++ b/challenge-225/deadmarshal/lua/ch-1.lua @@ -0,0 +1,19 @@ +#!/usr/bin/env lua + +local function max_words(t) + local count,max = 0,0 + for i=1, #t do + for _ in t[i]:gmatch("%s+") do count = count + 1 end + if count > max then max = count end + count = 0 + end + return max+1 +end + +print(max_words({"Perl and Raku belong to the same family.", + "I love Perl.", + "The Perl and Raku Conference."})) +print(max_words({"The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."})) + diff --git a/challenge-225/deadmarshal/lua/ch-2.lua b/challenge-225/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..e6fe5162de --- /dev/null +++ b/challenge-225/deadmarshal/lua/ch-2.lua @@ -0,0 +1,17 @@ +#!/usr/bin/env lua + +local function left_right_sum_diff(t) + local left,right = 0,0 + for i=2,#t do right = right + t[i] end + for i=1,#t do + io.write(math.abs(left - right), ' ') + left = left + t[i] + if i < #t then right = right - t[i+1] end + end + print() +end + +left_right_sum_diff({10,4,8,3}) +left_right_sum_diff({1}) +left_right_sum_diff({1,2,3,4,5}) + diff --git a/challenge-225/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-225/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..cf8547b0b7 --- /dev/null +++ b/challenge-225/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,30 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO,Text; + +VAR + A1:ARRAY[0..2] OF TEXT := ARRAY OF TEXT{"Perl and Raku belong to the same family.", + "I love Perl", + "The Perl and Raku Conference."}; + A2:ARRAY[0..2] OF TEXT := ARRAY OF TEXT{"The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."}; + +PROCEDURE MaxWords(VAR A:ARRAY OF TEXT):INTEGER = + VAR Count,Max:INTEGER := 0; + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + FOR J := 0 TO Text.Length(A[I])-1 DO + IF Text.GetChar(A[I],J) = ' ' THEN INC(Count) END; + END; + IF Count > Max THEN Max := Count END; + Count := 0; + END; + RETURN Max+1 + END MaxWords; + +BEGIN + IO.PutInt(MaxWords(A1)); IO.Put("\n"); + IO.PutInt(MaxWords(A2)); IO.Put("\n"); +END Ch1. + diff --git a/challenge-225/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-225/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..0ee72d695b --- /dev/null +++ b/challenge-225/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch1") +program("ch1") + diff --git a/challenge-225/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-225/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..654652f587 --- /dev/null +++ b/challenge-225/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,29 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{10,4,8,3}; + A2:ARRAY[0..0] OF INTEGER := ARRAY OF INTEGER{1}; + A3:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{1,2,3,4,5}; + +PROCEDURE LeftRightSumDiff(VAR A:ARRAY OF INTEGER) = + VAR + Left,Right:INTEGER := 0; +BEGIN + FOR I := FIRST(A)+1 TO LAST(A) DO INC(Right,A[I]) END; + FOR I := FIRST(A) TO LAST(A) DO + IO.PutInt(ABS(Left-Right)); + IO.PutChar(' '); + INC(Left,A[I]); + IF I < LAST(A) THEN DEC(Right,A[I+1]) END; + END; + IO.Put("\n") +END LeftRightSumDiff; + +BEGIN + LeftRightSumDiff(A1); + LeftRightSumDiff(A2); + LeftRightSumDiff(A3); +END Ch2. + diff --git a/challenge-225/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-225/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..5c32bbc4bb --- /dev/null +++ b/challenge-225/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch2") +program("ch2") + diff --git a/challenge-225/deadmarshal/oberon/Ch1.Mod b/challenge-225/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..f36154315c --- /dev/null +++ b/challenge-225/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,42 @@ +MODULE Ch1; + + IMPORT Out; + + TYPE + PArr = POINTER TO ARRAY OF ARRAY OF CHAR; + + VAR + A1,A2:PArr; + + PROCEDURE Init; + BEGIN + NEW(A1,3,41); + NEW(A2,3,43); + COPY("Perl and Raku belong to the same family.",A1[0]); + COPY("I love perl.",A1[1]); + COPY("The Perl and Raku Conference.",A1[2]); + COPY("The Weekly Challenge.",A2[0]); + COPY("Python is the most popular guest language.",A2[1]); + COPY("Team PWC has over 300 members.",A2[2]); + END Init; + + PROCEDURE MaxWords(arr:PArr):LONGINT; + VAR + i,j,count,max:LONGINT; + BEGIN + count := 0; max := 0; + FOR i := 0 TO LEN(arr^)-1 DO + FOR j := 0 TO LEN(arr[i])-1 DO IF arr[i][j] = ' ' THEN INC(count) END + END; + IF count > max THEN max := count END; + count := 0 + END; + RETURN max+1 + END MaxWords; + +BEGIN + Init; + Out.Int(MaxWords(A1),0); Out.Ln; + Out.Int(MaxWords(A2),0); Out.Ln +END Ch1. + diff --git a/challenge-225/deadmarshal/oberon/Ch2.Mod b/challenge-225/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..c01ec20722 --- /dev/null +++ b/challenge-225/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,39 @@ +MODULE Ch2; + + IMPORT Out; + + VAR + A1:ARRAY 4 OF LONGINT; + A2:ARRAY 1 OF LONGINT; + A3:ARRAY 5 OF LONGINT; + + PROCEDURE Init; + BEGIN + A1[0] := 10; A1[1] := 4; A1[2] := 8; A1[3] := 3; + A2[0] := 1; + A3[0] := 1; A3[1] := 2; A3[2] := 3; A3[3] := 4; A3[4] := 5; + END Init; + + PROCEDURE LeftRightSumDiff(VAR arr:ARRAY OF LONGINT); + VAR + i:LONGINT; + left,right:LONGINT; + BEGIN + left := 0; right := 0; + FOR i := 1 TO LEN(arr)-1 DO INC(right, arr[i]) END; + FOR i := 0 TO LEN(arr)-1 DO + Out.Int(ABS(left - right),0); + Out.Char(' '); + INC(left,arr[i]); + IF i < LEN(arr)-1 THEN DEC(right,arr[i+1]) END + END; + Out.Ln + END LeftRightSumDiff; + +BEGIN + Init; + LeftRightSumDiff(A1); + LeftRightSumDiff(A2); + LeftRightSumDiff(A3); +END Ch2. + diff --git a/challenge-225/deadmarshal/pascal/ch1.pas b/challenge-225/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..50f7deee89 --- /dev/null +++ b/challenge-225/deadmarshal/pascal/ch1.pas @@ -0,0 +1,32 @@ +program Ch1; + +{$mode objfpc} +uses + SysUtils,Types,StrUtils; + +var + A1,A2:TStringDynArray; + +function MaxWords(var A:TStringDynArray):SizeInt; +var + I,Count:SizeInt; +begin + Result := 0; + for I := Low(A) to high(A) do + begin + Count := WordCount(A[I],[' ']); + if Count > Result then Result := Count; + end; +end; + +begin + A1 := ['Perl and Raku belong to the same family.', + 'I love Perl.', + 'The Perl and Raku Conference.']; + A2 := ['The Weekly Challenge.', + 'Python is the most popular guest language.', + 'Team PWC has over 300 members.']; + WriteLn(MaxWords(A1)); + WriteLn(MaxWords(A2)); +end. + diff --git a/challenge-225/deadmarshal/pascal/ch2.pas b/challenge-225/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..c76866b3bd --- /dev/null +++ b/challenge-225/deadmarshal/pascal/ch2.pas @@ -0,0 +1,33 @@ +program Ch2; + +{$mode objfpc} +uses + SysUtils,Types; + +var + A1,A2,A3:TIntegerDynArray; + +procedure LeftRightSumDiff(var A:TIntegerDynArray); +var + I,Left,Right:Integer; +begin + Left := 0; Right := 0; + for I := Low(A)+1 to High(A) do Inc(Right,A[I]); + for I := Low(A) to High(A) do + begin + Write(Abs(Left - Right), ' '); + Inc(Left,A[I]); + if I < High(A) then Dec(Right,A[I+1]); + end; + WriteLn +end; + +begin + A1 := [10,4,8,3]; + A2 := [1]; + A3 := [1,2,3,4,5]; + LeftRightSumDiff(A1); + LeftRightSumDiff(A2); + LeftRightSumDiff(A3); +end. + diff --git a/challenge-225/deadmarshal/perl/ch-1.pl b/challenge-225/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..24ebb1bcdc --- /dev/null +++ b/challenge-225/deadmarshal/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(max); + +sub max_words +{ + max map{scalar split ' ',$_} @{$_[0]}; +} + +printf "%d\n", max_words(["Perl and Raku belong to the same family.", + "I love Perl.", + "The Perl and Raku Conference."]); +printf "%d\n", max_words(["The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."]); diff --git a/challenge-225/deadmarshal/perl/ch-2.pl b/challenge-225/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..f609b1ebb1 --- /dev/null +++ b/challenge-225/deadmarshal/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(sum0); + +sub left_right_sum_diff +{ + my ($arr) = @_; + my ($left,$right) = (0,sum0 $arr->@[1..$#$arr]); + my @diffs; + foreach(0..$#$arr){ + push @diffs, abs $left - $right; + $left += $arr->[$_]; + $right -= $_ < $#$arr ? $arr->[$_+1] : 0; + } + @diffs +} + +printf "(%s)\n", join ', ', left_right_sum_diff([10,4,8,3]); +printf "(%s)\n", join ', ', left_right_sum_diff([1]); +printf "(%s)\n", join ', ', left_right_sum_diff([1,2,3,4,5]); + diff --git a/challenge-225/deadmarshal/python/ch1.py b/challenge-225/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..113fe83ccb --- /dev/null +++ b/challenge-225/deadmarshal/python/ch1.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +def max_words(arr): + return max(map(lambda s: len(s.split(sep=' ')),arr)) + +print(max_words(["Perl and Raku belong to the same family.", + "I love Perl.", + "The Perl and Raku Conference."])) +print(max_words(["The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."])) + diff --git a/challenge-225/deadmarshal/python/ch2.py b/challenge-225/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..ee2b4d7d18 --- /dev/null +++ b/challenge-225/deadmarshal/python/ch2.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +def left_right_sum_diff(arr): + left,right = 0,sum(arr[1:]) + for i in range(len(arr)): + print(abs(left - right),' ',end=' ') + left += arr[i] + if i < len(arr)-1: right -= arr[i+1] + print() + +left_right_sum_diff([10,4,8,3]) +left_right_sum_diff([1]) +left_right_sum_diff([1,2,3,4,5]) + diff --git a/challenge-225/deadmarshal/raku/ch-1.raku b/challenge-225/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..57d8f2a39d --- /dev/null +++ b/challenge-225/deadmarshal/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +sub max_words(@arr) +{ + @arr.map({.split(' ').elems}).max; +} + +say max_words(["Perl and Raku belong to the same family.", + "I love Perl.", + "The Perl and Raku Conference."]); +say max_words(["The Weekly Challenge.", + "Python is the most popular guest language.", + "Team PWC has over 300 members."]); + diff --git a/challenge-225/deadmarshal/raku/ch-2.raku b/challenge-225/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..02de04b240 --- /dev/null +++ b/challenge-225/deadmarshal/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku + +sub left-right-sum-diff(@arr) +{ + my ($left,$right) = (0,@arr[1..@arr.end].sum); + my @diffs; + for 0..@arr.end + { + @diffs.push(abs $left - $right); + $left += @arr[$_]; + $right -= $_ < @arr.end ?? @arr[$_+1] !! 0; + } + @diffs +} + +say left-right-sum-diff([10,4,8,3]); +say left-right-sum-diff([1]); +say left-right-sum-diff([1,2,3,4,5]); + |
