Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-pu2rile #35

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added pu2rile/.DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion pu2rile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
| 6μ°¨μ‹œ | 2024.05.10 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [ATM](https://www.acmicpc.net/problem/11399) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909)
| 7μ°¨μ‹œ | 2024.05.10 | μ™„μ „ 탐색 μ•Œκ³ λ¦¬μ¦˜ | [μ˜ν™”κ°λ… 숌](https://www.acmicpc.net/problem/1436) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909)
| 8μ°¨μ‹œ | 2024.05.14 | 그리디 μ•Œκ³ λ¦¬μ¦˜ | [νŒ”](https://www.acmicpc.net/problem/1105) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/28#issue-2295901384)
| 9μ°¨μ‹œ | 2024.05.27 | κ΅¬ν˜„ | [μ˜€λŠ˜λ„ μ‘Œλ‹€](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288)
| 9μ°¨μ‹œ | 2024.05.27 | κ΅¬ν˜„ | [μ˜€λŠ˜λ„ μ‘Œλ‹€](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288)
| 10μ°¨μ‹œ | 2024.07.11 | μŠ€νƒ | [ν™”ν•™μ‹λŸ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
Binary file added pu2rile/μŠ€νƒ/.DS_Store
Binary file not shown.
22 changes: 22 additions & 0 deletions pu2rile/μŠ€νƒ/ν™”ν•™μ‹λŸ‰.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
chemical = input()
stack = []
atomic = {'H':1, 'C':12, 'O':16} # μ›μžλŸ‰ λ”•μ…”λ„ˆλ¦¬

for c in chemical:
if c =='(':
stack.append(c)
elif c == 'H' or c == 'C' or c == 'O':
stack.append(atomic[c]) # μž…λ ₯받은 μ›μžμ˜ μ›μžλŸ‰μ„ μŠ€νƒμ— μΆ”κ°€
elif c == ')':
temp = 0 # λ‹«λŠ” κ΄„ν˜Έλ₯Ό λ§Œλ‚˜λ©΄ temp μ΄ˆκΈ°ν™”
while True:
if stack[-1] == '(': # μŠ€νƒμ˜ top이 μ—¬λŠ” κ΄„ν˜Έλ©΄
stack.pop() # μŠ€νƒμ—μ„œ μ—¬λŠ” κ΄„ν˜Έλ₯Ό μ‚­μ œ
stack.append(temp) # μŠ€νƒμ— temp μΆ”κ°€
break
else:
temp += stack.pop() # μ—¬λŠ” κ΄„ν˜Έ μ „κΉŒμ§€μ˜ μŠ€νƒ μ•ˆμ˜ λͺ¨λ“  값을 temp에 μ €μž₯
Comment on lines +10 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ—¬κΈ°μ„œ break μ‚¬μš© λŒ€μ‹  while문에 νƒˆμΆœμ‘°κ±΄μ„ λ„£μ–΄μ£ΌλŠ” 방법도 μžˆμŠ΅λ‹ˆλ‹€.

    temp = 0
    while stack[-1] != '(':
        temp += stack.pop()
    stack.pop() 
    stack.append(temp)

else: # cκ°€ 숫자라면
stack.append(stack.pop()*int(c)) # μŠ€νƒμ˜ top κ°’κ³Ό 숫자λ₯Ό κ³±ν•˜μ—¬ μŠ€νƒμ— μΆ”κ°€

print(sum(stack))