Python/algorithm

[몸풀기] 핸드폰 번호 가리기 22.08.31

baecode 2022. 8. 31. 09:34
반응형

문제 = 전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요.

 

내 코드

def solution(phone_number):
    head_number = phone_number[:-4]
    
    return f"{head_number.join('*')*len(head_number)}{phone_number[len(head_number):]}"
    
#phone_number		return
#"01033334444"		"*******4444"
#"027778888"		"*****8888"

 

다른 사람의 코드

def hide_numbers(s):
    return "*"*(len(s)-4) + s[-4:]

# 아래는 테스트로 출력해 보기 위한 코드입니다.
print("결과 : " + hide_numbers('01033334444'));

 

 

차이점 

 

slice를 사용한 것은 같으나 코드구성과 , 변수 설정을 안한점 좀 더 깔끔하고 가독성이 좋음

반응형

'Python > algorithm' 카테고리의 다른 글

커트라인 [22.09.04]  (0) 2022.09.04
[자료구조] Array  (0) 2022.09.03
[몸풀기] 자릿수 더하기(22.09.01)  (0) 2022.09.01
[몸풀기] 짝수와 홀수 (22.08.29)  (0) 2022.08.29
[몸풀기] 문자열 내 p와 y의 개수 (22.08.29)  (0) 2022.08.29