diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 00:28:56 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 00:28:56 +0000 |
| commit | 203478bbfe21091f94c4559377f730f75e688dfd (patch) | |
| tree | ac6700738a351c6dbdbe0f16503cba14cf2d91d2 /challenge-196/paulo-custodio/cpp | |
| parent | 6737a18810400c88848bb9364f9abf5297a5caef (diff) | |
| download | perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.gz perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.bz2 perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.zip | |
Add Perl, C, C++, BASIC and Forth solutions
Diffstat (limited to 'challenge-196/paulo-custodio/cpp')
| -rw-r--r-- | challenge-196/paulo-custodio/cpp/ch-1.cpp | 60 | ||||
| -rw-r--r-- | challenge-196/paulo-custodio/cpp/ch-2.cpp | 59 |
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-196/paulo-custodio/cpp/ch-1.cpp b/challenge-196/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..8c62d7d84e --- /dev/null +++ b/challenge-196/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,60 @@ +/* +Challenge 197 + +Task 1: Pattern 132 +Submitted by: Mohammad S Anwar +You are given a list of integers, @list. + +Write a script to find out subsequence that respect Pattern 132. Return empty array if none found. + + +Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j]. + + +Example 1 +Input: @list = (3, 1, 4, 2) +Output: (1, 4, 2) respect the Pattern 132. +Example 2 +Input: @list = (1, 2, 3, 4) +Output: () since no susbsequence can be found. +Example 3 +Input: @list = (1, 3, 2, 4, 6, 5) +Output: (1, 3, 2) if more than one subsequence found then return the first. +Example 4 +Input: @list = (1, 3, 4, 2) +Output: (1, 3, 2) +*/ + +#include <iostream> +#include <vector> + +void print_patter132(const std::vector<int>& nums) { + int nums_size=static_cast<int>(nums.size()); + std::cout<<"("; + for (int i=0; i<nums_size-2; i++) { + for (int j=i+1; j<nums_size-1; j++) { + for (int k=j+1; k<nums_size; k++) { + if (nums[i]<nums[k] && nums[k]<nums[j]) { + std::cout<<nums[i]<<" "<<nums[j]<<" "<<nums[k]; + goto end; + } + } + } + } +end: + std::cout<<")"<<std::endl; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr<<"usage: ch-1 nums..."<<std::endl; + return EXIT_FAILURE; + } + + std::vector<int> nums; + for (int i=0; i<argc; i++) + nums.push_back(atoi(argv[i])); + + print_patter132(nums); +} diff --git a/challenge-196/paulo-custodio/cpp/ch-2.cpp b/challenge-196/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..04c5a3f0b1 --- /dev/null +++ b/challenge-196/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,59 @@ +/* +Challenge 197 + +Task 2: Range List +Submitted by: Mohammad S Anwar +You are given a sorted unique integer array, @array. + +Write a script to find all possible Number Range i.e [x, y] represent range +all integers from x and y (both inclusive). + + +Each subsequence of two or more contiguous integers + + +Example 1 +Input: @array = (1,3,4,5,7) +Output: [3,5] +Example 2 +Input: @array = (1,2,3,6,7,9) +Output: [1,3], [6,7] +Example 3 +Input: @array = (0,1,2,4,5,6,8,9) +Output: [0,2], [4,6], [8,9] +*/ + +#include <iostream> +#include <string> +#include <vector> + +void print_ranges(const std::vector<int>& nums) { + int nums_size=static_cast<int>(nums.size()); + std::cout<<"("; + std::string sep=""; + for (int i=0; i<nums_size; i++) { + int j=0; + while (i+j<nums_size && nums[i]+j==nums[i+j]) + j++; + if (j>1) { + std::cout<<sep<<"["<<nums[i]<<","<<nums[i+j-1]<<"]"; + sep=", "; + i+=j-1; + } + } + std::cout<<")"<<std::endl; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr<<"usage: ch-1 nums..."<<std::endl; + return EXIT_FAILURE; + } + + std::vector<int> nums; + for (int i=0; i<argc; i++) + nums.push_back(atoi(argv[i])); + + print_ranges(nums); +} |
