Given a list of N integers, each with N digits, output a number which differs from the first number because of the first digit, the second number because of the second digit, etc.
Example
Given this list:
1234
4815
1623
4211
The number 2932's first digit is different from the first number's first digit, its second digit is different from the second number's second digit, etc. Therefore it would be a valid output.
Inputs
- You may take both the list and
Nas input, or only the list if you wish. - Each integer in the list will necessarily have as many digits as the length of the list (
N) - Numbers will not have any leading zeroes
- The input list must contain numbers and not strings.
- You may take inputs as function arguments, through
STDIN, or anything similar. - You may assume that the list will not be longer than 10 elements (and no number in the list will be bigger than
2147483647)
Outputs
- It is not sufficient that the output is not in the list. The digits must differ as explained above.
- You can use any digit selection strategy that respects the constraint of different digits.
- The number cannot have leading zeroes
- You may output the number through
STDOUT, return it from a function, etc.
Test cases
Input:
12345678
23456789
34567890
45678901
56789012
67890123
78901234
89012345
Possible output: 24680246
Input:
1
Possible output: 2
Scoring
This is code-golf, so the shortest answer in bytes wins.
20 Answers 20
CJam ((削除) 15 (削除ここまで) 14 bytes)
qN/ee{:=i2%)}%
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
}%
-
1\$\begingroup\$ @Adnan, that does work, thanks. Alternatively
i2%)could be49&), working directly with characters rather than ASCII codes. \$\endgroup\$Peter Taylor– Peter Taylor2016年05月05日 14:17:23 +00:00Commented May 5, 2016 at 14:17
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.
Jelly, (削除) 8 (削除ここまで) 7 bytes
1 byte saved thanks to Dennis.
DŒDḢỊ‘Ḍ
Explanation
DŒDḢỊ‘Ḍ Main link. Takes list as argument.
D Convert each integer to decimal.
ŒD Get the diagonals.
Ḣ Get the first diagonal.
Ị Check if every digit <= 1.
‘ Increment every digit.
Ḍ Convert back to integer from decimal.
Converts each digit to 1, except 0 and 1 becomes 2.
MATL, (削除) 11 (削除ここまで) (削除) 10 (削除ここまで) 9 bytes
VXd9\QV!U
Takes only a column vector of integers as input. N is not provided.
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
-
1\$\begingroup\$ @LuisMendo Oh crud. It looks like it has a leading zero issue though when the first digit is
2: matl.tryitonline.net/… \$\endgroup\$Suever– Suever2016年05月05日 13:51:18 +00:00Commented May 5, 2016 at 13:51 -
\$\begingroup\$ Maybe
VXd9\QV!U\$\endgroup\$Suever– Suever2016年05月05日 13:51:59 +00:00Commented May 5, 2016 at 13:51 -
\$\begingroup\$ Oh, I hadn't thought of leading zero... \$\endgroup\$Luis Mendo– Luis Mendo2016年05月05日 13:52:24 +00:00Commented May 5, 2016 at 13:52
-
\$\begingroup\$ @LuisMendo Does it matter that they aren't unique? As long as they aren't the same value as the input it shouldn't matter. \$\endgroup\$Suever– Suever2016年05月05日 13:55:07 +00:00Commented May 5, 2016 at 13:55
-
\$\begingroup\$ You're totally right. I was thinking about it the wrong way \$\endgroup\$Luis Mendo– Luis Mendo2016年05月05日 13:56:01 +00:00Commented May 5, 2016 at 13:56
Pyth, 11 bytes
jk.eh!ts@`b
Simple loop, change every digit to 1, except 1 becomes 2.
-
\$\begingroup\$ Nice usage of the implicit
Qandk! You can save one byte with during the digit-transformation though:s.eh-12@`b\$\endgroup\$Jakube– Jakube2016年05月06日 07:50:39 +00:00Commented May 6, 2016 at 7:50
Java, 93 bytes
String k(int n,int[]a){String s="";for(int i=0;i<n;)s+=(a[i]+s).charAt(i++)<57?9:1;return s;}
Ungolfed
String k(int n, int[] a) {
String s = "";
for (int i = 0; i < n; )
s += (a[i] + s).charAt(i++) < 57 ? 9 : 1;
return s;
}
Output
Input:
12345678
23456789
34567890
45678901
56789012
67890123
78901234
89012345
Output:
99991999
Input:
1234
4815
1623
4211
Output:
9999
Retina, (削除) 39 (削除ここまで) (削除) 38 (削除ここまで) 37
(?<=(.*¶)*)(?<-1>.)*(.).*¶
2ドル
T`d`121
Saved 1 byte thanks Martin!
Requires a trailing linefeed in the input.
Gets diagonals and translates 0 and 2-9 to 1 and 1 to 2.
The basic idea for getting the diagonals is to push a capture for each row above the current row and then consume a capture to match a character, then keep the next character.
J, (削除) 26 (削除ここまで) 22 bytes
1+1>:i.@#{"_1"."0@":"0
Similar approach to the others using the <= 1 and increment method of the diagonal.
Usage
Only requires the list of integers as an argument.
f =: 1+1>:i.@#{"_1"."0@":"0
f 1234 4815 1623 4211
2 1 1 2
f 92345678 23456789 34567890 45678901 56789012 67890123 78901234 89012345
1 1 1 1 1 2 1 1
-
\$\begingroup\$ Sorry to break the 1 streak... \$\endgroup\$NoOneIsHere– NoOneIsHere2016年05月06日 05:06:02 +00:00Commented May 6, 2016 at 5:06
Python 2, 54 bytes
d,r=1,""
for n in input():r+=`1+-~n/d%9`;d*=10
print r
-
\$\begingroup\$ Sounds familiar.
-~n\$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年05月06日 09:22:28 +00:00Commented May 6, 2016 at 9:22
Java, 94 bytes
int c(int[]d){int p=1,r=0,l=d.length,i=0;for(;i<l;p*=10)r+=(d[l-++i]/p%10==1?2:1)*p;return r;}
Pure numeric operations for the win! :)
Sample input/output:
8 <-- size
12345678 <-- start of list
23456789
34567890
45678901
56789012
67890123
78901234
89012345 <-- end of list
21111211 <-- result from ungolfed code
21111211 <-- result from golfed code
Full program (with ungolfed code):
import java.util.Scanner;
public class Q79444 {
int cantor_ungolfed(int[] data){
int power = 1;
int result = 0;
for(int i=0;i<data.length;i++){
result += (((data[data.length-i-1]/power))%10==1? 2 : 1)*power;
power *= 10;
}
return result;
}
int c(int[]d){int p=1,r=0,l=d.length,i=0;for(;i<l;p*=10)r+=(d[l-++i]/p%10==1?2:1)*p;return r;}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] input = new int[n];
for(int i=0;i<n;i++){
input[i] = sc.nextInt();
}
System.out.println(new Q79444().cantor_ungolfed(input));
System.out.println(new Q79444().c(input));
sc.close();
}
}
Reng v.3.3, 60 bytes
k1-#kaiír1ø ~; !nb$<
1[å{$}k*$k1-#k)9(-#oa$;]o)ks^$
For Reng, that was rather simple. Try it here! Input is a space-separated list of numbers.
1: init
k1-#kaiír1ø
k is the number of inputs (number of numbers), and we decrement by 1 and restore for the loop stage. aií takes all input. r reverses the stack for the processing of input. 1ø goes to the next line.
2: loop
1[å{$}k*$k1-#k)9(-#oa$;]o)ks^$
1[ takes the top item off the stack and into a new stack. å splits it into digits. {$} pushes a code block containing the operation "drop"; this is repeated k times (k*) and the code block is dropped ($. k1-#k decrements k. )9( puts 9 at the STOS, and - subtracts TOS from the STOS. #o stores this number in o, and a$; drops all the members of the stack. ] closes the parent stack. o puts o back at the top; this is our digit we're saving. ) moves it to the bottom so that we can continue our looping. s usually checks for non-input (i.e., equality to -1), but we can use it to break out of our loop when k == -1. So s^ goes up when k == -1. $ drops k from the stack, and our loop begins again.
3: final
~; !nb$<
< directs the pointer left, and $ drops k from the stack. b is a leftway mirror, so we enter through it, but it bounce back when hitting ;, a stack-condition mirror. !n prints a digit if and only if we're going left. ~ ends the program when we're done printing.
Ruby, 21 bytes
$><<$_[$.-1].hex%2+1
A full program. Run with the -n flag. Uses the following mapping: n -> n%2+1.
Perl, 18 bytes
Includes +1 for -p
Run with the input lines on STDIN. Output is 1 except 2 when the diagonal is 1
cantor.pl
#!/usr/bin/perl -p
pos=$.;$_=/1\G/+1
J, 37 bytes
f@(9&-@"."0@((>:@#*i.@#){f=:[:,":"0))
Can probably be golfed, but I forgot if there was a command for "diagonals".
-
\$\begingroup\$ I've seen it used before, Martin used the anti-diagonals here. \$\endgroup\$FryAmTheEggman– FryAmTheEggman2016年05月05日 13:31:08 +00:00Commented May 5, 2016 at 13:31
-
\$\begingroup\$ @FryAmTheEggman Yeah, that's close. I'm still looking, but there might not be. \$\endgroup\$Conor O'Brien– Conor O'Brien2016年05月05日 15:11:41 +00:00Commented May 5, 2016 at 15:11
-
\$\begingroup\$ This seems to produce a leading zero if the diagonal begins with a 9. \$\endgroup\$Zgarb– Zgarb2016年05月05日 15:59:25 +00:00Commented May 5, 2016 at 15:59
-
\$\begingroup\$ You can create the digits table if you take the input
nusing<list> (#:~#&10) <n>. The first diagonal can be found with(< 0 1) |: <list>where(< 0 1)is a box for the axes to select, using both, with|:\$\endgroup\$miles– miles2016年05月05日 16:37:19 +00:00Commented May 5, 2016 at 16:37
Mathematica 52 bytes
FromDigits[Mod[#,2]+1&/@Diagonal[IntegerDigits/@#]]&
This follows the approach of Peter Taylor and others (without using Ascii codes).
Example
FromDigits[Mod[#,2]+1&/@Diagonal[IntegerDigits/@ #]]&[{1234,4815,1623,4211}]
2112
ClojureScript, 58 chars
#(int(apply str(map-indexed(fn[i x](- 9(get(str x)i)))%)))
Type requirements made this a little longer than necessary, and map-indexed being so many chars didn't help.
Often my submissions are valid Clojure as well, but this is using some of ClojureScript's leakiness with JavaScript. Subtraction of a number and string coerces the string to a number--that is, (- 9 "5") equals 4.
PHP, 46/41/40 bytes
while($a=$argv[++$i])echo($b=9-$a[$i-1])?$b:1;
while($a=$argv[++$i])echo$a[$i-1]==7?6:7;
while($a=$argv[++$i])echo($a[$i-1]%2)+1;
Various digit selectors for comparison. I thought "9-digit" would be shortest, but the special case needed to keep a zero out of the first digit overwhelms it.
Fed from CLI arguments:
php -r "while($a=$argv[++$i])echo($b=9-$a[$i-1])?$b:1;" 12345678 23456789 34567890 45678901 56789012 67890123 78901234 89012345
86421864
JavaScript (ES6), 41
The %9+1 trick is borrowed from Suever's answer. For once, .reduce beats .map. Note, the += operator is used to avoid parentheses.
a=>+a.reduce((t,n,d)=>t+=(n+t)[d]%9+1,'')
STDINto integer before parsing? \$\endgroup\$STDINis string by default... \$\endgroup\$