Finite Cantor's Diagonal
CJam (15 14 bytes)
qN/ee{:=i2%)}%
Online demo
Thanks to Adnan for a one-byte saving.
Dissection
qN/ e# Split input on newlines
ee{ e# Label each line with its index and map:
:=i e# Get the character at the same index (i.e. diagonalise)
2%) e# Compute (ASCII value mod 2) + 1
e# Maps 0 1 2 3 4 5 6 7 8 9
e# to 1 2 1 2 1 2 1 2 1 2
}%
Python 2, 47 45 bytes
lambda x,n:int(`x`[1::n+3])%(10**n/2)+10**n/9
Thanks to @xnor for golfing off 2 bytes!
Test it on Ideone.
How it works
`x`
yields a string representation of the list x.
For the first test case, this gives the string
[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
[1::n+3]
retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for ,
, we retrieve the first digit of the first number, the second digit of the second number, etc.
[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
^ ^ ^ ^ ^ ^ ^ ^
We now take the number modulo 10n ÷ 2 to map the first digit in the range [0, 4].
For 93579135, we get 93579135 % 50000000 = 43579135.
Finally, we add 10n ÷ 9 to the last result, which increments – wrapping around from 9 to 0 – all digits by 1 (no carry) or 2 (with carry).
For 43579135, we get 43579135 + 11111111 = 54690246.
MATL, 11 10 9 bytes
VXd9\QV!U
Takes only a column vector of integers as input. N
is not provided.
Try it Online
Explanation
% Implicity grab input as column vector of numbers
V % Convert the input column vector into a 2D character array
Xd % Grab the diagonal elements of the character array
9\ % Take the modulus of each ASCII code and 9
Q % Add 1 to remove all zeros
V % Convert the result to a string
! % Transpose to yield a row vector of characters
U % Convert back to an integer (as per the rules)
% Implicitly display result