본문 바로가기

스크립트

[코딩테스트 입문] n의 배수 고르기

반응형

n의 배수 고르기

1안) solution.py

def solution(n, numlist):
    answer = []
    
    for num in numlist:
        if (num % n == 0):
            answer.append(num)

    return answer

2안) solution.py

def solution(n, numlist):
    answer = []
    
    answer = list(filter(lambda num: num % n == 0, numlist))

    return answer

3안) solution.py

def solution(n, numlist):
    answer = []
    
    answer =  [num for num in numlist if num % n == 0]

    return answer

 

파이션 lambda() 함수

x = lambda a: a + 10
print(x(5))
15
x = lambda a, b: a * b
print(x(5, 6))
30
def myfunc(n):
  return lambda a : a * n

mytripler = myfunc(3)

print(mytripler(11))
33

 

참고URL

- w3schools : https://www.w3schools.com/python/python_lambda.asp

- w3schools : https://www.w3schools.com/python/gloss_python_lambda.asp

- w3schools : https://www.w3schools.com/python/python_operators.asp

- 프로그래머스(코딩테스트 연습) : https://school.programmers.co.kr/learn/courses/30/lessons/120905

 

728x90
반응형