filter
- 프로그래머스 문제 중 배열 내 중복된 숫자의 갯수를 구하는 문제
- filter를 사용해서 풀 수도 있었다..!
import Foundation
func solution(_ array:[Int], _ n:Int) -> Int { array.filter {$0 == n}.count }
where
- 프로그래머스 문제 중 짝수의 합을 구하는 문제
- for문과 where을 같이 쓸 수 있다!
import Foundation
func solution(_ n:Int) -> Int {
var result = 0
for i in 1...n where i % 2 == 0 {
result += i
}
return result
}
map
- 프로그래머스 문제 중 배열 자르기, 외계행성의 나이 문제
- 다양한 방법으로 쓰일 수 있을 듯
import Foundation
func solution(_ numbers:[Int], _ num1:Int, _ num2:Int) -> [Int] {
return (num1...num2).map{numbers[$0]}
}
func solution(_ numbers:[Int], _ num1:Int, _ num2:Int) -> [Int] {
return Array(numbers[num1...num2])
}
func solution(_ age:Int) -> String {
let ageDic = [
"0": "a",
"1": "b",
"2": "c",
"3": "d",
"4": "e",
"5": "f",
"6": "g",
"7": "h",
"8": "i",
"9": "j"
]
return String(age)
.map { String($0) }
.map { String(ageDic[$0]!) }
.joined()
}