aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-19 22:44:19 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-19 22:44:19 +0000
commit79cf1a10567ddfeddb644a408043d4d45827ad05 (patch)
tree876fff08b295acc6fd30888c8ca4c0f2838e3a1f
parenta1045c99cbfc1c89f9322b0c841b7951d9608d03 (diff)
downloadperlweeklychallenge-club-79cf1a10567ddfeddb644a408043d4d45827ad05.tar.gz
perlweeklychallenge-club-79cf1a10567ddfeddb644a408043d4d45827ad05.tar.bz2
perlweeklychallenge-club-79cf1a10567ddfeddb644a408043d4d45827ad05.zip
Add Perl, C, C++, BASIC and Forth solutions
-rw-r--r--challenge-198/paulo-custodio/Makefile2
-rw-r--r--challenge-198/paulo-custodio/basic/ch-1.bas61
-rw-r--r--challenge-198/paulo-custodio/basic/ch-2.bas67
-rw-r--r--challenge-198/paulo-custodio/c/ch-1.c62
-rw-r--r--challenge-198/paulo-custodio/c/ch-2.c63
-rw-r--r--challenge-198/paulo-custodio/cpp/ch-1.cpp51
-rw-r--r--challenge-198/paulo-custodio/cpp/ch-2.cpp61
-rw-r--r--challenge-198/paulo-custodio/forth/ch-1.fs72
-rw-r--r--challenge-198/paulo-custodio/forth/ch-2.fs55
-rw-r--r--challenge-198/paulo-custodio/perl/ch-1.pl48
-rw-r--r--challenge-198/paulo-custodio/perl/ch-2.pl57
-rw-r--r--challenge-198/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-198/paulo-custodio/t/test-2.yaml25
13 files changed, 639 insertions, 0 deletions
diff --git a/challenge-198/paulo-custodio/Makefile b/challenge-198/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-198/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-198/paulo-custodio/basic/ch-1.bas b/challenge-198/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..6c3c389f17
--- /dev/null
+++ b/challenge-198/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,61 @@
+' Challenge 198
+'
+' Task 1: Max Gap
+' Submitted by: Mohammad S Anwar
+' You are given a list of integers, @list.
+'
+' Write a script to find the total pairs in the sorted list where 2 consecutive
+' elements has the max gap. If the list contains less then 2 elements then
+' return 0.
+'
+'
+' Example 1
+' Input: @list = (2,5,8,1)
+' Output: 2
+'
+' Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+' Example 2
+' Input: @list = (3)
+' Output: 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 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
+
+function max_gap(nums() as integer) as integer
+ dim cur_gap as integer, gap_count as integer, gap as integer, i as integer
+ cur_gap=0: gap_count=0
+ sort nums()
+ for i=0 to ubound(nums)-1
+ gap=nums(i+1)-nums(i)
+ if gap>cur_gap then
+ cur_gap=gap
+ gap_count=1
+ elseif gap=cur_gap then
+ gap_count=gap_count+1
+ end if
+ next
+ max_gap=gap_count
+end function
+
+dim nums() as integer
+collect_args nums()
+print max_gap(nums())
diff --git a/challenge-198/paulo-custodio/basic/ch-2.bas b/challenge-198/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..85c026fad9
--- /dev/null
+++ b/challenge-198/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,67 @@
+' Challenge 198
+'
+' Challenge 198
+'
+' Task 2: Prime Count
+' Submitted by: Mohammad S Anwar
+' You are given an integer $n > 0.
+'
+' Write a script to print the count of primes less than $n.
+'
+' Example 1
+' Input: $n = 10
+' Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+' Example 2
+' Input: $n = 15
+' Output: 6
+' Example 3
+' Input: $n = 1
+' Output: 0
+' Example 4
+' Input: $n = 25
+' Output: 9
+
+' https://en.wikipedia.org/wiki/Primality_test
+function is_prime(n as integer) as boolean
+ dim i as integer
+ if n=2 or n=3 then
+ is_prime=true
+ elseif n<=1 or (n mod 2)=0 or (n mod 3)=0 then
+ is_prime=false
+ else
+ i=5
+ do while i*i<=n
+ if (n mod i)=0 or (n mod (i+2))=0 then
+ is_prime=false
+ exit function
+ end if
+ i=i+6
+ loop
+ is_prime=true
+ end if
+end function
+
+function next_prime(p as integer) as integer
+ if p<2 then
+ next_prime=2
+ else
+ p=p+1
+ do while not is_prime(p)
+ p=p+1
+ loop
+ next_prime=p
+ end if
+end function
+
+function count_primes_below_n(n as integer) as integer
+ dim p as integer, count as integer
+ count=0
+ p=2
+ do while p<n
+ count=count+1
+ p=next_prime(p)
+ loop
+ count_primes_below_n=count
+end function
+
+print count_primes_below_n(val(command(1)))
diff --git a/challenge-198/paulo-custodio/c/ch-1.c b/challenge-198/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..e5e884cdd4
--- /dev/null
+++ b/challenge-198/paulo-custodio/c/ch-1.c
@@ -0,0 +1,62 @@
+/*
+Challenge 198
+
+Task 1: Max Gap
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to find the total pairs in the sorted list where 2 consecutive
+elements has the max gap. If the list contains less then 2 elements then
+return 0.
+
+
+Example 1
+Input: @list = (2,5,8,1)
+Output: 2
+
+Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+Example 2
+Input: @list = (3)
+Output: 0
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+int compare(const void* a, const void* b) {
+ return *(int*)a - *(int*)b;
+}
+
+int max_gap(int* nums, int nums_size) {
+ qsort(nums, nums_size, sizeof(int), compare);
+ int max_gap=0;
+ int gap_count=0;
+ for (int i=0; i<nums_size-1; i++) {
+ int gap=nums[i+1]-nums[i];
+ if (gap>max_gap) {
+ max_gap=gap;
+ gap_count=1;
+ }
+ else if (gap==max_gap) {
+ gap_count++;
+ }
+ }
+ return gap_count;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ int* nums = check_mem(malloc(argc*sizeof(int)));
+ for (int i = 0; i < argc; i++)
+ nums[i] = atoi(argv[i]);
+ printf("%d\n", max_gap(nums, argc));
+ free(nums);
+}
diff --git a/challenge-198/paulo-custodio/c/ch-2.c b/challenge-198/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..8be5caeab4
--- /dev/null
+++ b/challenge-198/paulo-custodio/c/ch-2.c
@@ -0,0 +1,63 @@
+/*
+Challenge 198
+
+Task 2: Prime Count
+Submitted by: Mohammad S Anwar
+You are given an integer $n > 0.
+
+Write a script to print the count of primes less than $n.
+
+Example 1
+Input: $n = 10
+Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+Example 2
+Input: $n = 15
+Output: 6
+Example 3
+Input: $n = 1
+Output: 0
+Example 4
+Input: $n = 25
+Output: 9
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+// https://en.wikipedia.org/wiki/Primality_test
+bool is_prime(int n) {
+ if (n==2 || n==3) return true;
+ if (n<=1 || n%2==0 || n%3==0) return false;
+ for (int i=5; i*i<=n; i+=6)
+ if (n%i==0 || n%(i+2)==0) return false;
+ return true;
+}
+
+int next_prime(int p) {
+ p++;
+ while (!is_prime(p)) p++;
+ return p;
+}
+
+int count_primes_below_n(int n) {
+ int count=0;
+ int p=2;
+ while (p<n) {
+ count++;
+ p=next_prime(p);
+ }
+ return count;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc!=1) {
+ fputs("usage: ch-2 n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ int n=atoi(argv[0]);
+ int count=count_primes_below_n(n);
+ printf("%d\n", count);
+}
diff --git a/challenge-198/paulo-custodio/cpp/ch-1.cpp b/challenge-198/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..56fe743987
--- /dev/null
+++ b/challenge-198/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,51 @@
+/*
+Challenge 198
+
+Task 1: Max Gap
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to find the total pairs in the sorted list where 2 consecutive
+elements has the max gap. If the list contains less then 2 elements then
+return 0.
+
+
+Example 1
+Input: @list = (2,5,8,1)
+Output: 2
+
+Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+Example 2
+Input: @list = (3)
+Output: 0
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+int max_gap(std::vector<int>& nums) {
+ std::sort(nums.begin(), nums.end());
+ int max_gap=0;
+ int gap_count=0;
+ int nums_size=static_cast<int>(nums.size());
+ for (int i=0; i<nums_size-1; i++) {
+ int gap=nums[i+1]-nums[i];
+ if (gap>max_gap) {
+ max_gap=gap;
+ gap_count=1;
+ }
+ else if (gap==max_gap) {
+ gap_count++;
+ }
+ }
+ return gap_count;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ std::vector<int> nums;
+ for (int i = 0; i < argc; i++)
+ nums.push_back(atoi(argv[i]));
+ std::cout << max_gap(nums) << std::endl;
+}
diff --git a/challenge-198/paulo-custodio/cpp/ch-2.cpp b/challenge-198/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e139693cf3
--- /dev/null
+++ b/challenge-198/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,61 @@
+/*
+Challenge 198
+
+Task 2: Prime Count
+Submitted by: Mohammad S Anwar
+You are given an integer $n > 0.
+
+Write a script to print the count of primes less than $n.
+
+Example 1
+Input: $n = 10
+Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+Example 2
+Input: $n = 15
+Output: 6
+Example 3
+Input: $n = 1
+Output: 0
+Example 4
+Input: $n = 25
+Output: 9
+*/
+
+#include <iostream>
+
+// https://en.wikipedia.org/wiki/Primality_test
+bool is_prime(int n) {
+ if (n==2 || n==3) return true;
+ if (n<=1 || n%2==0 || n%3==0) return false;
+ for (int i=5; i*i<=n; i+=6)
+ if (n%i==0 || n%(i+2)==0) return false;
+ return true;
+}
+
+int next_prime(int p) {
+ p++;
+ while (!is_prime(p)) p++;
+ return p;
+}
+
+int count_primes_below_n(int n) {
+ int count=0;
+ int p=2;
+ while (p<n) {
+ count++;
+ p=next_prime(p);
+ }
+ return count;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc!=1) {
+ std::cerr << "usage: ch-2 n" << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ int n=atoi(argv[0]);
+ int count=count_primes_below_n(n);
+ std::cout << count << std::endl;
+}
diff --git a/challenge-198/paulo-custodio/forth/ch-1.fs b/challenge-198/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..f58d68b74a
--- /dev/null
+++ b/challenge-198/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,72 @@
+#! /usr/bin/env gforth
+
+\ Challenge 198
+\
+\ Task 1: Max Gap
+\ Submitted by: Mohammad S Anwar
+\ You are given a list of integers, @list.
+\
+\ Write a script to find the total pairs in the sorted list where 2 consecutive
+\ elements has the max gap. If the list contains less then 2 elements then
+\ return 0.
+\
+\
+\ Example 1
+\ Input: @list = (2,5,8,1)
+\ Output: 2
+\
+\ Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+\ Example 2
+\ Input: @list = (3)
+\ Output: 0
+
+0 VALUE nums
+0 VALUE nums_size
+
+: nums[] ( i -- addr )
+ CELLS nums +
+;
+
+\ collect arguments from input, convert to minutes and store in items
+: collect_args ( -- )
+ HERE TO nums
+ BEGIN NEXT-ARG DUP WHILE \ while argments
+ 0 0 2SWAP >NUMBER 2DROP DROP ,
+ REPEAT
+ 2DROP
+ HERE nums - CELL / TO nums_size
+;
+
+\ sort array of integers
+: sort ( -- )
+ 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
+;
+
+\ count max gaps
+: max_gap ( -- n )
+ 0 ( count )
+ nums_size 2 >= IF
+ sort
+ 0 { max_gap }
+ nums_size 1- 0 DO
+ I 1+ nums[] @ I nums[] @ - { gap }
+ gap max_gap > IF
+ gap TO max_gap
+ DROP 1
+ ELSE gap max_gap = IF
+ 1+
+ THEN THEN
+ LOOP
+ THEN
+;
+
+collect_args max_gap . CR
+BYE
diff --git a/challenge-198/paulo-custodio/forth/ch-2.fs b/challenge-198/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..b4fc1a992e
--- /dev/null
+++ b/challenge-198/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,55 @@
+#! /usr/bin/env gforth
+
+\ Challenge 198
+\
+\ Task 2: Prime Count
+\ Submitted by: Mohammad S Anwar
+\ You are given an integer $n > 0.
+\
+\ Write a script to print the count of primes less than $n.
+\
+\ Example 1
+\ Input: $n = 10
+\ Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+\ Example 2
+\ Input: $n = 15
+\ Output: 6
+\ Example 3
+\ Input: $n = 1
+\ Output: 0
+\ Example 4
+\ Input: $n = 25
+\ Output: 9
+
+\ https://en.wikipedia.org/wiki/Primality_test
+: is_prime { n -- f }
+ n 2 = n 3 = OR IF TRUE ELSE
+ n 1 <= n 2 MOD 0= n 3 MOD 0= OR OR IF FALSE ELSE
+ 5 { i }
+ BEGIN i i * n <= WHILE
+ n i MOD 0= n i 2 + MOD 0= OR IF FALSE EXIT THEN
+ i 6 + TO i
+ REPEAT
+ TRUE
+ THEN THEN
+;
+
+: next_prime ( p -- p )
+ 1+
+ BEGIN DUP is_prime 0= WHILE
+ 1+
+ REPEAT
+;
+
+: count_primes_below_n { n -- count }
+ 0 2 ( count prime )
+ BEGIN DUP n < WHILE
+ SWAP 1+ SWAP
+ next_prime
+ REPEAT
+ DROP
+;
+
+NEXT-ARG S>NUMBER? 0= THROW DROP
+count_primes_below_n . CR
+BYE
diff --git a/challenge-198/paulo-custodio/perl/ch-1.pl b/challenge-198/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..058b53b538
--- /dev/null
+++ b/challenge-198/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Challenge 198
+#
+# Task 1: Max Gap
+# Submitted by: Mohammad S Anwar
+# You are given a list of integers, @list.
+#
+# Write a script to find the total pairs in the sorted list where 2 consecutive
+# elements has the max gap. If the list contains less then 2 elements then
+# return 0.
+#
+#
+# Example 1
+# Input: @list = (2,5,8,1)
+# Output: 2
+#
+# Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8)
+# Example 2
+# Input: @list = (3)
+# Output: 0
+
+use Modern::Perl;
+
+sub max_gap {
+ my(@n)=@_;
+ return 0 if @n<2;
+ @n=sort @n;
+ my $max_gap=0;
+ my $gap_count=0;
+ for my $i (0..$#n-1) {
+ my $gap=$n[$i+1]-$n[$i];
+ if ($gap>$max_gap) {
+ $max_gap=$gap;
+ $gap_count=1;
+ }
+ elsif ($gap==$max_gap) {
+ $gap_count++;
+ }
+ }
+ return $gap_count;
+}
+
+@ARGV or die "usage: ch-1.pl nums\n";
+my @n=@ARGV;
+say max_gap(@n);
+
+
diff --git a/challenge-198/paulo-custodio/perl/ch-2.pl b/challenge-198/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..44fe133864
--- /dev/null
+++ b/challenge-198/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# Challenge 198
+#
+# Task 2: Prime Count
+# Submitted by: Mohammad S Anwar
+# You are given an integer $n > 0.
+#
+# Write a script to print the count of primes less than $n.
+#
+# Example 1
+# Input: $n = 10
+# Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7.
+# Example 2
+# Input: $n = 15
+# Output: 6
+# Example 3
+# Input: $n = 1
+# Output: 0
+# Example 4
+# Input: $n = 25
+# Output: 9
+
+use Modern::Perl;
+
+# https://en.wikipedia.org/wiki/Primality_test
+sub is_prime {
+ my($n) = @_;
+ return 1 if $n == 2 || $n == 3;
+ return 0 if $n <= 1 || $n % 2 == 0 || $n % 3 == 0;
+ for (my $i = 5; $i * $i <= $n; $i += 6) {
+ return 0 if $n % $i == 0 || $n % ($i + 2) == 0;
+ }
+ return 1;
+}
+
+sub next_prime {
+ my($p) = @_;
+ $p++;
+ $p++ while !is_prime($p);
+ return $p;
+}
+
+sub count_primes_below_n {
+ my($n) = @_;
+ my $count=0;
+ my $p=2;
+ while ($p<$n) {
+ $count++;
+ $p=next_prime($p);
+ }
+ return $count;
+}
+
+@ARGV==1 or die "usage: solve.pl limit\n";
+my $limit = shift;
+say count_primes_below_n($limit);
diff --git a/challenge-198/paulo-custodio/t/test-1.yaml b/challenge-198/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..09274db43a
--- /dev/null
+++ b/challenge-198/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2 5 8 1
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 2 5 9 1
+ input:
+ output: 1
diff --git a/challenge-198/paulo-custodio/t/test-2.yaml b/challenge-198/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..9272ef6ea9
--- /dev/null
+++ b/challenge-198/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 15
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 25
+ input:
+ output: 9
+- setup:
+ cleanup:
+ args: 13
+ input:
+ output: 5