aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-17 22:46:59 +0100
committerGitHub <noreply@github.com>2020-05-17 22:46:59 +0100
commitfe0f2e25a10e20f8e10e791dffdb2abf3ff476d3 (patch)
tree144149f9407d5242c8080fba5107263f26dd5678
parent3230d67ec2127441fe85836751dcb2019cbd8323 (diff)
parent17cac125bbe38a377f283320deb62cbeb4c39514 (diff)
downloadperlweeklychallenge-club-fe0f2e25a10e20f8e10e791dffdb2abf3ff476d3.tar.gz
perlweeklychallenge-club-fe0f2e25a10e20f8e10e791dffdb2abf3ff476d3.tar.bz2
perlweeklychallenge-club-fe0f2e25a10e20f8e10e791dffdb2abf3ff476d3.zip
Merge pull request #1729 from dcw803/master
two nice simple questions this week, easy enough to do
-rw-r--r--challenge-060/duncan-c-white/README60
-rwxr-xr-xchallenge-060/duncan-c-white/perl/ch-1.pl43
-rwxr-xr-xchallenge-060/duncan-c-white/perl/ch-2.pl82
3 files changed, 153 insertions, 32 deletions
diff --git a/challenge-060/duncan-c-white/README b/challenge-060/duncan-c-white/README
index 99887d193e..254b444d7a 100644
--- a/challenge-060/duncan-c-white/README
+++ b/challenge-060/duncan-c-white/README
@@ -1,49 +1,45 @@
-Task 1: "Linked List
+Task 1: "Excel Column
-You are given a linked list and a value k. Write a script to partition
-the linked list such that all nodes less than k come before nodes greater
-than or equal to k. Make sure you preserve the original relative order
-of the nodes in each of the two partitions.
+Write a script that accepts a number and returns the Excel Column Name
+it represents and vice-versa.
-For example:
+Excel columns start at A and increase lexicographically using the 26
+letters of the English alphabet, A..Z. After Z, the columns pick up an
+extra letter, going from AA, AB, etc., which could (in theory)
+continue to an arbitrary number of digits. In practice, Excel sheets
+are limited to 16,384 columns.
-Linked List: 1 -> 4 -> 3 -> 2 -> 5 -> 2
+Example
-k = 3
-
-Expected Output: 1 -> 2 -> 2 -> 4 -> 3 -> 5.
+Input Number: 28
+Output: AB
+Input Column Name: AD
+Output: 30
"
-My notes: why a linked list not an array, it would be so simple with an
-array. Ok, ok, a linked list: presumably want to reuse the existing nodes.
-Build two lists (reusing the existing nodes), one "before", the other "after".
-Delink each node, append it to whichever list is appropriate. Repeat.
-
-
-Task 2: "Bit Sum
+My notes: very straightforward.
-For this task, you will most likely need a function f(a,b) which returns
-the count of different bits of binary representation of a and b.
-For example, f(1,3) = 1, since:
+Task 2: "Find Numbers
-Binary representation of 1 = 01
+Write a script that accepts list of positive numbers (@L) and two positive
+numbers $X and $Y.
-Binary representation of 3 = 11
+The script should print all possible numbers made by concatenating the
+numbers from @L, whose length is exactly $X but value is less than $Y.
+Example
-There is only 1 different bit. Therefore the subroutine should return
-1. Note that if one number is longer than the other in binary, the most
-significant bits of the smaller number are padded (i.e., they are assumed
-to be zeroes).
+Input:
-Script Output
+@L = (0, 1, 2, 5);
+$X = 2;
+$Y = 21;
-Your script should accept n positive numbers. Your script should sum
-the result of f(a,b) for every pair of numbers given:
+Output:
-For example, given 2, 3, 4, the output would be 6, since f(2,3) + f(2,4)
-+ f(3,4) = 1 + 2 + 3 = 6
+10, 11, 12, 15, 20
"
-My notes: sounds very straightforward.
+My notes: sounds quite straightforward. I note that "01" is not a solution,
+so leading zeroes aren't allowed.
diff --git a/challenge-060/duncan-c-white/perl/ch-1.pl b/challenge-060/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..872e17d850
--- /dev/null
+++ b/challenge-060/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+#
+# Task 1: "Excel Column
+#
+# Write a script that accepts a number and returns the Excel Column Name
+# it represents and vice-versa.
+#
+# Excel columns start at A and increase lexicographically using the 26
+# letters of the English alphabet, A..Z. After Z, the columns pick up an
+# extra letter, going from AA, AB, etc., which could (in theory)
+# continue to an arbitrary number of digits. In practice, Excel sheets
+# are limited to 16,384 columns.
+#
+# Example
+#
+# Input Number: 28
+# Output: AB
+#
+# Input Column Name: AD
+# Output: 30
+# "
+#
+# My notes: very straightforward.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+
+
+die "Usage: excel-column N\n" unless @ARGV==1;
+my $n = shift;
+die "excel-column: N should be 1..16384\n" unless $n>0 && $n<=16384;
+
+my $str = "";
+while( $n > 26 )
+{
+ my $digit = 1 + ($n-1) % 26;
+ $str .= chr(ord('@')+$digit);
+ $n = int( ($n-1) / 26 );
+}
+$str .= chr(ord('@')+$n);
+say scalar reverse $str;
diff --git a/challenge-060/duncan-c-white/perl/ch-2.pl b/challenge-060/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..158d923f63
--- /dev/null
+++ b/challenge-060/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+#
+# Task 2: "Find Numbers
+#
+# Write a script that accepts list of positive numbers (@L) and two positive
+# numbers $X and $Y.
+#
+# The script should print all possible numbers made by concatenating the
+# numbers from @L, whose length is exactly $X but value is less than $Y.
+# Example
+#
+# Input:
+#
+# @L = (0, 1, 2, 5);
+# $X = 2;
+# $Y = 21;
+#
+# Output:
+#
+# 10, 11, 12, 15, 20
+# "
+#
+# My notes: sounds quite straightforward. I note that "01" is not a solution,
+# so leading zeroes aren't allowed.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Function::Parameters;
+#use Data::Dumper;
+
+#
+# find_recursive( $prefix, $x, $y, $lref, $resultref );
+# Find all possible numbers made by concatenating the numbers from @$lref,
+# whose length is exactly $X but value is less than $Y, given that
+# the prefix (numeric string) is $prefix. Add answers to @$resultref.
+#
+fun find_recursive( $prefix, $x, $y, $lref, $resultref )
+{
+ foreach my $el (@$lref)
+ {
+ my $newp = $prefix.$el;
+ my $newlen = length($newp);
+ next if $newp >= $y || $newlen > $x;
+
+ push @$resultref, $newp if $newp < $y && $newlen==$x;
+
+ # try with that element appended to the prefix
+ find_recursive( $newp, $x, $y, $lref, $resultref );
+ }
+}
+
+
+
+#
+# my @result = findall( $x, $y, @l );
+# Find all possible numbers made by concatenating the numbers from @l,
+# whose length is exactly $X but value is less than $Y.
+#
+fun findall( $x, $y, @l )
+{
+ my @result;
+ foreach my $el (@l)
+ {
+ next if $el =~ /^0/;
+ next if $el >= $y || length($el) > $x;
+ push @result, $el if $el < $y && length($el)==$x;
+
+ # try that element as the prefix of the number..
+ find_recursive( $el, $x, $y, \@l, \@result );
+ }
+ return @result;
+}
+
+
+
+die "Usage: find-numbers X Y Numlist\n" if @ARGV < 3;
+my( $x, $y, @l ) = @ARGV;
+
+my @result = findall( $x, $y, @l );
+say join(', ',@result);