Hex words: Difference between revisions
Content added Content deleted
(New post.) |
(New post.) |
||
Line 691: | Line 691: | ||
7 fade 64222 |
7 fade 64222 |
||
9 face 64206 |
9 face 64206 |
||
1 deaf 57007 |
1 deaf 57007</syntaxhighlight> |
||
9 cafe 51966 |
9 cafe 51966 |
||
6 bead 48813 |
6 bead 48813 |
||
Line 697: | Line 697: | ||
3 abed 44013 |
3 abed 44013 |
||
Found 13 hex words with 4 or more distinct |
Found 13 hex words with 4 or more distinct |
||
</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <algorithm> |
|||
#include <cstdint> |
|||
#include <fstream> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
#include <string> |
|||
#include <unordered_set> |
|||
#include <vector> |
|||
struct Item { |
|||
std::string word; |
|||
int32_t number; |
|||
int32_t digital_root; |
|||
}; |
|||
void display(const std::vector<Item>& items) { |
|||
std::cout << " Word Decimal value Digital root" << std::endl; |
|||
std::cout << "----------------------------------------" << std::endl; |
|||
for ( const Item& item : items ) { |
|||
std::cout << std::setw(7) << item.word << std::setw(15) << item.number |
|||
<< std::setw(12) << item.digital_root << std::endl; |
|||
} |
|||
std::cout << "\n" << "Total count: " << items.size() << "\n" << std::endl; |
|||
} |
|||
int32_t digital_root(int32_t number) { |
|||
int32_t result = 0; |
|||
while ( number > 0 ) { |
|||
result += number % 10; |
|||
number /= 10; |
|||
} |
|||
return ( result <= 9 ) ? result : digital_root(result); |
|||
} |
|||
bool contains_only(const std::string& word, const std::unordered_set<char>& acceptable) { |
|||
return std::all_of(word.begin(), word.end(), |
|||
[acceptable](char ch) { return acceptable.find(ch) != acceptable.end(); }); |
|||
} |
|||
int main() { |
|||
const std::unordered_set<char> hex_digits{ 'a', 'b', 'c', 'd', 'e', 'f' }; |
|||
std::vector<Item> items; |
|||
std::fstream file_stream; |
|||
file_stream.open("unixdict.txt"); |
|||
std::string word; |
|||
while ( file_stream >> word ) { |
|||
if ( word.length() >= 4 && contains_only(word, hex_digits)) { |
|||
const int32_t value = std::stoi(word, 0, 16); |
|||
int32_t root = digital_root(value); |
|||
items.push_back(Item(word, value, root)); |
|||
} |
|||
} |
|||
auto compare = [](Item a, Item b) { |
|||
return ( a.digital_root == b.digital_root ) ? a.word < b.word : a.digital_root < b.digital_root; |
|||
}; |
|||
std::sort(items.begin(), items.end(), compare); |
|||
display(items); |
|||
std::vector<Item> filtered_items; |
|||
for ( const Item& item : items ) { |
|||
if ( std::unordered_set<char>(std::begin(item.word), std::end(item.word)).size() >= 4 ) { |
|||
filtered_items.push_back(item); |
|||
} |
|||
} |
|||
auto comp = [](Item a, Item b) { return a.number > b.number; }; |
|||
std::sort(filtered_items.begin(), filtered_items.end(), comp); |
|||
display(filtered_items); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Word Decimal value Digital root |
|||
---------------------------------------- |
|||
ababa 703162 1 |
|||
abbe 43966 1 |
|||
dada 56026 1 |
|||
deaf 57007 1 |
|||
decade 14600926 1 |
|||
cede 52958 2 |
|||
feed 65261 2 |
|||
abed 44013 3 |
|||
added 712173 3 |
|||
bade 47838 3 |
|||
beebe 782014 4 |
|||
decca 912586 4 |
|||
dade 56030 5 |
|||
bead 48813 6 |
|||
deface 14613198 6 |
|||
babe 47806 7 |
|||
fade 64222 7 |
|||
dead 57005 8 |
|||
efface 15727310 8 |
|||
facade 16435934 8 |
|||
accede 11325150 9 |
|||
beef 48879 9 |
|||
cafe 51966 9 |
|||
dacca 896202 9 |
|||
deed 57069 9 |
|||
face 64206 9 |
|||
Total count: 26 |
|||
Word Decimal value Digital root |
|||
---------------------------------------- |
|||
facade 16435934 8 |
|||
efface 15727310 8 |
|||
deface 14613198 6 |
|||
decade 14600926 1 |
|||
accede 11325150 9 |
|||
decca 912586 4 |
|||
fade 64222 7 |
|||
face 64206 9 |
|||
deaf 57007 1 |
|||
cafe 51966 9 |
|||
bead 48813 6 |
|||
bade 47838 3 |
|||
abed 44013 3 |
|||
Total count: 13 |
|||
</pre> |
</pre> |
||