Skip to content

Commit 6aea255

Browse files
add class HashTable
1 parent 695c0e6 commit 6aea255

File tree

1 file changed

+194
-3
lines changed

1 file changed

+194
-3
lines changed

data_structures/4_HashTable_2_Collisions/Solutions.ipynb

Lines changed: 194 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,15 @@
473473
},
474474
{
475475
"cell_type": "code",
476-
"execution_count": 38,
476+
"execution_count": 41,
477477
"id": "105f0fdf-ab40-4c49-ae65-64841a91ba96",
478478
"metadata": {},
479479
"outputs": [],
480480
"source": [
481481
"def hash_func(str_el: str, num_of_el: int) -> int:\n",
482482
" sum_h = 0\n",
483483
" for el in str_el:\n",
484-
" sum_h += oct(el)\n",
484+
" sum_h += ord(el)\n",
485485
" \n",
486486
" mod = sum_h % num_of_el\n",
487487
" \n",
@@ -490,9 +490,200 @@
490490
},
491491
{
492492
"cell_type": "code",
493-
"execution_count": null,
493+
"execution_count": 42,
494494
"id": "ee7df7db-a121-463f-9c0f-a023d418ed90",
495495
"metadata": {},
496+
"outputs": [
497+
{
498+
"data": {
499+
"text/plain": [
500+
"5"
501+
]
502+
},
503+
"execution_count": 42,
504+
"metadata": {},
505+
"output_type": "execute_result"
506+
}
507+
],
508+
"source": [
509+
"hash_func('august', 10)"
510+
]
511+
},
512+
{
513+
"cell_type": "code",
514+
"execution_count": 75,
515+
"id": "676b82f8-9483-4f7c-b838-63054c64c4c3",
516+
"metadata": {},
517+
"outputs": [],
518+
"source": [
519+
"class HashTable:\n",
520+
" def __init__(self, num_of_rows):\n",
521+
" self.num_of_rows = num_of_rows\n",
522+
" self.arr = [None for _ in range(self.num_of_rows)]\n",
523+
" \n",
524+
" def get_hash(self, key):\n",
525+
" sum_h = 0\n",
526+
" for el in key:\n",
527+
" sum_h += ord(el)\n",
528+
" mod = sum_h % self.num_of_rows\n",
529+
" return mod \n",
530+
" \n",
531+
" def __getitem__(self, index):\n",
532+
" h = self.get_hash(index)\n",
533+
" return self.arr[h]\n",
534+
" \n",
535+
" def __setitem__(self, key, value):\n",
536+
" h = self.get_hash(key)\n",
537+
" self.arr[h] = value\n",
538+
" \n",
539+
" def __delitem__(self, key):\n",
540+
" h = self.get_hash(key)\n",
541+
" del self.arr[h]"
542+
]
543+
},
544+
{
545+
"cell_type": "code",
546+
"execution_count": 76,
547+
"id": "e67073a3-532d-4184-8f2d-b189c33fd7dd",
548+
"metadata": {},
549+
"outputs": [],
550+
"source": [
551+
"ht = HashTable(10)"
552+
]
553+
},
554+
{
555+
"cell_type": "code",
556+
"execution_count": 77,
557+
"id": "fefc5014-05d0-4b4a-a12c-00975af70741",
558+
"metadata": {},
559+
"outputs": [],
560+
"source": [
561+
"ht[\"march 6\"] = 120\n",
562+
"ht[\"march 8\"] = 67\n",
563+
"ht[\"march 9\"] = 4\n",
564+
"ht[\"march 17\"] = 459"
565+
]
566+
},
567+
{
568+
"cell_type": "code",
569+
"execution_count": 78,
570+
"id": "1869462d-76d7-4c07-8c6c-9152a149a8b4",
571+
"metadata": {},
572+
"outputs": [
573+
{
574+
"data": {
575+
"text/plain": [
576+
"459"
577+
]
578+
},
579+
"execution_count": 78,
580+
"metadata": {},
581+
"output_type": "execute_result"
582+
}
583+
],
584+
"source": [
585+
"ht[\"march 6\"] "
586+
]
587+
},
588+
{
589+
"cell_type": "code",
590+
"execution_count": 79,
591+
"id": "fb72afda-ac03-43b6-b7a9-607f5fa91f76",
592+
"metadata": {},
593+
"outputs": [
594+
{
595+
"data": {
596+
"text/plain": [
597+
"9"
598+
]
599+
},
600+
"execution_count": 79,
601+
"metadata": {},
602+
"output_type": "execute_result"
603+
}
604+
],
605+
"source": [
606+
"ht.get_hash(\"march 6\")"
607+
]
608+
},
609+
{
610+
"cell_type": "code",
611+
"execution_count": 80,
612+
"id": "9d218d68-2c14-4968-87a3-c88f72070382",
613+
"metadata": {},
614+
"outputs": [
615+
{
616+
"data": {
617+
"text/plain": [
618+
"9"
619+
]
620+
},
621+
"execution_count": 80,
622+
"metadata": {},
623+
"output_type": "execute_result"
624+
}
625+
],
626+
"source": [
627+
"ht.get_hash(\"march 17\")"
628+
]
629+
},
630+
{
631+
"cell_type": "code",
632+
"execution_count": 81,
633+
"id": "81ee57f5-44a1-4315-9396-a3c3306a307e",
634+
"metadata": {},
635+
"outputs": [
636+
{
637+
"data": {
638+
"text/plain": [
639+
"[None, 67, 4, None, None, None, None, None, None, 459]"
640+
]
641+
},
642+
"execution_count": 81,
643+
"metadata": {},
644+
"output_type": "execute_result"
645+
}
646+
],
647+
"source": [
648+
"ht.arr"
649+
]
650+
},
651+
{
652+
"cell_type": "code",
653+
"execution_count": 82,
654+
"id": "df7b2f63-7f78-484b-9811-823caca1e168",
655+
"metadata": {},
656+
"outputs": [],
657+
"source": [
658+
"del ht['march 8']"
659+
]
660+
},
661+
{
662+
"cell_type": "code",
663+
"execution_count": 83,
664+
"id": "716067ec-3837-4752-b176-05c2ed14fa94",
665+
"metadata": {},
666+
"outputs": [
667+
{
668+
"data": {
669+
"text/plain": [
670+
"9"
671+
]
672+
},
673+
"execution_count": 83,
674+
"metadata": {},
675+
"output_type": "execute_result"
676+
}
677+
],
678+
"source": [
679+
"len(ht.arr)"
680+
]
681+
},
682+
{
683+
"cell_type": "code",
684+
"execution_count": null,
685+
"id": "bddee77e-9c11-47f6-aa9f-a344a61305d7",
686+
"metadata": {},
496687
"outputs": [],
497688
"source": []
498689
}

0 commit comments

Comments
 (0)