def testCollision(self):
        htable = ChainedHash(True, 10)
        trial = 1
        while not htable.has_collision:
            htable.insert(random.randint(0, 10000))
            trial += 1

        self.assertLess(trial, 80)
    def testInsertion(self):
        htable = ChainedHash()
        htable.insert(11)
        htable.insert(33)
        htable.insert(102)
        htable.insert(101)

        self.assertTrue(htable.search(11))
        self.assertTrue(htable.search(33))
        self.assertTrue(htable.search(102))
        self.assertFalse(htable.search(1012))

        self.assertTrue(htable.delete(11))
        self.assertFalse(htable.search(11))
Beispiel #3
0
from enum import Enum
from chained_hash import ChainedHash

Menu = Enum("Menu", ['add', 'remove', 'search', 'dump', 'quit'])


def select_menu() -> Menu:
    s = [f'({m.value}) {m.name}' for m in Menu]
    while True:
        print(*s, sep='    ', end='')
        n = int(input(": "))
        if 1 <= n <= len(Menu):
            return Menu(n)


hash = ChainedHash(13)  # constructing a hash table with the size of 13

while True:
    menu = select_menu()

    if menu == Menu.add:
        key = int(input("Enter a key to add: "))
        val = input("Enter a value to add: ")
        if not hash.add(key, val):
            print("Addition failed")

    elif menu == Menu.remove:
        key = int(input("Enter a key to remove: "))
        if not hash.remove(key):
            print("Remove failed.")
from enum import Enum
from chained_hash import ChainedHash

Menu = Enum('Menu', ['추가', '삭제', '검색', '덤프', '종료'])


def select_menu() -> Menu:
    s = [f'({m.value}){m.name}' for m in Menu]
    while True:
        print(*s, sep=' ', end='')
        n = int(input(': '))
        if 1 <= n <= len(Menu):
            return Menu(n)


hash = ChainedHash(13)

while True:
    menu = select_menu()

    if menu == Menu.추가:
        key = int(input('추가할 키를 입력하시오 >> '))
        val = input('추가할 값을 입력하시오 >> ')
        if not hash.add(key, val):
            print('추가에 실패')
    elif menu == Menu.삭제:
        key = int(input('삭제할 키를 입력하시오 >> '))
        if not hash.remove(key):
            print('삭제에 실패')
    elif menu == Menu.검색:
        key = int(input('검색할 키를 입력하시오 >> '))
Beispiel #5
0
from chained_hash import ChainedHash

Menu = Enum('Menu', ['추가', '삭제', '검색', '덤프', '종료'])


def select_menu() -> Menu:
    """ 메뉴 선택 """
    s = [f'({m.value}){m.name}' for m in Menu]
    while True:
        print(*s, sep='  ', end='')
        n = int(input(': '))
        if 1 <= n <= len(Menu):
            return Menu(n)


hash = ChainedHash(13)  # 크기가 13인 해시 테이블을 생성

while True:
    menu = select_menu()  # 메뉴 선택

    if menu == Menu.추가:
        key = int(input('추가할 키를 입력하세요.: '))
        val = input('추가할 값을 입력하세요.: ')
        if not hash.add(key, val):
            print('추가를 실패했습니다.')

    elif menu == Menu.삭제:
        key = int(input('삭제할 키를 입력하세요.: '))
        if not hash.remove(key):
            print('삭제를 실패했습니다.')