ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‹คํŠธ๊ฒŒ์ž„(Swift)

2022. 10. 1. 08:51ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‹คํŠธ๊ฒŒ์ž„(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/17682

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 ๋‚˜์˜ ํ’€์ด

 

์„ ์–ธ์ ์œผ๋กœ ํ‘ผ ๊ฒƒ์ด์•„๋‹Œ ๋ฌธ์ œ์˜ ์š”๊ตฌ์‚ฌํ•ญ ๊ทธ๋Œ€๋กœ ์ ˆ์ฐจ์ ์œผ๋กœ ํ’€์—ˆ๋‹ค

switch์™€ if๋ฌธ ๋„๋ฐฐ๋กœ ์ธํ•œ ๋ณ„๋กœ ์ข‹์ง€์•Š์€ ์ฝ”๋“œ์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ’€์—ˆ๋‹ค๋Š” ๊ฒƒ์— ์˜์˜๋ฅผ ๋‘๋ ค๊ณ  ํ•œ๋‹ค! ๐Ÿ’ช

 

import Foundation
func solution(_ dartResult:String) -> Int {
    var idx = -1
    var arr = [Int](repeating:0, count: 3)
    var makeNum = ""
    for x in dartResult {
        if x.isNumber {
            makeNum += String(x)
        } else {
            switch x {
                case "S":
                idx += 1
                arr[idx] = Int(makeNum)!
                arr[idx] = Int(pow(Double(arr[idx]), 1.0))
                case "D":
                idx += 1
                arr[idx] = Int(makeNum)!
                arr[idx] = Int(pow(Double(arr[idx]), 2.0))
                case "T":
                idx += 1
                arr[idx] = Int(makeNum)!
                arr[idx] = Int(pow(Double(arr[idx]), 3.0))
                default:
                break
            }
            if x == "*" {
                if idx == 0 {
                    arr[idx] *= 2
                } else {
                    arr[idx] *= 2
                    arr[idx-1] *= 2
                }
            } else if x == "#" {
                arr[idx] *= -1
            }
            makeNum = ""
        }
    }
    print(arr)

    return arr.reduce(0, +)
}

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

split์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ์„ ์–ธ์ ์œผ๋กœ ๋งŒ๋“  ํ’€์ด์ด๋‹ค.

์„œ๋กœ ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜๋“ค์— ๋Œ€ํ•ด์„œ zip์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๊ณ  ์žˆ๋Š” score์—์„œ ๋‹ค์Œ ์ธ๋ฑ์Šค์˜ ํŠน์ •๋ฌธ์ž์—ด ํฌํ•จ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์ต์ˆ™ํ•ด์งˆ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. 

 

import Foundation
func solution(_ dartResult:String) -> Int {
    let numberArr = dartResult.split(whereSeparator: { $0.isLetter || $0 == "*" || $0 == "#" })
    let letterArr = dartResult.split(whereSeparator: { $0.isNumber })
    
    var totalScore = 0
    
    for (i, (number, letter)) in zip(numberArr, letterArr).enumerated() {
        var score = 0
        if let num = Int(number) {
            score = letter.contains("D") ? num * num : letter.contains("T") ? num * num * num : num
        }
        
        if letter.contains("*") {
            score *= 2
        } else if letter.contains("#") {
            score = -score
        }
        
        if i != 2 {
            if letterArr[i+1].contains("*") {
                score *= 2
            }
        }
        totalScore += score
    }
    return totalScore
}

 

 

 ํ”ผ๋“œ๋ฐฑ

 

split๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ชผ๊ฐœ๋ฉด ๋ฌด์กฐ๊ฑด ๊ทธ ์ชผ๊ฐ  ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋‰˜๋Š” ์ค„ ์•Œ์•˜๋‹ค. ํ•˜์ง€๋งŒ ์กฐ๊ฑด์ด ์—ฐ์†์œผ๋กœ ๋ถ€ํ•ฉ๋œ๋‹ค๋ฉด ๋ฌถ์—ฌ์ง„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ("1#*3"์„ whereSeparator: {$0 == "#" || $0 == "*")๋กœ ํ•œ๋‹ค๋ฉด [1, 3]์ด ๋œ๋‹ค.