jm_p_op

모의고사 해설2 본문

수학/알고리즘

모의고사 해설2

jm_p_op 2023. 3. 25. 14:15

https://school.programmers.co.kr/learn/courses/30/lessons/172928#

# https://school.programmers.co.kr/learn/courses/30/lessons/172928#

park = ["SOO", "OOO", "OOO"]
routes = ["E 2", "S 2", "W 1"]


class DogP:
    def __init__(self, pl):
        self.place = pl

    def movedog(self, direction, park):
        # 방향설정
        match(direction[0]):
            case("E"):
                dir = [0, 1]
            case("W"):
                dir = [0, -1]
            case("S"):
                dir = [1, 0]
            case("N"):
                dir = [-1, 0]
            case __:
                dir = [0,0]
        # 움직임 검사

        try:
            check_place_0 = self.place[0]
            check_place_1 = self.place[1]
            if check_place_0+dir[0]*int(direction[2]) >= 0 and len(park) > check_place_0+dir[0]*int(direction[2]):
                # print("1번")
                for i in list(range(int(direction[2]))):
                    if park[check_place_0+dir[0]*(i+1)][check_place_1] == "X":
                        #print("bug")
                        raise

            else:
                #print("2번")
                raise
            if check_place_1+dir[1]*int(direction[2]) >= 0 and len(park[0]) > check_place_1+dir[1]*int(direction[2]):
                # print("3번")
                for i in list(range(int(direction[2]))):
                    if park[check_place_0][check_place_1+dir[1]*(i+1)] == "X":
                        #print("bug")
                        raise
            else:
                # print("4번")
                raise
            # 블록이동
            # print("5번")
            self.place = [check_place_0 + dir[0] *
                          int(direction[2]), check_place_1+dir[1]*int(direction[2])]

        except:
            pass


def solution(park, routes):
    # 시작 좌표
    for i in park:
        if (i.find("S") >= 0):
            dogp = DogP([park.index(i), i.index("S")])
            for route_dir in routes:
                dogp.movedog(route_dir, park)
            break

    return dogp.place


print(solution(park, routes))

python에서 실행시 잘 작동하나 프로그래머스에선 match가 작동 안한다.

        # 방향설정
        if direction[0]=="E":
            dir = [0,1]
        elif direction[0]=="W":
            dir = [0,-1]
        elif direction[0]=="S":
            dir = [1,0]
        elif direction[0]=="N":
            dir = [-1,0]
        else :
            dir = [0,0]

 

match/case - python에서의 swith/case (python 3.10이상)

match(direction[0]):
            case("E"):
                dir = [0, 1]
            case("W"):
                dir = [0, -1]
            case("S"):
                dir = [1, 0]
            case("N"):
                dir = [-1, 0]
            case __:
                dir = [0,0]
  • case __ : 예외 처리

 

except - try/except 도중 try에서 예외 처리, 고의적 오류

pass - 함수뒤 아무것도 없을시 사용

try:
	raise
except:
    pass

class문

class ClassF:
    def __init__(self, pl):
        self.place = pl
    def class_f(self, direction, park):
    	self.place = f(self.place,direction, park)
        
a=ClassF(pla)
#a.place의 값 pla
b=ClassF(plb)
#b.place의 값 plb
a.class_f(self.place,direction, park)
#a.place의 값 f(a.place,direction, park)
b.class_f(self.place,direction, park)
#b.place의 값 f(b.place,direction, park)

__init__ - 초기값설정

self - 설정변수 ( a,b)

f() - 함수

 

'수학 > 알고리즘' 카테고리의 다른 글

모의고사 해설 4. 둘만의 암호  (0) 2023.04.20
모의고사 해설 3. 방의갯수  (0) 2023.04.18
.py 알고리즘 모의고사 코딩리뷰 + 수정  (0) 2023.04.05
모의고사 해설 3. array  (0) 2023.04.03
모의고사 해설  (1) 2023.03.20