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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV1 ์ถ”์–ต ์ ์ˆ˜

๐ŸŽฎinspirer9 2023. 6. 22. 23:16
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์‚ฌ์ง„๋“ค์„ ๋ณด๋ฉฐ ์ถ”์–ต์— ์ –์–ด ์žˆ๋˜ ๋ฃจ๋Š” ์‚ฌ์ง„๋ณ„๋กœ ์ถ”์–ต ์ ์ˆ˜๋ฅผ ๋งค๊ธธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ง„ ์†์— ๋‚˜์˜ค๋Š” ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•œ ๊ฐ’์ด ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["may", "kein", "kain"]์ด๊ณ  ๊ฐ ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ [5์ , 10์ , 1์ ]์ผ ๋•Œ ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 16(5 + 10 + 1)์ ์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["kali", "mari", "don", "tony"]์ด๊ณ  ["kali", "mari", "don"]์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ๊ฐ๊ฐ [11์ , 1์ , 55์ ]]์ด๊ณ , "tony"๋Š” ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ์—†์„ ๋•Œ, ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 3๋ช…์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด name, ๊ฐ ์‚ฌ๋žŒ๋ณ„ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด yearning, ๊ฐ ์‚ฌ์ง„์— ์ฐํžŒ ์ธ๋ฌผ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ์ด์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด photo๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ์ง„๋“ค์˜ ์ถ”์–ต ์ ์ˆ˜๋ฅผ photo์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ name์˜ ๊ธธ์ด = yearning์˜ ๊ธธ์ด≤ 100
    • 3 ≤ name์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 7
    • name์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • name์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 1 ≤ yearning[i] ≤ 100
    • yearning[i]๋Š” i๋ฒˆ์งธ ์‚ฌ๋žŒ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • 3 ≤ photo์˜ ๊ธธ์ด ≤ 100
    • 1 ≤ photo[i]์˜ ๊ธธ์ด ≤ 100
    • 3 ≤ photo[i]์˜ ์›์†Œ(๋ฌธ์ž์—ด)์˜ ๊ธธ์ด ≤ 7
    • photo[i]์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • photo[i]์˜ ์›์†Œ๋“ค์€ ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

name yearning photo result
["may", "kein", "kain", "radi"] [5, 10, 1, 3] [["may", "kein", "kain", "radi"],["may", "kein", "brin", "deny"], ["kon", "kain", "may", "coni"]] [19, 15, 6]
["kali", "mari", "don"] [11, 1, 55] [["kali", "mari", "don"], ["pony", "tom", "teddy"], ["con", "mona", "don"]] [67, 0, 55]
["may", "kein", "kain", "radi"] [5, 10, 1, 3] [["may"],["kein", "deny", "may"], ["kon", "coni"]] [5, 15, 0]

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

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1
    • ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† "may", "kein", "kain", "radi"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 19(5 + 10 + 1 + 3)์  ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ธ "may"์™€ "kein"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 15(5 + 10)์ ์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์‚ฌ์ง„์˜ ๊ฒฝ์šฐ "kain"๊ณผ "may"๋งŒ ๊ทธ๋ฆฌ์›Œํ•˜๋ฏ€๋กœ ๋‘˜์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 6(1 + 5)์ ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [19, 15, 6]์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2
    • ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ธ "kali", "mari", "don"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ ์†์—” ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์ธ๋ฌผ์ด ์—†์œผ๋ฏ€๋กœ 0์ ์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์€ "don"๋งŒ ์žˆ์œผ๋ฏ€๋กœ 55์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [67, 0, 55]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #3
    • ์„ค๋ช… ์ƒ๋žต

ํ’€์ด

  • ๊ฐ„๋งŒ์˜ ๋ฌด์ง€์„ฑ ํ’€์ด ์‹œ์ž‘!
  • ๋จผ์ € ์‚ฌ๋žŒ ์ด๋ฆ„์— ๋Œ€์‘ํ•˜๋Š” ์ ์ˆ˜๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅํ•œ๋‹ค. (์ค‘๋ณต์ด ์—†์Œ)
  • ๊ทธ๋ฆฌ๊ณ  ํฌํ† ๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ ์ด๋ฆ„์„ ์ฐพ์•„์„œ ๊ฐ’์„ ํ•ฉ์‚ฐํ•˜๋ฉด ๋.
  • ํ’€๋‹ค๋ณด๋‹ˆ๊นŒ ๋”•์…”๋„ˆ๋ฆฌ์— ์—†๋Š” ์ด๋ฆ„์ด photo์—์„œ ๋‚˜์˜ฌ ๊ฒฝ์šฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ...
def solution(name, yearning, photo):
    answer = []
    dic = {}
    for i in range(len(name)):
        dic[name[i]] = yearning[i]
    for p in photo:
        _tmp = 0
        for pp in p:
            try:
                _tmp += dic[pp]
            except:
                _tmp += 0
        answer.append(_tmp)
    return answer
  • ์—ญ์‹œ ๋ฌด์ง€์„ฑ ใ…‹ใ…‹ใ…‹
728x90
๋ฐ˜์‘ํ˜•