Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <iterator>
- #include <set>
- #include <map>
- namespace utility {
- auto get_specials() {
- return std::set<char>{'!', '@', '#', '$', '%',
- '^', '&', '*', '(', ')',
- '.', '-', '_', '=', '+'};
- }
- auto get_digits() {
- return std::set<char>{'1', '2', '3', '4', '5',
- '6', '7', '8', '9', '0'};
- }
- auto get_lowercases() {
- std::set<char> chars;
- for (char c = 'a'; c <= 'z'; c++) {
- chars.insert(c);
- }
- return chars;
- }
- auto get_uppercases() {
- std::set<char> chars;
- for (char c = 'A'; c <= 'Z'; c++) {
- chars.insert(c);
- }
- return chars;
- }
- }
- namespace lexic {
- /**
- * Comment - *lhs* and *rhs* are abbreviations for "left hand side"
- * and "right hadn side". I recommend using them if you don't have
- * any better / more meaningful names for your parameters (of course
- * if there are two of them).
- */
- auto most_common_char(const std::string& str) {
- std::map<char, int> frequencies;
- std::for_each(str.begin(), str.end(), [&frequencies](const auto c) mutable {
- frequencies[c]++;
- });
- return std::max_element(
- frequencies.begin(), frequencies.end(),
- [](const auto& lhs, const auto& rhs) {
- return lhs.second < rhs.second;
- }
- )->first;
- }
- /**
- * Comment - std::size_t is a default type for representing containers size.
- * In contrary, in Java we would use int, but in C++ we use std::size_t.
- */
- auto most_common_chars(const std::string& str, std::size_t n) {
- std::map<char, int> frequencies;
- std::for_each(str.begin(), str.end(), [&frequencies](const auto c) mutable {
- frequencies[c]++;
- });
- std::vector<std::pair<char, int>> frequency_pairs(frequencies.begin(), frequencies.end());
- std::sort(
- frequency_pairs.begin(), frequency_pairs.end(),
- [](const auto& lhs, const auto& rhs) {
- return lhs.second > rhs.second;
- }
- );
- frequency_pairs.erase(frequency_pairs.begin() + n, frequency_pairs.end());
- return frequency_pairs;
- }
- }
- namespace modifiers {
- auto sort_by_length(std::vector<std::string>& vec) {
- std::sort(vec.begin(), vec.end(), [](const auto& lhs, const auto& rhs) {
- return lhs.size() < rhs.size();
- });
- }
- auto order_by_case(std::vector<std::string>& vec) {
- std::partition(vec.begin(), vec.end(), [](const auto& str) {
- return 'a' <= str[0] && str[0] <= 'z';
- });
- }
- }
- namespace printers {
- auto print_permutations(std::vector<std::string> vec) {
- std::sort(vec.begin(), vec.end());
- do {
- std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, " "));
- std::cout << '\n';
- } while (std::next_permutation(vec.begin(), vec.end()));
- }
- auto print_anagram_groups(const std::vector<std::string>& vec) {
- std::map<std::string, std::set<std::string>> groups;
- std::for_each(vec.begin(), vec.end(), [&groups](const auto& str) {
- auto sorted_copy = str;
- std::sort(sorted_copy.begin(), sorted_copy.end());
- groups[sorted_copy].insert(str);
- });
- std::for_each(groups.begin(), groups.end(), [](const auto& pair) {
- std::cout << "[ ";
- std::copy(pair.second.begin(), pair.second.end(),
- std::ostream_iterator<std::string>(std::cout, " "));
- std::cout << "]\n";
- });
- }
- }
- namespace mappers {
- auto extract_lengths(const std::vector<std::string>& vec) {
- std::vector<int> lengths;
- std::transform(vec.begin(), vec.end(), std::back_inserter(lengths), [](const auto& str) {
- return str.size();
- });
- return lengths;
- }
- auto get_intersection(const std::set<int>& lhs, const std::set<int>& rhs) {
- std::vector<int> intersection;
- std::set_intersection(
- lhs.begin(), lhs.end(),
- rhs.begin(), rhs.end(),
- std::back_inserter(intersection)
- );
- return intersection;
- }
- }
- namespace security {
- auto verify(const std::string& str) {
- const auto specials = utility::get_specials();
- const auto digits = utility::get_digits();
- const auto uppercase = utility::get_uppercases();
- const auto lowercase = utility::get_lowercases();
- return str.size() >= 9
- && std::count_if(str.begin(), str.end(),
- [&specials](const auto c) {
- return specials.find(c) != specials.end();
- }) >= 2
- && std::any_of(str.begin(), str.end(),
- [&digits](const auto c) {
- return digits.find(c) != digits.end();
- })
- && std::count_if(str.begin(), str.end(),
- [&uppercase](const auto c) {
- return uppercase.find(c) != uppercase.end();
- }) >= 2
- && std::count_if(str.begin(), str.end(),
- [&lowercase](const auto c) {
- return lowercase.find(c) != lowercase.end();
- }) >= 2
- && std::none_of(str.begin(), str.end(), [](const auto c) {
- return c == ' ' || c == '\t' || c == '\n';
- });
- }
- }
- int main() {
- // convenient instances for less writing onwards...
- auto char_out = std::ostream_iterator<char>(std::cout, " ");
- auto int_out = std::ostream_iterator<int>(std::cout, " ");
- auto string_out = std::ostream_iterator<std::string>(std::cout, " ");
- std::cout << "\n########## example I ##########\n";
- const auto specials = utility::get_specials();
- const auto digits = utility::get_digits();
- const auto lowercases = utility::get_lowercases();
- const auto uppercases = utility::get_uppercases();
- std::copy(specials.begin(), specials.end(), char_out);
- std::cout << '\n';
- std::copy(digits.begin(), digits.end(), char_out);
- std::cout << '\n';
- std::copy(lowercases.begin(), lowercases.end(), char_out);
- std::cout << '\n';
- std::copy(uppercases.begin(), uppercases.end(), char_out);
- std::cout << "\n########## example II ##########\n";
- std::cout << lexic::most_common_char("phantom blood");
- std::cout << "\n########## example III ##########\n";
- const auto common = lexic::most_common_chars("battle tendency", 3);
- for (const auto[chr, count] : common) {
- std::cout << chr << ": " << count << '\n';
- }
- std::cout << "\n########## example IV ##########\n";
- std::vector<std::string> words = {"sono", "chi", "no", "sadame"};
- modifiers::sort_by_length(words);
- std::copy(words.begin(), words.end(), string_out);
- std::cout << "\n########## example V ##########\n";
- words = {"Star", "dust", "Crusa", "ders"};
- modifiers::order_by_case(words);
- std::copy(words.begin(), words.end(), string_out);
- std::cout << "\n########## example VI ##########\n";
- words = {"Diamond", "is", "unbreakable"};
- printers::print_permutations(words);
- std::cout << "\n########## example VII ##########\n";
- const auto lengths = mappers::extract_lengths({"Golden", "Wind"});
- for (const auto length : lengths) {
- std::cout << length << ' ';
- }
- std::cout << "\n########## example VIII ##########\n";
- for (const auto e : mappers::get_intersection({1, 3, 3, 2, 7},
- {1, 3, 5, 7})) {
- std::cout << e << ' ';
- }
- std::cout << "\n########## example IX ##########\n";
- std::cout << std::boolalpha
- << security::verify("ala ma kota") << '\n'
- << security::verify("Ala Ma Kota") << '\n'
- << security::verify("Al4 Ma Kota") << '\n'
- << security::verify("Al4_Ma K()ta") << '\n'
- << security::verify("Al4_Ma_K()ta") << '\n';
- std::cout << "\n########## example X ##########\n";
- printers::print_anagram_groups(
- {"andes", "danes", "deans", "evil",
- "gals", "lags", "levi", "live",
- "sedan", "slag", "streets", "testers",
- "uprising", "veil", "vile"}
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement