aboutsummaryrefslogtreecommitdiff
path: root/challenge-200/paulo-custodio/cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-11 23:20:09 +0000
committerGitHub <noreply@github.com>2023-03-11 23:20:09 +0000
commit993b5cfec02444f8ab158507ea64f42bcd3d175b (patch)
tree3678bcde9af7c2fb9a2fcb9f8cd5deefd17f59c2 /challenge-200/paulo-custodio/cpp
parent51465a4186e14e5f9ae26e7f5b20dbb9f5ea8aae (diff)
parent0ad64a5e5a740655432a36b2af0c35760289cf89 (diff)
downloadperlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.gz
perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.bz2
perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.zip
Merge pull request #7709 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-200/paulo-custodio/cpp')
-rw-r--r--challenge-200/paulo-custodio/cpp/ch-1.cpp68
-rw-r--r--challenge-200/paulo-custodio/cpp/ch-2.cpp113
2 files changed, 181 insertions, 0 deletions
diff --git a/challenge-200/paulo-custodio/cpp/ch-1.cpp b/challenge-200/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..46948327c7
--- /dev/null
+++ b/challenge-200/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,68 @@
+/*
+Challenge 200
+
+Task 1: Arithmetic Slices
+Submitted by: Mohammad S Anwar
+You are given an array of integers.
+
+Write a script to find out all Arithmetic Slices for the given array of integers.
+
+An integer array is called arithmetic if it has at least 3 elements and the
+differences between any three consecutive elements are the same.
+
+
+Example 1
+Input: @array = (1,2,3,4)
+Output: (1,2,3), (2,3,4), (1,2,3,4)
+Example 2
+Input: @array = (2)
+Output: () as no slice found.
+*/
+
+#include <iostream>
+#include <vector>
+
+bool is_arithmetic(int nums[], int nums_size) {
+ if (nums_size < 3)
+ return false;
+ int step = nums[1] - nums[0];
+ for (int i = 2; i < nums_size; i++) {
+ if (nums[i] - nums[i-1] != step)
+ return false;
+ }
+ return true;
+}
+
+void print_slices(std::vector<int> nums) {
+ const char* sep = "";
+ int nums_size = static_cast<int>(nums.size());
+
+ std::cout << "(";
+ for (int i = 0; i < nums_size-2; i++) {
+ for (int j = i+2; j < nums_size; j++) {
+ if (is_arithmetic(&nums[i], j-i+1)) {
+ std::cout << sep;
+ sep = "), (";
+ for (int k = i; k <= j; k++) {
+ std::cout << nums[k];
+ if (k != j)
+ std::cout << ",";
+ }
+ }
+ }
+ }
+ std::cout << ")" << std::endl;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ std::cerr << "usage: ch-1 nums..." << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ std::vector<int> nums;
+ for (int i = 0; i < argc; i++)
+ nums.push_back(atoi(argv[i]));
+ print_slices(nums);
+}
diff --git a/challenge-200/paulo-custodio/cpp/ch-2.cpp b/challenge-200/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d7ae53b788
--- /dev/null
+++ b/challenge-200/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,113 @@
+/*
+Challenge 200
+
+Task 2: Seven Segment 200
+Submitted by: Ryan J Thompson
+A seven segment display is an electronic component, usually used to display
+digits. The segments are labeled 'a' through 'g' as shown:
+
+
+Seven Segment
+
+
+The encoding of each digit can thus be represented compactly as a truth table:
+
+my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>;
+For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’ enabled.
+
+Write a program that accepts any decimal number and draws that number as a
+horizontal sequence of ASCII seven segment displays, similar to the following:
+
+
+------- ------- -------
+ | | | | |
+ | | | | |
+-------
+| | | | |
+| | | | |
+------- ------- -------
+To qualify as a seven segment display, each segment must be drawn (or not drawn)
+according to your @truth table.
+
+The number "200" was of course chosen to celebrate our 200th week!
+*/
+
+#include <iostream>
+#include <string>
+
+#define A 1
+#define B 2
+#define C 4
+#define D 8
+#define E 16
+#define F 32
+#define G 64
+
+int truth[10] = {
+ A|B|C|D|E|F, B|C, A|B|D|E|G, A|B|C|D|G, B|C|F|G,
+ A|C|D|F|G, A|C|D|E|F|G, A|B|C, A|B|C|D|E|F|G, A|B|C|F|G
+};
+
+void draw_number(int n) {
+ std::string s = std::to_string(n);
+
+ // draw a
+ for (size_t i = 0; i < s.size(); i++) {
+ int digit = s[i] - '0';
+ if ((truth[digit] & A)==A)
+ std::cout << " #### ";
+ else
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+ // draw f, b
+ for (int j = 0; j < 2; j++) {
+ for (size_t i = 0; i < s.size(); i++) {
+ int digit = s[i] - '0';
+ if ((truth[digit] & F)==F) std::cout << "#"; else std::cout << " ";
+ std::cout << " ";
+ if ((truth[digit] & B)==B) std::cout << "#"; else std::cout << " ";
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+ }
+ // draw g
+ for (size_t i = 0; i < s.size(); i++) {
+ int digit = s[i] - '0';
+ if ((truth[digit] & G)==G)
+ std::cout << " #### ";
+ else
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+ // draw e, c
+ for (int j = 0; j < 2; j++) {
+ for (size_t i = 0; i < s.size(); i++) {
+ int digit = s[i] - '0';
+ if ((truth[digit] & E)==E) std::cout << "#"; else std::cout << " ";
+ std::cout << " ";
+ if ((truth[digit] & C)==C) std::cout << "#"; else std::cout << " ";
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+ }
+ // draw d
+ for (size_t i = 0; i < s.size(); i++) {
+ int digit = s[i] - '0';
+ if ((truth[digit] & D)==D)
+ std::cout << " #### ";
+ else
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ std::cerr << "usage: ch-2 num" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ draw_number(atoi(argv[0]));
+}