과제, 프로젝트

Python 개인 과제 Level 3.

Wat_zy 2025. 9. 19. 20:22
 

문제 1

def normalize_weights(weight_list):
    new_list = []
    for weight in weight_list:
          if "kg" in weight:
               weight = weight.replace("kg","")
               weight = float(weight)
               weight = int(weight * 1000)
               weight = (str(weight)+ "g")
               new_list.append(weight)
          else: #weight 문자가 g인 경우
               new_list.append(weight)
    return new_list

# 제품 무게 리스트
weight_list = ["0.5kg", "500g", "1kg", "750g", "2kg"]
# 단위 통일 실행
normalize_weights(weight_list)

# 결과
['500g', '500g', '1000g', '750g', '2000g']

in과 replace 사용법을 검색을 통해 알게 된 이후 어느 정도 코드 작성은 완료되었지만, g로 통일되게 한 리스트를 하나만 결과로 보고 싶었지만 들여쓰기를 계속해서 실수하여 반복문 속에 속하게 하거나 아예 결과가 나타나지 않고 들여쓰기 오류가 발생하는 것으로 나타내어 시간이 오래 걸렸다. tap키를 활용하여 들여쓰기를 진행하였는데 for 문과 1칸의 차이가 발생하여 계속해서 들여쓰기 문제가 반복되는 것을 몰랐던 것을 기억하며 코드를 하나하나 천천히 읽으면서 어느 부분에서 오류가 발생하였는지 코드를 보는 연습이 필요하겠다는 생각이 들었다.

 

문제 2

def equipment_code_decoder(code_list):
    for code in code_list:
        converted_code = code
        if "영" in code:
            converted_code = converted_code.replace("영", "0")
        if "일" in code:
            converted_code = converted_code.replace("일", "1")
        if "이" in code:
            converted_code = converted_code.replace("이", "2")
        if "삼" in code:
            converted_code = converted_code.replace("삼", "3")
        if "사" in code:
            converted_code = converted_code.replace("사", "4")
        if "오" in code:
            converted_code = converted_code.replace("오", "5")
        if "육" in code:
            converted_code = converted_code.replace("육", "6")
        if "칠" in code:
            converted_code = converted_code.replace("칠", "7")
        if "팔" in code:
            converted_code = converted_code.replace("팔", "8")
        if "구" in code:
            converted_code = converted_code.replace("구", "9")
        
        print(f"[{code}]에 대한 설비 코드는 [{converted_code}]입니다.")

# 설비 코드 목록
code_list = [
    "삼5이사",
    "0오6칠",
    "48삼구",
    "이74팔",
    "9일이삼"
]

# 코드 변환 함수 실행
equipment_code_decoder(code_list)

# 결과
[삼5이사]에 대한 설비 코드는 [3524]입니다.
[0오6칠]에 대한 설비 코드는 [0567]입니다.
[48삼구]에 대한 설비 코드는 [4839]입니다.
[이74팔]에 대한 설비 코드는 [2748]입니다.
[9일이삼]에 대한 설비 코드는 [9123]입니다.

코드를 작성하면서 한글 숫자를 아라비아 숫자로 변환하는 코드는 쉽게 작성할 수 있었지만, print() 내에서 작동하는 코드는 각 객체에 대한 내용을 converted_code로 변환한 내용을 전달하는 것인데 code가 list 전체를 변환하여 알려주면서 코드 수정에 시간이 조금 걸렸다. 아예 처음에 code를 바꾸는 것 없이 converted_code 변수에 넣어두고 converted_code만 아라비아 숫자로 변환하는 과정을 거치게 하면 원하는 결과를 찾아볼 수 있게 되었다. 

문제 3

import math
# 장비별 이동 거리 계산 함수

def calculate_single_distance(machine_positions):
    for machine, positions in machine_positions.items():
        # 위치는 항상 두 개의 좌표만 제공됨
        x1, y1 = positions[0]
        x2, y2 = positions[1]
        distance = math.sqrt((positions[1][0]-positions[0][0])**2 
        + (positions[1][1]-positions[0][1])**2)
        print(f"{machine}의 이동 거리: {distance:.2f} 미터")

 # 장비별 위치 데이터
machine_positions = {
    "Machine A": [(0, 0), (5, 5)],
    "Machine B": [(2, 2), (6, 8)],
    "Machine C": [(0, 0), (3, 4)]
}

# 이동 거리 계산 실행
calculate_single_distance(machine_positions)

 

코드는 어느 정도 잘 구성하여 작동은 하였지만 distance 식 내에서 계산 실수가 있어 계속 잘못된 값이 나왔었다. 이때, 정리가 제대로 되어 있지 않아서 코드를 보면서도 이상함을 느끼지 못하고 왜 계산이 다르게 나오는지 이해를 하지 못하였다. 이를 통해 코드가 길어지면 어느 정도 정리를 하면서 진행해야 결과가 다르게 나왔을 때 문제점을 쉽게 찾을 수 있을 것이라는 생각이 들었다.

'과제, 프로젝트' 카테고리의 다른 글

Library 개인 과제 Level 3.  (0) 2025.10.23
Library 개인 과제 Level 2.  (0) 2025.10.23
Library 개인 과제 Level 1.  (0) 2025.09.26
Python 개인 과제 Level 2.  (0) 2025.09.19
Python 개인 과제 Level 1.  (0) 2025.09.19