Powershell, 89 bytes
"$args"-notmatch'(.)(.*)(.)'-or(($m=$Matches).1-ge$m.3-and(.\g(''+(+$m.1+$m.3)%10+$m.2)))
Important! The script calls itself recursively. So save the script as g.ps1
file in the current directory. Also you can call a script block variable instead script file (see the test script below). That calls has same length.
Note 1: The script uses a lazy evaluation of logic operators -or
and -and
. If "$args"-notmatch'(.)(.*)(.)'
is True
then the right subexpression of -or
is not evaluated. Also if ($m=$Matches).1-ge$m.3
is False
then the right subexpression of -and
is not evaluated too. So we avoid infinite recursion.
Note 2: The regular expression '(.)(.*)(.)'
does not contain start and end anchors because the expression (.*)
is greedy by default.
Test script
$g={
"$args"-notmatch'(.)(.*)(.)'-or(($m=$Matches).1-ge$m.3-and(&$g(''+(+$m.1+$m.3)%10+$m.2)))
}
@(
,(2632, $true)
,(92258, $true)
,(60282, $true)
,(38410, $true)
,(3210, $true)
,(2302, $true)
,(2742, $true)
,(8628, $true)
,(6793, $true)
,(1, $true)
,(2, $true)
,(10, $true)
,(100, $true)
,(55, $true)
,(121, $true)
,(6724, $false)
,(47, $false)
,(472, $false)
,(60247, $false)
,(33265, $false)
,(79350, $false)
,(83147, $false)
,(93101, $false)
,(57088, $false)
,(69513, $false)
,(62738, $false)
,(54754, $false)
,(23931, $false)
,(7164, $false)
,(5289, $false)
,(3435, $false)
,(3949, $false)
,(8630, $false)
,(5018, $false)
,(6715, $false)
,(340, $false)
,(2194, $false)
) | %{
$n,$expected = $_
#$result = .\g $n # uncomment this line to call a script file g.ps1
$result = &$g $n # uncomment this line to call a script block variable $g
# the script block call and the script file call has same length
"$($result-eq-$expected): $result <- $n"
}
Output:
True: True <- 2632
True: True <- 92258
True: True <- 60282
True: True <- 38410
True: True <- 3210
True: True <- 2302
True: True <- 2742
True: True <- 8628
True: True <- 6793
True: True <- 1
True: True <- 2
True: True <- 10
True: True <- 100
True: True <- 55
True: True <- 121
True: False <- 6724
True: False <- 47
True: False <- 472
True: False <- 60247
True: False <- 33265
True: False <- 79350
True: False <- 83147
True: False <- 93101
True: False <- 57088
True: False <- 69513
True: False <- 62738
True: False <- 54754
True: False <- 23931
True: False <- 7164
True: False <- 5289
True: False <- 3435
True: False <- 3949
True: False <- 8630
True: False <- 5018
True: False <- 6715
True: False <- 340
True: False <- 2194
Powershell, 90 bytes
No recursion. No file name dependency and no script block name dependency.
for($s="$args";$s[1]-and$s-ge$s%10){$s=''+(2+$s[0]+$s)%10+($s|% S*g 1($s.Length-2))}!$s[1]
A Powershell implicitly converts a right operand to a type of a left operand. Therefore, $s-ge$s%10
calculates right operand $s%10
as integer
and compare it as a string
because type of the left operand is string
. And 2+$s[0]+$s
converts a char $s[0]
and string $s
to integer
because left operand 2
is integer.
$s|% S*g 1($s.Length-2)
is a shortcut to $s.Substring(1,($s.Length-2))
Can we take input as a string? – lirtosiast – 2018-12-07T08:20:55.193
@lirtosiast, yes, but not list of digits. – Vedant Kandoi – 2018-12-07T08:23:48.580
14They could be called Autocannibalistic Numbers. – Arnauld – 2018-12-07T10:20:19.157
6What is the reason we can't take as a list of digits? This problem already treats them as if they are lists of digits. Forcing them to be numbers means that you just have to pin extra code to convert them to a list. – Post Rock Garf Hunter – 2018-12-07T14:09:56.370
1Can two consistent distinct values be returned instead of truthy/falsy? – Olivier Grégoire – 2018-12-07T17:22:36.710
Now this is real cannibalism – FireCubez – 2018-12-09T14:22:55.463