1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw{ postderef say signatures state };
no warnings qw{ experimental };
my @numbers = qw{ 1234 91011 10203 };
for my $n (@numbers) {
say base($n);
}
# we are asked to return the sequence
# or the given number, and accounting
# for that makes recursion difficult,
# so we pass to base to determine that
sub base ( $n ) {
my $s = get_sequence($n);
return $s//$n;
}
# test for success and return if successful
# then add commas within (a copy of) the
# string
sub get_sequence ( $n ) {
my $t = test($n);
return $n if $t;
my $output;
my @n = split /,/, $n;
my $flag = 0;
map { $flag += 1 if $_ > 10 } @n;
if ( $flag > 0 ) {
for my $i ( 0 .. length $n ) {
my $cp = $n;
my $l = substr( $cp, $i, 1 );
substr( $cp, $i, 1 ) = ',' . $l;
next if $cp =~ m{^\,|\,\,|\,$};
my $x = get_sequence($cp);
return $x if $x;
}
}
return undef;
}
sub test ( $n ) {
my $t = 1;
my @n = split /,/, $n;
$t = 0 if $n[0] =~ m{^0}mx;
$t = 0 if scalar @n < 2;
for my $i ( 1 .. -1 + scalar @n ) {
my $h = $i - 1;
$t = 0 if $n[$i] =~ m{^0}mx;
$t = 0 unless $n[$h] + 1 == $n[$i];
}
return $t;
}
|