Тёмный

Hash Tables in Python 

PageKey
Подписаться 2 тыс.
Просмотров 35 тыс.
50% 1

Опубликовано:

 

9 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 49   
@jingyuchang1885
@jingyuchang1885 6 лет назад
Thank you for your effort to make the video! It really helped me!
@amorestperpe
@amorestperpe 4 года назад
I wish I could follow what you were doing. Seemed like you did a really good job, I'm just not at the level to follow yet.
@PageKey
@PageKey 4 года назад
Thanks for your comment! Let me know if I can do anything to help you understand.
@CaptainLungi
@CaptainLungi 4 года назад
This was a great explanation and using linked list. Thank you for making for this video!
@PageKey
@PageKey 4 года назад
Thanks for your comment! Glad you enjoyed!
@shawn3713
@shawn3713 5 лет назад
good explainer, thanks! your teaching is very good
@ionutz31tkd
@ionutz31tkd 4 года назад
Your hash method returns 1 for every character in the alphabet. This makes it hard to solve the 3 challenges you proposed. I had to modify it you " * ord(c)" instead of " ** ord(c)". For full strings works fine. Besides that, great implementation! Thank you!
@sabinkstudio
@sabinkstudio 5 лет назад
Dude, thanks for the video. What would make this better, is putting it into action on some data and having a look at the outputs. Then I can really understand what each part of the Class is doing and apply/tweak to my issue.
@PageKey
@PageKey 5 лет назад
Thanks for the feedback! I may have something like this coming down the road - I'll let you know when it's available!
@sabinkstudio
@sabinkstudio 5 лет назад
@@PageKey noice wann :)
@anirvansen2941
@anirvansen2941 4 года назад
Great content, One thing I wanted to share that we can decrease the insert time to the bucket in O(1) - Constant time by inserting an element to the head(Front) of the LinkedList.Thanks
@bu443
@bu443 6 лет назад
Subscribed, You helped me so much. Thank you a lot.
@anjelpatel36
@anjelpatel36 4 года назад
I thought my monitor suddenly had very thick bezels. Great video btw.
@mundeepcool
@mundeepcool 3 года назад
Oh yeah yeah
@gabrielcavalcante4063
@gabrielcavalcante4063 5 лет назад
Fantastic video. Thank you.
@jobelixte
@jobelixte 5 лет назад
This was a great explainer. Thanks!
@PageKey
@PageKey 5 лет назад
I'm glad it helped you! Thanks for your comment!
@LakshmiKarthikFilms
@LakshmiKarthikFilms 3 года назад
Good One... It would be great if you add the print function, which prints the buckets with the inserted key, values
@PageKey
@PageKey 3 года назад
Great idea, thanks! Maybe this can be added in a future version. 🙂
@ClydeHoadley
@ClydeHoadley 5 лет назад
Updated URL to blog article: blog.pagekeysolutions.com/2017/11/24/how-to-implement-a-hash-table-in-python
@hisaohorii7320
@hisaohorii7320 4 года назад
Hey ! I was wondering if you could review 2 quick hashtable classes I've made. They're using the djb2, crc32 and K&R algorithm for the hash function and your video helped me a lot so feedback would really mean a lot to me :) Thanks for your superb videos !
@PageKey
@PageKey 4 года назад
Hey, thanks a lot! If you post it on GitHub or Gist or something I'll definitely review, but I haven't looked into any of those hashing algorithms. Sounds like you're ahead of me :)
@08himanshu
@08himanshu 6 лет назад
There is an issue with the remove function. Its not working properly. Suppose if the node is first node, then it is not handling the case properly and secondly the error is coming because the prev is not initialised.
@PageKey
@PageKey 6 лет назад
I agree fully. Thanks for pointing this out! Someone noticed the same thing and made an issue on GitHub. Check out this link to see the fix and explanation: github.com/stephengrice/youtube/issues/2
@benjaminfloyd3112
@benjaminfloyd3112 4 года назад
Your delete method does not initialize prev if the first node is a match. a prev = None declaration is missing.
@PageKey
@PageKey 2 года назад
Great point. I'll have to remake these videos at some point. There are definitely some mistakes in them
@m3hdim3hdi
@m3hdim3hdi 4 года назад
thanks for the video but I think in line 52 there is a mistake because prev can never be None edit: I think you should initialize prev with None
@russellzheng5782
@russellzheng5782 4 года назад
Can the actual hash function you are using be replaced by any of more popular ones like SHA- or MD-? I assume for the scope of the example problems it shouldnt matter?
@PageKey
@PageKey 4 года назад
Yes, I believe that the hash function should be interchangeable with others. Good point - Thanks for the comment!
@kvelez
@kvelez 10 месяцев назад
INITIAL_CAPACTIY = 50 class Node: def __init__(self, key, value): self.key = key self.value = value self.next = Node class HashTable: def __init__(self): self.capacity = INITIAL_CAPACTIY self.size = 0 self.buckets = [None] * self.capacity def hash(self, key): hash_sum = 0 for idx, c in enumerate(key): hash_sum += (idx + len(key)) ** ord(c) hash_sum %= self.capacity return hash_sum def insert(self, key, value): self.size += 1 index = self.hash(key) node = self.buckets[index] if node is None: self.buckets[index] = Node(key, value) return prev = node while node is not None: prev = node node = node.next prev.next = Node(key, value) def find(self, key): index = self.hash(key) node = self.buckets[index] while node is not None and node.key != key: node = node.next if node is None: return None else: return node.value def remove(self, key): index = self.hash(key) node = self.buckets[index] while node is not None and node.key != key: prev = node node = node.next if node is None: return None else: self.size -= 1 result = node.value if prev is None: node = None else: prev.next = prev.next.next return result
@hakankanplay
@hakankanplay 5 лет назад
how would you define the character code?
@PageKey
@PageKey 5 лет назад
hakan topaloglu I don't think I understand your question. What do you mean?
@makhus8337
@makhus8337 5 лет назад
blog is unavailable
@PageKey
@PageKey 5 лет назад
Sorry, I'm the worst with updating links. Here's the new one: linebylinecode.com/2017/11/24/how-to-implement-a-hash-table-in-python
@christys9482
@christys9482 5 лет назад
Hi, the link of the blog article is 404 Not Found
@PageKey
@PageKey 5 лет назад
Whoops, my mistake. Thanks for pointing that out! I fixed the link: blog.pagekeysolutions.com/2017/11/24/how-to-implement-a-hash-table-in-python
@Adminadmin-wh5cn
@Adminadmin-wh5cn 5 лет назад
@@PageKey It still show 404 Not Found
@tlili_belga
@tlili_belga 4 года назад
@@Adminadmin-wh5cn link works fine
@almuntasirabir4511
@almuntasirabir4511 5 лет назад
why the hashsum is so convoluted? couldn't we just have used a simpler coding?
@PageKey
@PageKey 5 лет назад
Muntasir, thanks for the question - great point. The hashsum is definitely the part of the hashtable where you have the most freedom in how you implement it. The simplest possible way of converting your "key" to an index in the internal array is to always return 0. If we did this, we would just have a linked list, because everything would be under the first position, or "bucket" in the array. When two keys are stored under the same "bucket", it's called a collision. In the above example, inserting an element into the hash table would cause a collision 100% of the time. A goal of the hashsum is avoid these collisions, keeping the hash table's performance as close to O(1) as possible. With everything under a LinkedList, you're getting O(n) performance instead (at least for the find() operation). To avoid collisions, you have to come up with a clever way of making sure that all the keys you're supplied with are evenly distributed throughout the internal array. If I remember, in this video, we simply added up the values of the character codes for the string. This was the example I've always seen used, but I know that there are other, more complex solutions out there that probably do a much better job. I just don't know what they are :) Another way could be just generating a pseudo-random number to store the item under, using a built-in Math.random() function or something like that. It would be simpler to code, and it would probably be a good enough quick-and-dirty solution, at least as a learning tool. Please let me know if this answers your question, and don't be afraid to ask if you have more. I'm no expert, but I can try my best to answer :)
@almuntasirabir4511
@almuntasirabir4511 5 лет назад
@@PageKey thanks a lot you explained it very well . i was little confused about that hash function . now its clear.
@PageKey
@PageKey 4 года назад
@Duke Wellington again, I'm no expert, but I'm willing to bet this would work just fine for most applications. There could be a way to optimize it for high-performance scenarios when the code would be used en masse, but just running as a test on your laptop, I don't think you'd ever even notice the difference. I'd expect the rate of collisions to be really low. Thanks for your thoughts on this!
@almuntasirabir4511
@almuntasirabir4511 4 года назад
@Duke Wellington collision will be a pretty common case while using hash table . You can use method like 'linear probing' to resolve the issue.
@di3g04
@di3g04 5 лет назад
Tiny font
@mukulprasad6093
@mukulprasad6093 5 лет назад
Could you please help me in hash table please share you contact detail so that i can share the problem statement with you
@di3g04
@di3g04 5 лет назад
Man you talk way too slow. And the explanation is overkill
@almuntasirabir4511
@almuntasirabir4511 5 лет назад
why don't you go somewhere else and fuck your self.
@kurtcobain2355
@kurtcobain2355 5 лет назад
@@almuntasirabir4511 calm down bro, chill
@PageKey
@PageKey 2 года назад
Good feedback thanks
Далее
Stacks in Python
10:16
Просмотров 3,7 тыс.
Hash Tables and Hash Functions
13:56
Просмотров 1,6 млн
Python Logging - Tutorial
15:02
Просмотров 159 тыс.
Hash Tables explained with PYTHON
8:38
Просмотров 3,6 тыс.
Python Decorators in 15 Minutes
15:14
Просмотров 440 тыс.
Design HashSet - Leetcode 705 - Python
11:57
Просмотров 22 тыс.
If __name__ == "__main__" for Python Developers
8:47
Просмотров 400 тыс.
Image Processing with OpenCV and Python
20:38
Просмотров 155 тыс.