편집 기록

편집 기록
  • 프로필 HIAOAIH님의 편집
    날짜2021.02.14

    파이썬 해쉬 테이블 질문


    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라는 오류가 뜹니다.

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

  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.02.14

    파이썬 해쉬 테이블 질문


    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라는 오류가 뜹니다.

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