From c89851b1e5ca751a8aff5324a2bea42344c10a48 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 7 Apr 2019 21:07:22 +0100 Subject: - Renamed solution file to .p6 to be consistent. --- challenge-002/lars-balker/perl6/ch-1.p6 | 37 ++++++++++++++++++++++++++++++++ challenge-002/lars-balker/perl6/ch-1.pl6 | 37 -------------------------------- challenge-002/lars-balker/perl6/ch-2.p6 | 33 ++++++++++++++++++++++++++++ challenge-002/lars-balker/perl6/ch-2.pl6 | 33 ---------------------------- 4 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 challenge-002/lars-balker/perl6/ch-1.p6 delete mode 100644 challenge-002/lars-balker/perl6/ch-1.pl6 create mode 100644 challenge-002/lars-balker/perl6/ch-2.p6 delete mode 100644 challenge-002/lars-balker/perl6/ch-2.pl6 diff --git a/challenge-002/lars-balker/perl6/ch-1.p6 b/challenge-002/lars-balker/perl6/ch-1.p6 new file mode 100644 index 0000000000..31f9548d14 --- /dev/null +++ b/challenge-002/lars-balker/perl6/ch-1.p6 @@ -0,0 +1,37 @@ +# Write a script or one-liner to remove leading zeros from positive +# numbers. + +use v6; +use Test; + +sub remove_leading_zeros($num) { + $num.subst(/^( + 0* # match all leading 0s followed by + # 0. (not included in match) + || + 0* # or just all leading 0s + ) + /, + ''); +} + +# we let multi dispatch pick positive numbers +multi sub MAIN(Numeric $num where $num > 0) { + say remove_leading_zeros($num); +} + +# if not a positive number, just echo input +multi sub MAIN($other) { + say $other; +} + +# test if no input +multi sub MAIN() { + # we only test the legal number input, because other input is + # sorted out by multi methods + is(remove_leading_zeros("0001"), "1"); + is(remove_leading_zeros("0.001"), "0.001"); + is(remove_leading_zeros(".001"), ".001"); + is(remove_leading_zeros("000.001"), "0.001"); + done-testing(); +} diff --git a/challenge-002/lars-balker/perl6/ch-1.pl6 b/challenge-002/lars-balker/perl6/ch-1.pl6 deleted file mode 100644 index 31f9548d14..0000000000 --- a/challenge-002/lars-balker/perl6/ch-1.pl6 +++ /dev/null @@ -1,37 +0,0 @@ -# Write a script or one-liner to remove leading zeros from positive -# numbers. - -use v6; -use Test; - -sub remove_leading_zeros($num) { - $num.subst(/^( - 0* # match all leading 0s followed by - # 0. (not included in match) - || - 0* # or just all leading 0s - ) - /, - ''); -} - -# we let multi dispatch pick positive numbers -multi sub MAIN(Numeric $num where $num > 0) { - say remove_leading_zeros($num); -} - -# if not a positive number, just echo input -multi sub MAIN($other) { - say $other; -} - -# test if no input -multi sub MAIN() { - # we only test the legal number input, because other input is - # sorted out by multi methods - is(remove_leading_zeros("0001"), "1"); - is(remove_leading_zeros("0.001"), "0.001"); - is(remove_leading_zeros(".001"), ".001"); - is(remove_leading_zeros("000.001"), "0.001"); - done-testing(); -} diff --git a/challenge-002/lars-balker/perl6/ch-2.p6 b/challenge-002/lars-balker/perl6/ch-2.p6 new file mode 100644 index 0000000000..2cca57c3f8 --- /dev/null +++ b/challenge-002/lars-balker/perl6/ch-2.p6 @@ -0,0 +1,33 @@ +# Write a script that can convert integers to and from a base35 +# representation, using the characters 0-9 and A-Y. + +use v6; +use Test; + +sub to_base35(Int $num) { + return $num.base(35); +} + +sub to_base10(Str $base35) { + return :35($base35); +} + +multi sub MAIN($num) { + say "base35($num) -> ", to_base35($num) unless $num ~~ /\D/; + say "base10($num) -> ", to_base10($num); +} + +# kinda love this ARGV multi method dispatching in perl6 +multi sub MAIN() { + is(to_base35(0), "0"); + is(to_base35(10), "A"); + is(to_base35(35), "10"); + is(to_base35(1337), "137"); + is(to_base35(20190401), "DFVXL"); + is(to_base10("0"), "0"); + is(to_base10("A"), "10"); + is(to_base10("10"), "35"); + is(to_base10("137"), "1337"); + is(to_base10("DFVXL"), "20190401"); + done-testing(); +} diff --git a/challenge-002/lars-balker/perl6/ch-2.pl6 b/challenge-002/lars-balker/perl6/ch-2.pl6 deleted file mode 100644 index 2cca57c3f8..0000000000 --- a/challenge-002/lars-balker/perl6/ch-2.pl6 +++ /dev/null @@ -1,33 +0,0 @@ -# Write a script that can convert integers to and from a base35 -# representation, using the characters 0-9 and A-Y. - -use v6; -use Test; - -sub to_base35(Int $num) { - return $num.base(35); -} - -sub to_base10(Str $base35) { - return :35($base35); -} - -multi sub MAIN($num) { - say "base35($num) -> ", to_base35($num) unless $num ~~ /\D/; - say "base10($num) -> ", to_base10($num); -} - -# kinda love this ARGV multi method dispatching in perl6 -multi sub MAIN() { - is(to_base35(0), "0"); - is(to_base35(10), "A"); - is(to_base35(35), "10"); - is(to_base35(1337), "137"); - is(to_base35(20190401), "DFVXL"); - is(to_base10("0"), "0"); - is(to_base10("A"), "10"); - is(to_base10("10"), "35"); - is(to_base10("137"), "1337"); - is(to_base10("DFVXL"), "20190401"); - done-testing(); -} -- cgit