aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-10-13 18:30:51 +0700
committerPok <pok@goyangi>2025-10-13 18:30:51 +0700
commit334fd7168805dd07237d3f9024c5705b5e717be6 (patch)
treeb05e256acea387b0dbc950ead7168fd98a3aaafd
parent33718ba7b8b13720e2590b6ff0cf48833e784b27 (diff)
downloadperlweeklychallenge-club-334fd7168805dd07237d3f9024c5705b5e717be6.tar.gz
perlweeklychallenge-club-334fd7168805dd07237d3f9024c5705b5e717be6.tar.bz2
perlweeklychallenge-club-334fd7168805dd07237d3f9024c5705b5e717be6.zip
pwc343 minor revise and tidy up
-rw-r--r--challenge-343/pokgopun/go/ch-2.go16
-rw-r--r--challenge-343/pokgopun/lua/ch-2.lua16
-rw-r--r--challenge-343/pokgopun/python/ch-2.py21
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