From f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 27 Mar 2023 16:47:37 +0100 Subject: Add C++ solution --- challenge-194/paulo-custodio/cpp/ch-1.cpp | 54 ++++++++++++++++++++++++ challenge-194/paulo-custodio/cpp/ch-2.cpp | 68 +++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 challenge-194/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-194/paulo-custodio/cpp/ch-2.cpp 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 +#include + +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 +#include +#include +#include + +const int LETTERS = ('z'-'a'+1); + +int freq_equalizer(const std::string& s) { + std::vector 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