15
\$\begingroup\$

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 , so may the shortest program/function for each language win!
\$\endgroup\$
  • \$\begingroup\$ Related. \$\endgroup\$ – Charlie Aug 2 '17 at 12:05
  • \$\begingroup\$ Just to be sure, the list itself cannot wrap, right? (I misunderstood the digit wrapping as list wrapping) so [7,8,1,6] has a maximal run of [7,8] rather than [6,7,8], yes? \$\endgroup\$ – Jonathan Allan Aug 2 '17 at 16:23
  • 1
    \$\begingroup\$ @JonathanAllan yes, the maximal run is 78 in that case. \$\endgroup\$ – Charlie Aug 2 '17 at 16:43
4
\$\begingroup\$

Jelly, 18 bytes

I9,-;N¤yŒgỊS€ṀµÐṀḢ

Try it online!

Takes and returns as list of digits so as to preserve leading zeroes.

\$\endgroup\$
  • \$\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 Aug 2 '17 at 13:25
  • \$\begingroup\$ @JonathanAllan I think that's the right output? (there's 3210 in the first number btw) \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 13:27
  • \$\begingroup\$ Ah oops, this, sorry! \$\endgroup\$ – Jonathan Allan Aug 2 '17 at 13:30
  • \$\begingroup\$ @JonathanAllan Oh I see what you mean...it's probably because of the A in there. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 13:32
  • \$\begingroup\$ @JonathanAllan Fixed. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 17:46
3
\$\begingroup\$

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]])))

\$\endgroup\$
3
\$\begingroup\$

Jelly,  18 16  15 bytes

I%⁵Œ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%⁵Œ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, ...])
  ⁵             -   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)
\$\endgroup\$
  • \$\begingroup\$ V€ not sure about that, you may have to count leading zeroes. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 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 Aug 2 '17 at 12:26
  • \$\begingroup\$ I think you're supposed to support leading zeroes. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 12:28
  • \$\begingroup\$ I do support leading zeros \$\endgroup\$ – Jonathan Allan Aug 2 '17 at 12:29
  • 1
    \$\begingroup\$ I read that as "That doesn't count..." \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 12:29
2
\$\begingroup\$

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Fails on input [[9,0,9,0],[1,2,3]]. \$\endgroup\$ – Zgarb Aug 2 '17 at 15:03
  • \$\begingroup\$ @Zgarb Oops, you’re right. Back to an old version I go. \$\endgroup\$ – Lynn Aug 2 '17 at 15:22
1
\$\begingroup\$

Husk, 20 bytes

←Ö¤<(→Of€1†%8gẊo%10-

Takes and returns a list of lists of digits. Try it online!

Explanation

←Ö¤<(→Of€1†%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]]
       f€1             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.
\$\endgroup\$
1
\$\begingroup\$

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)

Try it online!

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.