[๋ฐฑ์ค€/python] 1157๋ฒˆ : ๋‹จ์–ด ๊ณต๋ถ€

2021. 3. 9. 16:16

๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์„ ๋Œ€๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ, ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ?๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

from string import ascii_uppercase
alphabet_list = list(ascii_uppercase)

n = input().upper()
c = 0
for a in alphabet_list:
    temp = n.count(a)
    if c < temp:
        c = temp
        result = a
    elif c == temp:
        c = temp
        result = '?'
print(result)

ํŒŒ์ด์ฌ ๋‚ด์— ์ •์˜๋˜์–ด ์žˆ๋Š” ascii_uppercase๋ฅผ alphabet_list์— ์ €์žฅํ•ด๋†“์€ ํ›„ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์€ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋งŒ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ alphabet_list๋ฅผ ์ด์šฉํ•˜์—ฌ 

๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•ด ๊ฒ€์‚ฌํ•œ๋‹ค.

 

count๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์ด ๋ช‡ ๋ฒˆ ๋‚˜์™”๋Š”์ง€ countํ•˜์—ฌ temp์— ์ €์žฅํ•œ๋‹ค.

temp ๊ฐ’์ด ์ด์ „ temp๊ฐ’๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ๊ฒฝ์šฐ c์— temp ๊ฐ’์„ ์ €์žฅํ•˜๊ณ 

result์—๋Š” ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ ๋˜๋Š” "?"๋ฅผ ์ €์žฅํ•œ๋‹ค.

728x90

BELATED ARTICLES

more