aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-262/deadmarshal/blog.txt1
-rw-r--r--challenge-262/deadmarshal/c/ch-1.c22
-rw-r--r--challenge-262/deadmarshal/c/ch-2.c23
-rw-r--r--challenge-262/deadmarshal/cpp/ch-1.cpp21
-rw-r--r--challenge-262/deadmarshal/cpp/ch-2.cpp21
-rw-r--r--challenge-262/deadmarshal/d/ch1.d20
-rw-r--r--challenge-262/deadmarshal/d/ch2.d21
-rw-r--r--challenge-262/deadmarshal/modula-3/ch1/src/Ch1.m324
-rw-r--r--challenge-262/deadmarshal/modula-3/ch1/src/m3makefile5
-rw-r--r--challenge-262/deadmarshal/modula-3/ch2/src/Ch2.m325
-rw-r--r--challenge-262/deadmarshal/modula-3/ch2/src/m3makefile5
-rw-r--r--challenge-262/deadmarshal/perl/ch-1.pl15
-rw-r--r--challenge-262/deadmarshal/perl/ch-2.pl18
13 files changed, 221 insertions, 0 deletions
diff --git a/challenge-262/deadmarshal/blog.txt b/challenge-262/deadmarshal/blog.txt
new file mode 100644
index 0000000000..bff659f97d
--- /dev/null
+++ b/challenge-262/deadmarshal/blog.txt
@@ -0,0 +1 @@
+https://deadmarshal.blogspot.com/2024/03/twc262.html
diff --git a/challenge-262/deadmarshal/c/ch-1.c b/challenge-262/deadmarshal/c/ch-1.c
new file mode 100644
index 0000000000..4e26074c78
--- /dev/null
+++ b/challenge-262/deadmarshal/c/ch-1.c
@@ -0,0 +1,22 @@
+#include<stdio.h>
+#include<math.h>
+
+size_t max_positive_negative(int *arr,size_t sz)
+{
+ size_t neg = 0, pos = 0;
+ for(size_t i = 0; i < sz; ++i) if(arr[i] < 0) neg++; else pos++;
+ return (size_t)fmax(neg,pos);
+}
+
+int main(void)
+{
+ int arr1[] = {-3,1,2,-1,3,-2,4};
+ int arr2[] = {-1,-2,-3,1};
+ int arr3[] = {1,2};
+ size_t sz1 = 7, sz2 = 4, sz3 = 2;
+ printf("%zu\n",max_positive_negative(arr1,sz1));
+ printf("%zu\n",max_positive_negative(arr2,sz2));
+ printf("%zu\n",max_positive_negative(arr3,sz3));
+ return 0;
+}
+
diff --git a/challenge-262/deadmarshal/c/ch-2.c b/challenge-262/deadmarshal/c/ch-2.c
new file mode 100644
index 0000000000..377af259ad
--- /dev/null
+++ b/challenge-262/deadmarshal/c/ch-2.c
@@ -0,0 +1,23 @@
+#include<stdio.h>
+
+size_t count_equal_divisible(int *arr, size_t sz, int k)
+{
+ size_t count = 0;
+ for(size_t i = 0; i < sz-1; ++i){
+ for(size_t j = i+1; j < sz; ++j){
+ if((arr[i] == arr[j]) && ((i*j) % k == 0)) count++;
+ }
+ }
+ return count;
+}
+
+int main(void)
+{
+ int arr1[] = {3,1,2,2,2,1,3};
+ int arr2[] = {1,2,3};
+ size_t sz1 = 7,sz2 = 3;
+ printf("%zu\n",count_equal_divisible(arr1,sz1,2));
+ printf("%zu\n",count_equal_divisible(arr2,sz2,1));
+ return 0;
+}
+
diff --git a/challenge-262/deadmarshal/cpp/ch-1.cpp b/challenge-262/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..391ea1f286
--- /dev/null
+++ b/challenge-262/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,21 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+std::size_t max_positive_negative(const std::vector<T> &vec)
+{
+ std::size_t neg{},pos{};
+ for(const T& e : vec) if(e < 0) neg++; else pos++;
+ return static_cast<size_t>(std::max(neg,pos));
+}
+
+int main()
+{
+ std::vector<int> vec1{-3,1,2,-1,3,-2,4},
+ vec2{-1,-2,-3,1},vec3{1,2};
+ std::cout << max_positive_negative<int>(vec1) << '\n'
+ << max_positive_negative<int>(vec2) << '\n'
+ << max_positive_negative<int>(vec3) << '\n';
+ return 0;
+}
+
diff --git a/challenge-262/deadmarshal/cpp/ch-2.cpp b/challenge-262/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b5da7bc33c
--- /dev/null
+++ b/challenge-262/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,21 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+std::size_t count_equal_divisible(const std::vector<T> &vec,int k)
+{
+ size_t count{};
+ for(size_t i = 0; i < vec.size()-1; ++i)
+ for(size_t j = i+1; j < vec.size(); ++j)
+ if((vec.at(i) == vec.at(j)) && ((i*j) % k == 0)) count++;
+ return count;
+}
+
+int main()
+{
+ std::vector<int> vec1{3,1,2,2,2,1,3},vec2{1,2,3};
+ std::cout << count_equal_divisible<int>(vec1,2) << '\n'
+ << count_equal_divisible<int>(vec2,1) << '\n';
+ return 0;
+}
+
diff --git a/challenge-262/deadmarshal/d/ch1.d b/challenge-262/deadmarshal/d/ch1.d
new file mode 100644
index 0000000000..63b96d4b4c
--- /dev/null
+++ b/challenge-262/deadmarshal/d/ch1.d
@@ -0,0 +1,20 @@
+import std.stdio:writeln;
+import std.algorithm:max;
+
+size_t max_positive_negative(int[] arr)
+{
+ size_t neg = 0, pos = 0;
+ foreach(ref e;arr) if(e < 0) neg++; else pos++;
+ return max(neg,pos);
+}
+
+void main()
+{
+ int[] arr1 = [-3,1,2,-1,3,-2,4];
+ int[] arr2 = [-1,-2,-3,1];
+ int[] arr3 = [1,2];
+ writeln(max_positive_negative(arr1));
+ writeln(max_positive_negative(arr2));
+ writeln(max_positive_negative(arr3));
+}
+
diff --git a/challenge-262/deadmarshal/d/ch2.d b/challenge-262/deadmarshal/d/ch2.d
new file mode 100644
index 0000000000..15f5611282
--- /dev/null
+++ b/challenge-262/deadmarshal/d/ch2.d
@@ -0,0 +1,21 @@
+import std.stdio:writeln;
+
+size_t count_equal_divisible(int[] arr, int k)
+{
+ size_t count = 0;
+ foreach(i;0..arr.length-2){
+ foreach(j;i+1..arr.length-1){
+ if((arr[i] == arr[j] && ((i*j) % k == 0))) ++count;
+ }
+ }
+ return count;
+}
+
+void main()
+{
+ int[] arr = [1,3,2,2,2,1,3];
+ int[] arr2 = [1,2,3];
+ writeln(count_equal_divisible(arr,2));
+ writeln(count_equal_divisible(arr2,1));
+}
+
diff --git a/challenge-262/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-262/deadmarshal/modula-3/ch1/src/Ch1.m3
new file mode 100644
index 0000000000..075d70c6a4
--- /dev/null
+++ b/challenge-262/deadmarshal/modula-3/ch1/src/Ch1.m3
@@ -0,0 +1,24 @@
+MODULE Ch1 EXPORTS Main;
+
+IMPORT SIO;
+
+VAR
+ A1:ARRAY[0..6] OF INTEGER := ARRAY OF INTEGER{-3,1,2,-1,3,-2,4};
+ A2:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{-1,-2,-3,1};
+ A3:ARRAY[0..1] OF INTEGER := ARRAY OF INTEGER{1,2};
+
+PROCEDURE MaxPositiveNegative(VAR A:ARRAY OF INTEGER):INTEGER =
+ VAR Neg,Pos:INTEGER := 0;
+ BEGIN
+ FOR I := FIRST(A) TO LAST(A) DO
+ IF A[I] < 0 THEN INC(Neg) ELSE INC(Pos) END
+ END;
+ RETURN MAX(Neg,Pos)
+ END MaxPositiveNegative;
+
+BEGIN
+ SIO.PutInt(MaxPositiveNegative(A1)); SIO.Nl();
+ SIO.PutInt(MaxPositiveNegative(A2)); SIO.Nl();
+ SIO.PutInt(MaxPositiveNegative(A3)); SIO.Nl();
+END Ch1.
+
diff --git a/challenge-262/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-262/deadmarshal/modula-3/ch1/src/m3makefile
new file mode 100644
index 0000000000..9f66e4a51f
--- /dev/null
+++ b/challenge-262/deadmarshal/modula-3/ch1/src/m3makefile
@@ -0,0 +1,5 @@
+import("libm3")
+import("libsio")
+implementation("Ch1")
+program("ch1")
+
diff --git a/challenge-262/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-262/deadmarshal/modula-3/ch2/src/Ch2.m3
new file mode 100644
index 0000000000..e2cbd1349b
--- /dev/null
+++ b/challenge-262/deadmarshal/modula-3/ch2/src/Ch2.m3
@@ -0,0 +1,25 @@
+MODULE Ch2 EXPORTS Main;
+
+IMPORT SIO;
+
+VAR
+ A1:ARRAY[0..6] OF CARDINAL := ARRAY OF CARDINAL{3,1,2,2,2,1,3};
+ A2:ARRAY[0..2] OF CARDINAL := ARRAY OF CARDINAL{1,2,3};
+
+PROCEDURE CountEqualDivisible(VAR A:ARRAY OF CARDINAL;
+ READONLY K:CARDINAL):CARDINAL =
+ VAR Count:CARDINAL := 0;
+ BEGIN
+ FOR I := FIRST(A) TO LAST(A)-1 DO
+ FOR J := I+1 TO LAST(A) DO
+ IF (A[I] = A[J]) AND ((I*J) MOD K = 0) THEN INC(Count) END
+ END
+ END;
+ RETURN Count
+ END CountEqualDivisible;
+
+BEGIN
+ SIO.PutInt(CountEqualDivisible(A1,2)); SIO.Nl();
+ SIO.PutInt(CountEqualDivisible(A2,1)); SIO.Nl();
+END Ch2.
+
diff --git a/challenge-262/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-262/deadmarshal/modula-3/ch2/src/m3makefile
new file mode 100644
index 0000000000..798c627ef3
--- /dev/null
+++ b/challenge-262/deadmarshal/modula-3/ch2/src/m3makefile
@@ -0,0 +1,5 @@
+import("libm3")
+import("libsio")
+implementation("Ch2")
+program("ch2")
+
diff --git a/challenge-262/deadmarshal/perl/ch-1.pl b/challenge-262/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..fd05ba6a99
--- /dev/null
+++ b/challenge-262/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util qw(max);
+
+sub max_positive_negative{
+ my ($neg,$pos) = 0 x 2;
+ map{$neg++ if $_ < 0;$pos++ if $_ > 0}@{$_[0]};
+ max $neg,$pos
+}
+
+printf "%d\n",max_positive_negative([-3,1,2,-1,3,-2,4]);
+printf "%d\n",max_positive_negative([-1,-2,-3,1]);
+printf "%d\n",max_positive_negative([1,2]);
+
diff --git a/challenge-262/deadmarshal/perl/ch-2.pl b/challenge-262/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..d8068920d2
--- /dev/null
+++ b/challenge-262/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub count_equal_divisible{
+ my ($arr,$k) = @_;
+ my $count = 0;
+ foreach my $i(0..@$arr-2){
+ foreach my $j($i+1..@$arr-1){
+ ++$count if $arr->[$i] == $arr->[$j] && ($i*$j) % $k == 0
+ }
+ }
+ $count
+}
+
+printf "%d\n",count_equal_divisible([3,1,2,2,2,1,3],2);
+printf "%d\n",count_equal_divisible([1,2,3],1);
+