► Swift/Programmers

[Programmers] Swift 알고리즘 | Lv.0 문자 반복 출력하기

다람트리 2024. 1. 24. 20:29
반응형

🔒 문제 설명

문자열 my_string 정수 n 매개변수로 주어질 , my_string 들어있는 문자를 n만큼 반복한 문자열을 return 하도록 solution 함수를 완성해보세요.


🔒 제한사항  

  • 2 ≤ my_string 길이 ≤ 5
  • 2 ≤ n ≤ 10
  • "my_string" 영어 대소문자로 이루어져 있습니다.

🔒 입출력 예

my_string n result
"hello" 3 "hhheeellllllooo"

🔒 입출력 예 설명

입출력 예 #1

  • "hello" 문자를 번씩 반복한 "hhheeellllllooo" return 합니다.

 

🔐 solution of mine

Array()

for 문

Array.count()

import Foundation

func solution (_ my_string: String, _ n:Int) -> String {
    var arr = Array(my_string)
    var answer:String = ""
    for i in 0..<arr.count {
        for _ in 0..<n{
            answer += String(arr[i])
        }

    }
    return answer
}

 


 

🔐 solution of others

for 문

repeatElement( _ , count: _ )

import Foundation

func solution (_ my_string: String, _ n:Int) -> String {
    var answer = ""
    for i in my_string{
        answer += repeatElement(i, count: n)
    }
    return answer
}

print(solution("hello",3)) // expected result: "hhheeellllllooo"

 

🔐 solution of others

String.map {String($0)} → 문자를 배열로 바꾸는 방법

String( repeating: _ , count: _ ) → 문자를 반복하는 방법

Array.joined()

import Foundation

func solution (_ my_string: String, _ n:Int) -> String {my_string.map{String(repeating: $0, count: n)}.joined()}

print(solution("hello",3)) // expected result: "hhheeellllllooo"

 

◆ 해설집 - 풀이과정

import Foundation

func solution (_ my_string: String, _ n:Int) -> String {
    print(my_string.map{String($0)})                                  // result: ["h", "e", "l", "l", "o"]
    print(my_string.map{String(repeating: $0, count: n)})             // result: ["hhh", "eee", "lll", "lll", "ooo"]
    print(my_string.map{String(repeating: $0, count: n)}.joined())    // result: hhheeellllllooo
    return ""
}

print(solution("hello",3)) // expected result: "hhheeellllllooo"

 

◆ 해설집

map() - 문자열을 배열로 만들기 : String to Array 

let str = "aBcDeF"

// Character형을 요소로 갖는 배열
let charArr = str.map {$0}
print("\(charArr) : \(type(of: charArr))") // ["a", "B", "c", "D", "e", "F"] : Array<Character>


// String형을 요소로 갖는 배열
let strArr = str.map {String($0)}
print("\(strArr) : \(type(of: strArr))") // ["a", "B", "c", "D", "e", "F"] : Array<String>

 - 참고링크: https://tngusmiso.tistory.com/46

 

[Swift] String 관련 함수들

▼▼▼Apple 공식 문서 | String ▼▼▼ developer.apple.com/documentation/swift/string Apple Developer Documentation Structure String | A Unicode string value that is a collection of characters. developer.apple.com Swift는 문자열 다루기가

tngusmiso.tistory.com

 

🔐 solution of others

Array() → 문자를 배열로 바꾸는 방법 (output: Array<character>)

Array.append() → 배열에 삽입하는 방법

String() → Array<character> 배열을 문자로 바꾸는 방법

import Foundation

func solution (_ my_string:String, _ n:Int) -> String {
    var arr = Array(my_string)
    var arr2 : [Character] = []
    for i in arr {
        for _ in 0..<n{
            arr2.append(i)
        }
    }
    return String(arr2)
}

print(solution("hello",3)) // expected result: "hhheeellllllooo"

 


 
반응형