| Bytes | Lang | Time | Link |
|---|---|---|---|
| 059 | JavaScript V8 | 250428T032111Z | Steve Be |
| 043 | Ruby | 250427T164924Z | Jordan |
| 030 | R | 211229T233010Z | Dominic |
| 007 | Husk | 211229T224651Z | Natte |
| 045 | Factor | 211229T193641Z | chunes |
| 073 | Python 3 | 191001T201940Z | Matthew |
| 009 | Japt h | 190930T121134Z | Shaggy |
| 006 | 05AB1E | 190930T090857Z | Dorian |
| nan | 120105T063002Z | Blazer | |
| 005 | Jelly | 180803T105331Z | lynn |
| 055 | Julia 0.6 | 180803T082010Z | Sundar R |
| 034 | R | 180801T192729Z | JayCe |
| 009 | Japt | 180731T140310Z | Luis fel |
| nan | 141011T180601Z | edem | |
| 031 | Mathematica | 141011T141157Z | Martin E |
| 024 | Just for fun | 141011T144602Z | Optimize |
| nan | 120114T214920Z | Włodar | |
| nan | 120105T164656Z | agmcleod | |
| nan | 120108T060332Z | elssar | |
| nan | 120107T163500Z | Gareth | |
| nan | Since I can't edit code | 120106T192837Z | Samuel D |
| 079 | Python | 120106T171627Z | Steven R |
| 087 | Python | 120106T092113Z | Ante |
| 021 | APL | 120105T070051Z | Dillon C |
| nan | 120105T193102Z | mellamok | |
| 023 | J | 120105T172009Z | J B |
| 023 | GolfScript | 120105T135711Z | Howard |
| 054 | Haskell | 120105T102948Z | J B |
| nan | 120105T062405Z | Joanis |
JavaScript (V8), 59 bytes
a=>a.reduce((n,c,i)=>c+1==a[i+1]?(++s>n?n=s:n):(s=1,n),s=1)
Ruby, 43 bytes
->a{a.chunk_while{_2-_1<2}.map(&:size).max}
J-uby, 35 bytes
A port of the above.
:chunk_while+(~:-|:>&2)|:*&:+@|:max
R, 30 bytes
x=scan();max(rle(x-seq(!x))$l)
Approach boringly copied from other answers, but different from (and slightly shorter than) JayCe's earlier R answer.
Husk, 7 bytes
▲mLgz-N
Explanation
▲mLgz-N
z N zip input and infinite list [1..]
- with '-'
g group adjacent equal values
mL map length
▲ maximum
Python 3, 73 bytes
def f(l):s=[n-i for i,n in enumerate(l)];return max(s.count(i)for i in s)
This works by subtracting the index from each number in the list and finding the greatest frequency in the list.
05AB1E, 7 6 bytes
∞-γ€gZ
Thanks to @Kevin Cruijssen for -1 byte
Code:
∞ push endless list [1, 2, 3, 4, 5, ...]
- subtract that from input
γ group by equal elements
€g push lengths of each group
Z push max length
implicitly print top of stack
Python
new:
def s(q):
i = r = 1
l = -1
for n in q:
if n == l + 1:
i += 1
elif i > r:
r,i = i,1
else:
i=1
l=n
return i if i > r else r
old (buggy):
def s(q):
i=r=1;l=-1
for n in q:
if n==l+1:i+=1
elif i>r:r,i=i,1
l=n
return r
test case:
x = [1,2,3,50,56,58,60,61,90,100]
print x
print s(x)
>>> 3
edit: forgot the input numbers had to be in order. fixed edit2: fixed a problem when all the numbers are a sequence, eg 1,2,3,4,5,6,7,8,9,10, and it returned just '1' edit3: oops, didn't realize it wasn't code golf.
Jelly, 5 bytes
_JŒɠṀ
_J Subtract [1,2,3…] from the input.
This will turn a run like [48,49,50] into something like [45,45,45].
Œɠ Get group lengths. (“aaaabbc” → [4,2,1])
Ṁ Maximum.
Julia 0.6, 55 bytes
a->((d=diff(a))[d.!=1]=x=0;maximum(x=(x+y)y for y=d)+1)
Going for \$ O(n) \$,
82 bytes
a->(l=m=0;i=1;while i<length(a);a[i]==a[i+1]-1 ? l+=1 : l=1;l>m&&(m=l);i+=1;end;m)
which ungolfed is:
function f(a)
l = m = 0
i = 1
while i < length(a)
a[i] == a[i+1]-1 ? l+=1 : l=1
if l > m
m = l
end
i += 1
end
return m
end
R, 34 bytes
u=rle(1:99%in%scan());max(u$l*u$v)
This is a copy-paste of Micky T's answer to this challenge. Upvote him!
Perl
my @o = (1,4,4,6,7,45,46,47,48,98);
sub r{
$c=1;
$l=pop@_;
$m=1;
while($n=pop@_){
if($l-$n==1){++$c;$m=$c if $c>$m}
else{$c=1}
$l=$n
}
$m
}
print "MAX: " . r(@o);
Output: MAX: 4
Mathematica, 50 31 bytes
Max[Length/@Split[#,#2-#==1&]]&
This is an anonymous function. You can either give it a name, by assigning it to something, or you can just append @{6, 9, 50, 51, 52, 72, 81, 83, 90, 92} to use it straight away.
Here is how it works:
#2-#==1& is a nested anonymous function, which takes two arguments and returns True if the arguments are consecutive integers (and False for other integer pairs).
Split then partitions the resulting array into runs of elements, for which the above function returns True.
Now we map Length onto the result to figure out how many numbers each run contains, and select the maximum.
Thanks to David Carraher for eliminating large parts by using a test function in Split!
Just for fun, CJam, 24 bytes
l~]M\{:H(=+H}*;0a/:,$W=)
I know this is not valid as the language did not exist at the time of asking the question, but I thought I will give it a try.
How it works:
l~]M\ "Read the input, convert to array, put an empty array M before it";
{ }* "For each pair of the array, run the code block";
:H( "Assign the second number from the pair to H and decrement it";
=+ "Check if it is equal to previous number and put result in M";
H "Put back the second number so as to be used in next iteration":
;0a "Drop the trailing last number and put [0] on stack";
/ "Split M on [0]";
:, "For each split element, replace it with its length";
$W= "Sort the final array and take its last element";
) "Increment this highest element from the array";
Java
public static int getConsecutiveCount(int[] A) {
int r = 1, t = 1;
boolean c = false;
for (int i = 0; i < A.length; i++) {
if (i + 1 < A.length) {
if (A[i + 1] - A[i] == 1) {
t++;
c = true;
} else {
c = false;
}
} else {
c = false;
}
if (t > r && !c) {
r = t;
t = 1;
}
}
return r;
}
Ruby
I'm generally not that great at knowing more efficient methods. Might be some ruby standard lib things I'm missing.
def consec_count(arr)
tg = 1
cg = 1
l = arr[0]
arr.each do |v|
if l + 1 == v
cg += 1
tg = cg if tg < cg
else
cg = 1
end
l = v
end
tg
end
Python
def s(n):
l=0
t=1
for i in range(0,9):
if n[i]+1==n[i+1]:
t+=1
else:
if l<t:
l=t
t=1
return l
Scala
def maxconsec(ln:List[Int]):Int=
{
var (max,current,last)=(1,1,-1)
for(n<-ln)
{
if(n==last+1)
{
current+=1;
if(current>max)max=current
}
else
{
current=1
}
last=n
}
return max
}
I think this is O(n); is it possible to be more efficient? Can the OP elaborate on how efficiency is being measured here?
Usage: (you can test it on simplyscala.com)
maxconsec(List(6,9,50,51,52,72,81,83,90,92))
Since I can't edit code, I'll redefine the Scheme answer (Racket or not, portable and easier to read). Variables have descriptive names and there is only one recursion of the list while being error free (i think). Who cares about characters when you got efficiency?
(define (count-consecutive lst)
(call-with-current-continuation
(lambda (return)
(define (test n)
(unless (integer? n)
(return (error "Not integer: " n))))
(define (g best) (f best n (cdr lst)))
(let ([prev (car lst)] [count 0]) (test prev)
(let f ([best 0] [prev prev]
[lst (cdr lst)])
(if (null? lst) best
(let ([n (car lst)]) (test n)
(if (= (- n prev) 1)
(let ([count (+ count 1)])
(if (> count best) (g count)
(g best)))
(let ([count 0])
(g best))))))))))
Python, 79 characters
f=lambda l:max(map(len,''.join(' x'[a-b==1]for a,b in zip(l[1:],l)).split()))+1
Python, 87 characters
Recursive solution:
def s(l):
n=1
while n<len(l)and l[n]-l[0]==n:n+=1
return max(n,s(l[n:])if l else 0)
Testing:
>>> s([1, 2, 33, 44, 55, 66, 77, 88, 98])
2
>>> s([1, 3, 23, 24, 30, 48, 49, 70, 80])
2
>>> s([6, 9, 50, 51, 52, 72, 81, 83, 92])
3
APL, 26 21 characters
1+⌈/+/^\9 9⍴0,1=-2-/⎕
Here's the 26-character solution:
i←⎕⋄⌈/{1++/^\⍵↓1=-2-/i}¨⍳9
I used Dyalog APL as my interpreter, and ⎕IO should be set to 0 for the 26-character version.
Example:
1+⌈/+/^\9 9⍴0,1=-2-/⎕
⎕:
6 7 51 51 53 51 54 55 56 55
3
This answer explains most of what I in the 26-character solution, but I might have to write a new one up for the 21-character version.
JavaScript (80 93 107)
for(n=prompt(c=m=1).split(' '),i=9;i;m=++c>m?c:m)c*=!(--n[i]-n[--i])|0;alert(m);
Demo: http://jsfiddle.net/QqfY4/3/
Edit 1: Replaced a with n[i] and b with n[i-1]. Converted Math.max to ternary if. Moved initialization statement into for(.
Edit 2: Reversed iteration direction to eliminate need for i++ by changing to --i in second n[--i]. Replaced i++ with part of if body. Changed condition to i to take advantage of ending at 0 = false. Hard-code starting value of 9 due to spec 10 random numbers.
J, 23
>:>./(+*[)/\.(}.=>:&}:)
Sample use:
>:>./(+*[)/\.(}.=>:&}:) 1 2 33 44 55 66 77 88 98
2
>:>./(+*[)/\.(}.=>:&}:) 1 3 23 24 30 48 49 70 80
2
>:>./(+*[)/\.(}.=>:&}:) 6 9 50 51 52 72 81 83 92
3
GolfScript, 23 characters
~]1\{.@-(!@*).@}*;]$)p;
Test cases:
"1 2 33 44 55 66 77 88 98" --> 2
"1 3 23 24 30 48 49 70 80" --> 2
"6 9 50 51 52 72 81 83 92" --> 3
Haskell, 54
import List
f=maximum.map length.group.zipWith(-)[1..]
Scheme/Racket
Here's a trivial answer in Scheme...
(define f
(λ (l)
(letrec ((g (λ (l M c p)
(cond ((null? l)
(max M c))
((= (car l) (+ p 1))
(g (cdr l) M (+ c 1) (car l)))
(else (g (cdr l) (max M c) 1 (car l)))))))
(g l 1 1 -2))))
(f '(1 2 33 44 55 66 77 88 98)) ;=> 2
(f '(1 3 23 24 30 48 49 70 80)) ;=> 2
(f '(6 9 50 51 52 72 81 83 92)) ;=> 3
where
lis thelistMis theMaximum consecutive number count found so farcis thecurrent consecutive number countpis theprevious number in the list