제주코딩베이스캠프 Python 81~88제

이하연·2020년 9월 3일
1

문제81 : 지뢰찾기

minesweeper = '0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0'
minesweeper = minesweeper.replace('1','f')
minesweeper = minesweeper.split(' ')


for i in range(len(minesweeper)) :

    if minesweeper[i] == 'f' :
        if i % 5 > 0 :
            minesweeper[i-1] = '*'
        if (i+1) % 5 > 0 :
            minesweeper[i+1] = '*'
        if i > 4 :
            minesweeper[i - 5] = '*'
        if i < 20 :
            minesweeper[i + 5] = '*'

for i in range(len(minesweeper)) :
    if i%5==0 and i!=0:
        print()
    print(minesweeper[i],end='')
출력 : *f*00
      0*0*0
      00*f*
      0*f*0
      00*00
      

문제 82 : 수학 괄호 파싱

  • 문자열 길이만큼 for문 돌리면서 '('가 나오면 스택에 추가
  • ')'가 나오면 스택에서 제거
  • 지울 때 스택에 아무것도 없으면 올바르지 않은 괄호니까 리턴 False
  • for문 다 돌았는데 마지막에 스택이 비어있지 않으면 리턴 False

  • is는 변수가 같은 Object(객체)를 가리키면 True
  • ==는 변수가 같은 Value(값)을 가지면 True
def math(ex) :
    stack = []

    for i in range(len(ex)) :
        if ex[i] == '(':
            stack.append(i)

        elif ex[i] == ')':
            if len(stack) == 0:
                return False
            stack.pop()

    if len(stack) != 0:
        return False

    return True

ex = list("3+5")
print(math(ex))

문제 83 : 수학 괄호 파싱2 .. 틀림 다시

def mathSub(ex) :
    stack = []
    for i in range(len(ex)) :
        if ex[i] == '(':
            stack.append(i)
        elif ex[i] == '}' :
            return False
        elif ex[i] == ')' :
            if len(stack) == 0:
                return False
            stack.pop()


    if len(stack) != 0:
        return False

    return True

def mathMain(ex) :
    stack = []
    for i in range(len(ex)) :
        if ex[i] == '{':
            stack.append(i)
            if mathSub(list(ex[i+1:])) is False:
                return False
        elif ex[i] == '}':
            if len(stack) == 0:
                return False
            stack.pop()
        else :
            return mathSub(list(ex[i + 1:]))

    if len(stack) != 0:
        return False

    return True



ex = list("(5+7){*(3*5)}")
print(mathMain(ex))

문제 84 : 숫자 뽑기

from itertools import permutations

num = 1723
k = 2

perNum = list(permutations(list(str(num)),k))

perList = []
for i in perNum :
    str = ''
    for j in i :
        str+=j
    perList.append(str)

print(max(perList))

문제 85 : 숫자 놀이

def test(ex) :
    maxNum = max([int(i) for i in str(ex)])
    countNum = [ str(i)+str(str(ex).count(str(i))) for i in range(1,maxNum+1) ]
    return ''.join(countNum)

def soultion(N):
    num = 1
    if N == 1 :
        return num
    else :
        for i in range(N-1) :
            num = test(num)
    return num
N = 6
print(soultion(N))
  • 이전의 숫자가 누적이 되어서 해당 숫자의 갯수와 함께 출력
  • N=1 이면 그대로 1
  • N!=1이면 N만큼 for문을 돌린다.
  • 넘겨준 숫자에서 가장 최대 숫자를 뽑아낸다
  • 이를 이용하여 숫자의 count를 해당 숫자와 합쳐 리스트로 만든다.
  • 이를 join을 이용하여 합쳐 출력

문제 86 : 회전 초밥

point = [1,1,3,2,5]
#point = [5,2,3,1,2,5]
dish = 1

def solution(point,dish) :
    dish-=1
    cnt = 0
    pointSort = sorted(point)

    while True :
        print(dish)
        firstDish = point.pop(0)
        if pointSort[0] == firstDish :
            if dish == 0 :
                break
            dish-=1
            pointSort.pop(0)
        else :
            point.append(firstDish)
            dish = len(point)-1 if dish == 0 else dish - 1
        cnt+=1
    return cnt

print("cnt : ",solution(point,dish))

문제 87 : 천하 제일 먹기 대회

name = ['손오공','야모차','메지터','비콜로']
point = [70,10,55,40]

def solution(name,point) :
    d = {}
    z = list([i,j] for i,j in zip(name,point))
    z = sorted(z,key=lambda z:z[1], reverse=True)

    for i in range(len(z)) :
        d[z[i][0]] = i+1

    return d
print(solution(name,point))

문제 88 : 지식이의 게임개발

1개의 댓글

comment-user-thumbnail
2020년 9월 3일

열심히 하는 모습 보기 좋네요 👍🏼

답글 달기