๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ Swift

by Fomagran ๐Ÿ’ป 2020. 3. 1.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ *์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • s๋Š” ๊ธธ์ด 4 ์ด์ƒ, 20์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

phone_number return
027778888 *****8888
01033334444 *******4444

ํ’€์ด: ํœด๋Œ€ํฐ๋ฒˆํ˜ธ ๋ 4์ž๋ฆฌ๋Š” suffix๋ฅผ ํ†ตํ•ด ์•Œ์•„๋‚ด๊ณ  ๊ทธ 4๊ฐœ๋ฅผ ๋บ€ ์ˆซ์ž๋“ค์€ *๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฏ€๋กœ for๋ฌธ์„ ๊ทธ๋งŒํผ ๋Œ๋ ค์ค˜์„œ ๋”ํ•ด์ค€๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
import Foundation
 
 
func solution(_ phone_number:String-> String {
    var answer = String()
    for _ in 0..<phone_number.count-4{
        answer += "*"
    }
    answer += phone_number.suffix(4)
    return answer
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด ์ค‘ ๊ฐ€์žฅ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ

ํ’€์ด:๊ฑ String(repeating:,count)๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ฐ˜๋ณต๋œ๋‹ค.

1
2
3
func solution(_ phone_number:String-> String {
    return String("\(String(repeating: "*", count: phone_number.count - 4))\(phone_number.suffix(4))")
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€