Guest User

Untitled

a guest
May 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## Hash Function"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "alphabet_prime = {'a': 2, 'b': 3, 'c': 5, 'd': 7, 'e': 11, 'f': 13, 'g': 17, 'h': 19, 'i': 23,\n",
  17. " 'j': 29, 'k': 31, 'l': 37, 'm': 41, 'n': 43, 'o': 47, 'p': 53, 'q': 59, 'r': 61, 's': 67,\n",
  18. " 't': 71, 'u': 73, 'v': 79, 'w': 83, 'x': 89, 'y': 97, 'z': 101 }"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "SIZE = 6"
  28. ]
  29. },
  30. {
  31. "cell_type": "code",
  32. "execution_count": null,
  33. "metadata": {},
  34. "outputs": [],
  35. "source": [
  36. "def hash_function(key, array_size=SIZE):\n",
  37. " hash = 0\n",
  38. " for k in key.lower():\n",
  39. " hash += alphabet_prime[k]\n",
  40. " return hash % array_size"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": null,
  46. "metadata": {},
  47. "outputs": [],
  48. "source": [
  49. "hash_function('apple')"
  50. ]
  51. },
  52. {
  53. "cell_type": "markdown",
  54. "metadata": {},
  55. "source": [
  56. "a + p + p + l + e"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": null,
  62. "metadata": {},
  63. "outputs": [],
  64. "source": [
  65. "2 + 53 + 53 + 37 + 11"
  66. ]
  67. },
  68. {
  69. "cell_type": "code",
  70. "execution_count": null,
  71. "metadata": {},
  72. "outputs": [],
  73. "source": [
  74. "156 % SIZE"
  75. ]
  76. },
  77. {
  78. "cell_type": "markdown",
  79. "metadata": {},
  80. "source": [
  81. "## Key to index"
  82. ]
  83. },
  84. {
  85. "cell_type": "code",
  86. "execution_count": null,
  87. "metadata": {},
  88. "outputs": [],
  89. "source": [
  90. "hash_function('apple')"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": null,
  96. "metadata": {},
  97. "outputs": [],
  98. "source": [
  99. "hash_function('blueberry')"
  100. ]
  101. },
  102. {
  103. "cell_type": "code",
  104. "execution_count": null,
  105. "metadata": {},
  106. "outputs": [],
  107. "source": [
  108. "hash_function('grape')"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": null,
  114. "metadata": {},
  115. "outputs": [],
  116. "source": [
  117. "hash_function('strawberry')"
  118. ]
  119. },
  120. {
  121. "cell_type": "code",
  122. "execution_count": null,
  123. "metadata": {},
  124. "outputs": [],
  125. "source": [
  126. "hash_function('cherry')"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": null,
  132. "metadata": {},
  133. "outputs": [],
  134. "source": [
  135. "hash_function('pear')"
  136. ]
  137. },
  138. {
  139. "cell_type": "code",
  140. "execution_count": null,
  141. "metadata": {},
  142. "outputs": [],
  143. "source": [
  144. "hash_function('kiwi')"
  145. ]
  146. },
  147. {
  148. "cell_type": "code",
  149. "execution_count": null,
  150. "metadata": {},
  151. "outputs": [],
  152. "source": [
  153. "hash_function('melon')"
  154. ]
  155. },
  156. {
  157. "cell_type": "code",
  158. "execution_count": null,
  159. "metadata": {},
  160. "outputs": [],
  161. "source": [
  162. "hash_function('peach')"
  163. ]
  164. }
  165. ],
  166. "metadata": {
  167. "gist": {
  168. "data": {
  169. "description": "Workspace/csbootcamp/Prime Hash.ipynb",
  170. "public": true
  171. },
  172. "id": ""
  173. },
  174. "kernelspec": {
  175. "display_name": "fastai",
  176. "language": "python",
  177. "name": "fastai"
  178. },
  179. "language_info": {
  180. "codemirror_mode": {
  181. "name": "ipython",
  182. "version": 3
  183. },
  184. "file_extension": ".py",
  185. "mimetype": "text/x-python",
  186. "name": "python",
  187. "nbconvert_exporter": "python",
  188. "pygments_lexer": "ipython3",
  189. "version": "3.6.2"
  190. }
  191. },
  192. "nbformat": 4,
  193. "nbformat_minor": 2
  194. }
Add Comment
Please, Sign In to add comment