Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <set>
  6. #include <map>
  7.  
  8. namespace utility {
  9.     auto get_specials() {
  10.         return std::set<char>{'!', '@', '#', '$', '%',
  11.                               '^', '&', '*', '(', ')',
  12.                               '.', '-', '_', '=', '+'};
  13.     }
  14.  
  15.     auto get_digits() {
  16.         return std::set<char>{'1', '2', '3', '4', '5',
  17.                               '6', '7', '8', '9', '0'};
  18.     }
  19.  
  20.     auto get_lowercases() {
  21.         std::set<char> chars;
  22.  
  23.         for (char c = 'a'; c <= 'z'; c++) {
  24.             chars.insert(c);
  25.         }
  26.  
  27.         return chars;
  28.     }
  29.  
  30.     auto get_uppercases() {
  31.         std::set<char> chars;
  32.  
  33.         for (char c = 'A'; c <= 'Z'; c++) {
  34.             chars.insert(c);
  35.         }
  36.  
  37.         return chars;
  38.     }
  39. }
  40.  
  41. namespace lexic {
  42.     /**
  43.      * Comment - *lhs* and *rhs* are abbreviations for "left hand side"
  44.      * and "right hadn side". I recommend using them if you don't have
  45.      * any better / more meaningful names for your parameters (of course
  46.      * if there are two of them).
  47.      */
  48.     auto most_common_char(const std::string& str) {
  49.         std::map<char, int> frequencies;
  50.  
  51.         std::for_each(str.begin(), str.end(), [&frequencies](const auto c) mutable {
  52.             frequencies[c]++;
  53.         });
  54.  
  55.         return std::max_element(
  56.                 frequencies.begin(), frequencies.end(),
  57.                 [](const auto& lhs, const auto& rhs) {
  58.                     return lhs.second < rhs.second;
  59.                 }
  60.         )->first;
  61.     }
  62.  
  63.     /**
  64.      * Comment - std::size_t is a default type for representing containers size.
  65.      * In contrary, in Java we would use int, but in C++ we use std::size_t.
  66.      */
  67.     auto most_common_chars(const std::string& str, std::size_t n) {
  68.         std::map<char, int> frequencies;
  69.  
  70.         std::for_each(str.begin(), str.end(), [&frequencies](const auto c) mutable {
  71.             frequencies[c]++;
  72.         });
  73.  
  74.         std::vector<std::pair<char, int>> frequency_pairs(frequencies.begin(), frequencies.end());
  75.  
  76.         std::sort(
  77.                 frequency_pairs.begin(), frequency_pairs.end(),
  78.                 [](const auto& lhs, const auto& rhs) {
  79.                     return lhs.second > rhs.second;
  80.                 }
  81.         );
  82.  
  83.         frequency_pairs.erase(frequency_pairs.begin() + n, frequency_pairs.end());
  84.  
  85.         return frequency_pairs;
  86.     }
  87. }
  88.  
  89. namespace modifiers {
  90.     auto sort_by_length(std::vector<std::string>& vec) {
  91.         std::sort(vec.begin(), vec.end(), [](const auto& lhs, const auto& rhs) {
  92.             return lhs.size() < rhs.size();
  93.         });
  94.     }
  95.  
  96.     auto order_by_case(std::vector<std::string>& vec) {
  97.         std::partition(vec.begin(), vec.end(), [](const auto& str) {
  98.             return 'a' <= str[0] && str[0] <= 'z';
  99.         });
  100.     }
  101. }
  102.  
  103. namespace printers {
  104.     auto print_permutations(std::vector<std::string> vec) {
  105.         std::sort(vec.begin(), vec.end());
  106.         do {
  107.             std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, " "));
  108.             std::cout << '\n';
  109.         } while (std::next_permutation(vec.begin(), vec.end()));
  110.     }
  111.  
  112.     auto print_anagram_groups(const std::vector<std::string>& vec) {
  113.         std::map<std::string, std::set<std::string>> groups;
  114.  
  115.         std::for_each(vec.begin(), vec.end(), [&groups](const auto& str) {
  116.             auto sorted_copy = str;
  117.             std::sort(sorted_copy.begin(), sorted_copy.end());
  118.  
  119.             groups[sorted_copy].insert(str);
  120.         });
  121.  
  122.         std::for_each(groups.begin(), groups.end(), [](const auto& pair) {
  123.             std::cout << "[ ";
  124.             std::copy(pair.second.begin(), pair.second.end(),
  125.                       std::ostream_iterator<std::string>(std::cout, " "));
  126.             std::cout << "]\n";
  127.         });
  128.     }
  129. }
  130.  
  131. namespace mappers {
  132.     auto extract_lengths(const std::vector<std::string>& vec) {
  133.         std::vector<int> lengths;
  134.  
  135.         std::transform(vec.begin(), vec.end(), std::back_inserter(lengths), [](const auto& str) {
  136.             return str.size();
  137.         });
  138.  
  139.         return lengths;
  140.     }
  141.  
  142.     auto get_intersection(const std::set<int>& lhs, const std::set<int>& rhs) {
  143.         std::vector<int> intersection;
  144.  
  145.         std::set_intersection(
  146.                 lhs.begin(), lhs.end(),
  147.                 rhs.begin(), rhs.end(),
  148.                 std::back_inserter(intersection)
  149.         );
  150.  
  151.         return intersection;
  152.     }
  153. }
  154.  
  155. namespace security {
  156.     auto verify(const std::string& str) {
  157.         const auto specials = utility::get_specials();
  158.         const auto digits = utility::get_digits();
  159.         const auto uppercase = utility::get_uppercases();
  160.         const auto lowercase = utility::get_lowercases();
  161.  
  162.         return str.size() >= 9
  163.                && std::count_if(str.begin(), str.end(),
  164.                                 [&specials](const auto c) {
  165.                                     return specials.find(c) != specials.end();
  166.                                 }) >= 2
  167.                && std::any_of(str.begin(), str.end(),
  168.                               [&digits](const auto c) {
  169.                                   return digits.find(c) != digits.end();
  170.                               })
  171.                && std::count_if(str.begin(), str.end(),
  172.                                 [&uppercase](const auto c) {
  173.                                     return uppercase.find(c) != uppercase.end();
  174.                                 }) >= 2
  175.                && std::count_if(str.begin(), str.end(),
  176.                                 [&lowercase](const auto c) {
  177.                                     return lowercase.find(c) != lowercase.end();
  178.                                 }) >= 2
  179.                && std::none_of(str.begin(), str.end(), [](const auto c) {
  180.             return c == ' ' || c == '\t' || c == '\n';
  181.         });
  182.     }
  183. }
  184.  
  185. int main() {
  186.     // convenient instances for less writing onwards...
  187.     auto char_out = std::ostream_iterator<char>(std::cout, " ");
  188.     auto int_out = std::ostream_iterator<int>(std::cout, " ");
  189.     auto string_out = std::ostream_iterator<std::string>(std::cout, " ");
  190.  
  191.     std::cout << "\n########## example I ##########\n";
  192.     const auto specials = utility::get_specials();
  193.     const auto digits = utility::get_digits();
  194.     const auto lowercases = utility::get_lowercases();
  195.     const auto uppercases = utility::get_uppercases();
  196.  
  197.     std::copy(specials.begin(), specials.end(), char_out);
  198.     std::cout << '\n';
  199.     std::copy(digits.begin(), digits.end(), char_out);
  200.     std::cout << '\n';
  201.     std::copy(lowercases.begin(), lowercases.end(), char_out);
  202.     std::cout << '\n';
  203.     std::copy(uppercases.begin(), uppercases.end(), char_out);
  204.  
  205.     std::cout << "\n########## example II ##########\n";
  206.     std::cout << lexic::most_common_char("phantom blood");
  207.  
  208.     std::cout << "\n########## example III ##########\n";
  209.     const auto common = lexic::most_common_chars("battle tendency", 3);
  210.     for (const auto[chr, count] : common) {
  211.         std::cout << chr << ": " << count << '\n';
  212.     }
  213.  
  214.     std::cout << "\n########## example IV ##########\n";
  215.     std::vector<std::string> words = {"sono", "chi", "no", "sadame"};
  216.     modifiers::sort_by_length(words);
  217.     std::copy(words.begin(), words.end(), string_out);
  218.  
  219.     std::cout << "\n########## example V ##########\n";
  220.     words = {"Star", "dust", "Crusa", "ders"};
  221.     modifiers::order_by_case(words);
  222.     std::copy(words.begin(), words.end(), string_out);
  223.  
  224.     std::cout << "\n########## example VI ##########\n";
  225.     words = {"Diamond", "is", "unbreakable"};
  226.     printers::print_permutations(words);
  227.  
  228.     std::cout << "\n########## example VII ##########\n";
  229.     const auto lengths = mappers::extract_lengths({"Golden", "Wind"});
  230.     for (const auto length : lengths) {
  231.         std::cout << length << ' ';
  232.     }
  233.  
  234.     std::cout << "\n########## example VIII ##########\n";
  235.     for (const auto e : mappers::get_intersection({1, 3, 3, 2, 7},
  236.                                                   {1, 3, 5, 7})) {
  237.         std::cout << e << ' ';
  238.     }
  239.  
  240.     std::cout << "\n########## example IX ##########\n";
  241.     std::cout << std::boolalpha
  242.               << security::verify("ala ma kota") << '\n'
  243.               << security::verify("Ala Ma Kota") << '\n'
  244.               << security::verify("Al4 Ma Kota") << '\n'
  245.               << security::verify("Al4_Ma K()ta") << '\n'
  246.               << security::verify("Al4_Ma_K()ta") << '\n';
  247.  
  248.     std::cout << "\n########## example X ##########\n";
  249.     printers::print_anagram_groups(
  250.             {"andes", "danes", "deans", "evil",
  251.              "gals", "lags", "levi", "live",
  252.              "sedan", "slag", "streets", "testers",
  253.              "uprising", "veil", "vile"}
  254.     );
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement