Python

[Python] lambda 뽀개기

chillcoder 2023. 4. 27. 06:20

문제)  최소직사각형 (모든 명함을 수납할 수 있는 가장 작은 지갑)

 

 

 

 

 

 

 

 

 

 

 

 

 

풀이 1)

def solution(sizes):
    # 가로와 세로를 비교해서 세로길이가 가로길이보다 크면 돌리기
    for i in range(len(sizes)):
        if sizes[i][0] < sizes[i][1]:
            sizes[i][0], sizes[i][1] = sizes[i][1], sizes[i][0]    
    a = max(sizes, key=lambda x: x[0])[0] # 가장 큰 가로값 반환
    b = max(sizes, key=lambda x: x[1])[1] # 가장 큰 세로값 반환
    return a*b

풀이 2)

def solution(sizes):
    # 가로와 세로를 비교해서 세로길이가 가로길이보다 크면 돌리기
    for i, lst in enumerate(sizes):
        if sizes[i][0] < sizes[i][1]:
            sizes[i][0], sizes[i][1] = sizes[i][1], sizes[i][0]    
    a = max(sizes, key=lambda x: x[0])[0] # 가장 큰 가로값 반환
    b = max(sizes, key=lambda x: x[1])[1] # 가장 큰 세로값 반환
    return a*b

 

 

 

https://docs.python.org/ko/3/library/functions.html#max

 

Built-in Functions

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

docs.python.org

 

 

 

https://docs.python.org/ko/3/library/functions.html#enumerate

 

Built-in Functions

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

docs.python.org

 

 

 

 

 

 

문제) 구구단 - N을 입력받은 뒤, 구구단 N단을 출력하는 프로그램을 작성

 

풀이)

n = int(input())
print('\n'.join(map(lambda i: f'{n} * {i} = {n * i}', range(1, 10))))

 

 

문제) 상수

 

풀이)

a, b = map(int, input().split())

# reversed()함수를 정렬하기위해 list로 만들어서 다시 .join()으로 합침
c = ''.join(list(reversed(str(a))))
d = ''.join(list(reversed(str(b))))
print(max(c, d))

# 숏
print(max(input()[::-1].split()))

 

 

 

https://docs.python.org/ko/3/library/functions.html#map

 

Built-in Functions

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

docs.python.org