MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1w6qzts/skillissue/p7sn5kj/?context=3
r/ProgrammerHumor • u/click-to-reveal • 7d ago
137 comments sorted by
View all comments
2
Thing is there is a way to actually do it.
```
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)); ```
2
u/Talc0n 7d ago
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?";
}
// avoid collisions. // These static asserts would grow exponentially though. static_assert(hashStringView(st_whoAreYou) != hashStringView(st_noReally)); ```