[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 6์ฃผ์ฐจ : ๋ณต์„œ ์ •๋ ฌํ•˜๊ธฐ

2021. 9. 7. 20:07

๋ฌธ์ œ ์„ค๋ช…

๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ weights์™€, ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ์ „์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” head2head๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ํ›„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

  1. ์ „์ฒด ์Šน๋ฅ ์ด ๋†’์€ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค. ์•„์ง ๋‹ค๋ฅธ ๋ณต์„œ๋ž‘ ๋ถ™์–ด๋ณธ ์ ์ด ์—†๋Š” ๋ณต์„œ์˜ ์Šน๋ฅ ์€ 0%๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
  2. ์Šน๋ฅ ์ด ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž์‹ ๋ณด๋‹ค ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.
  3. ์ž์‹ ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๊นŒ์ง€ ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž๊ธฐ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋ฌด๊ฑฐ์šด ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.
  4. ์ž๊ธฐ ๋ชธ๋ฌด๊ฒŒ๊นŒ์ง€ ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž‘์€ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.

 

์ œํ•œ์‚ฌํ•ญ

  • weights์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • weights์˜ ๋ชจ๋“  ๊ฐ’์€ 45 ์ด์ƒ 150 ์ดํ•˜์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    • weights[i] ๋Š” i+1๋ฒˆ ๋ณต์„œ์˜ ๋ชธ๋ฌด๊ฒŒ(kg)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • head2head์˜ ๊ธธ์ด๋Š” weights์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • head2head์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์€ ๊ธธ์ด๊ฐ€ weights์˜ ๊ธธ์ด์™€ ๋™์ผํ•˜๋ฉฐ, 'N', 'W', 'L'๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • head2head[i] ๋Š” i+1๋ฒˆ ๋ณต์„œ์˜ ์ „์ ์„ ์˜๋ฏธํ•˜๋ฉฐ, head2head[i][j]๋Š” i+1๋ฒˆ ๋ณต์„œ์™€ j+1๋ฒˆ ๋ณต์„œ์˜ ๋งค์น˜ ๊ฒฐ๊ณผ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'N' (None)์€ ๋‘ ๋ณต์„œ๊ฐ€ ์•„์ง ๋ถ™์–ด๋ณธ ์ ์ด ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'W' (Win)๋Š” i+1๋ฒˆ ๋ณต์„œ๊ฐ€ j+1๋ฒˆ ๋ณต์„œ๋ฅผ ์ด๊ฒผ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'L' (Lose)๋Š” i+1๋ฒˆ ๋ณต์‚ฌ๊ฐ€ j+1๋ฒˆ ๋ณต์„œ์—๊ฒŒ ์กŒ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i์— ๋Œ€ํ•ด์„œ head2head[i][i] ๋Š” ํ•ญ์ƒ 'N'์ž…๋‹ˆ๋‹ค. ์ž๊ธฐ ์ž์‹ ๊ณผ ์‹ธ์šธ ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'W' ์ด๋ฉด, head2head[j][i] = 'L'์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'L' ์ด๋ฉด, head2head[j][i] = 'W'์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'N' ์ด๋ฉด, head2head[j][i] = 'N'์ž…๋‹ˆ๋‹ค.

 

์ •๋‹ต

def solution(weights, head2head):
    # ์„ ์ˆ˜๋ฒˆํ˜ธ : [์Šน๋ฆฌ ํšŸ์ˆ˜(๋‚˜์ค‘์— ์Šน๋ฅ ๋กœ ๋ฐ”๋€œ), ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ ์Šน๋ฆฌ ํšŸ์ˆ˜, ์ž๊ธฐ ๋ชธ๋ฌด๊ฒŒ]
    player = { i+1:[0.0, 0, weights[i]] for i in range(len(weights)) }   

    for idx, score in enumerate(head2head):
        cnt = 0
        for i, s in enumerate(score):
            if s != "N": cnt += 1   # ๋Œ€๊ฒฐ ํšŸ์ˆ˜ -1
            if s == "W":
                player[idx+1][0] += 1   # ์Šน๋ฆฌ ํšŸ์ˆ˜ +1
                if weights[idx] < weights[i]:
                    player[idx+1][1] += 1   # ๋” ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ ์Šน๋ฆฌ ํšŸ์ˆ˜ +1
        player[idx+1][0] = player[idx+1][0] / cnt * 100 if cnt != 0 else 0

    player = sorted(player.items(), key=lambda x:(x[1], -x[0]), reverse=True)
    answer = [i[0] for i in player]

    return answer

๐Ÿ’ฅ (์Šน๋ฆฌ ํšŸ์ˆ˜ / ๊ฒฝ๊ธฐ ํšŸ์ˆ˜) != ์Šน๋ฅ 

๐Ÿ’ฅ (์Šน๋ฆฌ ํšŸ์ˆ˜ / (๊ฒฝ๊ธฐ ํšŸ์ˆ˜ - ๋น„๊ธด ํšŸ์ˆ˜)) == ์Šน๋ฅ 

๐Ÿ’ฅ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋” ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์„ธ์•ผ์ง€ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๊ฐ™์€ ๋ณต์„œ๋ฅผ ํฌํ•จํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

๐Ÿ’ฅ ์Šน๋ฅ  ๊ตฌํ•  ๋•Œ 0์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ERROR

๐Ÿ’ฅ ๋žŒ๋‹ค์‹์—์„œ ๊ธฐ์ค€์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์ฃผ๊ธฐ

๐Ÿ’ฅ ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ์ด ์„ž์—ฌ์žˆ๋‹ค๋ฉด -๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

728x90

BELATED ARTICLES

more