aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-12-29 18:35:11 +0000
committerGitHub <noreply@github.com>2019-12-29 18:35:11 +0000
commit4379a3b73cd240ad66f50476a5c849b4d68c50dc (patch)
tree9092bd4d32ea97ba9177af573a5a84a5969ee60c
parent4bc270c0ff8268a9fddc6bd2ee48602eeb07cd21 (diff)
parent75b3d80ae83c78e3530302f22156dcd82b1c7f8b (diff)
downloadperlweeklychallenge-club-4379a3b73cd240ad66f50476a5c849b4d68c50dc.tar.gz
perlweeklychallenge-club-4379a3b73cd240ad66f50476a5c849b4d68c50dc.tar.bz2
perlweeklychallenge-club-4379a3b73cd240ad66f50476a5c849b4d68c50dc.zip
Merge pull request #1079 from dcw803/master
imported my (dcw803) solutions
-rw-r--r--challenge-040/duncan-c-white/README77
-rwxr-xr-xchallenge-040/duncan-c-white/perl5/ch-1.pl57
-rwxr-xr-xchallenge-040/duncan-c-white/perl5/ch-2.pl43
3 files changed, 139 insertions, 38 deletions
diff --git a/challenge-040/duncan-c-white/README b/challenge-040/duncan-c-white/README
index 8fe990172f..5bca8e63b0 100644
--- a/challenge-040/duncan-c-white/README
+++ b/challenge-040/duncan-c-white/README
@@ -1,47 +1,48 @@
-Challenge 1: "A guest house had a policy that the light remain ON as
-long as the at least one guest is in the house. There is guest book
-which tracks all guest in/out time. Write a script to find out how long
-in minutes the light were ON.
-
-Guest Book
-
-1) Alex IN: 09:10 OUT: 09:45
-2) Arnold IN: 09:15 OUT: 09:33
-3) Bob IN: 09:22 OUT: 09:55
-4) Charlie IN: 09:25 OUT: 10:05
-5) Steve IN: 09:33 OUT: 10:01
-6) Roger IN: 09:44 OUT: 10:12
-7) David IN: 09:57 OUT: 10:23
-8) Neil IN: 10:01 OUT: 10:19
-9) Chris IN: 10:10 OUT: 11:00
+Task 1: "Show multiple arrays content:
+
+You are given two or more arrays. Write a script to display values of
+each list at a given index.
+
+For example:
+Array 1: [ I L O V E Y O U ]
+Array 2: [ 2 4 0 3 2 0 1 9 ]
+Array 3: [ ! ? £ $ % ^ & * ]
+
+We expect the following output:
+
+I 2 !
+L 4 ?
+O 0 £
+V 3 $
+E 2 %
+Y 0 ^
+O 1 &
+U 9 *
"
+My notes: trivial. Each array on command line as a word, i.e. array elements
+are single characters? Allow any number of arrays (arguments) >= 2.
+Also handle the case where the arrays are not necessarily of the same length,
+by displaying '?' for any element off the end of an array.
-My notes: Nice question. Looks reasonably straightforward, especially if
-we may assume that the IN times are in time order (as they are in the example
-data above, and would naturally be in a physical guest book, where each person
-writes their name and "IN" time in the first free row.
-Probably need to store the "OUT times that haven't happened yet" as
-future-time diary events a la Discrete Event Simulation.. Core idea is:
-store a set of users IN at the current time, and modify the set as time
-passes. Think I have a Delta Time queue Perl module somewhere to reuse.
+Task 2: "Sort SubList
+You are given a list of numbers and set of indices belong to the
+list. Write a script to sort the values belongs to the indices.
-Challenge 2: "Write a script to demonstrate Reverse Polish
-notation(RPN). Checkout https://en.wikipedia.org/wiki/Reverse_Polish_notation
-for more information about RPN. (Contributed by Andrezgz) "
+For example:
-My notes: That's pretty open ended - but on the other hand I know RPN
-very well over the years so that gives quite some scope to play:-)
-An RPN evaluator, or a coventional expression->RPN translator, would
-be obvious tools to build, the evaluator in particular is trivial to
-do and a great opportunity to use callback functions.
+List: [ 10, 4, 1, 8, 12, 3 ]
+Indices: 0,2,5
-BTW, almost any script in Postscript would demonstrate RPN as Postscript
-is totally RPN-based, so this may is a great opportunity to go for
-Postscript again:-)
+We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3.
-I spent quite a lot of time trying to build a tiny language and translate
-it to Perl and Postscript, in order to show RPN in the Postscript version,
-but I ran out of time; maybe another time..
+Final List would look like below:
+List: [ 1, 4, 3, 8, 12, 10 ]
+"
+
+My notes: looks very easy. I guess the decision is: sort in place, or
+extract items via an array slice, sort them, then update those items
+in the original array (via another array slice?) The latter sounds
+much the easier, so let's do that:-).
diff --git a/challenge-040/duncan-c-white/perl5/ch-1.pl b/challenge-040/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..5a434893bf
--- /dev/null
+++ b/challenge-040/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+#
+# Task 1: "Show multiple arrays content:
+#
+# You are given two or more arrays. Write a script to display values of
+# each list at a given index.
+#
+# For example:
+# Array 1: [ I L O V E Y O U ]
+# Array 2: [ 2 4 0 3 2 0 1 9 ]
+# Array 3: [ ! ? £ $ % ^ & * ]
+#
+# We expect the following output:
+#
+# I 2 !
+# L 4 ?
+# O 0 £
+# V 3 $
+# E 2 %
+# Y 0 ^
+# O 1 &
+# U 9 *
+# "
+#
+# My notes: trivial. Each array on command line as a word, i.e. array elements
+# are single characters? Allow any number of arrays (arguments) >= 2.
+# Also handle the case where the arrays are not necessarily of the same length,
+# by displaying '?' for any element off the end of an array.
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+#use Data::Dumper;
+
+die "Usage: ch-1.pl ARRAY1 ARRAY2 [ARRAYn..]\n" if @ARGV<2;
+
+my $n = @ARGV;
+my $len = length($ARGV[0]);
+
+foreach my $i (1..$#ARGV)
+{
+ my $al = length($ARGV[$i]);
+ # find longest
+ $len = $al if $al > $len;
+}
+
+foreach my $i (0..$len-1)
+{
+ my @el;
+ foreach my $arg (@ARGV)
+ {
+ my $oneel = length($arg)>$i ? substr($arg,$i,1) : '?';
+ push @el, $oneel;
+ }
+ say join( " ", @el );
+}
diff --git a/challenge-040/duncan-c-white/perl5/ch-2.pl b/challenge-040/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..8fab6c1a7f
--- /dev/null
+++ b/challenge-040/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+#
+# Task 2: "Sort SubList
+#
+# You are given a list of numbers and set of indices belong to the
+# list. Write a script to sort the values belongs to the indices.
+#
+# For example:
+#
+# List: [ 10, 4, 1, 8, 12, 3 ]
+# Indices: 0,2,5
+#
+# We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3.
+#
+# Final List would look like below:
+# List: [ 1, 4, 3, 8, 12, 10 ]
+# "
+#
+# My notes: looks very easy. I guess the decision is: sort in place, or
+# extract items via an array slice, sort them, then update those items
+# in the original array (via another array slice?). The latter sounds
+# much the easier, so let's do that:-).
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+#use Data::Dumper;
+
+die "Usage: ch-2.pl ARRAYLIST INDEXLIST\n" unless @ARGV==2;
+my @array = split(/,/, $ARGV[0]);
+my @index = split(/,/, $ARGV[1]);
+
+my @subarray = @array[@index];
+#die Dumper \@subarray;
+
+@subarray = sort { $a <=> $b } @subarray;
+#die Dumper \@subarray;
+
+@array[@index] = @subarray;
+#die Dumper \@array;
+
+say "List: [ ", join(', ', @array), " ]";