UnsortedList - Python

이세진·2022년 4월 3일
0

Computer Science

목록 보기
31/74

생성일: 2021년 9월 29일 오후 4:53

C++로 구현한 코드를 파이썬으로 구현해보았다.

from enum import Enum

MAX_ITEMS = 100

class Compare(Enum):
    LESS = 0
    GREATER = 1
    EQUAL = 2

class ItemType:
    """ Item Type """

    def __init__(self, val):
        self.value = val

    def compared_to(self, otherItem):
        if self.value < otherItem.value:
            return Compare.LESS
        elif self.value > otherItem.value:
            return Compare.GREATER
        return Compare.EQUAL 

    def __str__(self):
        return str(self.value)

class UnsortedType:
    """ Chapter 3: Unsorted List """

    def __init__(self):
        self.length = 0
        self.info = []
        self.current_pos = -1

    def make_empty(self):
        self.length = 0

    def length_is(self):
        return self.length

    def is_full(self):
        if self.length == MAX_ITEMS:
            return True
        return False

    def insert_item(self, item):
        self.info.append(item)
        self.length += 1

    def retrieve_item(self, item):
        moreToSearch = bool
        location = 0
        found = False
        moreToSearch = (location < self.length)
        
        while moreToSearch and not found:
            if item.compared_to(self.info[location]) == Compare.LESS:
                location += 1
                moreToSearch = (location < self.length)
            elif item.compared_to(self.info[location]) == Compare.GREATER:
                location += 1
                moreToSearch = (location < self.length)
            elif item.compared_to(self.info[location]) == Compare.EQUAL:
                found = True
                break
            else:
                print("Can not compare.")
                break
        return found
                

    def delete_item(self, item):
        location = 0

        while item.compared_to(self.info[location]) != Compare.EQUAL:
            location += 1
        self.info[location] = self.info[self.length - 1]
        self.length -= 1

    def reset_list(self):
        self.current_pos = -1

    def get_next_item(self):
        self.current_pos += 1
        return self.info[self.current_pos]

    def __str__(self):
        self.reset_list()
        
        print_list = []
        for i in range(self.length):
            print_list.append(str(self.get_next_item()))
        
        return str(" ".join(print_list))
import os
from UnsortedType import *

if __name__ == '__main__':
    l = UnsortedType()
    f = open('./data.txt', 'r')
    line = f.readline()
    while line:
        t = int(line)
        i = ItemType(t)
        l.insert_item(i)
        line = f.readline()
    f.close()
    
    print("Before:")
    print(l)
    
    print("After deleting 65:")
    a = ItemType(65)
    l.delete_item(a)
    print(l)
    print()
    
    a = ItemType(3)
    if l.retrieve_item(a) == True:
        print(str(a) + " is in the list.")
    else:
        print(str(a) + " is not in the list.")
        
    a = ItemType(2)
    if l.retrieve_item(a) == True:
        print(str(a) + " is in the list.")
    else:
        print(str(a) + " is not in the list.")
profile
나중은 결코 오지 않는다.

0개의 댓글