diff options
| -rw-r--r-- | challenge-110/duncan-c-white/README | 81 | ||||
| -rwxr-xr-x | challenge-110/duncan-c-white/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-110/duncan-c-white/perl/ch-2.pl | 77 | ||||
| -rw-r--r-- | challenge-110/duncan-c-white/perl/ch1input | 5 | ||||
| -rw-r--r-- | challenge-110/duncan-c-white/perl/ch2input | 5 |
5 files changed, 183 insertions, 47 deletions
diff --git a/challenge-110/duncan-c-white/README b/challenge-110/duncan-c-white/README index 18bce46ea5..c8cf5c8876 100644 --- a/challenge-110/duncan-c-white/README +++ b/challenge-110/duncan-c-white/README @@ -1,65 +1,52 @@ -Task 1: "Chowla Numbers +Task 1: "Valid Phone Numbers -Write a script to generate first 20 Chowla Numbers, named after, -Sarvadaman D. S. Chowla, a London born Indian American mathematician. It -is defined as: +You are given a text file. -C(n) = sum of divisors of n except 1 and n +Write a script to display all valid phone numbers in the given text file. +Acceptable Phone Number Formats -NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40]. ++nn nnnnnnnnnn +(nn) nnnnnnnnnn +nnnn nnnnnnnnnn -Output: +Input File -0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 -" +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 -My notes: easy and fun. +Output +0044 1148820341 + +44 1148820341 +(44) 1148820341 +" -Task 2: "Four Squares Puzzle +My notes: as the valid formats are fixed, just a load of regexes. +Would have been more interesting if the formats were inputs too. -You are given four squares as below with numbers named a,b,c,d,e,f,g. - (1) (3) - =============== =============== - | | | | - | a | | e | - | |(2) | | (4) - | --------------- -------------- - | | | | | | | | - | | b | | d | | f | | - | | | | | | | | - | | | | | | | | - ==========|==== ====|========== | - | c | | g | - | | | | - | | | | - --------------- -------------- +Task 2: "Transpose File -Write a script to place the given unique numbers in the square box so -that sum of numbers in each box is the same. +You are given a text file. -Example +Write a script to transpose the contents of the given file. +Input File -Input: 1,2,3,4,5,6,7 +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f Output: - a = 6 - b = 4 - c = 1 - d = 5 - e = 2 - f = 3 - g = 7 - - Box 1: a + b = 6 + 4 = 10 - Box 2: b + c + d = 4 + 1 + 5 = 10 - Box 3: d + e + f = 5 + 2 + 3 = 10 - Box 4: f + g = 3 + 7 = 10 +name,Mohammad,Joe,Julie,Cristina +age,45,20,35,10 +sex,m,m,f,f " -My notes: sounds simple enough. Find a,b,c,d,e,f st a+b = b+c+d = d+e+f = f+g -Of course, we'll need to try all permutations of the values given. There -are lots of CPAN modules (eg. Algorithm::Permute), but here I decided to -generate the permutations myself via Rosetta Stone code.. +My notes: simple to state. Basically read into 2-D array and transpose +array. diff --git a/challenge-110/duncan-c-white/perl/ch-1.pl b/challenge-110/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..fa00225d78 --- /dev/null +++ b/challenge-110/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# Task 1: "Valid Phone Numbers +# +# You are given a text file. +# +# Write a script to display all valid phone numbers in the given text file. +# Acceptable Phone Number Formats +# +# +nn nnnnnnnnnn +# (nn) nnnnnnnnnn +# nnnn nnnnnnnnnn +# +# Input File +# +# 0044 1148820341 +# +44 1148820341 +# 44-11-4882-0341 +# (44) 1148820341 +# 00 1148820341 +# +# Output +# +# 0044 1148820341 +# +44 1148820341 +# (44) 1148820341 +# " +# +# My notes: as the valid formats are fixed, just a load of regexes. +# Would have been more interesting if the formats were inputs too. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +#use Data::Dumper; + + +# +# my $isvalid = validno($line); +# Return 1 iff $line contains a valid phone no. +# 0 otherwise. +# +fun validno( $line ) +{ + $line =~ s/^\s+//; + $line =~ s/\s+$//; + return 1 if $line =~ /^\+\d\d\s+\d{10}$/; + return 1 if $line =~ /^\(\d\d\)\s+\d{10}$/; + return 1 if $line =~ /^\d{4}\s+\d{10}$/; + return 0; +} + + +die "Usage: phone-numbers\n" unless @ARGV==0; + + +while( <> ) +{ + print if validno($_); +} diff --git a/challenge-110/duncan-c-white/perl/ch-2.pl b/challenge-110/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..51a146b8fc --- /dev/null +++ b/challenge-110/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# +# Task 2: "Transpose File +# +# You are given a text file. +# +# Write a script to transpose the contents of the given file. +# +# Input File +# +# name,age,sex +# Mohammad,45,m +# Joe,20,m +# Julie,35,f +# Cristina,10,f +# +# Output: +# +# name,Mohammad,Joe,Julie,Cristina +# age,45,20,35,10 +# sex,m,m,f,f +# " +# +# My notes: simple to state. Basically read into 2-D array and transpose +# array. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Text::CSV; +#use Data::Dumper; + + +# +# my @t = transpose(@a); +# Transpose @a into @t. +# +fun transpose( @a ) +{ + my $cols = @{$a[0]}; + my @result; + foreach my $r (0..$#a) + { + foreach my $c (0..$cols-1) + { + $result[$c][$r] = $a[$r][$c]; + } + } + return @result; +} + + +die "Usage: tranposecsv\n" unless @ARGV==0; + +my @a; # 2-d array + +my $csv = Text::CSV->new ( { binary => 1 } ) + || die "Cannot use CSV: ".Text::CSV->error_diag (); + +while( <> ) +{ + my $status = $csv->parse($_); + die "bad line $_\n" unless $status; + my @f = $csv->fields(); + push @a, \@f; +} + +#die Dumper \@a; + +my @t = transpose(@a); +foreach my $row (@t) +{ + $csv->print( \*STDOUT, $row); + print "\n"; +} diff --git a/challenge-110/duncan-c-white/perl/ch1input b/challenge-110/duncan-c-white/perl/ch1input new file mode 100644 index 0000000000..48d6254741 --- /dev/null +++ b/challenge-110/duncan-c-white/perl/ch1input @@ -0,0 +1,5 @@ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 diff --git a/challenge-110/duncan-c-white/perl/ch2input b/challenge-110/duncan-c-white/perl/ch2input new file mode 100644 index 0000000000..716ebdce75 --- /dev/null +++ b/challenge-110/duncan-c-white/perl/ch2input @@ -0,0 +1,5 @@ +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f |
