[Algorithm🧬] 프로그래머스 1단계 - 1

또상·2022년 2월 18일
0

Algorithm

목록 보기
37/133
post-thumbnail

1단계 문제가 생각보다 어렵다고 생각했는데 (예를 들어 카카오 코테의 핸드폰 최소 거리 구하는 문제), 해당 문제들은 최근에 나온 문제라서 그런거였다...! 쉬운 문제들은 모아서 포스팅 하기로 결정!

x만큼 간격이 있는 n개의 숫자

문제 / 풀이.py

def solution(x, n):
    answer = []
    
    for i in range(1, n + 1):
        answer.append(x*i)
        
    return answer



핸드폰 번호 가리기

문제 / 풀이.py

def solution(phone_number):
    return "*" * (len(phone_number) - 4) + phone_number[-4:]



하샤드 수

문제 / 풀이.py

def solution(x):
    sum = 0
    copyx = x
    
    while x > 0:
        sum += x % 10
        x = int(x/10)

    return True if copyx % sum == 0 else False



평균 구하기

문제 / 풀이.py

from functools import reduce

def solution(arr):
    return reduce(lambda x, y: x + y, arr) / len(arr)

제일 중요한건 from functools import reduce 인 것 같다. 쓸 때마다 어디서 갖고와야하는지 기억이 안남...

profile
0년차 iOS 개발자입니다.

0개의 댓글