새소식

인기 검색어

프로그래머스/Lv.0

[Swift] 잘라서 배열로 저장하기

  • -
import Foundation

func solution(_ my_str:String, _ n:Int) -> [String] {

    let my_str = Array(my_str)
    var result = [String]()

    for i in stride(from: 0, to: my_str.count, by: n) {
        let j = i + n - 1
        j < my_str.count ? result.append(my_str[i...j].reduce("") {$0 + String($1)}) : result.append(my_str[i...my_str.count - 1].reduce("") {$0 + String($1)})
    }

    return result
}

'프로그래머스 > Lv.0' 카테고리의 다른 글

[Swift] 제곱수 판별하기  (0) 2023.01.05
[Swift] 저주의 숫자 3  (0) 2023.01.05
[Swift] 자릿수 더하기  (0) 2023.01.05
[Swift] 인덱스 바꾸기  (0) 2023.01.05
[Swift] 이진수 더하기  (0) 2023.01.05
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.