๐Ÿง ๋”•์…”๋„ˆ๋ฆฌ (Dictionary). { key : value }

๐Ÿค” ๋”•์…”๋„ˆ๋ฆฌ ๊ฐœ๋…

key-value๋ฅผ ํ•˜๋‚˜์˜ ์Œ์œผ๋กœ ๊ฐ–๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋‹ค๋ฅธ ์–ธ์–ด์—์˜ map๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

์ด๋•Œ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค๊ฐ€ ์ค‘๋ณต๋˜๋Š”๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค!

์ด๋•Œ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค๊ฐ€ ์ค‘๋ณต๋˜๋Š”๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค!

 

 

๐Ÿค” ๋”•์…”๋„ˆ๋ฆฌ์˜ ์ƒ์„ฑ๊ณผ ์‚ญ์ œ

dict_a['name'] = 'b'
dict_b[3] = 2

del dict_a['name']  # del a[key]

 

๐Ÿค” ๋”•์…”๋„ˆ๋ฆฌ key, value์— ๋Œ€ํ•œ ์ ‘๊ทผ-1

dict_a.keys() 	# key ๊ฐ์ฒด๋“ค์„ return
dict_a.values() # value ๊ฐ์ฒด๋“ค์„ return 
dict_a.items()	# key-value ์Œ์„ return

 

๐Ÿค” ๋”•์…”๋„ˆ๋ฆฌ key, value์— ๋Œ€ํ•œ ์ ‘๊ทผ-2. (key๋กœ value์–ป๊ธฐ, value๋กœ key์–ป๊ธฐ)

dict_a.get(key)  # key์— ํ•ด๋‹นํ•˜๋Š” "value๊ฐ’ ์ถœ๋ ฅ"

for i in dict_a  			# ๊ธฐ๋ณธ์ ์œผ๋กœ for๋ฌธ์—์„œ i๋ฅผ ํ†ตํ•ด key๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
for i in dict_a.values()    # ๊ธฐ๋ณธ์ ์œผ๋กœ for๋ฌธ์—์„œ i๋ฅผ ํ†ตํ•ด value๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

for key, value in dict_a.items():  # key์™€ value๋ฅผ ํ•œ๋ฒˆ์— ์‚ฌ์šฉ
for key, value in dict_a: 		   # ์œ„์™€ ๋™์ผํ•œ ์‚ฌ์šฉ๋ฒ•

 

๐Ÿ˜ถ ํ™œ์šฉ 

for i in construct.keys():
    if construct[i] == n:  # key์˜ ์–ด๋–ค ๊ฐ’์ด n๊ณผ ๊ฐ™๋‹ค๋ฉด
        ans.append(i)      # {construct[i] : i} key์— ํ•ด๋‹นํ•˜๋Š” value, i๋ฅผ ์ถ”๊ฐ€

 

 

 

๐Ÿง ๋ฐฑ์ค€ 2798  (๋”•์…”๋„ˆ๋ฆฌ ํ™œ์šฉ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 1. brute force (์™„์ „ํƒ์ƒ‰)

์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ brute force๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค,.

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 2.  ๋”•์…”๋„ˆ๋ฆฌ์™€ set 

cf. ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด์˜ get()ํ•จ์ˆ˜

dict.get(key, default = None) 
# ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธด ๊ฐ’ (๊ด„ํ˜ธ ์•ˆ์˜ ๊ฐ’)์ด ํ‚ค์— ์†ํ•˜์ง€ ์•Š์œผ๋ฉด? None ์ถœ๋ ฅ

getํ•จ์ˆ˜์˜ return๊ฐ’์€ ์ฒซ๋ฒˆ์งธ ์ธ์ž์˜ ํ‚ค๊ฐ’์ด๋‹ค.



๐Ÿค”
 Algorithm ๊ณผ์ • 
1. ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋“ค์—์„œ 3๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๊ณจ๋ผ ๋”ํ•œ ๊ฐ’์„ set์— ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
2. ์ด sum_set์— ๋Œ€ํ•ด ์ž…๋ ฅ๋œ M์„ ๋„˜์ง€ ์•Š๋Š” ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์„ธ ์ˆซ์ž์˜ ํ•ฉ๊ณผ m๊ณผ์˜ ์ฐจ์ด๋ฅผ ์ €์žฅํ•œ๋‹ค.
3. ์ด๋•Œ, ์ €์žฅํ•œ m๊ณผ์˜ ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ์ด m๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฒƒ์ด๋ฏ€๋กœ ๋‹ต์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
 

๐Ÿคซ  solution_2798

n, m = map(int, input().split())
num = list(map(int, input().split()))

sum_set = set()
sum_dict = {}

for i in range(n):
    for j in range(i+1, n):
        for k in range(j+1, n):
            sum_set.add(num[i] + num[j] + num[k])

for i in sum_set:
    if m - i >= 0:
        sum_dict[i] = m - i
    else:
        pass

print(min(sum_dict, key = sum_dict.get))

 

 

๐Ÿง ๋ฐฑ์ค€ 2231  (๋”•์…”๋„ˆ๋ฆฌ ํ™œ์šฉ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 1. ์ž์—ฐ์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž ๊ตฌํ•˜๊ธฐ.

# ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ์ˆซ์ž์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
def digit(n):
    if n < 10:
        return n
    else:
        return digit(n // 10) + digit(n % 10)


๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 2.  ๋”•์…”๋„ˆ๋ฆฌ์˜ ์‚ฌ์šฉ 

construct = {}
for i in range(n+1):
    construct[i] = i + digit(i)

ans = []

for i in construct:
    if construct[i] == n:
        ans.append(i)



๐Ÿค” Algorithm ๊ณผ์ • 
216 => 198 + 1 + 9 + 8  ... ์ด๋Ÿฐ ๋ฐฉ์‹์€ ๊ฑฐ์˜ ๋ถˆ๊ฐ€ (์—ญ์ถ”์  ์ง„ํ–‰ ์‹œ ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ž„์„ ๋„์ถœํ•  ์ˆ˜ ์—†์Œ)

๋งตํ•‘์„ ํ†ตํ•ด 1~N๊นŒ์ง€ ๋˜‘๊ฐ™์€ value๋“ค ์ค‘ ์ตœ์†Œ key๋ฅผ ์ฐพ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋‚˜๊ฐˆ ๊ฒƒ์ด๋‹ค.

{1:1}
{2:2}
...
{13:17}
{14:19}
{15:21}
...
{198:216}
...
{N:N'}

 

๐Ÿคซ  solution_2231

n = int(input())

# ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ์ˆซ์ž์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
def digit(n):
    if n < 10:
        return n
    else:
        return digit(n // 10) + digit(n % 10)

construct = {}
for i in range(n+1):
    construct[i] = i + digit(i)

ans = []

for i in construct:
    if construct[i] == n:
        ans.append(i)
print(0) if len(ans) == 0 else print(min(ans))

 

 

๐Ÿง ๋ฐฑ์ค€ 7568 (C++์˜ pair์™€ ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•ด! โ˜…โ˜…โ˜…โ˜…โ˜…

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ.  pair ์ฒ˜๋Ÿผ ์ž…๋ ฅ๋ฐ›์€ key์™€ value๊ฐ€ ๊ฒน์ณ๋„ ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑํ•˜์ž!.

for _ in range(int(input())):
    w, h = map(int, input().split())
    person.append((w, h))
# print(person)  => [(55, 185), (58, 183), (88, 186), (60, 175), (46, 155)]




๐Ÿค” Algorithm ๊ณผ์ • 
1. ๊ฐ๊ฐ ๋ฐ›์€ w, h์— ๋Œ€ํ•ด ๋“ฑ์ˆ˜๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ rank = 1๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.
2. ์ž…๋ ฅํ•œ ๋ณ€์ˆ˜๋“ค์„ ์ €์žฅํ•œ person๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด
0๋ฒˆ์งธ ์ธ๋ฑ์Šค(๋ชธ๋ฌด๊ฒŒ)์™€ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค(ํ‚ค)๊ฐ€ ๋ชจ๋‘ ํฐ ๊ฒฝ์šฐ์— rank๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

 

๐Ÿคซ  solution_7568

person = []

for _ in range(int(input())):
    w, h = map(int, input().split())
    person.append((w, h))

# print(person)  => [(55, 185), (58, 183), (88, 186), (60, 175), (46, 155)]


for i in person:
    rank = 1
    for j in person:
        if i[0] < j[0] and i[1] < j[1]:
            rank += 1
    print(rank, end = " ")

 

 

๐Ÿง ๋ฐฑ์ค€ 1436

๐Ÿค” Algorithm ๊ณผ์ • 
1. ์ฒ˜์Œ 666์ธ ์ˆ˜๋ฅผ terminal๋ณ€์ˆ˜์— ์ €์žฅ
2. n์ด 0์ด ์•„๋‹ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ, ๋ฌธ์ž์—ด terminal์„ updateํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ๊ฒƒ์ด๋‹ค.
3. terminal ๋ฌธ์ž์—ด ์•ˆ์— 666์ด ๋“ค์–ด์žˆ๋‹ค๋ฉด, n์„ 1๋งŒํผ ๊ฐ์†Œ์‹œํ‚ค๊ณ  
4. ๋งŒ์•ฝ n์ด 0์ด๋ผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
5. ์ดํ›„ terminal์˜ ๊ฐ’์„ 1๋งŒํผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
 

๐Ÿคซ  solution_1436

n = int(input())

terminal = 666
while n != 0:
    if '666' in str(terminal):
        n -= 1
        if n == 0:
            break
    terminal += 1
print(terminal)

 

 

๐Ÿง ๋ฐฑ์ค€ 1018 (CNN ์•Œ๊ณ ๋ฆฌ์ฆ˜)

๐Ÿคซํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ. CNN Algorithm (Convolution Neural Network)

CNN์—์„œ filter๋ฅผ ์ด์šฉํ•ด ํ•ฉ์„ฑ๊ณฑ ๊ณ„์ธต์˜ ๋™์ž‘์ฒ˜๋Ÿผ 8X8์˜ ์ •๋‹ต ์ฒด์ŠคํŒ์„ ๋งŒ๋“ค๊ณ  
CNN์•Œ๊ณ ๋ฆฌ์ฆ˜์ฒ˜๋Ÿผ ์ž…๋ ฅ๋ฐ›์€ ์ฒด์ŠคํŒ์„ ์Šค์บ”ํ•˜๋ฉด์„œ ํ‹€๋ฆฐ ๊ฒฝ์šฐ๋ฅผ ์นด์šดํŒ…ํ•ด์„œ ํ‹€๋ฆฐ๊ฒฝ์šฐ์˜์ˆ˜๊ฐ€ ์ตœ์†Œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๐Ÿค” CNN_Algorithm ๊ณผ์ • 
https://wikidocs.net/164823

 

๐Ÿคซ  solution_1436

n, m = map(int, input().split())

chess = [list(input()) for _ in range(n)]
ans_chess_W , ans_chess_B = [], []
for i in range(4):
    ans_chess_W.append('WBWBWBWB')
    ans_chess_W.append('BWBWBWBW')
    ans_chess_B.append('BWBWBWBW')
    ans_chess_B.append('WBWBWBWB')


def wrongcount_W (x, y):
    cnt = 0
    for i in range(8):
        for j in range(8):
            if chess[x+i][y+j] != ans_chess_W[i][j]:
                cnt += 1
    return cnt

def wrongcount_B (x, y):
    cnt = 0
    for i in range(8):
        for j in range(8):
            if chess[x+i][y+j] != ans_chess_B[i][j]:
                cnt += 1
    return cnt

# CNN์ฒ˜๋Ÿผ 8x8 size๋กœ ์ฒด์ŠคํŒ ์ž๋ฅด๊ธฐ
cnt = []
for i in range(n-8 +1):
    for j in range(m-8 +1):
        cnt.append(wrongcount_W(i, j))
        cnt.append(wrongcount_B(i, j))
print(min(cnt))

 

+ Recent posts