aboutsummaryrefslogtreecommitdiff
path: root/challenge-343
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-19 21:16:22 +0100
committerGitHub <noreply@github.com>2025-10-19 21:16:22 +0100
commit1b363b218726b8dda3de28481dc697d8d585bd3f (patch)
treea6b3d3dda8be45112a1bd2f082ba41da431dc103 /challenge-343
parent2a7340b4037ab852921bf0b804ed4cade0044dfe (diff)
parent03668e5a0aed210ee393b266aad4cef646816f44 (diff)
downloadperlweeklychallenge-club-1b363b218726b8dda3de28481dc697d8d585bd3f.tar.gz
perlweeklychallenge-club-1b363b218726b8dda3de28481dc697d8d585bd3f.tar.bz2
perlweeklychallenge-club-1b363b218726b8dda3de28481dc697d8d585bd3f.zip
Merge pull request #12870 from vinodk89/branch-for-challenge-342
Solutions for Challenge-343 (Perl and Python)
Diffstat (limited to 'challenge-343')
-rw-r--r--challenge-343/vinod-k/perl/ch-1.pl44
-rw-r--r--challenge-343/vinod-k/perl/ch-2.pl96
-rw-r--r--challenge-343/vinod-k/python/ch-1.py25
-rw-r--r--challenge-343/vinod-k/python/ch-2.py68
4 files changed, 233 insertions, 0 deletions
diff --git a/challenge-343/vinod-k/perl/ch-1.pl b/challenge-343/vinod-k/perl/ch-1.pl
new file mode 100644
index 0000000000..c880d9d202
--- /dev/null
+++ b/challenge-343/vinod-k/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub distance_to_zero {
+ my @nums = @_;
+
+ if (grep { $_ == 0 } @nums) {
+ print "Values closest to 0: 0 (distance = 0)\n";
+ print "Exact zero wins regardless of other close values.\n";
+ return 0;
+ }
+
+ my $min_distance;
+ foreach my $num (@nums) {
+ my $abs_val = abs($num);
+ if (!defined $min_distance || $abs_val < $min_distance) {
+ $min_distance = $abs_val;
+ }
+ }
+
+ my @closest = grep { abs($_) == $min_distance } @_;
+
+ print 'Values closest to 0: '
+ . join(' and ', @closest)
+ . ' (distance = '
+ . join(' and ', map { abs($_) } @closest)
+ . ")\n";
+
+ return $min_distance;
+}
+
+my @tests = (
+ [4, 2, -1, 3, -2],
+ [-5, 5, -3, 3, -1, 1],
+ [7, -3, 0, 2, -8],
+ [-2, -5, -1, -8],
+ [-2, 2, -4, 4, -1, 1],
+);
+
+foreach my $case (@tests) {
+ print "Input: \@nums = (" . join(', ', @$case) . ")\n";
+ print 'Output: ' . distance_to_zero(@$case) . "\n\n";
+}
diff --git a/challenge-343/vinod-k/perl/ch-2.pl b/challenge-343/vinod-k/perl/ch-2.pl
new file mode 100644
index 0000000000..5bfb17e20a
--- /dev/null
+++ b/challenge-343/vinod-k/perl/ch-2.pl
@@ -0,0 +1,96 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub find_champion {
+ my @grid = @_;
+ my $n = scalar @grid;
+
+ my @wins = (0) x $n;
+ my @beaten = ();
+
+ for my $i (0..$n-1) {
+ my @beat_teams = ();
+ for my $j (0..$n-1) {
+ if ($grid[$i][$j] == 1) {
+ $wins[$i]++;
+ push @beat_teams, $j;
+ }
+ }
+ $beaten[$i] = \@beat_teams;
+ }
+
+ for my $i (0..$n-1) {
+ my $beat_list = join ', ', map { "Team $_" } @{$beaten[$i]};
+ if ($beat_list eq '') {
+ print "[" . join(", ", @{$grid[$i]}) . "] => Team $i loses to all\n";
+ } else {
+ print "[" . join(", ", @{$grid[$i]}) . "] => Team $i beats $beat_list\n";
+ }
+ }
+
+ my $max_wins = 0;
+ for my $w (@wins) {
+ $max_wins = $w if $w > $max_wins;
+ }
+
+ my @max_win_teams = grep { $wins[$_] == $max_wins } 0..$n-1;
+
+ my $champion;
+ if (scalar @max_win_teams == 1) {
+ $champion = $max_win_teams[0];
+ } else {
+ foreach my $team (@max_win_teams) {
+ my $is_strongest = 1;
+ foreach my $other (@max_win_teams) {
+ next if $team == $other;
+ if ($grid[$team][$other] == 0) {
+ $is_strongest = 0;
+ last;
+ }
+ }
+ if ($is_strongest) {
+ $champion = $team;
+ last;
+ }
+ }
+ }
+ print "\nOutput: Team $champion\n\n";
+}
+
+my @tests = (
+ [
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0],
+ ],
+ [
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0],
+ ],
+ [
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0],
+ ],
+ [
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0],
+ ],
+ [
+ [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],
+ ],
+);
+
+for my $idx (0..$#tests) {
+ print "Example ", $idx+1, ":\n";
+ find_champion(@{$tests[$idx]});
+}
diff --git a/challenge-343/vinod-k/python/ch-1.py b/challenge-343/vinod-k/python/ch-1.py
new file mode 100644
index 0000000000..3eafd714cf
--- /dev/null
+++ b/challenge-343/vinod-k/python/ch-1.py
@@ -0,0 +1,25 @@
+def distance_to_zero(nums):
+ if 0 in nums:
+ print("Values closest to 0: 0 (distance = 0)")
+ print("Exact zero wins regardless of other close values.")
+ return 0
+
+ min_distance = min(abs(num) for num in nums)
+ closest = [num for num in nums if abs(num) == min_distance]
+
+ print('Values closest to 0: ' + ' and '.join(map(str, closest))
+ + ' (distance = ' + ' and '.join(str(abs(num)) for num in closest) + ')')
+ return min_distance
+
+# Test inputs
+tests = [
+ [4, 2, -1, 3, -2],
+ [-5, 5, -3, 3, -1, 1],
+ [7, -3, 0, 2, -8],
+ [-2, -5, -1, -8],
+ [-2, 2, -4, 4, -1, 1],
+]
+
+for case in tests:
+ print(f"Input: @nums = ({', '.join(map(str, case))})")
+ print('Output: ' + str(distance_to_zero(case)) + "\n")
diff --git a/challenge-343/vinod-k/python/ch-2.py b/challenge-343/vinod-k/python/ch-2.py
new file mode 100644
index 0000000000..da19c615a6
--- /dev/null
+++ b/challenge-343/vinod-k/python/ch-2.py
@@ -0,0 +1,68 @@
+def find_champion(grid):
+ n = len(grid)
+
+ wins = [0] * n
+ beaten = [[] for _ in range(n)]
+
+ for i in range(n):
+ for j in range(n):
+ if grid[i][j] == 1:
+ wins[i] += 1
+ beaten[i].append(j)
+
+ for i in range(n):
+ if beaten[i]:
+ beat_list = ', '.join(f"Team {team}" for team in beaten[i])
+ print(f"[{', '.join(map(str, grid[i]))}] => Team {i} beats {beat_list}")
+ else:
+ print(f"[{', '.join(map(str, grid[i]))}] => Team {i} loses to all")
+
+ max_wins = max(wins)
+ max_win_teams = [i for i, w in enumerate(wins) if w == max_wins]
+
+ if len(max_win_teams) == 1:
+ champion = max_win_teams[0]
+ else:
+ champion = None
+ for team in max_win_teams:
+ if all(grid[team][other] == 1 or team == other for other in max_win_teams):
+ champion = team
+ break
+
+ print(f"\nOutput: Team {champion}\n")
+
+tests = [
+ [
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0],
+ ],
+ [
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0],
+ ],
+ [
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0],
+ ],
+ [
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0],
+ ],
+ [
+ [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],
+ ],
+]
+
+for idx, test in enumerate(tests, start=1):
+ print(f"Example {idx}:")
+ find_champion(test)