Simple challenge: given a series of positive integer numbers, find the number that contains among its digits the longest run of consecutive digits. The trick? It's allowed for the digits in the runs to wrap around the possible values (0123456789) and to run backwards. So both 2345, 89012 and 5432109 are valid runs of consecutive digits (but not 3456765 nor 321090123 as the run must be always in the same direction, although 3456765 can be considered as two runs: 34567 and 765). In the case of ties, return the first one.
Test cases:
Input: [3274569283, 387652323, 23987654323648, 2345687913624]
Output: 23987654323648
(The run is 98765432; run length: 8)
Input: [123012363672023, 098761766325432, 15890123456765]
Output: 15890123456765
(The run is 8901234567; run length: 10)
Input: [43, 19, 456]
Output: 456
Input: [5, 9, 0]
Output: 5
Input: [71232107, 7012347]
Output: 7012347
Input: [1234, 32109876]
Output: 32109876
Input: [9090, 123]
Output: 123
Notes:
- There will be at least one number in the input.
- Input numbers can contain leading zeroes.
- Input and output can be in any reasonable format. So input numbers can be taken as strings, lists of digits/characters...
- Output can contain trailing and/or leading whitespaces and newlines as long as the number is printed.
- This is code-golf, so may the shortest program/function for each language win!
6 Answers 6
Jelly, 18 bytes
I9,-;N¤yŒgỊS€ṀμÐṀḢ
Takes and returns as list of digits so as to preserve leading zeroes.
-
\$\begingroup\$ Similar problem to mine - try this out for size (I believe it returns the wrong result - I've suggested it as a test case just to be sure). \$\endgroup\$Jonathan Allan– Jonathan Allan2017年08月02日 13:25:19 +00:00Commented Aug 2, 2017 at 13:25
-
\$\begingroup\$ @JonathanAllan I think that's the right output? (there's
3210in the first number btw) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 13:27:04 +00:00Commented Aug 2, 2017 at 13:27 -
\$\begingroup\$ Ah oops, this, sorry! \$\endgroup\$Jonathan Allan– Jonathan Allan2017年08月02日 13:30:55 +00:00Commented Aug 2, 2017 at 13:30
-
\$\begingroup\$ @JonathanAllan Oh I see what you mean...it's probably because of the
Ain there. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 13:32:41 +00:00Commented Aug 2, 2017 at 13:32 -
\$\begingroup\$ @JonathanAllan Fixed. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 17:46:03 +00:00Commented Aug 2, 2017 at 17:46
JavaScript (ES6), (削除) 104 (削除ここまで) (削除) 102 (削除ここまで) 98 bytes
Takes input as a list of lists of digits. Returns the best one.
a=>a.map(s=>s.map(n=>(i=(d=(x-(x=n)+11)%10)&&d-2?0:d-p?(p=d,1):i+1)>j&&(r=s,j=i),p=x=-10),j=-1)&&r
Test cases
let f =
a=>a.map(s=>s.map(n=>(i=(d=(x-(x=n)+11)%10)&&d-2?0:d-p?(p=d,1):i+1)>j&&(r=s,j=i),p=x=-10),j=-1)&&r
console.log(JSON.stringify(f([[3,2,7,4,5,6,9,2,8,3], [3,8,7,6,5,2,3,2,3], [2,3,9,8,7,6,5,4,3,2,3,6,4,8], [2,3,4,5,6,8,7,9,1,3,6,2,4]])))
console.log(JSON.stringify(f([[1,2,3,0,1,2,3,6,3,6,7,2,0,2,3], [0,9,8,7,6,1,7,6,6,3,2,5,4,3,2], [1,5,8,9,0,1,2,3,4,5,6,7,6,5]])))
console.log(JSON.stringify(f([[4,3], [1,9], [4,5,6]])))
console.log(JSON.stringify(f([[5], [9], [0]])))
Jelly, (削除) 18 16 (削除ここまで) 15 bytes
I%5Œg%8ċ1ドルṀμÐṀḢ
A monadic link taking a list of lists of digits, and returning the leftmost one containing a maximal run as described.
Try it online! or see a test suite (with processing to make I/O look like it is in the question).
How?
I%5Œg%8ċ1ドルṀμÐṀḢ - Link: list of lists of integers (digits) from [0-9]
μÐṀ - keep elements for which the link to the left is maximal:
I - incremental differences (i.e. [a2-a1, a3-a2, ...])
5 - literal 10
% - modulo by (i.e. [(a2-a1)%10, (a3-a2)%10, ...])
- this equates deltas of -9 and -1 with 1 and 9 respectively
Œg - group runs of equal elements
%8 - modulo by 8; vectorised (9s become 1s, others unaffected)
ċ1ドル - count number of 1s in €ach group
Ṁ - maximum
Ḣ - head (get the first one of those that were maximal)
-
\$\begingroup\$
V€not sure about that, you may have to count leading zeroes. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 12:25:59 +00:00Commented Aug 2, 2017 at 12:25 -
\$\begingroup\$ That does count leading zeros of the sting input, however I see we may take lists of lists of digits... \$\endgroup\$Jonathan Allan– Jonathan Allan2017年08月02日 12:26:39 +00:00Commented Aug 2, 2017 at 12:26
-
\$\begingroup\$ I think you're supposed to support leading zeroes. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 12:28:31 +00:00Commented Aug 2, 2017 at 12:28
-
\$\begingroup\$ I do support leading zeros \$\endgroup\$Jonathan Allan– Jonathan Allan2017年08月02日 12:29:33 +00:00Commented Aug 2, 2017 at 12:29
-
1\$\begingroup\$ I read that as "That doesn't count..." \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月02日 12:29:48 +00:00Commented Aug 2, 2017 at 12:29
Python 2, 118 bytes
Takes a list of lists of digits a; returns one of its lists.
lambda a:max(a,key=lambda l:len(max(re.findall('1+|9*',`[(x-y)%10for x,y in zip(l,l[1:])]`[1::3]),key=len)))
import re
Husk, 20 bytes
←Ö¤>(→Of1ドル†%8gẊo%10-
Takes and returns a list of lists of digits. Try it online!
Explanation
←Ö¤>(→Of1ドル†%8gẊo%10- Implicit input.
← Return first element of
Ö the input sorted in a stable manner
> in descending order
¤ ( with respect to the following function:
Argument is list of digits, say [5,2,1,0,9,1,0].
- Differences
o%10 mod 10
Ẋ of all adjacent pairs: [7,9,9,9,2,1]
g Group adjacent equal elements: [[7],[9,9,9],[2],[1]]
†%8 Vectorized mod 8: [[7],[1,1,1],[2],[1]]
f1ドル Keep those runs where 1 occurs: [[1,1,1],[1]]
O Sort in ascending order: [[1],[1,1,1]]
→ Take last element (gives [] on empty list): [1,1,1]
This is a list of 1s with length one less than
the longest run of consecutive digits.
-
\$\begingroup\$ Doesn't seem to give the right answer for multiple test cases. \$\endgroup\$Razetime– Razetime2020年10月14日 10:09:00 +00:00Commented Oct 14, 2020 at 10:09
-
\$\begingroup\$ @Razetime Fixed. I'm not sure, but I suspect that either
Öor<has been changed since this challenge, since the fix was so simple. \$\endgroup\$Zgarb– Zgarb2020年10月14日 11:25:59 +00:00Commented Oct 14, 2020 at 11:25
MATLAB, 130 bytes
Take input to array, array of column differences [X(2)-X(1),...,X(n)-X(n-1)], check the most frequent value in the array (1 ascending order -1 otherwise), get the index for either the most frequent value or -9 multiplied by the most frequent value (-9 occurs in ascending order, 9 otherwise), find the consecutive indices (i.e. whose difference is equal to 1) and sum it please, because it's late. Output the largest.
a=input('')
t=[]
for i=1:numel(a)
b=diff(num2str(a(i))-'0')
c=mode(b)
t=[t sum(diff(find(b==c|b==-9*c))==1)]
end
[t,I]=max(t),a(I)
[7,8,1,6]has a maximal run of[7,8]rather than[6,7,8], yes? \$\endgroup\$78in that case. \$\endgroup\$