aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-197')
-rw-r--r--challenge-197/paulo-custodio/Makefile2
-rw-r--r--challenge-197/paulo-custodio/basic/ch-1.bas64
-rw-r--r--challenge-197/paulo-custodio/basic/ch-2.bas74
-rw-r--r--challenge-197/paulo-custodio/c/ch-1.c66
-rw-r--r--challenge-197/paulo-custodio/c/ch-2.c70
-rw-r--r--challenge-197/paulo-custodio/cpp/ch-1.cpp53
-rw-r--r--challenge-197/paulo-custodio/cpp/ch-2.cpp55
-rw-r--r--challenge-197/paulo-custodio/forth/ch-1.fs75
-rw-r--r--challenge-197/paulo-custodio/forth/ch-2.fs86
-rw-r--r--challenge-197/paulo-custodio/perl/ch-1.pl25
-rw-r--r--challenge-197/paulo-custodio/perl/ch-2.pl40
-rw-r--r--challenge-197/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-197/paulo-custodio/t/test-2.yaml20
13 files changed, 645 insertions, 0 deletions
diff --git a/challenge-197/paulo-custodio/Makefile b/challenge-197/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-197/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-197/paulo-custodio/basic/ch-1.bas b/challenge-197/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..836b6a7bf3
--- /dev/null
+++ b/challenge-197/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,64 @@
+' 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)
+
+sub collect_args(nums() as integer)
+ dim i as integer
+ i=0
+ do while command(i+1)<>""
+ redim preserve nums(i) as integer
+ nums(i)=val(command(i+1))
+ i=i+1
+ loop
+end sub
+
+sub print_nums(nums() as integer)
+ dim i as integer
+
+ for i=0 to ubound(nums)
+ print nums(i);
+ next
+ print
+end sub
+
+sub move_zeros(nums() as integer)
+ dim copy(ubound(nums)) as integer, i as integer, p as integer
+
+ for i=0 to ubound(nums)
+ copy(i)=nums(i)
+ next
+ p=0
+ for i=0 to ubound(copy)
+ if copy(i)<>0 then
+ nums(p)=copy(i)
+ p=p+1
+ end if
+ next
+ for i=0 to ubound(copy)
+ if copy(i)=0 then
+ nums(p)=copy(i)
+ p=p+1
+ end if
+ next
+end sub
+
+dim nums() as integer
+collect_args nums()
+move_zeros nums()
+print_nums nums()
diff --git a/challenge-197/paulo-custodio/basic/ch-2.bas b/challenge-197/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..6979b9b01b
--- /dev/null
+++ b/challenge-197/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,74 @@
+' 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)
+
+sub collect_args(nums() as integer)
+ dim i as integer
+ i=0
+ do while command(i+1)<>""
+ redim preserve nums(i) as integer
+ nums(i)=val(command(i+1))
+ i=i+1
+ loop
+end sub
+
+sub print_nums(nums() as integer)
+ dim i as integer
+
+ for i=0 to ubound(nums)
+ print nums(i);
+ next
+ print
+end sub
+
+sub sort(nums() as integer)
+ dim i as integer, j as integer, n as integer
+
+ for i=lbound(nums) to ubound(nums)-1
+ for j=i+1 to ubound(nums)
+ if nums(i)>nums(j) then
+ n=nums(i): nums(i)=nums(j): nums(j)=n
+ end if
+ next
+ next
+end sub
+
+sub copy_data(dst() as integer, dst_idx as integer, from() as integer, from_idx as integer)
+ do while dst_idx>=0
+ dst(dst_idx)=from(from_idx)
+ dst_idx=dst_idx-2
+ from_idx=from_idx+1
+ loop
+end sub
+
+sub wiggle_sort(nums() as integer)
+ dim i as integer, j as integer, copy(ubound(nums)) as integer
+
+ for i=0 to ubound(nums)
+ copy(i)=nums(i)
+ next
+ sort copy()
+
+ copy_data nums(), ubound(nums)-1, copy(), 0
+ copy_data nums(), ubound(nums), copy(), (ubound(nums)+1)/2
+end sub
+
+dim nums() as integer
+collect_args nums()
+wiggle_sort nums()
+print_nums nums()
diff --git a/challenge-197/paulo-custodio/c/ch-1.c b/challenge-197/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..ce6d9efb89
--- /dev/null
+++ b/challenge-197/paulo-custodio/c/ch-1.c
@@ -0,0 +1,66 @@
+/*
+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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+void move_zeros(int* nums, int nums_size) {
+ int* copy = check_mem(malloc(nums_size * sizeof(int)));
+ memcpy(copy, nums, nums_size * sizeof(int));
+ int p=0;
+ for (int i=0; i<nums_size; i++)
+ if (copy[i]!=0)
+ nums[p++]=copy[i];
+ for (int i=0; i<nums_size; i++)
+ if (copy[i]==0)
+ nums[p++]=copy[i];
+ free(copy);
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ fputs("usage: ch-1 nums...\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ int* nums = check_mem(malloc(argc * sizeof(int)));
+ for (int i=0; i<argc; i++)
+ nums[i]=atoi(argv[i]);
+
+ move_zeros(nums, argc);
+
+ for (int i=0; i<argc; i++)
+ printf("%d ", nums[i]);
+ printf("\n");
+
+ free(nums);
+}
diff --git a/challenge-197/paulo-custodio/c/ch-2.c b/challenge-197/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..8be22084c4
--- /dev/null
+++ b/challenge-197/paulo-custodio/c/ch-2.c
@@ -0,0 +1,70 @@
+/*
+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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+void copy_data(int* to, int to_idx, int* from, int from_idx) {
+ for (; to_idx>=0; to_idx-=2)
+ to[to_idx]=from[from_idx++];
+}
+
+int compare(const void* a, const void* b) {
+ return *(int*)a - *(int*)b;
+}
+
+void wiggle_sort(int* nums, int nums_size) {
+ int* copy = check_mem(malloc(nums_size * sizeof(int)));
+ memcpy(copy, nums, nums_size * sizeof(int));
+ qsort(copy, nums_size, sizeof(int), compare);
+ copy_data(nums, nums_size-2, copy, 0);
+ copy_data(nums, nums_size-1, copy, nums_size/2);
+ free(copy);
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ fputs("usage: ch-2 nums...\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ int* nums = check_mem(malloc(argc * sizeof(int)));
+ for (int i=0; i<argc; i++)
+ nums[i]=atoi(argv[i]);
+
+ wiggle_sort(nums, argc);
+
+ for (int i=0; i<argc; i++)
+ printf("%d ", nums[i]);
+ printf("\n");
+
+ free(nums);
+}
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;
+}
diff --git a/challenge-197/paulo-custodio/forth/ch-1.fs b/challenge-197/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..f7a375981f
--- /dev/null
+++ b/challenge-197/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,75 @@
+#! /usr/bin/env gforth
+
+\ 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)
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: nums[] ( idx -- addr )
+ CELLS nums +
+;
+
+: collect_args ( -- )
+ BEGIN NEXT-ARG DUP WHILE
+ 0 0 2SWAP >NUMBER 2DROP DROP
+ nums_size nums[] !
+ nums_size 1+ TO nums_size
+ REPEAT
+ 2DROP
+;
+
+: print_nums ( -- )
+ nums_size 0 ?DO
+ I nums[] @ .
+ LOOP
+ CR
+;
+
+: move_zeros ( -- )
+ nums_size 1 > IF
+ nums_size { p } \ copy to end of list
+ \ copy non-zero elements
+ nums_size 0 DO
+ I nums[] @ DUP 0<> IF
+ p nums[] !
+ p 1+ TO p
+ ELSE
+ DROP
+ THEN
+ LOOP
+ \ copy zero elements
+ nums_size 0 DO
+ I nums[] @ DUP 0= IF
+ p nums[] !
+ p 1+ TO p
+ ELSE
+ DROP
+ THEN
+ LOOP
+ \ move back to start of nums
+ nums_size nums[] 0 nums[] nums_size CELLS MOVE
+ THEN
+;
+
+collect_args
+move_zeros
+print_nums
+BYE
diff --git a/challenge-197/paulo-custodio/forth/ch-2.fs b/challenge-197/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..b75e2073bd
--- /dev/null
+++ b/challenge-197/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,86 @@
+#! /usr/bin/env gforth
+
+\ 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)
+
+CREATE nums 256 CELLS ALLOT
+CREATE copy 256 CELLS ALLOT
+0 VALUE nums_size
+
+: nums[] ( i -- addr )
+ CELLS nums +
+;
+
+: copy[] ( i -- addr )
+ CELLS copy +
+;
+
+: collect_args ( -- )
+ BEGIN NEXT-ARG DUP WHILE
+ 0 0 2SWAP >NUMBER 2DROP DROP
+ nums_size nums[] !
+ nums_size 1+ TO nums_size
+ REPEAT
+ 2DROP
+;
+
+: print_nums ( -- )
+ nums_size 0 ?DO
+ I nums[] @ .
+ LOOP
+ CR
+;
+
+: sort ( -- )
+ nums_size 1 > IF
+ nums_size 1- 0 ?DO
+ nums_size I 1+ ?DO
+ J nums[] @ I nums[] @ > IF
+ J nums[] @ I nums[] @
+ J nums[] ! I nums[] !
+ THEN
+ LOOP
+ LOOP
+ THEN
+;
+
+: copy_data { src dst -- }
+ BEGIN dst 0>= WHILE
+ src copy[] @ dst nums[] !
+ src 1+ TO src
+ dst 2 - TO dst
+ REPEAT
+;
+
+: copy_nums ( -- )
+ sort nums copy nums_size CELLS MOVE
+;
+
+: wiggle_sort ( -- )
+ nums_size 1 > IF
+ copy_nums
+ 0 nums_size 2 - copy_data
+ nums_size 2 / nums_size 1- copy_data
+ THEN
+;
+
+collect_args
+wiggle_sort
+print_nums
+BYE
diff --git a/challenge-197/paulo-custodio/perl/ch-1.pl b/challenge-197/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..5c9d599734
--- /dev/null
+++ b/challenge-197/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+
+# 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)
+
+use Modern::Perl;
+
+say join " ", (grep {$_} @ARGV), (grep {!$_} @ARGV);
diff --git a/challenge-197/paulo-custodio/perl/ch-2.pl b/challenge-197/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3180b734fc
--- /dev/null
+++ b/challenge-197/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# 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)
+
+use Modern::Perl;
+
+sub copy_data {
+ my($to, $to_idx, $from, $from_idx) = @_;
+ for (; $to_idx >= 0; $to_idx-=2) {
+ $to->[$to_idx]=$from->[$from_idx++];
+ }
+}
+
+sub wiggle_sort {
+ my(@nums) = @_;
+ my @copy = sort {$a<=>$b} @nums;
+ copy_data(\@nums, scalar(@nums)-2, \@copy, 0);
+ copy_data(\@nums, scalar(@nums)-1, \@copy, int(scalar(@nums)/2));
+ return @nums;
+}
+
+my @nums = wiggle_sort(@ARGV);
+say "@nums";
diff --git a/challenge-197/paulo-custodio/t/test-1.yaml b/challenge-197/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..fd2a3fd918
--- /dev/null
+++ b/challenge-197/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1 0 3 0 0 5
+ input:
+ output: 1 3 5 0 0 0
+- setup:
+ cleanup:
+ args: 1 6 4
+ input:
+ output: 1 6 4
+- setup:
+ cleanup:
+ args: 0 1 0 2 0
+ input:
+ output: 1 2 0 0 0
diff --git a/challenge-197/paulo-custodio/t/test-2.yaml b/challenge-197/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..1d7221da1d
--- /dev/null
+++ b/challenge-197/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 5 1 1 6 4
+ input:
+ output: 1 6 1 5 1 4
+- setup:
+ cleanup:
+ args: 1 3 2 2 3 1
+ input:
+ output: 2 3 1 3 1 2
+- setup:
+ cleanup:
+ args: 1 3 2 2 3 1
+ input:
+ output: 2 3 1 3 1 2
+- setup:
+ cleanup:
+ args: 5 1 6 4 1
+ input:
+ output: 6 1 5 1 4