This program takes a phrase, create bins for each word, and then records the instance position of each word in the bin. <br />
<br />
<!--
Module 8 - Hashing Part 1
*** work..ing in progress for optimization/improvement. After seeing Dr. Eaglin's example posted today (25 sep), and after having submitted this much earlier (14 Sep) - I realize now I may need to further encapsulate -- I hope to find the time to come back to do this.
The topic of hashing is quite easy Topic - Hashing Techniques - if you have done anything as simple as putting socks in one drawer and underwear in another, well you've exercised the principle of hashing. Put similar items in bins so they can be found more easily.
Assignment
If you look at some compression techniques (such as LZW Compression https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch ) they make use of the concept of hashing techniques to compress files. We are going to do something similar. Consider the phrase
To be or not to be, that is the question.
Even though it does not make much sense to do this as the phrase is short. We are going to create bins for each word, and record the position of the instances of the words in the proper bins. So write a program that first has a series of bins
to
be
or
not
etc....
Because the word to occurs in positions 1 and 5, the bin for the word to will contain 1 and 5
to: 1 5
be: 2 6
or: 3
Write an program that will take the phrase, create bins for each word, and then record the instance position of each word in the bin. It will then output each bin (word) and the locations of the word in the phrase. So if the input was;
I want what I want and I know what I want
The output would be;
I: 1 4 7 10
want: 2 5 11
what: 3 9
and: 6
know: 8
-->
<input type="text" id="userInput" value="I want what I want and I know what I want" size=100 /><br />
<input type="button" value="Click to Hash the String Into Bins" id="startHashing" />
<p id="output"></p>
CSS
* {
font: 14pt Arial;
}
JavaScript
// module 8 - program 1 - sheldon pasciak
/* Module 8 - stores linked lists as one to one index matching with preset collection of unique keys based on string input uses array for storage of keys uses further build of Dr. E's linked list to hold occurrence positions of each 'word' found in keys
*/
// built further from .. Dr. Eaglin's linked list example
var makeLinkedList = function(keyName) {
var instanceOfLinkedList = Object.create(methodsOfLinkedList);
instanceOfLinkedList.head = null;
instanceOfLinkedList.tail = null;
instanceOfLinkedList.key = keyName;
return instanceOfLinkedList;
};
var methodsOfLinkedList = {
add: function(value) {
var newNode = makeNode(value);
if (!this.head) {
this.head = newNode;
}
if (this.tail) {
this.tail.next = newNode;
}
this.tail = newNode;
},
asString: function() {
var strOut = "";//"{" + this.key + "} ";
var currentNode = this.head;
while (currentNode) {
strOut += currentNode.data + " ";
currentNode = currentNode.next;
}
return strOut;
},
remove: function() {
var currentNode = this.head;
this.head = currentNode.next;
currentNode = null;
},
contains: function(value) {
var currentNode = this.head;
while (currentNode) {
if (currentNode.data === value) {
return true;
}
currentNode = currentNode.next;
}
return false;
},
//this FIND function isnot needed because back storage array of keys is used to find one-to-one index matching location of key (0..n) to list(0..n) which points to the head of the linked list which contains the occurrences
find: function(value) {
var currentNode = this.head;
while (currentNode) {
if (currentNode.data === value) {
return currentNode;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.