๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ…Œ์ด๋ธ” ํ•ด์‹œ ํ•จ์ˆ˜

๐ŸŽฎinspirer9 2023. 2. 15. 19:49
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์™„ํ˜ธ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ•œ ํ…Œ์ด๋ธ”์€ ๋ชจ๋‘ ์ •์ˆ˜ ํƒ€์ž…์ธ ์ปฌ๋Ÿผ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์€ 2์ฐจ์› ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์—ด์€ ์ปฌ๋Ÿผ์„ ๋‚˜ํƒ€๋‚ด๊ณ , ํ–‰์€ ํŠœํ”Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
์ฒซ ๋ฒˆ์งธ ์ปฌ๋Ÿผ์€ ๊ธฐ๋ณธํ‚ค๋กœ์„œ ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด ๊ทธ ๊ฐ’์ด ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค. ์™„ํ˜ธ๋Š” ์ด ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  1. ํ•ด์‹œ ํ•จ์ˆ˜๋Š” col, row_begin, row_end์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  2. ํ…Œ์ด๋ธ”์˜ ํŠœํ”Œ์„ col๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•˜๋˜, ๋งŒ์•ฝ ๊ทธ ๊ฐ’์ด ๋™์ผํ•˜๋ฉด ๊ธฐ๋ณธํ‚ค์ธ ์ฒซ ๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  3. ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ์—์„œ S_i๋ฅผ i ๋ฒˆ์งธ ํ–‰์˜ ํŠœํ”Œ์— ๋Œ€ํ•ด ๊ฐ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ i ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋“ค์˜ ํ•ฉ์œผ๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  4. row_begin ≤ i ≤ row_end ์ธ ๋ชจ๋“  S_i๋ฅผ ๋ˆ„์ ํ•˜์—ฌ bitwise XOR ํ•œ ๊ฐ’์„ ํ•ด์‹œ ๊ฐ’์œผ๋กœ์„œ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ data์™€ ํ•ด์‹œ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ž…๋ ฅ col, row_begin, row_end์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ํ…Œ์ด๋ธ”์˜ ํ•ด์‹œ ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ data์˜ ๊ธธ์ด ≤ 2,500
  • 1 ≤ data์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 500
  • 1 ≤ data[i][j] ≤ 1,000,000
    • data[i][j]๋Š” i + 1 ๋ฒˆ์งธ ํŠœํ”Œ์˜ j + 1 ๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • 1 ≤ col ≤ data์˜ ์›์†Œ์˜ ๊ธธ์ด
  • 1 ≤ row_begin  row_end  data์˜ ๊ธธ์ด

์ž…์ถœ๋ ฅ ์˜ˆ

data col row_begin row_end result
[[2,2,6],[1,5,10],[4,2,9],[3,8,3]] 2 2 3 4

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

  • ์ •ํ•ด์ง„ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ํŠœํ”Œ์„ ์ •๋ ฌํ•˜๋ฉด {4, 2, 9}, {2, 2, 6}, {1, 5, 10}, {3, 8, 3} ์ด ๋ฉ๋‹ˆ๋‹ค.
  • S_2 = (2 mod 2) + (2 mod 2) + (6 mod 2) = 0 ์ž…๋‹ˆ๋‹ค.
  • S_3 = (1 mod 3) + (5 mod 3) + (10 mod 3) = 4 ์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ํ•ด์‹œ ๊ฐ’์€ S_2 XOR S_ 3 = 4 ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

์ด ๋ฌธ์ œ๋Š”...

  • ๊ทธ๋ƒฅ ์ง€๋ฌธ๋Œ€๋กœ ์ฝ”๋”ฉํ•ด์„œ ํ’€๋ฉด ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ๋‹ค.
  • ๋ญ”๊ฐ€ ๋Œ€๋‹จํ•œ ๊ฒŒ ์žˆ์„ ์ค„ ์•Œ์•˜๋Š”๋ฐ...
def solution(data, col, row_begin, row_end):
    sum_xor = 0
    data.sort(key=lambda x:(x[col-1],-x[0]))
    col_size = len(data[0])
    
    for c in range(col_size):
        sum_xor += data[row_begin-1][c] % row_begin
    #print(data[row_begin-1], sum_xor)
    
    for i in range(row_begin, row_end, 1):
        sum_mod = 0
        for c in range(col_size):
            sum_mod += data[i][c] % (i+1)
        sum_xor = sum_xor ^ sum_mod
        #print(data[row_begin-1], sum_mod)
    return sum_xor
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.37ms, 12.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (33.16ms, 57.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (45.04ms, 64.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (57.18ms, 64.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (69.37ms, 64.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (80.05ms, 64.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
  • ํ˜น์‹œ๋‚˜ ํ•ด์„œ ์ง์ˆ˜๊ฐœ๋ฉด ์–ด์ฐจํ”ผ 0์ด๋‹ˆ๊นŒ ์นด์šดํ„ฐ๋กœ ๋ชจ์•„์„œ ์ฒ˜๋ฆฌํ•ด๋ณด์•˜์ง€๋งŒ ์†๋„ ์ฒดํฌ์—์„œ ๋ณ„ ์ฐจ์ด์—†๊ฑฐ๋‚˜ ๋” ๋Š๋ ค์ง.
  • ๊ทธ๋ƒฅ ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ์ด ์ •๋‹ต.
from collections import Counter

def solution(data, col, row_begin, row_end):
    sum_xor = 0
    data.sort(key=lambda x:(x[col-1],-x[0]))

    mod_counter = Counter()
    for i in range(row_begin-1, row_end):
        row_mod = 0
        for c in range(len(data[0])):
            row_mod += data[i][c] % (i+1)
        mod_counter[row_mod] += 1

    for mod_val, count in mod_counter.items():
        if count % 2 != 0:
            sum_xor ^= mod_val
    
    return sum_xor
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.46ms, 12MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (36.10ms, 57.7MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (51.89ms, 64.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (73.27ms, 64.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (76.26ms, 64.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (76.45ms, 64.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
  • ํŒŒ์ด์ฌ์€ ์ฝ”๋“œ ๋ผ์ธ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค๋ฉด ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค.
  • ์™œ ๊ทธ๋Ÿด๊นŒ?
from collections import Counter

def solution(data, col, row_begin, row_end):
    sum_xor = 0
    data.sort(key=lambda x:(x[col-1],-x[0]))

    mod_counter = Counter()
    for i in range(row_begin-1, row_end):
        row_mod = 0
        row_mod = sum(data[i][c] % (i+1) for c in range(len(data[0])))
        mod_counter[row_mod] += 1

    for mod_val, count in mod_counter.items():
        if count % 2 != 0:
            sum_xor ^= mod_val
    
    return sum_xor
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.51ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.43ms, 12.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (30.97ms, 57.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (46.71ms, 64.6MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (58.16ms, 64.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (69.12ms, 64.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (72.41ms, 64MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜. ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค.
from functools import reduce

def solution(data, col, row_begin, row_end):
    data.sort(key = lambda x : (x[col-1], -x[0]))
    return reduce(lambda x, y: x ^ y,
                  [sum(map(lambda x: x%(i+1), data[i])) for i in range(row_begin-1, row_end)])
                  
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.99ms, 12.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (30.25ms, 57.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (45.84ms, 64.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (57.02ms, 64.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (64.75ms, 64.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (72.42ms, 64.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
 

ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ: 32.2 ๋žŒ๋‹ค ํ‘œํ˜„์‹๊ณผ map, filter, reduce ํ•จ์ˆ˜ ํ™œ์šฉํ•˜๊ธฐ

๋žŒ๋‹ค ํ‘œํ˜„์‹ ์ž‘์„ฑ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์œผ๋‹ˆ ์ด๋ฒˆ์—๋Š” ๋žŒ๋‹ค ํ‘œํ˜„์‹๊ณผ map, filter, reduce ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 32.2.1  ๋žŒ๋‹ค ํ‘œํ˜„์‹์— ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹ ์‚ฌ์šฉํ•˜๊ธฐ ๋จผ์ € ๋žŒ๋‹ค ํ‘œํ˜„์‹์—์„œ ์กฐ๊ฑด๋ถ€ ํ‘œ

dojang.io

  • ๋‹ฌ๋ ˆ์„œ
    • https://www.daleseo.com/python-functools-reduce/
    • ํŒŒ์ด์ฌ์˜ functools ๋‚ด์žฅ ๋ชจ๋“ˆ์˜ reduce() ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฃผ๋กœ ๋ˆ„์  ์ง‘๊ณ„๋ฅผ ๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€๋ฐ์š”. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฃจํ”„ ๋Œ๋ฉด์„œ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ๊ณ„์†ํ•ด์„œ ์ ์šฉํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ˆ„์ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. reduce(์ง‘๊ณ„ ํ•จ์ˆ˜, ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ[, ์ดˆ๊ธฐ๊ฐ’]) ์—ฌ๊ธฐ์„œ, ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ๋‘๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๋Š”๋ฐ์š”. ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ˆ„์ ์ž(accumulator), ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ํ˜„์žฌ๊ฐ’(current value)๊ฐ€ ๋„˜์–ด์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ˆ„์ ์ž๋Š” ํ•จ์ˆ˜ ์‹คํ–‰์˜ ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ ์žฌ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์ด๊ณ , ํ˜„์žฌ๊ฐ’์€ ๋ฃจํ”„ ๋Œ๋ฉด์„œ ๊ณ„์†ํ•ด์„œ ๋ฐ”๋€Œ๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค.
 

ํŒŒ์ด์ฌ reduce ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

Engineering Blog by Dale Seo

www.daleseo.com

 

728x90
๋ฐ˜์‘ํ˜•