aboutsummaryrefslogtreecommitdiff
path: root/challenge-275/atschneid/cpp
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2024-07-01 08:42:54 -0500
committerGitHub <noreply@github.com>2024-07-01 08:42:54 -0500
commit2f8cf79ce5a382c3a6bb122ba07817e7d05bf154 (patch)
treeed6bb59bd48eaf9ebfbcfd8dd1d1a1017713078e /challenge-275/atschneid/cpp
parent2eb1149be6bb0afef86164ec74ae0a15bc7c4ed8 (diff)
parentf18cb7a95e46b9ded70a2d1d932d0bb7b1772a67 (diff)
downloadperlweeklychallenge-club-2f8cf79ce5a382c3a6bb122ba07817e7d05bf154.tar.gz
perlweeklychallenge-club-2f8cf79ce5a382c3a6bb122ba07817e7d05bf154.tar.bz2
perlweeklychallenge-club-2f8cf79ce5a382c3a6bb122ba07817e7d05bf154.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-275/atschneid/cpp')
-rw-r--r--challenge-275/atschneid/cpp/ch-1.cpp42
-rw-r--r--challenge-275/atschneid/cpp/ch-2.cpp32
-rw-r--r--challenge-275/atschneid/cpp/makefile9
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-275/atschneid/cpp/ch-1.cpp b/challenge-275/atschneid/cpp/ch-1.cpp
new file mode 100644
index 0000000000..96a1bfb60a
--- /dev/null
+++ b/challenge-275/atschneid/cpp/ch-1.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <regex>
+#include <string>
+
+
+using namespace std;
+
+int find_unbroken_words( const string& line, const string& letters ) {
+ string letter_expression = "\\S*[" + letters + "]\\S*";
+
+ regex allwords_re ("\\S+");
+ regex badwords_re (letter_expression, regex::icase );
+
+ auto all_match = sregex_iterator(line.begin(), line.end(), allwords_re);
+ auto bad_match = sregex_iterator(line.begin(), line.end(), badwords_re);
+ auto base = sregex_iterator();
+
+ return std::distance(all_match, base) - std::distance(bad_match, base);
+}
+
+struct inputline {
+ string s;
+ const char* cs;
+};
+
+int main() {
+ inputline inputs[] = {
+ { "Perl Weekly Challenge", "la" },
+ { "Perl and Raku", "a" },
+ { "Well done Team PWC", "lo" },
+ { "The joys of polyglottism", "T" },
+ { "The joys o*f polyglottism", "*Tp"}
+ };
+
+ for (int i=0; i < 5; i++) {
+ auto input = inputs[i];
+ auto line = input.s;
+ auto broken_chars = input.cs;
+ cout << line << ", " << broken_chars << " :: " << find_unbroken_words(line, broken_chars) << "\n";
+ }
+}
+
diff --git a/challenge-275/atschneid/cpp/ch-2.cpp b/challenge-275/atschneid/cpp/ch-2.cpp
new file mode 100644
index 0000000000..10ded6914d
--- /dev/null
+++ b/challenge-275/atschneid/cpp/ch-2.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+
+class fillable_string: public std::string {
+public:
+ void fill_digits();
+};
+
+void fillable_string::fill_digits() {
+ auto s = this->data();
+ char fill_char = ' ';
+ for (int i=0; i < this->size(); i++) {
+ if (std::isdigit(s[i])) {
+ s[i] += fill_char - '0'; // to shift the char val
+ } else {
+ fill_char = s[i];
+ }
+ }
+}
+
+int main() {
+ fillable_string inputs[]{
+ {"a1c1e1"},
+ {"a1b2c3d4"},
+ {"b2b"},
+ {"a16z"}
+ };
+ for (int i=0; i < 4; i++) {
+ std::cout << inputs[i] << " => ";
+ inputs[i].fill_digits();
+ std::cout << inputs[i] << '\n';
+ }
+}
diff --git a/challenge-275/atschneid/cpp/makefile b/challenge-275/atschneid/cpp/makefile
new file mode 100644
index 0000000000..9c91d50c01
--- /dev/null
+++ b/challenge-275/atschneid/cpp/makefile
@@ -0,0 +1,9 @@
+CPPFLAGS := -g -std=c++17 -Wall -O3
+
+all: ch-1 ch-2
+
+ch-1: ch-1.cpp
+ c++ ch-1.cpp -o ch-1 $(CPPFLAGS)
+
+ch-2: ch-2.cpp
+ c++ ch-2.cpp -o ch-2 $(CPPFLAGS)