aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-01-12 22:33:36 +0000
committerGitHub <noreply@github.com>2020-01-12 22:33:36 +0000
commit0d2d7ec9de7cbe30a8a472e3e7ca36609336646e (patch)
treee431661f4829a686df417b3432f3550f22d710f5
parentd5681f780892315697f09e3a41fe5f524c064bc6 (diff)
parent51c8af91ac902481ae3ef136189e490cd924b6de (diff)
downloadperlweeklychallenge-club-0d2d7ec9de7cbe30a8a472e3e7ca36609336646e.tar.gz
perlweeklychallenge-club-0d2d7ec9de7cbe30a8a472e3e7ca36609336646e.tar.bz2
perlweeklychallenge-club-0d2d7ec9de7cbe30a8a472e3e7ca36609336646e.zip
Merge pull request #1131 from dcw803/master
imported my solutions to this week's challenge
-rw-r--r--challenge-042/duncan-c-white/README41
-rwxr-xr-xchallenge-042/duncan-c-white/perl5/ch-1.pl55
-rwxr-xr-xchallenge-042/duncan-c-white/perl5/ch-2.pl69
3 files changed, 149 insertions, 16 deletions
diff --git a/challenge-042/duncan-c-white/README b/challenge-042/duncan-c-white/README
index 8fe9330cd4..2424e913c2 100644
--- a/challenge-042/duncan-c-white/README
+++ b/challenge-042/duncan-c-white/README
@@ -1,26 +1,35 @@
-Task 1: "Write a script to display attractive numbers between 1 and 50.
+Task 1: "Octal Number System
-A number is an attractive number if the number of its prime factors is
-also prime number.
+Write a script to print decimal number 0 to 50 in Octal Number System.
-For example: The number 20 is an attractive number, whose prime factors
-are 2, 2 and 5. The total prime factors is 3 which is also a prime number.
+For example:
+Decimal 0 = Octal 0
+Decimal 1 = Octal 1
+Decimal 2 = Octal 2
+Decimal 3 = Octal 3
+Decimal 4 = Octal 4
+Decimal 5 = Octal 5
+Decimal 6 = Octal 6
+Decimal 7 = Octal 7
+Decimal 8 = Octal 10
+
+and so on.
"
-My notes: Think I have a prime-factors finder from an earlier challenge;
-should be easy given that.
+My notes: Trivial.
+Task #2: "Balanced Brackets
-Task 2: "Write a script to display the first 20 Leonardo Numbers. Read
-https://en.wikipedia.org/wiki/Leonardo_number for more information.
+Write a script to generate a string with random number of ( and ) brackets. Then make the script validate the string if it has balanced brackets.
For example:
-L(0) = 1
-L(1) = 1
-L(2) = L(0) + L(1) + 1 = 3
-L(3) = L(1) + L(2) + 1 = 5
-
-and so on.
+() - OK
+(()) - OK
+)( - NOT OK
+())() - NOT OK
"
-My notes: sounds very nearly as simple as Fibonacci numbers.
+My notes: sounds quite easy. Generate is like coin tossing, validator could
+either count how many nested brackets we're in, or we could use regex search
+and replace to repeatedly delete () pairs of adjacent characters, valid if
+we end up with the empty string.
diff --git a/challenge-042/duncan-c-white/perl5/ch-1.pl b/challenge-042/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..2a5e568a95
--- /dev/null
+++ b/challenge-042/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+#
+# Task 1: "Octal Number System
+#
+# Write a script to print decimal number 0 to 50 in Octal Number System.
+#
+# For example:
+# Decimal 0 = Octal 0
+# Decimal 1 = Octal 1
+# Decimal 2 = Octal 2
+# Decimal 3 = Octal 3
+# Decimal 4 = Octal 4
+# Decimal 5 = Octal 5
+# Decimal 6 = Octal 6
+# Decimal 7 = Octal 7
+# Decimal 8 = Octal 10
+#
+# and so on.
+# "
+#
+# My notes: Trivial.
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+
+die "Usage: ch-1.pl [N//50]\n" if @ARGV>1;
+
+my $n = shift // 50;
+
+#
+# my $o = to_oct_str( $x );
+# Convert x to an octal string, eg. 8 is "10"
+#
+sub to_oct_str
+{
+ my( $x ) = @_;
+ my $result = "";
+ if( $x > 7 )
+ {
+ $result = to_oct_str( $x>>3 );
+ }
+ $result .= $x&7;
+ return $result;
+}
+
+
+foreach my $x (0..$n)
+{
+ # convert x to octal..
+ my $o = to_oct_str( $x );
+ say "$x\t$o";
+}
+
diff --git a/challenge-042/duncan-c-white/perl5/ch-2.pl b/challenge-042/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..18710a1533
--- /dev/null
+++ b/challenge-042/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+#
+# Task #2: "Balanced Brackets
+#
+# Write a script to generate a string with random number of ( and ) brackets. Then make the script validate the string if it has balanced brackets.
+#
+# For example:
+# () - OK
+# (()) - OK
+# )( - NOT OK
+# ())() - NOT OK
+# "
+#
+# My notes: sounds quite easy. Generate is like coin tossing, validator
+# could either count how many nested brackets we're in, or we could use
+# regex search and replace to repeatedly delete () pairs of adjacent
+# characters, valid if we end up with the empty string.
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+#use Data::Dumper;
+
+
+#
+# my $brackstr = gen_bracketed_string();
+# Generate a bracketed string which may or may not
+# be balanced.
+#
+sub gen_bracketed_string
+{
+ my $len = 20+int(rand(20));
+ my $result="";
+ foreach (1..$len)
+ {
+ $result .= int(rand(2))==0?'(':')';
+ }
+ return $result;
+}
+
+
+#
+# my $isvalid = validate( $brackstr );
+# Return 1 iff $brackstr is valid, i.e. contains matching
+# brackets. Else return 0.
+#
+sub validate
+{
+ my( $brackstr ) = @_;
+ my( $origlen, $len );
+ do
+ {
+ $origlen = length($brackstr);
+ $brackstr =~ s/\(\)//g;
+ $len = length($brackstr);
+ } while( $len < $origlen );
+ return $len==0?1:0;
+}
+
+
+srand( $$ ^ time() );
+
+die "Usage: ch-2.pl [BRACKSTR]\n" if @ARGV>1;
+my $brackstr = shift // gen_bracketed_string();
+
+my $isvalid = validate( $brackstr );
+
+say "$brackstr valid? $isvalid";