aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 19:39:23 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 19:39:23 +0000
commit1c86d96be6cff3051f909f168d2ec69446641626 (patch)
tree5eb1f8858667246d9e59c958d980521b1b7210f8
parentaafda8bd1c6e2cfa527e5ff13dd09999af0c7398 (diff)
downloadperlweeklychallenge-club-1c86d96be6cff3051f909f168d2ec69446641626.tar.gz
perlweeklychallenge-club-1c86d96be6cff3051f909f168d2ec69446641626.tar.bz2
perlweeklychallenge-club-1c86d96be6cff3051f909f168d2ec69446641626.zip
Add BASIC solution
-rw-r--r--challenge-206/paulo-custodio/basic/ch-1.bas100
-rw-r--r--challenge-206/paulo-custodio/basic/ch-2.bas111
-rw-r--r--challenge-206/paulo-custodio/c/ch-1.c8
-rw-r--r--challenge-206/paulo-custodio/cpp/ch-1.cpp8
-rw-r--r--challenge-206/paulo-custodio/forth/ch-1.fs10
-rw-r--r--challenge-206/paulo-custodio/perl/ch-1.pl6
6 files changed, 223 insertions, 20 deletions
diff --git a/challenge-206/paulo-custodio/basic/ch-1.bas b/challenge-206/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..cce224cdb5
--- /dev/null
+++ b/challenge-206/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,100 @@
+' Challenge 206
+'
+' Task 1: Shortest Time
+' Submitted by: Mohammad S Anwar
+'
+' You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
+'
+' Write a script to find out the shortest time in minutes between any two time points.
+' Example 1
+'
+' Input: @time = ("00:00", "23:55", "20:00")
+' Output: 5
+'
+' Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
+'
+' Example 2
+'
+' Input: @array = ("01:01", "00:50", "00:57")
+' Output: 4
+'
+' Example 3
+'
+' Input: @array = ("10:10", "09:30", "09:00", "09:55")
+' Output: 15
+
+dim mins() as integer
+
+' parse hours:minutes, return minutes
+function parse_minutes(s as string) as integer
+ dim i as integer, hours as integer, minutes as integer
+
+ 'parse hours
+ i=1
+ do while i<=len(s) and mid(s,i,1)>="0" and mid(s,i,1)<="9"
+ i=i+1
+ loop
+ hours=val(left(s,i))
+ if mid(s,i,1)=":" then i=i+1
+ s=mid(s,i)
+
+ 'parse minutes
+ do while i<=len(s) and mid(s,i,1)>="0" and mid(s,i,1)<="9"
+ i=i+1
+ loop
+ minutes=val(left(s,i))
+ parse_minutes=60*hours+minutes
+end function
+
+' collect arguments
+sub collect_args(mins() as integer)
+ dim i as integer
+
+ i=0
+ do while command(i+1)<>""
+ redim preserve mins(i) as integer
+ mins(i)=parse_minutes(command(i+1))
+ i=i+1
+ loop
+end sub
+
+' sort array
+sub sort(mins() as integer)
+ dim i as integer, j as integer, n as integer
+
+ for i=lbound(mins) to ubound(mins)-1
+ for j=i+1 to ubound(mins)
+ if mins(i)>mins(j) then
+ n=mins(i): mins(i)=mins(j): mins(j)=n
+ end if
+ next
+ next
+end sub
+
+' prepare data for check - sort and add first+24hours to end
+sub setup_data(mins() as integer)
+ dim i as integer
+
+ collect_args mins()
+ sort mins()
+ i=ubound(mins)
+ redim preserve mins(i+1) as integer
+ mins(i+1)=mins(0)+24*60
+end sub
+
+
+' get minimum interval
+function get_min_interval(mins() as integer) as integer
+ dim i as integer, min as integer
+
+ min=mins(ubound(mins))-mins(0)
+ for i=0 to ubound(mins)-1
+ if mins(i+1)-mins(i)<min then
+ min=mins(i+1)-mins(i)
+ end if
+ next
+ get_min_interval=min
+end function
+
+setup_data mins()
+print get_min_interval(mins())
diff --git a/challenge-206/paulo-custodio/basic/ch-2.bas b/challenge-206/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..7a5bbfd7a0
--- /dev/null
+++ b/challenge-206/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,111 @@
+' Challenge 206
+'
+' Task 2: Array Pairings
+' Submitted by: Mohammad S Anwar
+'
+' You are given an array of integers having even number of elements..
+'
+' Write a script to find the maximum sum of the minimum of each pairs.
+' Example 1
+'
+' Input: @array = (1,2,3,4)
+' Output: 4
+'
+' Possible Pairings are as below:
+' a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
+' b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
+' c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
+'
+' So the maxium sum is 4.
+'
+' Example 2
+'
+' Input: @array = (0,2,1,3)
+' Output: 2
+'
+' Possible Pairings are as below:
+' a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
+' b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
+' c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
+'
+' So the maximum sum is 2.
+
+' append number to list
+sub push_elem(nums() as integer, n as integer)
+ dim i as integer
+
+ i=ubound(nums)
+ redim preserve nums(i+1) as integer
+ nums(i+1)=n
+end sub
+
+' remove number from list
+sub del_elem(nums() as integer, idx as integer)
+ dim i as integer
+
+ for i=idx to ubound(nums)-1
+ nums(i)=nums(i+1)
+ next
+ if ubound(nums)=0 then
+ erase nums
+ else
+ redim preserve nums(ubound(nums)-1) as integer
+ end if
+end sub
+
+' copy list
+sub copy_list(src() as integer, dst() as integer)
+ dim i as integer
+
+ redim dst(ubound(src)) as integer
+ for i=0 to ubound(src)
+ dst(i)=src(i)
+ next
+end sub
+
+' collect arguments
+sub collect_args(nums() as integer)
+ dim i as integer
+
+ i=0
+ do while command(i+1)<>""
+ push_elem nums(),val(command(i+1))
+ i=i+1
+ loop
+end sub
+
+' compute maximum sum of minimum of each pair
+sub compute_pairs_max(byref max as integer, set() as integer, pending() as integer)
+ dim i as integer, j as integer, sum as integer, n as integer
+
+ if ubound(pending)<0 then ' compute sum, set max
+ sum=0
+ for i=0 to ubound(set) step 2
+ if set(i)<set(i+1) then n=set(i) else n=set(i+1)
+ sum=sum+n
+ next
+ if sum>max then max=sum
+ else ' extract each pair and recurse
+ for i=0 to ubound(pending)-1
+ for j=i+1 to ubound(pending)
+ dim new_set() as integer
+ dim new_pending() as integer
+
+ copy_list set(), new_set()
+ push_elem new_set(), pending(i)
+ push_elem new_set(), pending(j)
+
+ copy_list pending(), new_pending()
+ del_elem new_pending(), j
+ del_elem new_pending(), i
+
+ compute_pairs_max max, new_set(), new_pending()
+ next
+ next
+ end if
+end sub
+
+dim empty() as integer, nums() as integer, max as integer
+collect_args nums()
+compute_pairs_max max, empty(), nums()
+print max
diff --git a/challenge-206/paulo-custodio/c/ch-1.c b/challenge-206/paulo-custodio/c/ch-1.c
index abb788e056..10e7e64ece 100644
--- a/challenge-206/paulo-custodio/c/ch-1.c
+++ b/challenge-206/paulo-custodio/c/ch-1.c
@@ -56,11 +56,9 @@ int main(int argc, char* argv[]) {
int min = items[num_items-1] - items[0];
for (int i = 0; i < num_items-1; i++) {
- for (int j = i+1; j < num_items; j++) {
- int n = items[j] - items[i];
- if (n < min)
- min = n;
- }
+ int n = items[i+1] - items[i];
+ if (n < min)
+ min = n;
}
printf("%d\n", min);
diff --git a/challenge-206/paulo-custodio/cpp/ch-1.cpp b/challenge-206/paulo-custodio/cpp/ch-1.cpp
index c866da59c5..de714209d8 100644
--- a/challenge-206/paulo-custodio/cpp/ch-1.cpp
+++ b/challenge-206/paulo-custodio/cpp/ch-1.cpp
@@ -56,11 +56,9 @@ int main(int argc, char* argv[]) {
int min = items.back() - items.front();
for (size_t i = 0; i < items.size() - 1; i++) {
- for (size_t j = i + 1; j < items.size(); j++) {
- int n = items[j] - items[i];
- if (n < min)
- min = n;
- }
+ int n = items[i+1] - items[i];
+ if (n < min)
+ min = n;
}
std::cout << min << std::endl;
diff --git a/challenge-206/paulo-custodio/forth/ch-1.fs b/challenge-206/paulo-custodio/forth/ch-1.fs
index b8115de9f2..5d210fad00 100644
--- a/challenge-206/paulo-custodio/forth/ch-1.fs
+++ b/challenge-206/paulo-custodio/forth/ch-1.fs
@@ -91,12 +91,10 @@
: compute-minimum-interval ( -- minutes )
24 60 * ( min )
num_items 1- 0 DO
- num_items I 1+ DO
- I items[] @
- J items[] @
- -
- MIN
- LOOP
+ I 1+ items[] @
+ I items[] @
+ -
+ MIN
LOOP
;
diff --git a/challenge-206/paulo-custodio/perl/ch-1.pl b/challenge-206/paulo-custodio/perl/ch-1.pl
index 6d0bb7cf05..fadba97c6a 100644
--- a/challenge-206/paulo-custodio/perl/ch-1.pl
+++ b/challenge-206/paulo-custodio/perl/ch-1.pl
@@ -32,10 +32,8 @@ my @in = sort { $a <=> $b } map { minutes($_) } @ARGV;
push @in, $in[0] + minutes("24:00");
my $min = $in[-1] - $in[0];
for my $i (0..$#in-1) {
- for my $j ($i+1..$#in) {
- my $n = $in[$j] - $in[$i];
- $min = $n if $n < $min;
- }
+ my $n = $in[$i+1] - $in[$i];
+ $min = $n if $n < $min;
}
say $min;