#digit analysis for hashing (asking for peer review)

33 messages · Page 1 of 1 (latest)

weak lynx
#

int digit_analysis(const std::string &key, int tableSize)
{
if (keystr.length() < 6)
{
std::cerr << "Key string is too short to extract digits 3 to 6." << std::endl;
return 0;
}

    long long keyValue = std::stoll(key);
    std::string keystr = std::(newValue);


    std::string backwardDigit = keystr.substr(2,4);
    std::reverse(backwardDigit.begin(), backwardDigit.end());
    int hash = std::stoi(backwardDigit);
    return hash % tableSize;
} 

how does this look?

steel egret
#

std::(newValue)?

weak lynx
#

std::(keyValue)

#

i had it renamed

steel egret
#

;compile cpp long long keyValue=42; std::(KeyValue);

glad viperBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:5:6: error: expected unqualified-id before '(' token
    5 | std::(KeyValue);
      |      ^
<source>:5:7: error: 'KeyValue' was not declared in this scope; did you mean 'keyValue'?
    5 | std::(KeyValue);
      |       ^~~~~~~~
      |       keyValue
Build failed
weak lynx
#

turns it back into a string

steel egret
#

so you meant std::to_string?

weak lynx
#

here is my midsquare method.

weak lynx
#

int digit_analysis(const std::string &key, int tableSize)
{
if (keystr.length() < 6)
{
std::cerr << "Key string is too short to extract digits 3 to 6." << std::endl;
return 0; //making sure the value is more than 6 digits
}

    long long keyValue = std::stoll(key); //turning the key into long long
    std::string keystr = std::to_string(keyValue); //same thinf as midsquare method


    std::string backwardDigit = keystr.substr(2,4);
    std::reverse(backwardDigit.begin(), backwardDigit.end()); //reveseing the begining and end of the digits 
    int hash = std::stoi(backwardDigit); //string to int 
    return hash % tableSize;
}
steel egret
#

do negative numbers need to be considered here

weak lynx
#

no i dont believe so

#

but that can be put into consideration

#

im hashing using a linked list class that i created

steel egret
#

looks alright, though I would recommend doing integer arithmetic instead of constructing a string and doing substr and reverse

weak lynx
#

like how?

steel egret
#

;compile cpp //assuming size()>5 here std::string key="12345678"; long long keyValue; if(std::from_chars(key.data()+2,key.data()+5,keyValue).ec!=std::errc()){ //handle error } keyValue=keyValue%10*100+keyValue/10%10*10+keyValue/100; std::cout<<keyValue<<'\n';

glad viperBOT
#
Program Output
543
steel egret
weak lynx
#

ok ill try that

steel egret
#

though if converting to long long and back to string matters then this would be different

steel egret
weak lynx
#

i moved it around. and added your code as comments

#

i want to let my prof know about the comment code first. just in case. becuse im not too sure what it does

#

there is also this, which i do not understand

hidden parcelBOT
weak lynx
#

thats for the length dependence?

weak lynx
steel egret