본문 바로가기
짜잘IT

22.08.30 백준풀기

by 정람지 2022. 8. 30.

<2504번 괄호의 값 >

저번에 99퍼센트에서 틀렸습니다 뜬 문제!

답글이 달렸다

오 그러쿠나 맨 처음부터 길이가 1이면..!틀린 문자열인데!!

세상엔 똑똑한 사람이 넘 많다!

 

 

 

 

 

 

 

 

 

 

from sys import stdin
Nlist = list(map(str,list(stdin.readline().replace('\n', ''))))

# ()   아니면 0
def this(a,b):
    Aa = 0
    if a =='(' and b==')':
        Aa = 2
    if a =='[' and b ==']':
        Aa = 3
    return Aa

# 숫자 처리 함수 1 (숫자)
def that(a,b,c):
    Aa = 0
    if a =='(' and type(b) == int and c ==')':
        Aa = b * 2
    if a =='[' and type(b) == int and c ==']':
        Aa = b * 3
    return Aa


# 숫자 처리 함수 2 숫자숫자
def thatthat(a,b):
    Aa = 0
    if type(a) == int and type(b) == int:
        Aa = a + b
    return Aa

# 처음단계 숫자 넣기
for i in range(len(Nlist)-1):
    This = this(Nlist[i],Nlist[i+1])
    if This != 0:
        Nlist[i] = This
        Nlist[i+1] = 'Nope'
    
while(1):
    try:
        Nlist.remove('Nope')
    except:
        break

# 99 반례 처리!
if len(Nlist) == 1 :
	Nlist = [0]
else:
    while(1):
        A = len(Nlist) # !!!
        # 숫자 처리1
        for i in range(len(Nlist)-2): 
            That = that(Nlist[i],Nlist[i+1],Nlist[i+2])
            if That != 0:
                Nlist[i] = That
                Nlist[i+1] = 'Nope'
                Nlist[i+2] = 'Nope'
        while(1):
            try:
                Nlist.remove('Nope')
            except:
                break
        # 숫자 처리2
        for i in range(len(Nlist)-1):
            ThatThat = thatthat(Nlist[i],Nlist[i+1])
            if ThatThat != 0:
                Nlist[i] = ThatThat
                Nlist[i+1] = 'Nope'
        while(1):
            try:
                Nlist.remove('Nope')
            except:
                break
        if len(Nlist) == 1:
            break
        if A == len(Nlist): # !!!
            Nlist = [0]
            break

for cha in Nlist:
    print(cha)

흠.. 저 반례 고쳤는데 아직도 99에서 틀렸습니다..ㅎ

도와주세요!

 

 

 

 

 

 

 

 

 


오..

from sys import stdin
Nlist = list(map(str,list(stdin.readline().replace('\n', ''))))

# ()   아니면 0
def this(a,b):
    Aa = 0
    if a =='(' and b==')':
        Aa = 2
    if a =='[' and b ==']':
        Aa = 3
    return Aa

# 숫자 처리 함수 1 (숫자)
def that(a,b,c):
    Aa = 0
    if a =='(' and type(b) == int and c ==')':
        Aa = b * 2
    if a =='[' and type(b) == int and c ==']':
        Aa = b * 3
    return Aa

# 숫자 처리 함수 2 숫자숫자
def thatthat(a,b):
    Aa = 0
    if type(a) == int and type(b) == int:
        Aa = a + b
    return Aa

# 처음단계 숫자 넣기
for i in range(len(Nlist)-1):
    This = this(Nlist[i],Nlist[i+1])
    if This != 0:
        Nlist[i] = This
        Nlist[i+1] = 'Nope'
    
while(1):
    try:
        Nlist.remove('Nope')
    except:
        break
# 99 반례 처리!
if len(Nlist) == 1 :
    if Nlist != [2] and Nlist != [3]:
	    Nlist = [0]
else:
    while(1):
        A = len(Nlist) # !!!
        # 숫자 처리1
        for i in range(len(Nlist)-2): 
            That = that(Nlist[i],Nlist[i+1],Nlist[i+2])
            if That != 0:
                Nlist[i] = That
                Nlist[i+1] = 'Nope'
                Nlist[i+2] = 'Nope'
        while(1):
            try:
                Nlist.remove('Nope')
            except:
                break
        # 숫자 처리2
        for i in range(len(Nlist)-1):
            ThatThat = thatthat(Nlist[i],Nlist[i+1])
            if ThatThat != 0:
                Nlist[i] = ThatThat
                Nlist[i+1] = 'Nope'
        while(1):
            try:
                Nlist.remove('Nope')
            except:
                break
        if len(Nlist) == 1:
            break
        if A == len(Nlist): # !!!
            Nlist = [0]
            break

for cha in Nlist:
    print(cha)

 

와아..!

굉장히 더럽고 먼가 덕지덕지 붙은 미련 넘치는 땜빵 가득한 코드지만 어쨌든 완성~!

솔맹이가 코드는 그 사람을 닮는다는데~ 정말 굉장하다~


그리고 이씨크루 9회차 모임에서 생겼던 의문점도 물어봤다.

여기 게시판 좋다 

고수님이 상주하고 있다.


id()  - 변수의 메모리 주소값을 리턴해줌

내가 의문을 가졌던

1번 케이스

B = [1, 2]
A = B
B.pop(1)

print(A, B)

# [1] [1]  출력됨

2번 케이스

 

B = [1, 2]
A = B
B = [3, 4]

print(A, B)

# [1, 2] [3, 4] 출력됨

 

- 변수의 내용을 수정해도(pop/del 같은거) 주소값은 같음 (immutable인 경우 불가)

- 재할당을 하게 되면 주소값이 달라짐

 

그래서 2번 케이스가 재할당하는 거라서 주소가 달라져서 값이 다르게 나왔구나!

 

변수 간의 대입 (내가 코딩한 거)

A에 B를 할당하면 B값이 할당되는 것이 아니라, A가 B의 메모리 주소를 바라본다!

그래서 B를 변경하면 같이 A도 바뀜

해결

리스트인 경우 슬라이싱을 통해서 값을 할당하면 새로운 id가 부여되며, 서로 영향을 받지 않음

 

B = A[:]

 

+얕은 복사(shallow copy)

  • 슬라이싱은 얕은 복사
  • A와 B의 주소는 다르지만, A[0], B[0]은 같음
  • copy 모듈의 copy 메소드 또한 얕은 복사입니다.

+깊은 복사(deep copy)

  • 깊은 복사는 내부까지 모두 새롭게 복사됨
  • copy.deepcopy메소드!
 

12. 얕은 복사(shallow copy)와 깊은 복사(deep copy)

## 1. mutable과 immutable 객체 객체에는 mutable과 immutable 객체가 있습니다. ❈ 객체 구분 표 class 설명 구분 ...

wikidocs.net


 

Curioustore

변수명 짓기, 컬럼명 짓기, 영어약자, 変数名 つけ方, カラム名建てる, 英語の略語, 命名变量, 命名该列, 英文缩写

www.curioustore.com

변수명 짓기를 도와주는 사이트

웅진싱크빅 멘토님이 알려주셨는데

영어공부가 될 것 같다.

.

.

.

솔민이가 말한 문자열 변수명 스트링치즈로 놓기 해보고 싶다.

'짜잘IT' 카테고리의 다른 글

22.09.03 백준풀이  (0) 2022.09.04
22.09.02 백준풀이  (0) 2022.09.03
22.08.27 백준풀기  (0) 2022.08.27
22.08.21 백준풀기, 개미수열  (0) 2022.08.15
22.08.14. 백준풀이  (0) 2022.08.14