diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-27 16:47:37 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-27 16:47:37 +0100 |
| commit | f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b (patch) | |
| tree | 259a7cbf8e8f492259b65b07add388eb64b9d0e8 | |
| parent | 50136c74cea4bd5ecc788bf57bc296dbf455eebc (diff) | |
| download | perlweeklychallenge-club-f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b.tar.gz perlweeklychallenge-club-f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b.tar.bz2 perlweeklychallenge-club-f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b.zip | |
Add C++ solution
| -rw-r--r-- | challenge-194/paulo-custodio/cpp/ch-1.cpp | 54 | ||||
| -rw-r--r-- | challenge-194/paulo-custodio/cpp/ch-2.cpp | 68 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-194/paulo-custodio/cpp/ch-1.cpp b/challenge-194/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..f4b8a6c537 --- /dev/null +++ b/challenge-194/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,54 @@ +/* +Challenge 194 + +Task 1: Digital Clock +Submitted by: Mohammad S Anwar +You are given time in the format hh:mm with one missing digit. + +Write a script to find the highest digit between 0-9 that makes it valid time. + +Example 1 +Input: $time = '?5:00' +Output: 1 + +Since 05:00 and 15:00 are valid time and no other digits can fit in the missing place. +Example 2 +Input: $time = '?3:00' +Output: 2 +Example 3 +Input: $time = '1?:00' +Output: 9 +Example 4 +Input: $time = '2?:00' +Output: 3 +Example 5 +Input: $time = '12:?5' +Output: 5 +Example 6 +Input: $time = '12:5?' +Output: 9 +*/ + +#include <iostream> +#include <string> + +int missing_digit(const std::string& clock) { + if (clock.size()!=5) return -1; + if (clock[0]=='?' && clock[1]<='3') return 2; + if (clock[0]=='?') return 1; + if (clock[0]<='1' && clock[1]=='?') return 9; + if (clock[1]=='?') return 3; + if (clock[3]=='?') return 5; + if (clock[4]=='?') return 9; + return -1; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc != 1) { + std::cerr << "usage: ch-1 hh:mm" << std::endl; + return EXIT_FAILURE; + } + + std::cout << missing_digit(argv[0]) << std::endl; +} diff --git a/challenge-194/paulo-custodio/cpp/ch-2.cpp b/challenge-194/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..437629244c --- /dev/null +++ b/challenge-194/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,68 @@ +/* +Challenge 194 + +Task 2: Frequency Equalizer +Submitted by: Mohammad S Anwar +You are given a string made of alphabetic characters only, a-z. + +Write a script to determine whether removing only one character can make the +frequency of the remaining characters the same. + +Example 1: +Input: $s = 'abbc' +Output: 1 since removing one alphabet 'b' will give us 'abc' where each +alphabet frequency is the same. +Example 2: +Input: $s = 'xyzyyxz' +Output: 1 since removing 'y' will give us 'xzyyxz'. +Example 3: +Input: $s = 'xzxz' +Output: 0 since removing any one alphabet would not give us string with same +frequency alphabet. +*/ + +#include <iostream> +#include <string> +#include <vector> +#include <cctype> + +const int LETTERS = ('z'-'a'+1); + +int freq_equalizer(const std::string& s) { + std::vector<int> freq; + freq.resize(LETTERS); + + if (s.empty()) return 0; + for (auto& c : s) { + if (isalpha(c)) + freq[tolower(c)-'a']++; + } + + int min=0, max=0; + for (auto& f : freq) { + if (f) { + if (min==0) min=max=f; + if (min>f) min=f; + if (max<f) max=f; + } + } + + if (min+1!=max) return 0; + + int count=0; + for (auto& f : freq) { + if (f==max) count++; + } + + return count==1 ? 1 : 0; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc != 1) { + std::cerr << "usage: ch-1 string" << std::endl; + return EXIT_FAILURE; + } + + std::cout << freq_equalizer(argv[0]) << std::endl; +} |
