파이썬 해쉬 테이블 질문

조회수 581회

chaining 기법을 이용해 해쉬충돌이 발생해도 정상적으로 실행되어야 한다고 합니다.

def hash_func(key):
    return ord(key[0]) % 10

class HashTable:
    def __init__(self):
        self.table = [None]*10

    def set(self, key, value):
        self.table[hash_func(key)] = value

    def get(self, key):
        return self.table[hash_func(key)]

class Node:

    def __init__(self, key, data):
        self.key = key
        self.data = data
        self.next = None

class ChainedHashTable(HashTable):

    index_key = get_key(data)
    hash_address = hash_function(index_key)
    if hash_table[hash_address] != 0:
        for index in range(len(hash_table[hash_address])):
            if hash_table[hash_address][index][0] == index_key:
                hash_table[hash_address][index][1] = value
                return
        hash_table[hash_address].append([index_key, value])
    else:
        hash_table[hash_address] = [[index_key, value]]



ht = ChainedHashTable()

ht.set('hello', 1)

ht.set('hello2', 2)

ht.set('hello3', 3)

ht.set('hello4', 4)

print(ht.get('hello'), end=' ')

print(ht.get('hello2'), end=' ')

print(ht.get('hello3'), end=' ')

print(ht.get('hello4'), end=' ')

print()

ht.set('hello2', 5)

print(ht.get('hello'), end=' ')

print(ht.get('hello2'), end=' ')

print(ht.get('hello3'), end=' ')

print(ht.get('hello4'), end=' ')

위 코드를 실행했는데 return outside function 이라는 오류가 뜨고 return을 외부로 빼내면 unexpected indent라는 오류가 뜹니다.

혹시 어떻게 해결하는지 알 수 있을까요?

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)