aboutsummaryrefslogtreecommitdiff
path: root/challenge-197/paulo-custodio/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-197/paulo-custodio/cpp')
-rw-r--r--challenge-197/paulo-custodio/cpp/ch-1.cpp53
-rw-r--r--challenge-197/paulo-custodio/cpp/ch-2.cpp55
2 files changed, 108 insertions, 0 deletions
diff --git a/challenge-197/paulo-custodio/cpp/ch-1.cpp b/challenge-197/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..a6001446f9
--- /dev/null
+++ b/challenge-197/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,53 @@
+/*
+Challenge 197
+
+Task 1: Move Zero
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to move all zero, if exists, to the end while maintaining
+the relative order of non-zero elements.
+
+
+Example 1
+Input: @list = (1, 0, 3, 0, 0, 5)
+Output: (1, 3, 5, 0, 0, 0)
+Example 2
+Input: @list = (1, 6, 4)
+Output: (1, 6, 4)
+Example 3
+Input: @list = (0, 1, 0, 2, 0)
+Output: (1, 2, 0, 0, 0)
+*/
+
+#include <iostream>
+#include <vector>
+
+void move_zeros(std::vector<int>& nums) {
+ std::vector<int> copy=nums;
+ size_t p=0;
+ for (size_t i=0; i<copy.size(); i++)
+ if (copy[i]!=0)
+ nums[p++]=copy[i];
+ for (size_t i=0; i<copy.size(); i++)
+ if (copy[i]==0)
+ nums[p++]=copy[i];
+}
+
+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]));
+
+ move_zeros(nums);
+
+ for (int i=0; i<argc; i++)
+ std::cout << nums[i] << " ";
+ std::cout << std::endl;
+}
diff --git a/challenge-197/paulo-custodio/cpp/ch-2.cpp b/challenge-197/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..f554a3248f
--- /dev/null
+++ b/challenge-197/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,55 @@
+/*
+Challenge 197
+
+Task 2: Wiggle Sort
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to perform Wiggle Sort on the given list.
+
+
+Wiggle sort would be such as list[0] < list[1] > list[2] < list[3]….
+
+
+Example 1
+Input: @list = (1,5,1,1,6,4)
+Output: (1,6,1,5,1,4)
+Example 2
+Input: @list = (1,3,2,2,3,1)
+Output: (2,3,1,3,1,2)
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+void copy_data(std::vector<int>& to, int to_idx, const std::vector<int>& from, int from_idx) {
+ for (; to_idx>=0; to_idx-=2)
+ to[to_idx]=from[from_idx++];
+}
+
+void wiggle_sort(std::vector<int>& nums) {
+ int nums_size=static_cast<int>(nums.size());
+ std::vector<int> copy=nums;
+ std::sort(copy.begin(), copy.end());
+ copy_data(nums, nums_size-2, copy, 0);
+ copy_data(nums, nums_size-1, copy, nums_size/2);
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ std::cerr << "usage: ch-2 nums..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> nums;
+ for (int i=0; i<argc; i++)
+ nums.push_back(atoi(argv[i]));
+
+ wiggle_sort(nums);
+
+ for (int i=0; i<argc; i++)
+ std::cout << nums[i] << " ";
+ std::cout << std::endl;
+}