You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can we simplify the function verifyRemoveSideEffect?
It's original version in the file is:
verifyRemoveSideEffect(key,removedPosition){consthash=this.hashCode(key);letindex=removedPosition+1;while(this.table[index]!=null){constposHash=this.hashCode(this.table[index].key);if(posHash<=hash||posHash<=removedPosition){// QUESTION IS ABOUT THIS LINEthis.table[removedPosition]=this.table[index];deletethis.table[index];removedPosition=index;}index++;}}
However, since hash is always less than removedPosition, can we just check for posHash <= removedPosition instead of posHash <= hash || posHash <= removedPosition?
This way the function would look like:
verifyRemoveSideEffect(key,removedPosition){consthash=this.hashCode(key);letindex=removedPosition+1;while(this.table[index]!=null){constposHash=this.hashCode(this.table[index].key);if(posHash<=removedPosition){// THIS LINE IS CHANGEDthis.table[removedPosition]=this.table[index];deletethis.table[index];removedPosition=index;}index++;}}
I tried this change and it passes all your tests.
Also, this way we do not need const hash = this.hashCode(key); and subsequently key param of the function:
verifyRemoveSideEffect(removedPosition){// THIS LINE IS CHANGED// const hash = this.hashCode(key); - THIS LINE IS REMOVEDletindex=removedPosition+1;while(this.table[index]!=null){constposHash=this.hashCode(this.table[index].key);if(posHash<=removedPosition){// THIS LINE IS CHANGEDthis.table[removedPosition]=this.table[index];deletethis.table[index];removedPosition=index;}index++;}}
(of course we would need to change the remove function as well, so it calls this.verifyRemoveSideEffect(position); instead of this.verifyRemoveSideEffect(key, position);
With those changes it also passes all tests.
Does it make sense or there are some edge cases that I do not see and they need this check for both possibilities (posHash <= hash || posHash <= removedPosition)?
Thank you again for sharing your wisdom :)
The text was updated successfully, but these errors were encountered:
Hi @ivanduka. Yes, you can simplify the code. The original idea was also to show how to handle the same algorithm in other languages as well - as in JavaScript we don't have to set the length of the array prior its creation, then we're good!
Hi Loiane!
Thank you for this excellent book! I enjoy reading it a lot!
The question is regarding hash-table-linear-probing.js:
Can we simplify the function verifyRemoveSideEffect?
It's original version in the file is:
However, since
hash
is always less thanremovedPosition
, can we just check forposHash <= removedPosition
instead ofposHash <= hash || posHash <= removedPosition
?This way the function would look like:
I tried this change and it passes all your tests.
Also, this way we do not need
const hash = this.hashCode(key);
and subsequentlykey
param of the function:(of course we would need to change the
remove
function as well, so it callsthis.verifyRemoveSideEffect(position);
instead ofthis.verifyRemoveSideEffect(key, position);
With those changes it also passes all tests.
Does it make sense or there are some edge cases that I do not see and they need this check for both possibilities (
posHash <= hash || posHash <= removedPosition
)?Thank you again for sharing your wisdom :)
The text was updated successfully, but these errors were encountered: