diff options
| -rw-r--r-- | challenge-337/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-337/deadmarshal/java/Ch1.java | 29 | ||||
| -rw-r--r-- | challenge-337/deadmarshal/java/Ch2.java | 25 | ||||
| -rw-r--r-- | challenge-337/deadmarshal/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-337/deadmarshal/perl/ch-2.pl | 31 |
5 files changed, 109 insertions, 0 deletions
diff --git a/challenge-337/deadmarshal/blog.txt b/challenge-337/deadmarshal/blog.txt new file mode 100644 index 0000000000..fa3eb4309f --- /dev/null +++ b/challenge-337/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/09/twc337.html diff --git a/challenge-337/deadmarshal/java/Ch1.java b/challenge-337/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..1ef262c742 --- /dev/null +++ b/challenge-337/deadmarshal/java/Ch1.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(Arrays.toString(smaller_than_current(new int[]{6, 5, 4, 8}))); + System.out.println(Arrays.toString(smaller_than_current(new int[]{7, 7, 7, 7}))); + System.out.println(Arrays.toString(smaller_than_current(new int[]{5, 4, 3 + , 2, 1}))); + System.out.println(Arrays.toString(smaller_than_current(new int[]{-1, 0, 3, -2, 1}))); + System.out.println(Arrays.toString(smaller_than_current(new int[]{0, 1, 1, 2, 0}))); + } + + private static int[] smaller_than_current(int[] nums) { + int[] arr = nums.clone(); + Arrays.sort(arr); + for (int i = 0; i < nums.length; ++i) nums[i] = search(arr, nums[i]); + return nums; + } + + private static int search(int[] nums, int x) { + int l = 0, r = nums.length; + while (l < r) { + int mid = (l + r) / 2; + if (nums[mid] >= x) r = mid; + else l = mid + 1; + } + return l; + } +} diff --git a/challenge-337/deadmarshal/java/Ch2.java b/challenge-337/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..0d13408113 --- /dev/null +++ b/challenge-337/deadmarshal/java/Ch2.java @@ -0,0 +1,25 @@ +public class Ch2 { + public static void main(String[] args) { + System.out.println(odd_matrix(2, 3, new int[][]{{0, 1}, {1, 1}})); + System.out.println(odd_matrix(2, 2, new int[][]{{1, 1}, {0, 0}})); + System.out.println(odd_matrix(3, 3, new int[][]{{0, 0}, {1, 2}, {2, 1}})); + System.out.println(odd_matrix(1, 5, new int[][]{{0, 2}, {0, 4}})); + System.out.println(odd_matrix(4, 2, new int[][]{{1, 0}, {3, 1}, {2, 0}, {0, 1}})); + } + + private static int odd_matrix(int row, int col, int[][] m) { + int res = 0; + int[][] g = new int[row][col]; + for (int[] e : m) { + int r = e[0], c = e[1]; + for (int i = 0; i < row; ++i) g[i][c]++; + for (int j = 0; j < col; ++j) g[r][j]++; + } + for (int[] rows : g) { + for (int v : rows) { + res += v % 2; + } + } + return res; + } +} diff --git a/challenge-337/deadmarshal/perl/ch-1.pl b/challenge-337/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..a990c504be --- /dev/null +++ b/challenge-337/deadmarshal/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; +use List::Util qw(sum0); +use List::MoreUtils qw(frequency); + +sub smaller_than_current{ + my %h = frequency(@{$_[0]}); + my @arr; + foreach my $i(0..$#{$_[0]}) { + my %h2 = %h{grep{$_ <= $_[0]->[$i]} keys %h}; + @arr[$i] = sum0(values %h2) - 1 + } + @arr +} + +print show smaller_than_current([6,5,4,8]); +print show smaller_than_current([7,7,7,7]); +print show smaller_than_current([5,4,3,2,1]); +print show smaller_than_current([-1,0,3,-2,1]); +print show smaller_than_current([0,1,1,2,0]); + diff --git a/challenge-337/deadmarshal/perl/ch-2.pl b/challenge-337/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..105659ff8b --- /dev/null +++ b/challenge-337/deadmarshal/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub odd_matrix{ + my ($row,$col,$m) = @_; + my @g; + push @g,[(0) x $col] foreach(0..$row-1); + foreach my $p(@$m){ + foreach(0..$col-1) { + ++$g[$p->[0]]->[$_] + } + foreach(0..$row-1){ + ++$g[$_]->[$p->[1]] + } + } + my $res = 0; + foreach my $i(0..$row-1){ + foreach my $j(0..$col-1) { + $res++ if $g[$i][$j] % 2 + } + } + $res +} + +printf "%d\n", odd_matrix(2,3,[[0,1],[1,1]]); +printf "%d\n", odd_matrix(2,2,[[1,1],[0,0]]); +printf "%d\n", odd_matrix(3,3,[[0,0],[1,2],[2,1]]); +printf "%d\n", odd_matrix(1,5,[[0,2],[0,4]]); +printf "%d\n", odd_matrix(4,2,[[1,0],[3,1],[2,0],[0,1]]); + |
