algorithm

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค level1] ๋กœ๋˜์˜ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„ - ํŒŒ์ด์ฌ

HANBEEN 2021. 6. 20. 18:19
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ๋ฌธ์ œ ์„ค๋ช…

๋กœ๋˜ 6/45(์ดํ•˜ '๋กœ๋˜'๋กœ ํ‘œ๊ธฐ)๋Š” 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ 6๊ฐœ๋ฅผ ์ฐ์–ด์„œ ๋งžํžˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ณต๊ถŒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋กœ๋˜์˜ ์ˆœ์œ„๋ฅผ ์ •ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

์ˆœ์œ„ ๋‹น์ฒจ ๋‚ด์šฉ

1 6๊ฐœ ๋ฒˆํ˜ธ๊ฐ€ ๋ชจ๋‘ ์ผ์น˜
2 5๊ฐœ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜
3 4๊ฐœ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜
4 3๊ฐœ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜
5 2๊ฐœ ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜
6(๋‚™์ฒจ) ๊ทธ ์™ธ

๋กœ๋˜๋ฅผ ๊ตฌ๋งคํ•œ ๋ฏผ์šฐ๋Š” ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋ฐœํ‘œ์ผ์„ ํ•™์ˆ˜๊ณ ๋Œ€ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ฏผ์šฐ์˜ ๋™์ƒ์ด ๋กœ๋˜์— ๋‚™์„œ๋ฅผ ํ•˜์—ฌ, ์ผ๋ถ€ ๋ฒˆํ˜ธ๋ฅผ ์•Œ์•„๋ณผ ์ˆ˜ ์—†๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋ฐœํ‘œ ํ›„, ๋ฏผ์šฐ๋Š” ์ž์‹ ์ด ๊ตฌ๋งคํ–ˆ๋˜ ๋กœ๋˜๋กœ ๋‹น์ฒจ์ด ๊ฐ€๋Šฅํ–ˆ๋˜ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„๋ฅผ ์•Œ์•„๋ณด๊ณ  ์‹ถ์–ด ์กŒ์Šต๋‹ˆ๋‹ค.
์•Œ์•„๋ณผ ์ˆ˜ ์—†๋Š” ๋ฒˆํ˜ธ๋ฅผ 0์œผ๋กœ ํ‘œ๊ธฐํ•˜๊ธฐ๋กœ ํ•˜๊ณ , ๋ฏผ์šฐ๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ 6๊ฐœ๊ฐ€ 44, 1, 0, 0, 31 25๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ๊ฐ€ 31, 10, 45, 1, 6, 19๋ผ๋ฉด, ๋‹น์ฒจ ๊ฐ€๋Šฅํ•œ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„์˜ ํ•œ ์˜ˆ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋‹น์ฒจ ๋ฒˆํ˜ธ3110451619๊ฒฐ๊ณผ

์ตœ๊ณ  ์ˆœ์œ„ ๋ฒˆํ˜ธ 31 0→10 44 1 0→6 25 4๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜, 3๋“ฑ
์ตœ์ € ์ˆœ์œ„ ๋ฒˆํ˜ธ 31 0→11 44 1 0→7 25 2๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜, 5๋“ฑ
  • ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด, ๊ตฌ๋งคํ•œ ๋กœ๋˜์— ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์œผ๋ฉด ๋งžํžŒ ๊ฑธ๋กœ ์ธ์ •๋ฉ๋‹ˆ๋‹ค.
  • ์•Œ์•„๋ณผ ์ˆ˜ ์—†๋Š” ๋‘ ๊ฐœ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ๊ฐ 10, 6์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด 3๋“ฑ์— ๋‹น์ฒจ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • 3๋“ฑ์„ ๋งŒ๋“œ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, 2๋“ฑ ์ด์ƒ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์•Œ์•„๋ณผ ์ˆ˜ ์—†๋Š” ๋‘ ๊ฐœ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ๊ฐ 11, 7์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด 5๋“ฑ์— ๋‹น์ฒจ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • 5๋“ฑ์„ ๋งŒ๋“œ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, 6๋“ฑ(๋‚™์ฒจ)์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋ฏผ์šฐ๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด lottos, ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด win_nums๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‹น์ฒจ ๊ฐ€๋Šฅํ•œ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

def solution(lottos, win_nums):
    counter = 0
    answer = []
    for lotto in lottos:
        if win_nums.count(lotto) > 0:
            counter += 1
    max = counter + lottos.count(0)
    min = counter

    if max == 6:
        answer.append(1)
    elif max == 5:
        answer.append(2)
    elif max == 4:
        answer.append(3)
    elif max == 3:
        answer.append(4)
    elif max == 2:
        answer.append(5)
    else:
        answer.append(6)

    if min == 6:
        answer.append(1)
    elif min == 5:
        answer.append(2)
    elif min == 4:
        answer.append(3)
    elif min == 3:
        answer.append(4)
    elif min == 2:
        answer.append(5)
    else:
        answer.append(6)

    return answer

์ฒ˜์Œ ํ‘ผ ๋‚˜์˜ ํ’€์ด๋ฅผ ๋ณด๋ฉด ์ •๋ง ๋”๋Ÿฝ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค..  ๋ถ€๋„๋Ÿฌ์šธ ์ •๋„

 

๋จผ์ €, win_nums์™€ lottos์˜ ๊ฐ™์€ ์ˆ˜๋ฅผ ์ฐพ๊ณ , lottos์˜ 0์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ˆœ์œ„๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์—

์ตœ๊ณ  ์ˆœ์œ„๋Š” ๊ฐ™์€ ์ˆ˜ + lottos.count(0)์œผ๋กœ ํ•˜๊ณ , ์ตœ์ € ์ˆœ์œ„๋Š” ๊ฐ™์€ ์ˆ˜๋กœ ์ง€์ •์„ ํ–ˆ๋‹ค.

 

 ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๋งž์€ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋กœ๋˜ ์ˆœ์œ„๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด ๋ถ€๋ถ„์ด ๊ณ ๋ฏผ์ด ๋˜์—ˆ๋‹ค

๊ทธ๋ƒฅ ๋ฌด์ž‘์ • if๋ฌธ์œผ๋กœ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋”๋‹ˆ ํ†ต๊ณผ๋Š” ํ–ˆ์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ํ’€์ดํ•˜๋ฉด ์•ˆ ๋˜๊ธฐ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณธ ๊ฒฐ๊ณผ

๋‹ค๋“ค ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๊น”๋”ํ•˜๊ฒŒ ํ•ด๊ฒฐ์„ ํ–ˆ๋‹ค..

 

์–ด์ œ ๋”•์…”๋„ˆ๋ฆฌ ์žŠ์ง€ ๋ง์ž ํ–ˆ๋Š”๋ฐ ํ•˜๋ฃจ ๋งŒ์— ์žŠ์—ˆ๋‹ค.. ๋ฐ˜์„ฑํ•˜์ž

 

๋‹ค์‹œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

๐Ÿ’ก ํ’€์ด ์ˆ˜์ • (๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉ)

def solution(lottos, win_nums):
    counter = 0
    answer = []
    dic = {6: 1, 5: 2, 4: 3, 3: 4, 2: 5, 1: 6, 0: 6}
    for lotto in lottos:
        if win_nums.count(lotto) > 0:
            counter += 1
    answer.append(dic[counter + lottos.count(0)])
    answer.append(dic[counter])
    return answer

์ข€ ๋” ๊น”๋”ํ•ด์กŒ๋‹ค

 

ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋‹ค

๋ฐ”๋กœ ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ’ก ์ œ์ผ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•

rank=[6,6,5,4,3,2,1]

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋.

์™œ ์ƒ๊ฐ์ด ์•ˆ ๋‚ฌ์„๊นŒ..

 

๋ฐ˜์‘ํ˜•