| Bytes | Lang | Time | Link |
|---|---|---|---|
| 147 | APLNARS | 250929T171212Z | Rosario |
| 077 | JavaScript Node.js | 250929T094742Z | l4m2 |
| 018 | 05AB1E | 250922T073932Z | Kevin Cr |
| 109 | Maple | 250921T154655Z | dharr |
| 072 | Python 3.8 | 250921T170728Z | Jonathan |
| 113 | Lua | 250921T162446Z | Someone |
| 033 | 05AB1E | 250920T033051Z | darthbee |
| 093 | Python 3 | 250920T044629Z | Ted |
| 034 | Charcoal | 250920T085150Z | Neil |
| 196 | Python3 | 250919T155817Z | Ajax1234 |
| 091 | JavaScript ES6 | 250919T181659Z | Arnauld |
APL(NARS), 147 chars
r←f w;a;b;k;d;s;i
k←≢w⋄a←0⋄r←⍬⋄→3
r←a,b⋄→0
→7×⍳k<a+←1⋄b←¯1
i←s←0⋄→3×⍳k<b+←1
→6×⍳k<d←b+1+i×a⋄s+←w[d]⋄i+←1⋄→5
→2×⍳a=s⋄→4
→0×⍳k<1⋄a←↑w⋄→0×⍳0≥a⋄b←0⋄→2
// +/18 16 9 16 17 32 11 28 = 147
f: Input one array of integers, output one array of integers.
It seen that as C or some structural languges it would use 2 [for] loops, one for change 'a', one for change 'b', and one hidden loop for find the sum. The break is reach when sum is "a" in that case return the couple a,b. The case a←↑w b←0 here is a separate case it is out of all the loops. It seems to me it is miss in the question the examples of the case "no solution is possible".
ungolfed:
r←f w;a;b;k;d;s;i
1: k←≢w⋄a←0⋄r←⍬⋄→3
2: r←a,b⋄→0
3: →7×⍳k<a+←1⋄b←¯1
4: i←s←0⋄→3×⍳k<b+←1
5: →6×⍳k<d←b+1+i×a⋄s+←w[d]⋄i+←1⋄→5
6: →2×⍳a=s⋄→4
7: →0×⍳k<1⋄a←↑w⋄→0×⍳0≥a⋄b←0⋄→2
test:
f ,1
1 0
f ,9
9 0
f 9 0 0 0 0 0
9 0
f 2 1 3 1 2
2 1
f 0 0 0 0 1 0
1 0
f 0 7 1 4 ¯1 3 7 ¯5
2 3
f 1 2 ¯2 0 0 0 0 0 0
1 0
f 9 1 8 ¯1 7 2 6 0 5
2 1
f 0 2 0 ¯1 0 0 0 0 1
1 4
f 1 2 3 4 5 6 7 8 ¯2
4 0
f ¯1 0 0 0 4 0 0 0 0
4 4
f ¯1 0 0 0 0 0 0 0 1
1 1
f ¯1 0 0 0 0 0 0 0 ¯1
f 0 0 0 0 0 0 0 0 ¯1
JavaScript (Node.js), 77 bytes
X=>(g=a=>X.some((S,i)=>(h=_=>1/X[i+=a]?h(S+=X[i]):S==a)(I=i))?[a,I]:g(-~a))()
05AB1E, 18 bytes
ā<VZLYâ.ΔYβYÃèOyнQ
Try it online or verify all test cases.
Explanation:
ā # Push a list in the range [1, (implicit) input-length]
< # Decrease each by 1 to make it 0-based: [0,length)
V # Pop and store this list in variable `Y`
Z # Push the maximum of the input-list
L # Pop and push a list in the range [1,max]
Y # Push list `Y`
â # Cartesian product to create all possible [a,b]-pairs
.Δ # Pop and find the first [a,b]-pair that's truthy for:
Yβ # For each value `Yv` in `Y`: calculate `a*Yv+b`
# (aka, for each value `Yv` in `Y`: convert the [a,b]-pair from a
# base-`Yv` list to a base-10 integer)
YÃ # Only keep the values also present in list `Y`
è # Get the values at those indices in the (implicit) input-list
O # Sum them together
yн # Push the first value of the pair: `a`
Q # Check if the sum equals this `a`
# (after which the found pair is output implicitly as result)
Maple, 109 bytes
proc(s)n:=nops(s);for a do seq(`if`(add(`if`((f:=a*x+b+1)>n,0,s[f]),x=0..n)=a,RETURN([a,b]),0),b=0..n)od end;
f:=proc(s)
n:=nops(s);
for a do # loop over a
seq( # loop over b from 0..n
`if`(add(`if`((f:=a*x+b+1)>n,0,s[f]),x=0..n)=a,RETURN([a,b]),0)
,b=0..n)
od
end;
0-indexed version (even though lists are 1-indexed in Maple). Loops over all a and b until acceptable [a,b] is found; then exits via premature RETURN. The old style RETURN (not return) can be used in an if expression allowing the inner b for loop to be converted to a seq.
Python 3.8, 72 bytes
Same approach as Ted in their Python 3 answer, with recursion and walrus use.
f=lambda a,c=0,m=1:[m,c]*(sum(s:=a[c::m])==m)or f(a,(i:=s>[])*c+i,m-i+1)
A recursive function that accepts a list of integers, \$a\$, with at least one positive integer and returns the minimal integers \$[m, c]\$ forming the linear relationship \$f(x) = m x+c \mid m \gt 0, c \ge 0\$ that aligns with \$a\$.
Try it online! (Test-suite created by Ted.)
How?
f=lambda a,c=0,m=1: # f is a function accepting a and c & m, initially 0 & 1
[m,c] # make the pair [m, c]
sum(s:=a[c::m]) # sum the slice, s, starting at index c in steps of m
==m # does that equal m?
*( ) # multiply [m,c] by that -> [m, c] if so else []
or f( , , ) # or (i.e. if that is []) call f again...
i:=s>[] # set i = is the slice, s non-empty?
a # a = a
(i )*c+i # c = 0 if the slice, s, is empty else c+1
m-i+1 # m = m + 1 if the slice, s, is empty else m
Lua, 113 bytes
function(l)for i=1,9 do for j=1,9 do s=0 for k=j,9,i do s=s+(l[k]or 0)end if i==s then return{i,j}end end end end
…or look at the uncompressed source code
Explanation
function(l)for i=1,9 do for j=1,9 do s=0 for k=j,9,i do s=s+(l[k]or 0)end if i==s then return{i,j}end end end end
function(l) end -- input the list
for i=1,9 do end -- loop through possible slopes (covers all test cases)
for j=1,9 do end -- loop through intercepts (covers all test cases)
s=0 -- initialize sum variable
for k=j,9,i do s=s+(l[k]or 0)end -- sum all relevant values (lua for loops are cool)
if i==s then return{i,j}end -- if the sum matches our slope, return the values
💎
Created with the help of Luminespire.
05AB1E, 33 bytes
Oƒ¼gFN©\εN¾*®+}ʒIg‹}ʒd}èO¾Qi¾¸®ªq
Explanation: Iterate through every possible a (constrained by the sum of the input) and b (constrained by the length of the input), get the number from every viable index, sum that, and if it's equal to a, print and quit the program.
Oƒ¼gFN©\εN¾*®+}ʒIg‹}ʒd}èO¾Qi¾¸®ªq
Oƒ¼gFN©\ ; double for loops to iterate through all possible values of a and b
Oƒ ; for loop sum of the input list
¼ ; increment the counter (this is a)
gF ; for loop over the length of the input list
N©\ ; set a variable to the current index (this is b)
εN¾*®+} ; apply ax+b to every item in the input array
εN } ; for the index of every item in the input array
¾* ; multiply by a
®+ ; add b
ʒIg‹}ʒd} ; filter out invalid function results
ʒIg‹} ; filter out numbers greater than the length of the input
ʒd} ; filter out negative numbers
èO¾Q ; check if the numbers indexed by the remaining valid function results sum to a
è ; transform every function result into the item it indexes
O ; sum them
¾Q ; duplicate this number on the stack and compare it to a
i¾¸®ªq ; if the sum is equal to a, return a and b
i ; if the sum is equal to a
¾¸ ; wrap a in a list
®ª ; append b to this list
q ; quit the program, returning [a, b]
Python 3, 93 bytes
- 6 bytes saved @Neil
lambda m:[[a,b]for a in range(1,max(len(m),*m)+1)for b in range(len(m))if sum(m[b::a])==a][0]
Uses the fact that the python slice notation is effectively a linear function.
Charcoal, 34 bytes
I⌊ΦΣE⌈⁺⟦Lθ⟧θEθ⟦⊕ιμ⟧⁼§ι⁰Σ✂θ§ι¹Lθ§ι⁰
Attempt This Online! Link is to verbose version of code. Explanation: Port of @Ted's Python answer.
θ Input array
L Take the length
⟦ ⟧ Make into list
θ Input array
⁺ Concatenate
⌈ Take the maximum
E Map over implicit range
θ Input array
E Map over elements
ι Outer value
⊕ Incremented
μ Current index
⟦ ⟧ Make into pair
Σ Flatten 1 level
Φ Filter over `[a, b]` pairs
θ Input array
✂ Sliced from
§ι¹ `b` value to
θ Input array
L Length
§ι⁰ Step `a` value
Σ Take the sum
⁼ Equals
§ι⁰ `a` value
⌊ Take the minimum
I Cast to string
Implicitly print
Python3, 196 bytes
def f(l):
q,s=[T:=(0,0)],[T]
for a,b in q:
x,t=0,0
if a:
while 0<=a*x+b<len(l):t+=l[a*x+b];x+=1
if t==a:return[a,b]
v=[u for j in[0,1]for J in[0,1]if(u:=(a+j,b+J))not in s];q+=v;s+=v
JavaScript (ES6), 91 bytes
f=a=>a.some((_,p)=>p*a.some((_,q)=>!a.reduce(t=>t-~~a[q+=p],p,o=[p,q],q-=p)))?o:f([...a,0])