새소식

인기 검색어

프로그래머스/Lv.1

[Swift] 신규 아이디 추천

  • -
import Foundation

func solution(_ new_id:String) -> String {
    
    var result = new_id.lowercased().components(separatedBy: ["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "=", "+", "[", "]", "{", "}", ":", "?", ",", "<", ">", "/"]).joined().components(separatedBy: ".").filter { $0 != ""
    }.joined(separator: ".").trimmingCharacters(in: ["."])
  
    
    if result.isEmpty {
        result = "a"
    }
    
    if 15 <= result.count {
        result = "\(result.prefix(15))"
    }
    
    result = result.trimmingCharacters(in: ["."])
    
    if result.count <= 2 {
        result += String.init(repeating: "\(result.last!)", count: 3-result.count)
    }
    
    return result
}

 

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

[Swift] 약수의 개수와 덧셈  (0) 2023.02.15
[Swift] 실패율  (0) 2023.02.15
[Swift] 신고 결과 받기  (0) 2023.02.15
[Swift] 시저 암호  (0) 2023.02.15
[Swift] 숫자 짝궁  (0) 2023.02.15
Contents

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

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