Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 2018 KAKAO BLIND RECRUITMENT
- 정보처리기사
- usb카메라연결
- parse
- 정보처리기사 실기 요약본
- cancelstouchesinview
- Xcode
- ios외부디바이스연결
- Decodable
- JSONParser
- UITableView
- 정보처리기사 실기
- IOS
- ios카메라유선연결
- CustomCode
- 프로그래머스
- swift
- 위클리챌린지
- Codable
- avcapturesession
- 카메라유선연결
- ios캡처감지
- 외부카메라감지
- Pod
- ios캡처방지
- cocoapods
- ios외부카메라연결
- JSON
- AVFoundation
- programmers
Archives
- Today
- Total
iOS 개발일기
[Swift] 프로그래머스(코딩테스트 연습: LEVEL 1) - 숫자 문자열과 영단어 본문
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
나의 풀이
func solution(_ s: String) -> Int {
let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
var result = s
numbers.enumerated().forEach { (i, num) in
result = result.replacingOccurrences(of: num, with: String(i))
}
return Int(result) ?? 0
}
처음에는 열거형(enum)으로 정리해보고 싶어서 했었습니다만
에러가 뜨더라구요...
(xCode에서는 잘 되던데...)
열거형(enum)을 사용한 풀이
enum Number: String, CaseIterable {
case zero, one, two, three, four, five, six, seven, eight, nine
var value: String {
switch self {
case .zero : return "0"
case .one : return "1"
case .two : return "2"
case .three: return "3"
case .four : return "4"
case .five : return "5"
case .six : return "6"
case .seven: return "7"
case .eight: return "8"
case .nine : return "9"
}
}
}
func solution(_ s: String) -> Int {
var result = s
Number.allCases.forEach { num in
result = result.replacingOccurrences(of: num.rawValue, with: num.value)
}
return Int(result) ?? 0
}
위 방법은 효율적이지 못하여 '이런 방법도 있구나' 정도로만 알아주시면 감사하겠습니다.
'코딩테스트' 카테고리의 다른 글
[Swift] 프로그래머스(위클리 챌린지 2주차) - 상호 평가 (0) | 2021.08.18 |
---|---|
[Swift] 프로그래머스(위클리 챌린지 1주차) -부족한 금액 계산하기 (0) | 2021.08.06 |
[Swift] 프로그래머스(코딩테스트 연습: LEVEL 1) - [1차] 비밀지도 (0) | 2021.08.06 |
[Swift] 프로그래머스(코딩테스트 연습: LEVEL 1) - [1차] 다트 게임 (0) | 2021.08.05 |