코딩테스트
-
[프로그래머스][Swift] 숫자 문자열과 영단어알고리즘 2022. 8. 4. 15:13
https://school.programmers.co.kr/learn/courses/30/lessons/81301?language=swift 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr func solution(_ s: String) -> Int { let arr: [String] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] var str = "" var result: String = "" for (_, j) in s.enumerated() { if ..
-
[프로그래머스][Swift] 문자열 내 p와 y의 개수알고리즘 2022. 7. 14. 10:36
func solution(_ s: String) -> Bool { var ans: Bool = false let pNum = s.filter { $0 == "p" || $0 == "P" }.count let yNum = s.filter { $0 == "y" || $0 == "Y" }.count return pNum == yNum ? !ans : ans } 다른 사람의 풀이 func solution2(_ s:String) -> Bool { let string = s.lowercased() return string.components(separatedBy: "p").count == string.components(separatedBy: "y").count } 문자열을 모두 소문자로 바꾼 후 개수 비교 잊고 ..
-
[프로그래머스][Swift] 자릿수 더하기알고리즘 2022. 6. 26. 22:47
func solution(_ n:Int) -> Int { var answer: Int = n var result: [Int] = [] var value: Int = Int() while answer >= 10 { value = answer % 10 answer = answer / 10 result.append(value) } result.append(answer) return result.reduce(0) { $0 + $1 } } func solution2(_ n:Int) -> Int { var result: Int = Int() for (_, num) in String(n).enumerated() { result += Int(String(num)) ?? 0 } return result } print(s..