Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스 푸드 파이트 대회
- getline()함수
- const l-value참조자
- 유형 변환
- C언어 스택 연산
- 괄호 검사 프로그램
- 운영체제 기능
- 회전 및 자리 이동 연산
- 범위 기반 for문
- C언어 계산기 프로그램
- 백준 파이썬
- 값/참조/주소에 의한 전달
- 프로그래머스 배열만들기4
- 입출력 관리자
- 원형 연결 구조 연결된 큐
- 문제해결 단계
- r-value참조자
- c언어 괄호검사
- LAN의 분류
- const화
- 문자형 배열
- 알고리즘 조건
- 주기억장치
- 네트워크 결합
- auto 키워드
- 논리 연산
- string유형
- l-value참조자
- IPv4 주소체계
- C언어 덱
Archives
- Today
- Total
chyam
[프로그래머스 Lv2,python] - [카카오 인턴] 수식 최대화 본문
https://school.programmers.co.kr/learn/courses/30/lessons/67257
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
from itertools import permutations
def operate(expression):
operator=[]
if '-' in expression:
operator.append('-')
if '+' in expression:
operator.append('+')
if '*' in expression:
operator.append('*')
return operator
def divide(expression): # 각 숫자와 연산자들로 나누기
new_list=[]
idx,front_idx=0,0
while idx<len(expression):
if expression[idx].isdigit() == False: # 연산자이면
new_list.append(expression[front_idx:idx]) # 앞의 숫자를 추가
new_list.append(expression[idx]) # 연산자도 추가
front_idx=idx+1
idx+=1
new_list.append(expression[front_idx:])
return new_list
def calc(a,b,op): # 계산
a,b= int(a),int(b)
if op=="+":
return a+b
if op=="-":
return a-b
if op=="*":
return a*b
def solution(expression):
operator = operate(expression) # 표현식 안에 있는 연산자
priorities = list(permutations(operator,len(operator))) # 연산자 우선순위
new_exp=divide(expression)
exp_tmp=new_exp[:] # 처음 값 저장
sums=[]
for priority in priorities:
new_exp= exp_tmp[:] # 처음 값 저장
i=0 # 우선순위
while i<len(priority): # 우선순위대로 다 돌기
stack=[] # 피연산자와 연산자 저장
while True:
if new_exp[0].isdigit(): # 숫자이면 스택에 추가
stack.append(new_exp[0])
else: # 연산자일때
if new_exp[0]==priority[i]: # 우선순위가 가장 높으면
front=stack.pop() # 앞 숫자
stack.append(str(calc(front,new_exp[1],priority[i]))) # 앞 숫자와 뒤 숫자 연산한 값을 스택에 추가
del new_exp[:1]
else: # 우선순위가 낮으면
stack.append(new_exp[0]) # 추가
new_exp.pop(0) # 없애주기
if len(new_exp)==0: # 비었으면 멈추기
break
new_exp=stack # 스택의 값을 다시 저장해주기
i+=1 # 1순위 제거
sums.append(abs(int(new_exp[0]))) # 절대값을 저장
return int(max(sums)) # 최댓값
'프로그래머스 > LV2' 카테고리의 다른 글
[프로그래머스 Lv2,python] - 행렬 테두리 회전하기 (0) | 2025.03.19 |
---|---|
[프로그래머스 Lv2,python] - 테이블 해시 함수 (0) | 2025.03.18 |
[프로그래머스 Lv2, python] - 괄호 변환 (0) | 2025.03.14 |
[프로그래머스 Lv2,python] - 줄 서는 방법 (0) | 2025.03.13 |
[프로그래머스 Lv2, python] - 리코쳇 로봇 (0) | 2025.03.12 |