1. 직사각형 넓이 구하기
def solution(dots):
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = dots
a = abs(x1 - x2) if x1 != x2 else abs(x1 - x3)
b = abs(y1 - y2) if y1 != y2 else abs(y1 - y3)
return a*b
↓ 삼항 연산자 활용해보기
def solution(dots):
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = dots
a, b = abs(x1 - x2) or abs(x1 - x3), abs(y1 - y2) or abs(y1 - y3)
return a * b
2. 문자열 최빈값 구하기
input = "hello my name is sparta"
def find_max_occurred_alphabet(string):
a = {}
for i in string:
if i not in a and i.isalpha():
a[i] = 1
elif i in a and i.isalpha():
a[i] += 1
cnt = max(a.values())
frequency = ''
for j,k in a.items():
if k == cnt:
frequency = j
break
return (f"{frequency}가 {cnt}회로 가장 많이 나왔습니다")
result = find_max_occurred_alphabet(input)
print(result)
↓ lambda, filter 활용해보기
input = "hello my name is sparta"
def find_max_occurred_alphabet(string):
a = {}
for i in string.lower():
if i.isalpha():
if i in a:
a[i] += 1
else:
a[i] = 1
cnt = max(a.values())
frequency = filter(lambda x: a[x] == cnt, a.keys())
return (f"{next(frequency)}가 {cnt}회로 가장 많이 나왔습니다")
result = find_max_occurred_alphabet(input)
print(result)
'WIL&TIL > TIL' 카테고리의 다른 글
20230404 TIL - 알고리즘, 2주차 숙제 (0) | 2023.04.04 |
---|---|
20230403 TIL (0) | 2023.04.03 |
20230331 TIL 알고리즘과 아이컨택하기 (2) | 2023.03.31 |
20230330 TIL class 아직도 모르는 사람 (1) | 2023.03.30 |
20230329 TIL 또 나만 모르는 VSCODE 단축키, 프로그래머스 문제풀이 (0) | 2023.03.29 |
댓글