3 분 소요

사용 언어: Python3

문제

부스트 캠프 8기 모집

주의!
정답은 공개되어 있지 않기에 나의 풀이가 정답이 아닐수도 있다..!

풀이

나의 풀이

arr = ["INT", "INT", "BOOL", "SHORT", "LONG"]
# arr = ["INT", "SHORT", "FLOAT", "INT", "BOOL"]
# arr = ["FLOAT", "SHORT", "BOOL", "BOOL", "BOOL", "INT"]
# arr = ["BOOL", "LONG", "SHORT", "LONG", "BOOL", "LONG", "BOOL", "LONG", "SHORT", "LONG", "LONG"]

tot_byte = 0 # 출력하는 메모리 최대 크기는 128바이트 = 16비트
res = []
cur_byte = '' # 현재 만들고 있는 바이트
for i, x in enumerate(arr):
    if x == 'INT':
        if len(cur_byte) + 8 <= 8: # 8비트 할당했다고 가정하고 남은 크기 확인
            res.append('#' * 8)
            tot_byte += 1
        else:
            cur_byte += '.' * (8 - len(cur_byte))
            res.append(cur_byte)
            res.append('#' * 8)
            tot_byte += 2  # 패딩으로 비트 수 꽉 채운 cur_byte와 INT
            cur_byte = ''
    elif x == 'LONG':
        if len(cur_byte) + 8 <= 8: # 8비트 할당했다고 가정하고 남은 크기 확인
            res.append('#' * 8)
            res.append('#' * 8)
            tot_byte += 2
        else:
            cur_byte += '.' * (8 - len(cur_byte))
            res.append(cur_byte)
            res.append('#' * 8)
            res.append('#' * 8)
            tot_byte += 3  # 패딩으로 비트 수 꽉 채운 cur_byte와 LONG
            cur_byte = ''
    elif x == 'BOOL':
        if len(cur_byte) + 1 <= 8:
            cur_byte += '#'
        else:
            res.append(cur_byte) # bool이 못 들어가면 이미 8비트 꽉 찬 경우
            tot_byte += 1
            cur_byte = '#'
    elif x == 'SHORT':
        if i > 0 and arr[i-1] == 'BOOL':
            cur_byte += '.' # 패딩
            if len(cur_byte) == 8:
                res.append(cur_byte)
                tot_byte += 1 # 완성 시킨 것 넣기
                cur_byte = '#' * 2
            else:
                cur_byte += '#' * 2
        else:
            if len(cur_byte) + 2 <= 8:
                cur_byte += '#' * 2
            else:
                cur_byte += '.' * (8 - len(cur_byte)) # 패딩으로 꽉 채워서
                res.append(cur_byte)
                tot_byte += 1
                cur_byte = '#' * 2
    elif x == 'FLOAT':
        if i > 0 and (arr[i-1] == 'BOOL' or arr[i-1] == 'SHORT'):
            cur_byte += '.' * (4 - len(cur_byte)) # 패딩
            cur_byte += '#' * 4 # FLOAT 넣기
            res.append(cur_byte)
            tot_byte += 1
            cur_byte = ''
        else:
            if len(cur_byte) + 4 <= 8:
                cur_byte += '#' * 4
            else:
                cur_byte += '.' * (8 - len(cur_byte)) # 패딩으로 꽉 채워서
                res.append(cur_byte)
                tot_byte += 1
                cur_byte = '#' * 4
    if tot_byte > 16:
        print('HALT')
        break # cut edge
else:
    if len(cur_byte) != 0: # 남은 것 처리
        cur_byte += '.' * (8 - len(cur_byte)) # 나머지는 패딩으로 채워서
        res.append(cur_byte)
        tot_byte += 1
    if tot_byte <= 16:
        print(res)
    else:
        print('HALT')

  • 테스트 케이스: 통과

다른 풀이

def solution(arr):
    answer = ""
    tmp = "" # 현재 만들고 있는 바이트
    for t in arr:
        if t == "BOOL":
            tmp += "#"
            if len(tmp) == 8:
                answer += tmp + ","
                tmp = ""
        elif t == "SHORT":
            n, mod = divmod(8 - len(tmp), 2)
            if n > 0:
                tmp += mod * "." + "#" * 2
                if len(tmp) == 8:
                    answer += tmp + ","
                    tmp = ""
            else:
                tmp += (8 - len(tmp))*"."
                answer += tmp
                tmp = "##"
        elif t == "FLOAT":
            n, mod = divmod((8-len(tmp)), 4)
            if n > 0:
                tmp += mod * "." + "#" * 4
                if len(tmp) == 8:
                    answer += tmp + ","
                    tmp = ""
            else:
                tmp += (8 - len(tmp)) * "."
                answer += tmp
                tmp = "#" * 4
        elif t == 'INT':
            if tmp != "":
                answer += tmp + (8 - len(tmp)) * "." + ","
                tmp = ""
            answer += "#" * 8 + ","
        elif t == "LONG":
            if tmp != "":
                answer += tmp + (8 - len(tmp)) * "." + ","
                tmp = ""
            answer += "#" * 8 + "," + "#" * 8 + ","

        if len(answer) > 128:
            return "HALT"

    if tmp != "":
        tmp += (8 - len(tmp)) * "." # 남은 것 붙이기
        answer += tmp
    else:
        answer = answer[:-1]

    return answer if len(answer) <= 128 else "HALT"

print(solution(["INT", "INT", "BOOL", "SHORT", "LONG"]))
print(solution(["INT", "SHORT", "FLOAT", "INT", "BOOL"]))
print(solution(["FLOAT", "SHORT", "BOOL", "BOOL", "BOOL", "INT"]))
print(solution(["BOOL", "LONG", "SHORT", "LONG", "BOOL", "LONG", "BOOL", "LONG", "SHORT", "LONG", "LONG"]))
  • 테스트 케이스: 통과

IMG_0470



💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기