diff options
| author | Ali <adeadmarshal@gmail.com> | 2025-10-13 14:03:49 +0330 |
|---|---|---|
| committer | Ali <adeadmarshal@gmail.com> | 2025-10-13 14:03:49 +0330 |
| commit | 0bcccf52bb7367d0b2dedd1d4f2f79ee971e22b7 (patch) | |
| tree | 31fa02f44ab09bede0a996aa780a3bb39fb6c718 | |
| parent | 35a27e16cb743acd46c1f315766530dc991ac587 (diff) | |
| download | perlweeklychallenge-club-0bcccf52bb7367d0b2dedd1d4f2f79ee971e22b7.tar.gz perlweeklychallenge-club-0bcccf52bb7367d0b2dedd1d4f2f79ee971e22b7.tar.bz2 perlweeklychallenge-club-0bcccf52bb7367d0b2dedd1d4f2f79ee971e22b7.zip | |
TWC343
| -rw-r--r-- | challenge-343/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/go/ch1.go | 25 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/go/ch2.go | 36 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/java/Ch1.java | 17 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/java/Ch2.java | 32 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-343/deadmarshal/perl/ch-2.pl | 36 |
7 files changed, 163 insertions, 0 deletions
diff --git a/challenge-343/deadmarshal/blog.txt b/challenge-343/deadmarshal/blog.txt new file mode 100644 index 0000000000..e5d813c52a --- /dev/null +++ b/challenge-343/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/10/twc343.html diff --git a/challenge-343/deadmarshal/go/ch1.go b/challenge-343/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..2082293bea --- /dev/null +++ b/challenge-343/deadmarshal/go/ch1.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math" +) + +func zeroFriend(arr []int) int { + res := math.MaxInt + for _, e := range arr { + ae := int(math.Abs(float64(e))) + if ae < res { + res = ae + } + } + return res +} + +func main() { + fmt.Println(zeroFriend([]int{4, 2, -1, 3, -2})) + fmt.Println(zeroFriend([]int{-5, 5, -3, 3, -1, 1})) + fmt.Println(zeroFriend([]int{7, -3, 0, 2, -8})) + fmt.Println(zeroFriend([]int{-2, -5, -1, -8})) + fmt.Println(zeroFriend([]int{-2, 2, -4, 4, -1, 1})) +} diff --git a/challenge-343/deadmarshal/go/ch2.go b/challenge-343/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..020ee87f44 --- /dev/null +++ b/challenge-343/deadmarshal/go/ch2.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" +) + +func championTeam(grid [][]int) int { + m := 0 + h := map[int]int{} + for y := 0; y < len(grid); y++ { + for x := 0; x < len(grid[y]); x++ { + w := 0 + if grid[y][x] == 1 { + w = y + } else { + w = x + } + h[w]++ + if h[w] > h[m] { + m = w + } + } + } + return m +} + +func main() { + fmt.Println(championTeam([][]int{{0, 1, 1}, {0, 0, 1}, {0, 0, 0}})) + fmt.Println(championTeam([][]int{{0, 1, 0, 0}, {0, 0, 0, 0}, + {1, 1, 0, 0}, {1, 1, 1, 0}})) + fmt.Println(championTeam([][]int{{0, 1, 0, 1}, {0, 0, 1, 1}, + {1, 0, 0, 0}, {0, 0, 1, 0}})) + fmt.Println(championTeam([][]int{{0, 1, 1}, {0, 0, 0}, {0, 1, 0}})) + fmt.Println(championTeam([][]int{{0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, + {1, 1, 0, 1, 1}, {1, 1, 0, 0, 0}, {1, 1, 0, 1, 0}})) +} diff --git a/challenge-343/deadmarshal/java/Ch1.java b/challenge-343/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..28047983fa --- /dev/null +++ b/challenge-343/deadmarshal/java/Ch1.java @@ -0,0 +1,17 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println(zero_friend(new int[]{4,2,-1,3,-2})); + System.out.println(zero_friend(new int[]{-5,5,-3,3,-1,1})); + System.out.println(zero_friend(new int[]{7,-3,0,2,-8})); + System.out.println(zero_friend(new int[]{-2,-5,-1,-8})); + System.out.println(zero_friend(new int[]{-2,2,-4,4,-1,1})); + } + + private static int zero_friend(int[] arr) { + int res = Integer.MAX_VALUE; + for(int e : arr) + if(Math.abs(e) < res) res = Math.abs(e); + return res; + } +} + diff --git a/challenge-343/deadmarshal/java/Ch2.java b/challenge-343/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..fe4b609d79 --- /dev/null +++ b/challenge-343/deadmarshal/java/Ch2.java @@ -0,0 +1,32 @@ +import java.util.Map; +import java.util.HashMap; + +public class Ch2 { + public void main(String[] args) { + System.out.println( + champion_team(new int[][] { { 0, 1, 1 }, { 0, 0, 1 }, { 0, 0, 0 } })); + System.out.println(champion_team(new int[][] { { 0, 1, 0, 0 }, + { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 1, 0 } })); + System.out.println(champion_team(new int[][] { { 0, 1, 0, 1 }, + { 0, 0, 1, 1 }, { 1, 0, 0, 0 }, { 0, 0, 1, 0 } })); + System.out.println( + champion_team(new int[][] { { 0, 1, 1 }, { 0, 0, 0 }, { 0, 1, 0 } })); + System.out.println( + champion_team(new int[][] { { 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 }, + { 1, 1, 0, 1, 1 }, { 1, 1, 0, 0, 0 }, { 1, 1, 0, 1, 0 } })); + } + + private static int champion_team(int[][] grid) { + int m = 0; + Map<Integer,Integer> h = new HashMap<>(); + for(int y = 0; y < grid.length; ++y) { + for(int x = 0; x < grid[y].length; ++x) { + int w = grid[y][x] == 1 ? y : x; + h.put(w,h.getOrDefault(w,0) + 1); + if(h.get(w) > h.getOrDefault(m,0)) m = w; + } + } + return m; + } +} + diff --git a/challenge-343/deadmarshal/perl/ch-1.pl b/challenge-343/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..b34ec63a6f --- /dev/null +++ b/challenge-343/deadmarshal/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub zero_friend{ + my $zf = abs shift @{$_[0]}; + abs $_ < $zf and $zf = abs for @{$_[0]}; + $zf +} + +printf "%d\n",zero_friend([4,2,-1,3,-2]); +printf "%d\n",zero_friend([-5,5,-3,3,-1,1]); +printf "%d\n",zero_friend([7,-3,0,2,-8]); +printf "%d\n",zero_friend([-2,-5,-1,-8]); +printf "%d\n",zero_friend([-2,2,-4,4,-1,1]); + diff --git a/challenge-343/deadmarshal/perl/ch-2.pl b/challenge-343/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..bd3f0f40d7 --- /dev/null +++ b/challenge-343/deadmarshal/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub champion_team{ + my $m = 0; + my %h; + foreach my $y(0..$#{$_[0]}){ + foreach my $x(0..$#{$_[0]->[$y]}){ + my $w = $_[0]->[$y][$x] ? $y : $x; + $m = $w if ++$h{$w} > $h{$m} + } + } + $m +} + +printf "%d\n",champion_team([[0, 1, 1], + [0, 0, 1], + [0, 0, 0]]); +printf "%d\n",champion_team([[0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0]]); +printf "%d\n",champion_team([[0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0]]); +printf "%d\n",champion_team([[0, 1, 1], + [0, 0, 0], + [0, 1, 0]]); +printf "%d\n",champion_team([[0, 0, 0, 0, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 1, 1], + [1, 1, 0, 0, 0], + [1, 1, 0, 1, 0]]); + |
