From 7624250e72f0928abf6aee43cf5b6e24e48f3653 Mon Sep 17 00:00:00 2001 From: Pok Date: Mon, 13 Oct 2025 14:21:58 +0700 Subject: pwc343 solution in python --- challenge-343/pokgopun/python/ch-1.py | 70 ++++++++++++++++ challenge-343/pokgopun/python/ch-2.py | 152 ++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 challenge-343/pokgopun/python/ch-1.py create mode 100644 challenge-343/pokgopun/python/ch-2.py diff --git a/challenge-343/pokgopun/python/ch-1.py b/challenge-343/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..18389cb57b --- /dev/null +++ b/challenge-343/pokgopun/python/ch-1.py @@ -0,0 +1,70 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +""" + +Task 1: Zero Friend + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of numbers. + + Find the number that is closest to zero and return its distance to + zero. + +Example 1 + +Input: @nums = (4, 2, -1, 3, -2) +Output: 1 + +Values closest to 0: -1 and 2 (distance = 1) + +Example 2 + +Input: @nums = (-5, 5, -3, 3, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Example 3 + +Input: @ums = (7, -3, 0, 2, -8) +Output: 0 + +Values closest to 0: 0 (distance = 0) +Exact zero wins regardless of other close values. + +Example 4 + +Input: @nums = (-2, -5, -1, -8) +Output: 1 + +Values closest to 0: -1 and -2 (distance = 1 and 2) + +Example 5 + +Input: @nums = (-2, 2, -4, 4, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Task 2: Champion Team +""" +### solution by pokgopun@gmail.com + +def zf(nums: list[int]) -> int: + return min(-e if e < 0 else e for e in nums) + +import unittest + +class TestZf(unittest.TestCase): + def test(self): + for inpt, otpt in { + (4, 2, -1, 3, -2): 1, + (-5, 5, -3, 3, -1, 1): 1, + (7, -3, 0, 2, -8): 0, + (-2, -5, -1, -8): 1, + (-2, 2, -4, 4, -1, 1): 1, + }.items(): + self.assertEqual(zf(inpt),otpt) + +unittest.main() diff --git a/challenge-343/pokgopun/python/ch-2.py b/challenge-343/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..7c5158fb03 --- /dev/null +++ b/challenge-343/pokgopun/python/ch-2.py @@ -0,0 +1,152 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +""" + +Task 2: Champion Team + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You have n teams in a tournament. A matrix grid tells you which team is + stronger between any two teams: +If grid[i][j] == 1, then team i is stronger than team j +If grid[i][j] == 0, then team j is stronger than team i + + Find the champion team - the one with most wins, or if there is no + single such team, the strongest of the teams with most wins. (You may + assume that there is a definite answer.) + +Example 1 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 1], + [0, 0, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 1] => Team 1 beats Team 2 +[0, 0, 0] => Team 2 loses to all + +Example 2 + +Input: @grid = ( + [0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0], + ) +Output: Team 3 + +[0, 1, 0, 0] => Team 0 beats only Team 1 +[0, 0, 0, 0] => Team 1 loses to all +[1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 +[1, 1, 1, 0] => Team 3 beats everyone + +Example 3 + +Input: @grid = ( + [0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0], + ) +Output: Team 0 + +[0, 1, 0, 1] => Team 0 beats teams 1 and 3 +[0, 0, 1, 1] => Team 1 beats teams 2 and 3 +[1, 0, 0, 0] => Team 2 beats team 0 +[0, 0, 1, 0] => Team 3 beats team 2 + +Of the teams with 2 wins, Team 0 beats team 1. + +Example 4 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 0], + [0, 1, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 0] => Team 1 loses to Team 2 +[0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 + +Example 5 + +Input: @grid = ( + [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], + ) +Output: Team 2 + +[0, 0, 0, 0, 0] => Team 0 loses to all +[1, 0, 0, 0, 0] => Team 1 beats only Team 0 +[1, 1, 0, 1, 1] => Team 2 beats everyone except self +[1, 1, 0, 0, 0] => Team 3 loses to Team 2 +[1, 1, 0, 1, 0] => Team 4 loses to Team 2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ct(grid: list[list[int]]) -> int: + ranks = sorted((sum(grid[i]),i) for i in range(len(grid))) ### list of tup where tup[1] is team_id and tup[0] is win count, asc sorted by win count + max = ranks[-1][0] ### max win count + tops = [ e[1] for e in ranks if e[0] == max ] ### list of top team_id that have max win cout + if len(tops) == 1: ### if only one team_id in the top team_id, return the team_id + return ranks[-1][1] + tops.sort() ### check the result between two teams in the top team and return the first team_id that wins + for i in tops: + for j in tops: + if grid[i][j] == 1: + return i + return None ### top teams cannot win each other + +import unittest + +class TestCt(unittest.TestCase): + def test(self): + for inpt, otpt in { + ( + (0, 1, 1), + (0, 0, 1), + (0, 0, 0), + ): 0, + ( + (0, 1, 0, 0), + (0, 0, 0, 0), + (1, 1, 0, 0), + (1, 1, 1, 0), + ): 3, + ( + (0, 1, 0, 1), + (0, 0, 1, 1), + (1, 0, 0, 0), + (0, 0, 1, 0), + ): 0, + ( + (0, 1, 1), + (0, 0, 0), + (0, 1, 0), + ): 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), + ): 2, + }.items(): + self.assertEqual(ct(inpt), otpt) + +unittest.main() -- cgit From f66527e2c96d4c61a5c72f8b2297d58dd595ea22 Mon Sep 17 00:00:00 2001 From: Pok Date: Mon, 13 Oct 2025 16:07:24 +0700 Subject: pwc343 solution in go --- challenge-343/pokgopun/go/ch-1.go | 103 ++++++++++++++++++++ challenge-343/pokgopun/go/ch-2.go | 199 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 challenge-343/pokgopun/go/ch-1.go create mode 100644 challenge-343/pokgopun/go/ch-2.go diff --git a/challenge-343/pokgopun/go/ch-1.go b/challenge-343/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..72fa8b44fb --- /dev/null +++ b/challenge-343/pokgopun/go/ch-1.go @@ -0,0 +1,103 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +/*# + +Task 1: Zero Friend + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of numbers. + + Find the number that is closest to zero and return its distance to + zero. + +Example 1 + +Input: @nums = (4, 2, -1, 3, -2) +Output: 1 + +Values closest to 0: -1 and 2 (distance = 1) + +Example 2 + +Input: @nums = (-5, 5, -3, 3, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Example 3 + +Input: @ums = (7, -3, 0, 2, -8) +Output: 0 + +Values closest to 0: 0 (distance = 0) +Exact zero wins regardless of other close values. + +Example 4 + +Input: @nums = (-2, -5, -1, -8) +Output: 1 + +Values closest to 0: -1 and -2 (distance = 1 and 2) + +Example 5 + +Input: @nums = (-2, 2, -4, 4, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Task 2: Champion Team +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type nums []int + +func (ns nums) process() int { + var mn int + for i, v := range ns { + if i == 0 { + if v == 0 { + return 0 + } else if v < 0 { + mn = -v + } else { + mn = v + } + continue + } + if v == 0 { + return 0 + } else if v < 0 { + v = -v + } + if mn > v { + mn = v + } + } + return mn +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{4, 2, -1, 3, -2}, 1}, + {nums{-5, 5, -3, 3, -1, 1}, 1}, + {nums{7, -3, 0, 2, -8}, 0}, + {nums{-2, -5, -1, -8}, 1}, + {nums{-2, 2, -4, 4, -1, 1}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) //blank if ok, otherwise show the difference + } +} diff --git a/challenge-343/pokgopun/go/ch-2.go b/challenge-343/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..deac6f2bac --- /dev/null +++ b/challenge-343/pokgopun/go/ch-2.go @@ -0,0 +1,199 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +/*# + +Task 2: Champion Team + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You have n teams in a tournament. A matrix grid tells you which team is + stronger between any two teams: +If grid[i][j] == 1, then team i is stronger than team j +If grid[i][j] == 0, then team j is stronger than team i + + Find the champion team - the one with most wins, or if there is no + single such team, the strongest of the teams with most wins. (You may + assume that there is a definite answer.) + +Example 1 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 1], + [0, 0, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 1] => Team 1 beats Team 2 +[0, 0, 0] => Team 2 loses to all + +Example 2 + +Input: @grid = ( + [0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0], + ) +Output: Team 3 + +[0, 1, 0, 0] => Team 0 beats only Team 1 +[0, 0, 0, 0] => Team 1 loses to all +[1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 +[1, 1, 1, 0] => Team 3 beats everyone + +Example 3 + +Input: @grid = ( + [0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0], + ) +Output: Team 0 + +[0, 1, 0, 1] => Team 0 beats teams 1 and 3 +[0, 0, 1, 1] => Team 1 beats teams 2 and 3 +[1, 0, 0, 0] => Team 2 beats team 0 +[0, 0, 1, 0] => Team 3 beats team 2 + +Of the teams with 2 wins, Team 0 beats team 1. + +Example 4 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 0], + [0, 1, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 0] => Team 1 loses to Team 2 +[0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 + +Example 5 + +Input: @grid = ( + [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], + ) +Output: Team 2 + +[0, 0, 0, 0, 0] => Team 0 loses to all +[1, 0, 0, 0, 0] => Team 1 beats only Team 0 +[1, 1, 0, 1, 1] => Team 2 beats everyone except self +[1, 1, 0, 0, 0] => Team 3 loses to Team 2 +[1, 1, 0, 1, 0] => Team 4 loses to Team 2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) sum() int { + sm := 0 + for _, v := range in { + sm += v + } + return sm +} + +type grid []ints + +func (gd grid) process() int { + mx := 0 + rank := make(ints, len(gd)) + for i, rw := range gd { + v := rw.sum() + rank[i] = v + if mx < v { + mx = v + } + } + var top ints + for i, v := range rank { + if v == mx { + top = append(top, i) + } + } + l := len(top) + if l == 1 { + return top[0] + } + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + a, b := top[i], top[j] + if gd[a][b] == 1 { + return a + } + if gd[b][a] == 1 { + return b + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + input grid + output int + }{ + { + grid{ + ints{0, 1, 1}, + ints{0, 0, 1}, + ints{0, 0, 0}, + }, 0}, + { + grid{ + ints{0, 1, 0, 0}, + ints{0, 0, 0, 0}, + ints{1, 1, 0, 0}, + ints{1, 1, 1, 0}, + }, 3}, + { + grid{ + ints{0, 1, 0, 1}, + ints{0, 0, 1, 1}, + ints{1, 0, 0, 0}, + ints{0, 0, 1, 0}, + }, 0}, + { + grid{ + ints{0, 1, 1}, + ints{0, 0, 0}, + ints{0, 1, 0}, + }, 0}, + { + grid{ + ints{0, 0, 0, 0, 0}, + ints{1, 0, 0, 0, 0}, + ints{1, 1, 0, 1, 1}, + ints{1, 1, 0, 0, 0}, + ints{1, 1, 0, 1, 0}, + }, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} -- cgit From 33718ba7b8b13720e2590b6ff0cf48833e784b27 Mon Sep 17 00:00:00 2001 From: Pok Date: Mon, 13 Oct 2025 18:06:03 +0700 Subject: pwc343 solution in lua --- challenge-343/pokgopun/lua/ch-1.lua | 94 +++++++++++++++++++ challenge-343/pokgopun/lua/ch-2.lua | 175 ++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 challenge-343/pokgopun/lua/ch-1.lua create mode 100644 challenge-343/pokgopun/lua/ch-2.lua diff --git a/challenge-343/pokgopun/lua/ch-1.lua b/challenge-343/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..7b7d4db732 --- /dev/null +++ b/challenge-343/pokgopun/lua/ch-1.lua @@ -0,0 +1,94 @@ +#!/usr/bin/env lua5.4 +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +--[[ + +Task 1: Zero Friend + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of numbers. + + Find the number that is closest to zero and return its distance to + zero. + +Example 1 + +Input: @nums = (4, 2, -1, 3, -2) +Output: 1 + +Values closest to 0: -1 and 2 (distance = 1) + +Example 2 + +Input: @nums = (-5, 5, -3, 3, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Example 3 + +Input: @ums = (7, -3, 0, 2, -8) +Output: 0 + +Values closest to 0: 0 (distance = 0) +Exact zero wins regardless of other close values. + +Example 4 + +Input: @nums = (-2, -5, -1, -8) +Output: 1 + +Values closest to 0: -1 and -2 (distance = 1 and 2) + +Example 5 + +Input: @nums = (-2, 2, -4, 4, -1, 1) +Output: 1 + +Values closest to 0: -1 and 1 (distance = 1) + +Task 2: Champion Team +--]] +--# solution by pokgopun@gmail.com + +local function zf(nums) + local mn = 0 + for i, v in ipairs(nums) do + if i == 1 then + if v == 0 then + return 0 + elseif v < 0 then + mn = -v + else + mn = v + end + else + if v == 0 then + return 0 + elseif v < 0 then + v = -v + end + if mn > v then + mn = v + end + end + end + return mn +end + +local lu = require("luaunit") + +function TestZf() + for _, data in ipairs({ + {Input = {4, 2, -1, 3, -2},Output= 1}, + {Input = {-5, 5, -3, 3, -1, 1},Output= 1}, + {Input = {7, -3, 0, 2, -8},Output= 0}, + {Input = {-2, -5, -1, -8},Output= 1}, + {Input = {-2, 2, -4, 4, -1, 1},Output= 1}}) do + --print(table.concat(data.Input," ") .. " : " .. data.Output) + lu.assertEquals(zf(data.Input),data.Output) + end +end + +lu.run() diff --git a/challenge-343/pokgopun/lua/ch-2.lua b/challenge-343/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..bd124d44a2 --- /dev/null +++ b/challenge-343/pokgopun/lua/ch-2.lua @@ -0,0 +1,175 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-343/ +--[[ + +Task 2: Champion Team + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You have n teams in a tournament. A matrix grid tells you which team is + stronger between any two teams: +If grid[i][j] == 1, then team i is stronger than team j +If grid[i][j] == 0, then team j is stronger than team i + + Find the champion team - the one with most wins, or if there is no + single such team, the strongest of the teams with most wins. (You may + assume that there is a definite answer.) + +Example 1 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 1], + [0, 0, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 1] => Team 1 beats Team 2 +[0, 0, 0] => Team 2 loses to all + +Example 2 + +Input: @grid = ( + [0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0], + ) +Output: Team 3 + +[0, 1, 0, 0] => Team 0 beats only Team 1 +[0, 0, 0, 0] => Team 1 loses to all +[1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 +[1, 1, 1, 0] => Team 3 beats everyone + +Example 3 + +Input: @grid = ( + [0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0], + ) +Output: Team 0 + +[0, 1, 0, 1] => Team 0 beats teams 1 and 3 +[0, 0, 1, 1] => Team 1 beats teams 2 and 3 +[1, 0, 0, 0] => Team 2 beats team 0 +[0, 0, 1, 0] => Team 3 beats team 2 + +Of the teams with 2 wins, Team 0 beats team 1. + +Example 4 + +Input: @grid = ( + [0, 1, 1], + [0, 0, 0], + [0, 1, 0], + ) +Output: Team 0 + +[0, 1, 1] => Team 0 beats Team 1 and Team 2 +[0, 0, 0] => Team 1 loses to Team 2 +[0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 + +Example 5 + +Input: @grid = ( + [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], + ) +Output: Team 2 + +[0, 0, 0, 0, 0] => Team 0 loses to all +[1, 0, 0, 0, 0] => Team 1 beats only Team 0 +[1, 1, 0, 1, 1] => Team 2 beats everyone except self +[1, 1, 0, 0, 0] => Team 3 loses to Team 2 +[1, 1, 0, 1, 0] => Team 4 loses to Team 2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +local function sum(nums) + local sm = 0 + for _, v in ipairs(nums) do + sm = sm + v + end + return sm +end + +local function ct(grid) + local mx, score = 0, {} + for i, nums in ipairs(grid) do + local sm = sum(nums) + table.insert(score, sm) + if mx < sm then + mx = sm + end + end + local top = {} + for i, v in ipairs(score) do + if v == mx then + table.insert(top, i) + end + end + local l = #top + if l == 1 then + return top[1] - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 + end + for i=1, l-1 do + for j=i+1, l do + local a, b = top[i], top[j] + if grid[a][b] == 1 then + return a - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 + end + if grid[b][a] == 1 then + return b - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 + end + end + end + return -1 +end + +local lu = require("luaunit") + +function TestCt() + for _, data in ipairs({ + {Input = { + {0, 1, 1}, + {0, 0, 1}, + {0, 0, 0}}, Output= 0}, + {Input = { + {0, 1, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 0, 0}, + {1, 1, 1, 0}},Output= 3}, + {Input = { + {0, 1, 0, 1}, + {0, 0, 1, 1}, + {1, 0, 0, 0}, + {0, 0, 1, 0}},Output= 0}, + {Input = { + {0, 1, 1}, + {0, 0, 0}, + {0, 1, 0}},Output= 0}, + {Input = { + {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}},Output= 2}}) do + lu.assertEquals(ct(data.Input), data.Output) + end + end + + lu.run() -- cgit From 334fd7168805dd07237d3f9024c5705b5e717be6 Mon Sep 17 00:00:00 2001 From: Pok Date: Mon, 13 Oct 2025 18:30:51 +0700 Subject: pwc343 minor revise and tidy up --- challenge-343/pokgopun/go/ch-2.go | 16 ++++++++-------- challenge-343/pokgopun/lua/ch-2.lua | 16 ++++++++-------- challenge-343/pokgopun/python/ch-2.py | 21 ++++++++++++--------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/challenge-343/pokgopun/go/ch-2.go b/challenge-343/pokgopun/go/ch-2.go index deac6f2bac..cac9359d34 100644 --- a/challenge-343/pokgopun/go/ch-2.go +++ b/challenge-343/pokgopun/go/ch-2.go @@ -122,27 +122,27 @@ type grid []ints func (gd grid) process() int { mx := 0 - rank := make(ints, len(gd)) + scores := make(ints, len(gd)) for i, rw := range gd { v := rw.sum() - rank[i] = v + scores[i] = v if mx < v { mx = v } } - var top ints - for i, v := range rank { + var top_ids ints + for i, v := range scores { if v == mx { - top = append(top, i) + top_ids = append(top_ids, i) } } - l := len(top) + l := len(top_ids) if l == 1 { - return top[0] + return top_ids[0] } for i := 0; i < l-1; i++ { for j := i + 1; j < l; j++ { - a, b := top[i], top[j] + a, b := top_ids[i], top_ids[j] if gd[a][b] == 1 { return a } diff --git a/challenge-343/pokgopun/lua/ch-2.lua b/challenge-343/pokgopun/lua/ch-2.lua index bd124d44a2..8d2824e335 100644 --- a/challenge-343/pokgopun/lua/ch-2.lua +++ b/challenge-343/pokgopun/lua/ch-2.lua @@ -108,27 +108,27 @@ local function sum(nums) end local function ct(grid) - local mx, score = 0, {} + local mx, scores = 0, {} for i, nums in ipairs(grid) do local sm = sum(nums) - table.insert(score, sm) + table.insert(scores, sm) if mx < sm then mx = sm end end - local top = {} - for i, v in ipairs(score) do + local top_ids = {} + for i, v in ipairs(scores) do if v == mx then - table.insert(top, i) + table.insert(top_ids, i) end end - local l = #top + local l = #top_ids if l == 1 then - return top[1] - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 + return top_ids[1] - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 end for i=1, l-1 do for j=i+1, l do - local a, b = top[i], top[j] + local a, b = top_ids[i], top_ids[j] if grid[a][b] == 1 then return a - 1 -- minus-one-offset as lua's collection start with index#1 while the challenge assumes more general index#0 end diff --git a/challenge-343/pokgopun/python/ch-2.py b/challenge-343/pokgopun/python/ch-2.py index 7c5158fb03..3672f6153d 100644 --- a/challenge-343/pokgopun/python/ch-2.py +++ b/challenge-343/pokgopun/python/ch-2.py @@ -101,15 +101,18 @@ SO WHAT DO YOU THINK ? def ct(grid: list[list[int]]) -> int: ranks = sorted((sum(grid[i]),i) for i in range(len(grid))) ### list of tup where tup[1] is team_id and tup[0] is win count, asc sorted by win count - max = ranks[-1][0] ### max win count - tops = [ e[1] for e in ranks if e[0] == max ] ### list of top team_id that have max win cout - if len(tops) == 1: ### if only one team_id in the top team_id, return the team_id - return ranks[-1][1] - tops.sort() ### check the result between two teams in the top team and return the first team_id that wins - for i in tops: - for j in tops: - if grid[i][j] == 1: - return i + mx = ranks[-1][0] ### max win count + top_ids = [ e[1] for e in ranks if e[0] == mx ] ### list of top team_id that have max win cout + l = len(top_ids) + if l == 1: ### if only one team_id in the top team_id, return the team_id + return top_ids[0] + for i in range(l-1): + for j in range(i+1,l): + a, b = top_ids[i], top_ids[j] + if grid[a][b] == 1: + return a + if grid[b][a] == 1: + return b return None ### top teams cannot win each other import unittest -- cgit