From 2a0208db37d4421ce17ab44da2504436faca42e3 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 15:32:20 +0200 Subject: solutions for challenge-060 --- challenge-060/jo-37/perl/ch-1.pl | 38 +++++++++++++++++ challenge-060/jo-37/perl/ch-2.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 challenge-060/jo-37/perl/ch-1.pl create mode 100755 challenge-060/jo-37/perl/ch-2.pl diff --git a/challenge-060/jo-37/perl/ch-1.pl b/challenge-060/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..f75023447c --- /dev/null +++ b/challenge-060/jo-37/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Input: column number (>=1), output: excel column name +# Input: excel column name ([A-Z]+), output: column number + +use strict; +use warnings; +use bigint; + +sub dectoxls { + my $dec = $_[0] - 1; + my @b26; + while ($dec >= 0) { + unshift @b26, chr(ord('A') + $dec % 26); + $dec = $dec / 26 - 1; + } + return join '', @b26; +} + +sub xlstodec { + my @b26 = split '', $_[0]; + my $dec = 0; + for my $b26 (@b26) { + $dec *= 26; + $dec += ord($b26) - ord('A') + 1; + } + return $dec; +} + +for ($ARGV[0]) { + if (/^\d+$/) { + print dectoxls($_), "\n"; + } elsif (/^[A-Z]+$/) { + print xlstodec($_), "\n"; + } else { + print "input invalid: $_\n"; + } +} diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..9ef127aae8 --- /dev/null +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# generate numbers of length $X that are smaller than $Y +# from the parts in @L. +# +# recursive "branch and cut" + +use Test2::V0; + +# expects a number of the desired length and a control hash. +# if the number fits, it is added to the result set. +# cut this branch otherwise +sub check_num { + my $num = shift; + my $ctl = shift; + if ($num lt $ctl->{limit}) { + return $ctl->{result}{$num} = 1 + }; + 0; +} + +# recursively constructs numbers from the given parts. +# expects the current recursion level and the config hash as parameters +sub gennum; +sub gennum { + my $level = shift; + my $ctl = shift; + + our @current; + # make the array element for the current level local to this + # invocation, thus auto-deleting it at return + local $current[$level]; + + # loop over parts at this level + foreach my $num (@{$ctl->{parts}}) { + # skip leading zero + next if $num == 0 && $level == 0; + + $current[$level] = "$num"; + my $stop; + + # construct current value from selected parts + my $value = join '', @current; + my $t = length($value) <=> $ctl->{length}; + if ($t < 0) { # $value is too short + if ($value gt $ctl->{limit}) { + # cut if current prefix is already too large + return; + } else { + # recurse to next level + gennum $level + 1, $ctl; + } + } elsif ($t == 0) { # $value has desired length + # cut if $value is too large + return unless check_num $value, $ctl; + } else { # $value is too long + return; + } + } +} + +# create numbers of given length, below given limit +# and assembled from given parts +# parts are not restricted to single digits +sub create_numbers { + my $length = shift; + my $limit = shift; + + # sort parts lexicographically (!) + my $parts = [sort @_]; + + my $ctl = {length => $length, limit => $limit, parts => $parts}; + + # enter generator + gennum 0, $ctl; + return sort keys %{$ctl->{result}}; +} + +my @L = (0, 1, 2, 5); +my $X = 2; +my $Y = 21; +my @result = create_numbers $X, $Y, @L; +is \@result, [10, 11, 12, 15, 20], 'example from challenge'; + +@L = (0, 1, 100000002); +$X = 9; +$Y = 100000003; +@result = create_numbers $X, $Y, @L; +is \@result, [100000000, 100000001, 100000002], 'cut search space example'; + +done_testing; -- cgit From 6b8db2a9cb876d9de843fdbcd728e5e0876faea9 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 15:32:20 +0200 Subject: solutions for challenge-060 --- challenge-060/jo-37/perl/ch-1.pl | 38 +++++++++++++++++ challenge-060/jo-37/perl/ch-2.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 challenge-060/jo-37/perl/ch-1.pl create mode 100755 challenge-060/jo-37/perl/ch-2.pl diff --git a/challenge-060/jo-37/perl/ch-1.pl b/challenge-060/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..f75023447c --- /dev/null +++ b/challenge-060/jo-37/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Input: column number (>=1), output: excel column name +# Input: excel column name ([A-Z]+), output: column number + +use strict; +use warnings; +use bigint; + +sub dectoxls { + my $dec = $_[0] - 1; + my @b26; + while ($dec >= 0) { + unshift @b26, chr(ord('A') + $dec % 26); + $dec = $dec / 26 - 1; + } + return join '', @b26; +} + +sub xlstodec { + my @b26 = split '', $_[0]; + my $dec = 0; + for my $b26 (@b26) { + $dec *= 26; + $dec += ord($b26) - ord('A') + 1; + } + return $dec; +} + +for ($ARGV[0]) { + if (/^\d+$/) { + print dectoxls($_), "\n"; + } elsif (/^[A-Z]+$/) { + print xlstodec($_), "\n"; + } else { + print "input invalid: $_\n"; + } +} diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..9ef127aae8 --- /dev/null +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# generate numbers of length $X that are smaller than $Y +# from the parts in @L. +# +# recursive "branch and cut" + +use Test2::V0; + +# expects a number of the desired length and a control hash. +# if the number fits, it is added to the result set. +# cut this branch otherwise +sub check_num { + my $num = shift; + my $ctl = shift; + if ($num lt $ctl->{limit}) { + return $ctl->{result}{$num} = 1 + }; + 0; +} + +# recursively constructs numbers from the given parts. +# expects the current recursion level and the config hash as parameters +sub gennum; +sub gennum { + my $level = shift; + my $ctl = shift; + + our @current; + # make the array element for the current level local to this + # invocation, thus auto-deleting it at return + local $current[$level]; + + # loop over parts at this level + foreach my $num (@{$ctl->{parts}}) { + # skip leading zero + next if $num == 0 && $level == 0; + + $current[$level] = "$num"; + my $stop; + + # construct current value from selected parts + my $value = join '', @current; + my $t = length($value) <=> $ctl->{length}; + if ($t < 0) { # $value is too short + if ($value gt $ctl->{limit}) { + # cut if current prefix is already too large + return; + } else { + # recurse to next level + gennum $level + 1, $ctl; + } + } elsif ($t == 0) { # $value has desired length + # cut if $value is too large + return unless check_num $value, $ctl; + } else { # $value is too long + return; + } + } +} + +# create numbers of given length, below given limit +# and assembled from given parts +# parts are not restricted to single digits +sub create_numbers { + my $length = shift; + my $limit = shift; + + # sort parts lexicographically (!) + my $parts = [sort @_]; + + my $ctl = {length => $length, limit => $limit, parts => $parts}; + + # enter generator + gennum 0, $ctl; + return sort keys %{$ctl->{result}}; +} + +my @L = (0, 1, 2, 5); +my $X = 2; +my $Y = 21; +my @result = create_numbers $X, $Y, @L; +is \@result, [10, 11, 12, 15, 20], 'example from challenge'; + +@L = (0, 1, 100000002); +$X = 9; +$Y = 100000003; +@result = create_numbers $X, $Y, @L; +is \@result, [100000000, 100000001, 100000002], 'cut search space example'; + +done_testing; -- cgit From b6047da8754e80b0877e3ea2ae6f071a4e357648 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 19:54:56 +0200 Subject: enhance comments --- challenge-060/jo-37/perl/ch-2.pl | 50 +++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index 9ef127aae8..e9da2dcdbf 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -7,7 +7,9 @@ use Test2::V0; -# expects a number of the desired length and a control hash. +# check_num expects: +# - number that has the desired length +# - control hash. # if the number fits, it is added to the result set. # cut this branch otherwise sub check_num { @@ -19,16 +21,18 @@ sub check_num { 0; } -# recursively constructs numbers from the given parts. -# expects the current recursion level and the config hash as parameters -sub gennum; -sub gennum { +# Recursively constructs numbers from the given parts. +# gen_num expects: +# - current recursion level +# - config hash +sub gen_num; +sub gen_num { my $level = shift; my $ctl = shift; our @current; - # make the array element for the current level local to this - # invocation, thus auto-deleting it at return + # localize the array element for the current level, + # will be auto-deleted at return local $current[$level]; # loop over parts at this level @@ -48,20 +52,21 @@ sub gennum { return; } else { # recurse to next level - gennum $level + 1, $ctl; + gen_num $level + 1, $ctl; } } elsif ($t == 0) { # $value has desired length # cut if $value is too large + # next cannot lead to something smaller, + # even if it is shorter return unless check_num $value, $ctl; - } else { # $value is too long - return; } + # else: $value is too long, next might be shorter } } -# create numbers of given length, below given limit -# and assembled from given parts -# parts are not restricted to single digits +# Create numbers of given length, below given limit +# and assembled from given parts. +# Parts are not restricted to single digits. sub create_numbers { my $length = shift; my $limit = shift; @@ -72,20 +77,23 @@ sub create_numbers { my $ctl = {length => $length, limit => $limit, parts => $parts}; # enter generator - gennum 0, $ctl; + gen_num 0, $ctl; return sort keys %{$ctl->{result}}; } -my @L = (0, 1, 2, 5); -my $X = 2; -my $Y = 21; -my @result = create_numbers $X, $Y, @L; -is \@result, [10, 11, 12, 15, 20], 'example from challenge'; +# main +my (@L, $X, $Y, @R); + +@L = (0, 1, 2, 5); +$X = 2; +$Y = 21; +@R = create_numbers $X, $Y, @L; +is \@R, [10, 11, 12, 15, 20], 'example from challenge'; @L = (0, 1, 100000002); $X = 9; $Y = 100000003; -@result = create_numbers $X, $Y, @L; -is \@result, [100000000, 100000001, 100000002], 'cut search space example'; +@R = create_numbers $X, $Y, @L; +is \@R, [100000000, 100000001, 100000002], 'cut example'; done_testing; -- cgit From 3192b529872e97805f1eb162b35ee92ad8c4432f Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 15:32:20 +0200 Subject: solutions for challenge-060 --- challenge-060/jo-37/perl/ch-1.pl | 38 +++++++++++++++++ challenge-060/jo-37/perl/ch-2.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 challenge-060/jo-37/perl/ch-1.pl create mode 100755 challenge-060/jo-37/perl/ch-2.pl diff --git a/challenge-060/jo-37/perl/ch-1.pl b/challenge-060/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..f75023447c --- /dev/null +++ b/challenge-060/jo-37/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# Input: column number (>=1), output: excel column name +# Input: excel column name ([A-Z]+), output: column number + +use strict; +use warnings; +use bigint; + +sub dectoxls { + my $dec = $_[0] - 1; + my @b26; + while ($dec >= 0) { + unshift @b26, chr(ord('A') + $dec % 26); + $dec = $dec / 26 - 1; + } + return join '', @b26; +} + +sub xlstodec { + my @b26 = split '', $_[0]; + my $dec = 0; + for my $b26 (@b26) { + $dec *= 26; + $dec += ord($b26) - ord('A') + 1; + } + return $dec; +} + +for ($ARGV[0]) { + if (/^\d+$/) { + print dectoxls($_), "\n"; + } elsif (/^[A-Z]+$/) { + print xlstodec($_), "\n"; + } else { + print "input invalid: $_\n"; + } +} diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..9ef127aae8 --- /dev/null +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# generate numbers of length $X that are smaller than $Y +# from the parts in @L. +# +# recursive "branch and cut" + +use Test2::V0; + +# expects a number of the desired length and a control hash. +# if the number fits, it is added to the result set. +# cut this branch otherwise +sub check_num { + my $num = shift; + my $ctl = shift; + if ($num lt $ctl->{limit}) { + return $ctl->{result}{$num} = 1 + }; + 0; +} + +# recursively constructs numbers from the given parts. +# expects the current recursion level and the config hash as parameters +sub gennum; +sub gennum { + my $level = shift; + my $ctl = shift; + + our @current; + # make the array element for the current level local to this + # invocation, thus auto-deleting it at return + local $current[$level]; + + # loop over parts at this level + foreach my $num (@{$ctl->{parts}}) { + # skip leading zero + next if $num == 0 && $level == 0; + + $current[$level] = "$num"; + my $stop; + + # construct current value from selected parts + my $value = join '', @current; + my $t = length($value) <=> $ctl->{length}; + if ($t < 0) { # $value is too short + if ($value gt $ctl->{limit}) { + # cut if current prefix is already too large + return; + } else { + # recurse to next level + gennum $level + 1, $ctl; + } + } elsif ($t == 0) { # $value has desired length + # cut if $value is too large + return unless check_num $value, $ctl; + } else { # $value is too long + return; + } + } +} + +# create numbers of given length, below given limit +# and assembled from given parts +# parts are not restricted to single digits +sub create_numbers { + my $length = shift; + my $limit = shift; + + # sort parts lexicographically (!) + my $parts = [sort @_]; + + my $ctl = {length => $length, limit => $limit, parts => $parts}; + + # enter generator + gennum 0, $ctl; + return sort keys %{$ctl->{result}}; +} + +my @L = (0, 1, 2, 5); +my $X = 2; +my $Y = 21; +my @result = create_numbers $X, $Y, @L; +is \@result, [10, 11, 12, 15, 20], 'example from challenge'; + +@L = (0, 1, 100000002); +$X = 9; +$Y = 100000003; +@result = create_numbers $X, $Y, @L; +is \@result, [100000000, 100000001, 100000002], 'cut search space example'; + +done_testing; -- cgit From ee21650706c6991ede923bb9254a13c7b03ac389 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 19:54:56 +0200 Subject: enhance comments --- challenge-060/jo-37/perl/ch-2.pl | 50 +++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index 9ef127aae8..e9da2dcdbf 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -7,7 +7,9 @@ use Test2::V0; -# expects a number of the desired length and a control hash. +# check_num expects: +# - number that has the desired length +# - control hash. # if the number fits, it is added to the result set. # cut this branch otherwise sub check_num { @@ -19,16 +21,18 @@ sub check_num { 0; } -# recursively constructs numbers from the given parts. -# expects the current recursion level and the config hash as parameters -sub gennum; -sub gennum { +# Recursively constructs numbers from the given parts. +# gen_num expects: +# - current recursion level +# - config hash +sub gen_num; +sub gen_num { my $level = shift; my $ctl = shift; our @current; - # make the array element for the current level local to this - # invocation, thus auto-deleting it at return + # localize the array element for the current level, + # will be auto-deleted at return local $current[$level]; # loop over parts at this level @@ -48,20 +52,21 @@ sub gennum { return; } else { # recurse to next level - gennum $level + 1, $ctl; + gen_num $level + 1, $ctl; } } elsif ($t == 0) { # $value has desired length # cut if $value is too large + # next cannot lead to something smaller, + # even if it is shorter return unless check_num $value, $ctl; - } else { # $value is too long - return; } + # else: $value is too long, next might be shorter } } -# create numbers of given length, below given limit -# and assembled from given parts -# parts are not restricted to single digits +# Create numbers of given length, below given limit +# and assembled from given parts. +# Parts are not restricted to single digits. sub create_numbers { my $length = shift; my $limit = shift; @@ -72,20 +77,23 @@ sub create_numbers { my $ctl = {length => $length, limit => $limit, parts => $parts}; # enter generator - gennum 0, $ctl; + gen_num 0, $ctl; return sort keys %{$ctl->{result}}; } -my @L = (0, 1, 2, 5); -my $X = 2; -my $Y = 21; -my @result = create_numbers $X, $Y, @L; -is \@result, [10, 11, 12, 15, 20], 'example from challenge'; +# main +my (@L, $X, $Y, @R); + +@L = (0, 1, 2, 5); +$X = 2; +$Y = 21; +@R = create_numbers $X, $Y, @L; +is \@R, [10, 11, 12, 15, 20], 'example from challenge'; @L = (0, 1, 100000002); $X = 9; $Y = 100000003; -@result = create_numbers $X, $Y, @L; -is \@result, [100000000, 100000001, 100000002], 'cut search space example'; +@R = create_numbers $X, $Y, @L; +is \@R, [100000000, 100000001, 100000002], 'cut example'; done_testing; -- cgit From 86a58bee718a42c5bd77ca5f2e3c9b39792bc1ac Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 May 2020 22:26:57 +0200 Subject: fix comments --- challenge-060/jo-37/perl/ch-2.pl | 43 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index e9da2dcdbf..128128876a 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -1,21 +1,26 @@ #!/usr/bin/perl -# generate numbers of length $X that are smaller than $Y -# from the parts in @L. +# sub create_numbers generates numbers +# - of length $X +# - that are smaller than $Y +# - from the parts in @L. +# See below. # -# recursive "branch and cut" +# Performs recursive "branch and cut". +# Though data is numeric, processing is string based. use Test2::V0; +use Data::Dumper; # check_num expects: -# - number that has the desired length +# - number to be checked # - control hash. -# if the number fits, it is added to the result set. -# cut this branch otherwise +# If the number fits, it is added to the result set. +# Cut this branch otherwise. sub check_num { my $num = shift; my $ctl = shift; - if ($num lt $ctl->{limit}) { + if ($num < $ctl->{limit}) { return $ctl->{result}{$num} = 1 }; 0; @@ -45,12 +50,14 @@ sub gen_num { # construct current value from selected parts my $value = join '', @current; + + # test length of current value my $t = length($value) <=> $ctl->{length}; if ($t < 0) { # $value is too short - if ($value gt $ctl->{limit}) { - # cut if current prefix is already too large + if ($value ge $ctl->{limit}) { + # cut if $value is too large return; - } else { + } else { # $value is not too large but too short # recurse to next level gen_num $level + 1, $ctl; } @@ -82,18 +89,24 @@ sub create_numbers { } # main -my (@L, $X, $Y, @R); +my (@L, $X, $Y); @L = (0, 1, 2, 5); $X = 2; $Y = 21; -@R = create_numbers $X, $Y, @L; -is \@R, [10, 11, 12, 15, 20], 'example from challenge'; +is [create_numbers $X, $Y, @L], [10, 11, 12, 15, 20], + 'example from challenge'; @L = (0, 1, 100000002); $X = 9; $Y = 100000003; -@R = create_numbers $X, $Y, @L; -is \@R, [100000000, 100000001, 100000002], 'cut example'; +is [create_numbers $X, $Y, @L], [100000000, 100000001, 100000002], + 'cut example'; done_testing; + +# another example +@L = (0, 7, 65, 543, 4321); +$X = 5; +$Y = 70001; +print Dumper [create_numbers $X, $Y, @L]; -- cgit From 4393ea61d3e1d29924cbd193b902bca0082898b6 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 18:02:17 +0200 Subject: rename --- challenge-060/jo-37/perl/ch-1.pl | 56 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-1.pl b/challenge-060/jo-37/perl/ch-1.pl index f75023447c..e26be8cafa 100755 --- a/challenge-060/jo-37/perl/ch-1.pl +++ b/challenge-060/jo-37/perl/ch-1.pl @@ -1,37 +1,53 @@ #!/usr/bin/perl -# Input: column number (>=1), output: excel column name -# Input: excel column name ([A-Z]+), output: column number +# Input: column numbers (>=1) or column labels ([A-Z]+), +# output: column labels or column numbers +# Without arguments, converts some example values + +# The numbering schema differs from the usual base(k) in the absence +# of a digit "zero". I.e. there are digits for 1 .. k instead of +# 0 .. (k -1). This results in the off-by-one modifications +# from the known formulae use strict; use warnings; use bigint; +use constant BASE => 26; -sub dectoxls { - my $dec = $_[0] - 1; - my @b26; - while ($dec >= 0) { - unshift @b26, chr(ord('A') + $dec % 26); - $dec = $dec / 26 - 1; +sub int2label { + my $int = $_[0] - 1; + my @label; + while ($int >= 0) { + unshift @label, chr(ord('A') + $int % BASE); + $int = $int / BASE - 1; } - return join '', @b26; + return join '', @label; } -sub xlstodec { - my @b26 = split '', $_[0]; - my $dec = 0; - for my $b26 (@b26) { - $dec *= 26; - $dec += ord($b26) - ord('A') + 1; +sub label2int { + my @label = split '', $_[0]; + my $int = 0; + for my $label (@label) { + $int *= BASE; + $int += ord($label) - ord('A') + 1; } - return $dec; + return $int; +} + +# last digit for BASE +my $last = chr(ord('A') + BASE - 1); + +# build example input data if none provided +unless (@ARGV) { + @ARGV = + map {((BASE ** ($_ + 1) - 1)/(BASE - 1) - 1, $last x $_)} (1 .. 14); } -for ($ARGV[0]) { +for (@ARGV) { if (/^\d+$/) { - print dectoxls($_), "\n"; - } elsif (/^[A-Z]+$/) { - print xlstodec($_), "\n"; + print "$_ -> ", int2label($_), "\n"; + } elsif (/^[A-$last]+$/) { + print "$_ -> ", label2int($_), "\n"; } else { print "input invalid: $_\n"; } -- cgit From 6de4ab4be336474a1c70e81237d03ca52df76658 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 18:02:53 +0200 Subject: use string compare --- challenge-060/jo-37/perl/ch-2.pl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index 128128876a..86172c303f 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -7,7 +7,7 @@ # See below. # # Performs recursive "branch and cut". -# Though data is numeric, processing is string based. +# Though data is numeric, processing is strictly string based. use Test2::V0; use Data::Dumper; @@ -20,7 +20,7 @@ use Data::Dumper; sub check_num { my $num = shift; my $ctl = shift; - if ($num < $ctl->{limit}) { + if ($num lt $ctl->{limit}) { return $ctl->{result}{$num} = 1 }; 0; @@ -43,7 +43,7 @@ sub gen_num { # loop over parts at this level foreach my $num (@{$ctl->{parts}}) { # skip leading zero - next if $num == 0 && $level == 0; + next if $num eq 0 && $level == 0; $current[$level] = "$num"; my $stop; @@ -110,3 +110,9 @@ done_testing; $X = 5; $Y = 70001; print Dumper [create_numbers $X, $Y, @L]; + +# non-numeric +@L = qw(a b c); +$X = 3; +$Y = 'abc'; +print Dumper [create_numbers $X, $Y, @L]; -- cgit From 7dcb721b9ed7e1cecb710c9d878999314a47cf29 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 18:49:38 +0200 Subject: refine comments --- challenge-060/jo-37/perl/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index 86172c303f..a2c05683b8 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -6,8 +6,8 @@ # - from the parts in @L. # See below. # -# Performs recursive "branch and cut". -# Though data is numeric, processing is strictly string based. +# Though data has been described as numeric, processing is +# strictly string based. (Example provided.) use Test2::V0; use Data::Dumper; @@ -73,7 +73,7 @@ sub gen_num { # Create numbers of given length, below given limit # and assembled from given parts. -# Parts are not restricted to single digits. +# Parts are not restricted to single digits (nor to numeric data). sub create_numbers { my $length = shift; my $limit = shift; -- cgit From 2111506d9c0b3116bbf40d18739af462c2382a7c Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 20:52:30 +0200 Subject: change examples to tests --- challenge-060/jo-37/perl/ch-2.pl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index a2c05683b8..632c9f9454 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -10,7 +10,6 @@ # strictly string based. (Example provided.) use Test2::V0; -use Data::Dumper; # check_num expects: # - number to be checked @@ -103,16 +102,17 @@ $Y = 100000003; is [create_numbers $X, $Y, @L], [100000000, 100000001, 100000002], 'cut example'; -done_testing; - -# another example -@L = (0, 7, 65, 543, 4321); -$X = 5; -$Y = 70001; -print Dumper [create_numbers $X, $Y, @L]; - -# non-numeric @L = qw(a b c); $X = 3; $Y = 'abc'; -print Dumper [create_numbers $X, $Y, @L]; +is [create_numbers $X, $Y, @L], [qw(aaa aab aac aba abb)], + 'non numeric'; + +@L = (0, 5, 43, 321); +$X = 4; +$Y = 5001; +is [create_numbers $X, $Y, @L], + [3210, 3215, 4300, 4305, 4343, 4350, 4355, 5000], + 'another example'; + +done_testing; -- cgit From 8b909a6f2c4e0a324d322d10f7f531ef490982a7 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 21:00:28 +0200 Subject: rename example --- challenge-060/jo-37/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-060/jo-37/perl/ch-2.pl b/challenge-060/jo-37/perl/ch-2.pl index 632c9f9454..4b0afd53bc 100755 --- a/challenge-060/jo-37/perl/ch-2.pl +++ b/challenge-060/jo-37/perl/ch-2.pl @@ -100,7 +100,7 @@ is [create_numbers $X, $Y, @L], [10, 11, 12, 15, 20], $X = 9; $Y = 100000003; is [create_numbers $X, $Y, @L], [100000000, 100000001, 100000002], - 'cut example'; + 'avoid too much scanning'; @L = qw(a b c); $X = 3; -- cgit From 08a22d39b3cf40f785ead0b29f30f2528473894f Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 13 May 2020 21:04:27 +0200 Subject: reformat code --- challenge-060/jo-37/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-060/jo-37/perl/ch-1.pl b/challenge-060/jo-37/perl/ch-1.pl index e26be8cafa..48c0f9d032 100755 --- a/challenge-060/jo-37/perl/ch-1.pl +++ b/challenge-060/jo-37/perl/ch-1.pl @@ -40,7 +40,7 @@ my $last = chr(ord('A') + BASE - 1); # build example input data if none provided unless (@ARGV) { @ARGV = - map {((BASE ** ($_ + 1) - 1)/(BASE - 1) - 1, $last x $_)} (1 .. 14); + map {((BASE**($_+1) - 1)/(BASE - 1) - 1, $last x $_)} (1 .. 14); } for (@ARGV) { -- cgit