[Programmers] ๋‹คํŠธ๊ฒŒ์ž„

Updated:

๋‹คํŠธ๊ฒŒ์ž„

๋‹คํŠธ๊ฒŒ์ž„ ์„ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

๋ถ„๋ช…ํžˆ Level 1 ์งœ๋ฆฌ ๋ฌธ์ œ๋กœ ๋˜์–ด์žˆ๋Š”๋ฐ ์ •๋ง ์ •๋ง ํž˜๋“ค๊ฒŒ ํ’€์—ˆ๋‹ค..

์ฒด๊ฐ์ƒ Level 3 ์งœ๋ฆฌ ์ธ ์ค„..

์šฐ์„  ํ’€๋‹ค๊ฐ€ ํžŒํŠธ๋ฅผ ํ•œ ๊ฐœ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘ํ–ˆ๋‹ค.. ์ฒ˜์Œ์— ๋‹คํŠธ ๊ธฐ๋ก์„ ๋ถ„๋ฆฌํ•  ๋•Œ 10์„ ๊ตฌ๋ณ„ํ•˜๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ๊นŒ๋‹ค๋กœ์› ๋Š”๋ฐ

replace ๋ฅผ ํ†ตํ•ด 10์„ k๋กœ ์น˜ํ™˜ ํ•˜๊ณ  ์‹œ์ž‘ํ–ˆ๋‹ค.

ํ’€์ด๋ฐฉ๋ฒ•์€ ๋ฌธ์ž์—ด ํ•˜๋‚˜๋กœ ๋˜์–ด์žˆ๋Š” ๋‹คํŠธ ๊ธฐ๋ก์„ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆˆ ๋’ค ๊ฐ์ž ๊ธฐ์ค€์— ๋งž๊ฒŒ ๊ณ„์‚ฐ์„ ํ•ด ์ ์ˆ˜๋ฅผ ๊ตฌํ•œ ํ›„ ๋‹ค ๋”ํ•ด์คฌ๋‹ค.

def solution(dartResult):
	answer = 0
	checkNum = ['0','1','2','3','4','5','6','7','8','9','k']
	dartList = []
	dartScore = []
	dartResult = dartResult.replace('10', 'k')

	
	while len(dartResult) > 3: # dart ๋ช…๋ น ๋ถ„๋ฆฌ
		orderCount = 0
		for index in range(1, len(dartResult)):
			if dartResult[index] not in checkNum:
				orderCount += 1
			else:
				break
		dartList.append(dartResult[:orderCount+1])
		dartResult = dartResult.replace(dartResult[:orderCount+1],'')
	dartList.append(dartResult)

	for i in range(3):
		for j in range(len(dartList[i])):
			if dartList[i][j] in checkNum:
				try:
					dartScore.append(int(dartList[i][j]))
				except:
					dartScore.append(10)

			elif dartList[i][j] == 'D':
				dartScore[i] = dartScore[i] ** 2

			elif dartList[i][j] == 'T':
				dartScore[i] = dartScore[i] ** 3

			elif dartList[i][j] == '*':
				dartScore[i] *= 2
				if i != 0:
					dartScore[i-1] *= 2

			elif dartList[i][j] == '#':
				dartScore[i] *= -1

	return sum(dartScore)

์ฒ˜์Œ์—” ํŒŒ์ด์ฌ์€ case ๋ช…๋ น์ด ์—†์–ด ์ ์ˆ˜๋ฅผ ์ „๋ถ€ if ๋ฌธ์œผ๋กœ ๋ฌถ์–ด์•ผ ํ•˜๋‚˜ ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ

๋ฏธ๋ฆฌ ์ฒดํฌ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋‘๊ณ  ๊ทธ ์•ˆ์— ์žˆ๋‹ค๋ฉด int ํ˜•์œผ๋กœ ๋ฐ”๊ฟ” ๊ณ„์‚ฐ์„ ํ•˜๋Š” ์‹์œผ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.

๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” S, D, T ์ธ ๊ฒฝ์šฐ ๊ฐ์ž ์ œ๊ณฑ, ์„ธ์ œ๊ณฑ์„ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ

๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ S, D, T ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฌถ์–ด index ๋ฅผ ํ™œ์šฉํ•ด ์ œ๊ณฑ, ์„ธ์ œ๊ณฑ์„ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋‚œ elif ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ elif ํ•œ ๊ฐœ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค..

์ง„์งœ ์†Œ๋ฆ„๋‹์•˜๋‹ค.


Categories:

Updated:

Leave a comment