공대생의 비망록

[프로그래머스][Lv. 1] 신규 아이디 추천 Swift 풀이 본문

Programming Language/Swift

[프로그래머스][Lv. 1] 신규 아이디 추천 Swift 풀이

myungsup1250 2022. 3. 14. 15:25

https://programmers.co.kr/learn/courses/30/lessons/72410

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr

풀이는 추후에 차차 올리도록 하겠습니다...

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Foundation
 
func solution(\_ new\_id:String-> String {
    var rec\_id\_01: String = ""
    for s in new\_id {
        rec\_id\_01 += s.lowercased()
    }
    
    var rec\_id\_02: String = ""
    for s in rec\_id\_01 {
        if s.isLowercase || s.isNumber || s == "-" || s == "\_" || s == "." {
            rec\_id\_02 += "\\(s)"
        }
    }
 
    rec\_id\_01 = ""
    var dots: String = ""    
    for s in rec\_id\_02 {
        if s == "." {
            dots = "."
        } else {
            if dots == "." {
                rec\_id\_01 += dots
                dots = ""
            }
             rec\_id\_01 += "\\(s)"
        }
    }
    if dots == "." {
        rec\_id\_01 += "\\(dots)"
    }
    
    if rec\_id\_01.first == "." {
        rec\_id\_01.removeFirst()
    }
    if rec\_id\_01.last == "." {
        rec\_id\_01.removeLast()
    }
    
    if rec\_id\_01.isEmpty {
        rec\_id\_01 += "a"
    }
    
    while rec\_id\_01.count > 15 {
        rec\_id\_01.removeLast()
    }
    if rec\_id\_01.last == "." {
        rec\_id\_01.removeLast()
    }
    while rec\_id\_01.count < 3 {
        rec\_id\_01 += "\\(rec\_id\_01.last!)"
    }        
 
    return rec\_id\_01
}
cs
Comments