| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | Uiua 0.12.0dev.1 | 240604T155258Z | RomanPro |
| 318 | Acc!! | 240524T123003Z | Mukundan |
| 007 | Vyxal | 220705T034825Z | naffetS |
| 062 | R | 220616T184143Z | pajonk |
| 010 | Japt | 171122T084542Z | Shaggy |
| 063 | Haskell | 171122T191727Z | colossus |
| nan | C# .NET Core | 171126T002515Z | Ayb4btu |
| 090 | JavaScript ES6 | 171122T101520Z | edc65 |
| 009 | Brachylog v2 | 171124T221536Z | user7624 |
| 064 | dc | 171123T115105Z | R. Kap |
| 011 | Pyth | 171122T064447Z | Mr. Xcod |
| 009 | Husk | 171122T141607Z | Mr. Xcod |
| 060 | Python 2 | 171122T064909Z | Colera S |
| 008 | Jelly | 171122T150007Z | Erik the |
| 054 | Ruby | 171122T080136Z | G B |
| 046 | J | 171122T115252Z | Galen Iv |
| 013 | MATL | 171122T115854Z | Giuseppe |
| 069 | Haskell | 171122T112633Z | Laikoni |
| 008 | Jelly | 171122T080611Z | DELETE_M |
| 008 | 05AB1E | 171122T065404Z | Emigna |
| 078 | Python 2 | 171122T082356Z | Chas Bro |
| 050 | Wolfram Language Mathematica | 171122T074850Z | JungHwan |
| 147 | Python 2 | 171122T063918Z | Neil |
Acc!!, 318 bytes
Count n while 1 {
_+N%8*8^n
Count i while 0^(_/8^n) {
_*n^n
Count i while n-i {
_+_/n^n/8^i%2*n^(_%n+1)+_/n^n/8^i%4/2
}
((_/n^2-_/n)+0^((_/n^(_%n)%n-_/n^(_%n-1)%n)/(_/n^2%n-_/n%n))*(_/n^(_%n)-_/n^(_%n-1)-_/n^2+_/n))%n+_/n^(_%n)%n*n+_/n%n*n^2
Count i while (_/n-_/n^2+_%n)%n {
Write 49)*(_/n^2%n
Write 9
_+_%n*n^2
}
}
}
Takes input as a newline-separated unary array and outputs a tab-separated unary array.
Explanation
# Format of accumulator:
# input:
# + buffer
# parsing:
# + buffer*n^n
# + arr[0]*n + arr[1]*n^2 + ... # supports upto n - 1 elements
# + len(arr)
# print:
# + curr*n^2
# + end*n
# + diff
#
# NOTE: Each element in 'arr' is stored as one more than its actual value.
Count n while 1 {
_+N%4*4^n # buffer[n] = getchar()%8
# if buffer[n] == '\0':
Count i while 0^(_/4^n) {
# Convert accumulator to parsing type
_*n^n
# Loop to parse into arr
Count i while n-i {
# _
# + n^(_%n+1) # arr[len(arr)] += 1
# + _/n^n/4^i%4/2 # len(arr) = buffer[i] == '\n'
_+n^(_%n+1)+_/n^n/4^i%4/2
}
# Convert accumulator to print type
# _/n%n*n^2 # curr = arr[0]
# + _/n^(_%n)%n*n # end = arr[len(arr) - 1]
# + ... # diff = ((arr[len(arr) - 1] - arr[len(arr) - 2]) or (arr[1] - arr[0])) % n # chosen based on which is closer to zero
_/n%n*n^2+_/n^(_%n)%n*n+((_/n^2-_/n)+0^((_/n^(_%n)%n-_/n^(_%n-1)%n)/(_/n^2%n-_/n%n))*(_/n^(_%n)-_/n^(_%n-1)-_/n^2+_/n))%n
# while curr != end + diff:
Count i while (_/n-_/n^2+_%n)%n {
Write 49)*(_/n^2%n-1 # Print '1' (curr % n - 1) times
Write 9 # Print '\t'
_+_%n*n^2 # curr += diff
}
}
}
Japt, 12 10 bytes
ÌõUÎUäa rm
ÌõUÎUäa rm :Implicit input of array U
Ì :Last element
UÎ :First element
õ :Range [first,last] with step size
Uä : Consecutive pairs of U
a : Reduced by absolute difference
r : Reduce by
m : Minimum
Haskell, 63 bytes
f(a:b:c)|s<-[a,b..last c],all(`elem`s)c=s
f a=r$f$r a
r=reverse
Basically works by trying to build the result from the front and, if that fails, from the back. This uses the fact that the first and last members of the input will always be correct, the fact that deleted members have to be consecutive, and the fact that there will always be at least three items in the input. All I have to do is assume that the second or second-to-last members are accurate and check if it works. Luckily Haskell has really beautiful syntax for creating arithmetic series.
EDIT: thanks to @xnor for pointing out a bug and providing a solution!
C# (.NET Core), 167+13=180 145+13=158 bytes
a=>{int x=a[1]-a[0],y=a[2]-a[1],d=x*x<y*y?x:y,s=Math.Abs((a[a.Length-1]-a[0])/d),i=0,j=a[0];var r=new int[s+1];for(;i<=s;j+=d)r[i++]=j;return r;}
+13 for using System;
Surprisingly, this challenge had more nuance that I initially anticipated.
Acknowledgements
-22 bytes saved due to some neat simplifications from @DLosc.
DeGolfed
a=>{
int x = a[1]-a[0], // difference between first and second numbers
y = a[2]-a[1], // difference between second to last and last numbers
d = x*x < y*y? x : y, // smallest absolute value difference
s = Math.Abs((a[a.Length-1] - a[0]) / d), // number of steps in the reconstructed sequence (not the number of elements)
i = 0, // step position
j = a[0]; // next number in reconstructed sequence
var r = new int[s+1];
// reconstruct the sequence
for(; i <= s; j+=d)
r[i++]=j;
return r;
}
JavaScript (ES6), 92 90
Edit 2 bytes saved thx Arnauld
Easy, as it is enough to check the differences between the first two and the last two. But still unbelievably long.
s=>(e=(z=s.pop(a=s[0]))-s.pop(d=s[1]-a),[...Array((z-(a-=d=e*e>d*d?d:e))/d)].map(_=>a+=d))
Less golfed
s=>{
a =s[0]
b =s[1]
z = s.pop()
y = s.pop()
d = b-a
e = z-y
d = e*e>d*d?d:e
n = (z-a)/d+1
return [...Array(n)].map((_,i) => a + i*d)
}
Test
var F=
s=>(e=(z=s.pop(a=s[0]))-s.pop(d=s[1]-a),[...Array((z-(a-=d=e*e>d*d?d:e))/d)].map(_=>a+=d))
var test=`In: 2 5 8 14 17 Out: 2 5 8 11 14 17
In: 2 5 17 Out: 2 5 8 11 14 17
In: 2 14 17 Out: 2 5 8 11 14 17
In: 21 9 6 3 Out: 21 18 15 12 9 6 3
In: 10 9 5 Out: 10 9 8 7 6 5
In: 1 10 91 100 Out: 1 10 19 28 37 46 55 64 73 82 91 100`.split`\n`
.map(r=>r.split`Out`.map(x=>x.match(/\d+/g)))
test.forEach(([i,k])=>{
var o=F(i.slice(0))
var ok = o+''==k
console.log(ok?'OK':'KO',i+' => '+o)
})
Brachylog v2, 9 bytes
⊆.s₂ᵇ-ᵐ=∧
This is a function submission. The Brachylog interpreter can be made to evaluate a function as though it were a full program by giving it Z as a command line argument; in this case, the input is specified in the format, e.g., [1, 2, 4] and the output is returned in a similar format, e.g. Z = [1, 2, 3, 4]. (Of course, for a function submission, the input and output aren't in any format at all; they're just lists.)
This actually solves a harder problem than the one the OP asked for: it works out the shortest arithmetic sequence of integers containing the specified values in the specified order, regardless of whether the deletions are consecutive or not. Because it uses brute force, it can be very slow if many values are deleted.
Explanation
The program has three main parts.
⊆ finds a supersequence of the input (i.e. a sequence that has the input as a subsequence). When there's more than one possible output from a Brachylog program, the output chosen is the first output in tiebreak order, and the tiebreak order is determined by the first command in the program that has an opinion on it; in this case, ⊆ specifies an order that favours short outputs over long ones. So the output we'll get will be the shortest supersequence of the input that obeys the restrictions in the rest of the program.
. … ∧ is used to output the value it sees as input (i.e. the supersequence in this case), but also assert that a specific condition holds on it. In other words, we're outputting the shortest supersequence that obeys a specific condition (and ignoring any intermediate results used to see if it obeys the condition).
Finally, we have s₂ᵇ-ᵐ =, i.e. "all the deltas of the sequence are equal", the condition we're applying to the output. (The return value from this is a list of deltas, rather than the supersequence itself, which is why we need the . … ∧ to ensure that the right thing is output.)
Brachylog is held back here by not having any builtins that can handle calculation of deltas, applying an operation to overlapping pairs from a list, or the like. Instead, we have to say what we mean explicitly: s₂ᵇ finds all (ᵇ) substrings (s) of length 2 (₂) (the use of ᵇ is required to keep a link between unknowns in the substrings and in the supersequence; the more commonly used ᶠ would break this link). Then -ᵐ does a subtraction on each of these pairs to produce a delta. It's annoying to have to write out five bytes s₂ᵇ-ᵐ for something that most modern golfing languages have a builtin for, but that's the way that codegolf goes sometimes, I guess.
Pyth, 11 bytes
%hS.+SQ}hQe
Thanks to Steven H. for saving a byte!
Pyth, 12 bytes
%.aiF.+Q}hQe
How it works
%.aiF.+Q}hQe ~ Full program.
.+Q ~ Get the deltas.
iF ~ Reduce by GCD.
.a ~ Absolute value.
% ~ Modular. Get every nth element of...
} ~ The inclusive numeric range between...
hQ ~ The first element, and...
e ~ The last element.
Husk, 9 bytes
m←C▼Ẋ≠⁰…⁰
Thanks a lot to H.PWiz for halving the byte count, by pointing that applying … on a list rangifies it!...
Python 2, 104 97 89 83 71 67 60 bytes
Thanks to Chas Brown for saving 4 bytes.
Thanks to ovs for saving 7 bytes.
Input the list by arguments.
lambda a,b,*c:range(a,c[-1],min(b-a,c[0]-b,key=abs))+[c[-1]]
Explanation:
Since the removed are consecutive, it is enough to check the differences between two pairs of consecutive elements.
Ruby, 65 62 55 54 bytes
->l{a,*,b,c=l;[*a.step(c,[l[1]-a,c-b].min_by(&:abs))]}
-1 byte thanks to Justin Mariner
J, 49, 47 46 bytes
(0-[:<./2|@-/\]){.@[&]\({.<.{:)+[:i.{:(+*)@-{.
Inspired by Emigna's solution.
How it works:
(0-[:<./2|@-/\]){.@[&]\({.<.{:)+[:i.{:(+*)@-{. - fork of 3 verbs
({.<.{:)+[:i.{:(+*)@-{. - generates a list in the entire range of values
{: -{. - last minus first element
(+*)@ - adds the signum of the difference
[:i. - makes a list
({.<.{:) - the smallest of first and last elements
+ - adds the offset to the list (translates all elements according to the first one)
(0-[:<./2|@-/\]) - finds the step
2|@-/\] - the absolute differences between all consecutive elements
[:<./ - the smallest one
0- - negate (for splitting)
{.@[&]\ - splits the list from the right verb into left verb's result sublists and takes their first elements
MATL, 13 bytes
1)0GdYkG0)3$:
Explanation:
Consider the example input [2 14 17]:
# implicit input, STACK: [[2 14 17]]
1) # push 1, index, STACK: [2]
0G # push 0, duplicate input, STACK: [2, 0, [2 14 17]]
d # take differences, STACK: [2, 0, [12, 3]]
Yk # get value in [12, 3] nearest to 0, STACK: [2, 3]
G0) # get last element in input, STACK: [2, 3, 17]
3$: # 3-input :, computes 2:3:17, the range from 2 to 17 by 3
# STACK: [[2 5 8 11 14 17]], implicit output.
Haskell, 73 69 bytes
f(h:t)=do(#)<-[(-),(+)];[h,h#minimum(abs<$>zipWith(-)t(h:t))..last t]
Jelly, 8 bytes
ṂrṀmIg/$
Notes:
Only work on some old version of Jelly. (this commit for example) (where
gusefractions.gcd, which have the result sign the same as input sign, instead ofmath.gcd, which always return positive value).The TIO link above is Python 3 TIO link, the Python code consists of Jelly source code from the commit I mentioned above, with the exception of everything (3 files) packed into the same file (for TIO to run) and
dictionary.pyhas been reduced to only some lines. Neverthelessdictionary.pyis unrelated to this answer, as it does not use compressed string. (the“...»construct)
Explanation:
First, because a continuous segment is deleted and at least 3 elements remains, there are two consecutive numbers in the old list remain, and the deltas will all be multiples of the step. Therefore the gcd of the differences (I, increments) list will be the absolute value of the step.
Fortunately the gcd is signed (see note above)
So the program does:
ṂrṀ
An increasing integer range from the Ṃinimum to the Ṁaximum.
m
Modular, pick every n'th element.
Ig/$
Monadic ($) chain combine I (increments, differences) and g/ (reduce gcd over elements of the list). If the increments are positive then the gcd will be positive and the returning list will be left-to-right (increasing), and vice-versa.
05AB1E, 9 8 bytes
Ÿs¥¿Äô€н
Explanation
- Construct the range [first, ..., last] with a difference of +/-1
- Calculate deltas of the input
- Get the absolute value of the gcd of the deltas
- Split the full range in chunks of that size
- Get the first element of each chunk
Saved 1 byte using gcd of deltas instead of min delta, inspired by user202729
Python 2, 78 bytes
lambda a:range(a[0],a[-1],[min,max][a[0]>a[1]](a[1]-a[0],a[-1]-a[-2]))+[a[-1]]
Wolfram Language (Mathematica), 50 bytes
Range[#&@@#,#[[-1]],#&@@Differences@#~SortBy~Abs]&
Python 2, 147 bytes
from fractions import*
a=input()
b=[j-i for i,j in zip(a[:-1],a[1:])]
l=min(gcd(i,j)for i in b for j in b)
print list(range(a[0],a[-1]+l/abs(l),l))