0% found this document useful (0 votes)
20 views2 pages

Class HashTable

The document defines a HashTable class with methods for initializing a hash table with a given size, hashing keys, printing the table, setting key-value pairs, getting values, and retrieving keys. It also includes functions for finding duplicate numbers in a list, finding the first non-repeating character in a string, finding pairs that sum to a target, grouping anagrams, and finding subarrays with a given sum.

Uploaded by

jovcevski743
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Class HashTable

The document defines a HashTable class with methods for initializing a hash table with a given size, hashing keys, printing the table, setting key-value pairs, getting values, and retrieving keys. It also includes functions for finding duplicate numbers in a list, finding the first non-repeating character in a string, finding pairs that sum to a target, grouping anagrams, and finding subarrays with a given sum.

Uploaded by

jovcevski743
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class HashTable:

def __init__(self,size = 7):


self.data_map = [None] * size

def __hash(self,key):
my_hash = 0
for letter in key:
my_hash = (my_hash + ord(letter) *23) % len(self.data_map)
return my_hash

def print_table(self):
for i, val in enumerate(self.data_map):
print(i,": ",val)

def set(self,key,value):
index = self.__hash(key)
if self.data_map[index] == None:
self.data_map[index] = []
self.data_map[index].append([key,value])

def get(self,key):
index = self.__hash(key)
if self.data_map(index) is not None:
for i in range(len(self.data_map[index])):
if self.data_map[index][i][0] == key:
return self.data_map[index][i][1]
return None

def keys(self):
all_keys = []
for i in range(len(self.data_map)):
if self.data_map[i] is not None:
for j in range(len(self.data_map[i])):
all_keys.append(self.data_map[i][j][0])
return all_keys

def find_duplicates(lista):
num_counts = {}
for num in lista:
if num in num_counts:
num_counts[num] += 1
else:
num_counts[num] = 1

duplicates = [num for num,count in num_counts.items() if count>1]


return duplicates

def first_non_repeating_char(string):
chars_count = {}

for char in string:


chars_count[char] = chars_count.get(char,0) + 1

for char in string:


if chars_count[char] == 1:
return char
return None
def two_sums(nums,target):
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
return []

def group_anagrams(strings):
anagram_groups = {}
for string in strings:
canonical = "".join(sorted(string))
if canonical in anagram_groups:
anagram_groups[canonical].append(string)
else:
anagram_groups[canonical] = [string]
return list(anagram_groups.values())

def subarray_sum(nums, target):


sum_index = {0: -1}
current_sum = 0
for i, num in enumerate(nums):
current_sum += num
if current_sum - target in sum_index:
return [sum_index[current_sum - target] + 1, i]
sum_index[current_sum] = i
return []

You might also like