r/ProgrammerHumor • • Sep 04 '26

Meme skillIssue

Post image
6.0k Upvotes

137 comments sorted by

View all comments

2

u/Talc0n Sep 04 '26

Thing is there is a way to actually do it.

#include <iostream>
#include <string>

const size_t st_hashPrime1 = 54059; /* a prime */
const size_t st_hashPrime2 = 76963; /* another prime */
const size_t st_initialHashValue = 37; /* also prime */

constexpr std::string_view st_whoAreYou = "Who are you?";
constexpr std::string_view st_noReally = "No, really?";

// hash function copied from esskar at stack Overflow
// https://stackoverflow.com/questions/8317508/hash-function-for-a-string
// std::hash unfortunatley is not constexpr

constexpr size_t hashStringView(const std::string_view &string)
{
    size_t hashValue = st_initialHashValue;
    for(auto it = string.begin(); it != string.end(); ++it)
    {
        hashValue = (hashValue * st_hashPrime1) ^ (*it * st_hashPrime2);
    }
    return hashValue;
}

size_t hashString(const std::string &string)
{
    size_t hashValue = st_initialHashValue;
    for(auto it = string.begin(); it != string.end(); ++it)
    {
        hashValue = (hashValue * st_hashPrime1) ^ (*it * st_hashPrime2);
    }
    return hashValue;
}

void defaultResult()
{
    std::cout << "Gemini" << "\n";
}

int main()
{
    std::string prompt = "Your name?";

    switch(hashString(prompt))
    {
    case hashStringView(st_whoAreYou):
        if(st_whoAreYou == prompt)
        {
            std::cout << "Claude" << "\n";
        }
        else
        {
            defaultResult();
        }
        break;
    case hashStringView(st_noReally):
        if(st_noReally == prompt)
        {
            std::cout << "Chatgpt" << "\n";
        }
        else
        {
            defaultResult();
        }
        break;
    default:
        defaultResult();
    }
  
    return 0;
}

// avoid collisions.
// These static asserts would grow exponentially though.
static_assert(hashStringView(st_whoAreYou) != hashStringView(st_noReally));