Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions challenge-002/lars-balker/perl5/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Write a script or one-liner to remove leading zeros from positive
# numbers.

# I chose to solve this in the strictest interpretation of the challenge:
# Make sure it's a number
# Make sure it's positive
# Don't remove the last 0 before period.

use v5.10;
use strict;
use warnings;
use Scalar::Util qw/looks_like_number/; # distributed with perl since 5.8

sub remove_leading_zeros {
my $num = shift;
# ask perl if it thinks input is a number, and make sure it's positive
if (looks_like_number($num) && $num > 0) {
$num =~ s/^
(
0* # match all leading 0s followed by
(?=0\.) # 0. (not included in match)
|
0* # or just all leading 0s
)
//x;
}
$num;
}

if (@ARGV) {
say remove_leading_zeros shift;
}
else {
eval "use Test::More";
is(remove_leading_zeros("000000"), "000000");
is(remove_leading_zeros("-0001"), "-0001");
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");
is(remove_leading_zeros("001bar"), "001bar");
is(remove_leading_zeros("fizzbuzz"), "fizzbuzz");
done_testing();
}
31 changes: 31 additions & 0 deletions challenge-002/lars-balker/perl5/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Write a script that can convert integers to and from a base35
# representation, using the characters 0-9 and A-Y.

use v5.10;
use strict;
use warnings;

sub to_base35 {
my $num = shift;
my $res = "";
my @val = (0..9, 'A'..'Y');
do {
$res .= $val[$num % 35];
$num = int($num / 35);
} while $num;
$res = reverse $res;
$res;
}

if (@ARGV) {
say to_base35 shift;
}
else {
eval "use Test::More";
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");
done_testing();
}
37 changes: 37 additions & 0 deletions challenge-002/lars-balker/perl6/ch-1.pl6
Original file line number Diff line number Diff line change
@@ -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
<?before 0\.> # 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();
}
23 changes: 23 additions & 0 deletions challenge-002/lars-balker/perl6/ch-2.pl6
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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);
}

multi sub MAIN($num) {
say to_base35($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");
done-testing();
}