如何在 gethash javascript 函数中查找具有特定字母的字符串

发布于 2025-01-19 16:04:41 字数 345 浏览 4 评论 0原文

function getHash( string ) {  
   let h = 7, letters = "acdefhlmnoprstuw";   
   for( var i = 0; i < string.length; i++ ) {  
      h = ( h * 37 + letters.indexOf( string[ i ] ) );  
   }  
   return h;  
}

我的任务是编写一个代码来查找包含以下一些字母的字符串:ACDEFHLMNOPRSTUW
这样函数 getHash(THE SEARCHING STRING) 得到的结果就是 18794359164。

function getHash( string ) {  
   let h = 7, letters = "acdefhlmnoprstuw";   
   for( var i = 0; i < string.length; i++ ) {  
      h = ( h * 37 + letters.indexOf( string[ i ] ) );  
   }  
   return h;  
}

My task is to write a code which finds the string containing some of following letters: ACDEFHLMNOPRSTUW
Sothat the function getHash(THE SEARCHING STRING) gets the result of 18794359164.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2025-01-26 16:04:41

我不会给你一些代码,只是一些提示。

为了理解代码,你已经并且想要得到的是,用你的命令获取一个字符串并从中获取一个哈希值。

例如,采用 'wonder' 并获取

19017519751

哈希值。

该值包含起始值 7 (h = 7),并且对于每个字母,它将 h37 相乘(该值看起来像是为 37 个不同的字符制作)并添加字母的索引值。

要获得与数值相反的值,您需要用 37 分隔除法的其余部分以获取字母的索引,与使用字母完全相反,并向散列添加一个值。

例如,取上面的值 19017519751 并除以 37 得到剩下的部分

19017519751 % 37 -> 11  r

现在用最后一个字母(记住编码从单词的开头到结尾,解码从结尾),您需要获得一个不带最后一个字母的值。

通过编码,您将每个最后的总和乘以 37,这在这里也适用,但以相反的方式,您需要一个整数值。只需除以 37 并获取下一个字母的下限值即可。

其余的看起来像这样:

  513987020 % 37 ->  3  e
   13891541 % 37 ->  2  d
     375447 % 37 ->  8  n
      10147 % 37 ->  9  o
        274 % 37 -> 15  w

最后,您需要进行检查以停止迭代,并且该值是 7 编码的第一个值。

I am not giving you some code, just some hints.

To get an understanding of the code, you have, and the one, you want to get, is to take a string with you gunction and get a hash value from it.

For example take 'wonder' and get

19017519751

as hash value.

This value contains a start value of seven (h = 7) and for each letter it multiplies h with 37 (this value looks like it is made for 37 different characters) and adds the index value of letters.

To get the opposite with a numerical value, you need to separate a rest of a division by 37 to get an index of a letter, exact the opposite of using a letter and add a value to the hash.

For example take the above value 19017519751 and get the rest of the division by 37

19017519751 % 37 -> 11  r

Now with the last letter (remember encoding goes from start to end of the word, decoding starts with the end), you need get a value without the last letter.

By encoding, you multiply each last sum by 37 and this works here as well, but in reversed manner and you need an integer value. Just divide by 37 and take the floored value for the next letter.

The rest looks like this:

  513987020 % 37 ->  3  e
   13891541 % 37 ->  2  d
     375447 % 37 ->  8  n
      10147 % 37 ->  9  o
        274 % 37 -> 15  w

Finially, you need to have a check to stop the iteration ans this value is 7 the first value of encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文