| Bytes | Lang | Time | Link |
|---|---|---|---|
| 014 | Uiua | 250821T203208Z | phidas |
| 027 | Haskell | 250814T222140Z | Hayden B |
| 016 | APLNARS | 250817T161915Z | Rosario |
| 014 | x8664 machine code | 250817T134543Z | m90 |
| 006 | 05AB1E | 250815T012608Z | Lucenapo |
| 005 | Vyxal 3 | 250814T175309Z | pacman25 |
| 043 | Maple | 250815T164919Z | dharr |
| 075 | Nibbles | 250815T125717Z | Adam |
| 047 | Google Sheets | 250814T184436Z | doubleun |
| 023 | APL+WIN | 250815T090128Z | Graham |
| 039 | JavaScript V8 | 250814T172924Z | Arnauld |
| 025 | PARI/GP | 250815T015150Z | alephalp |
| 013 | Charcoal | 250814T204410Z | Neil |
| 017 | Dyalog APL | 250814T202402Z | Aaron |
| 038 | Python 3.8 | 250814T182720Z | Jonathan |
| 005 | Jelly | 250814T175853Z | Jonathan |
Uiua, 15 14 Bytes
⨬1¯1◿2+⊙(<⇡)⊸⇡
Adds one to each index before modulo. If we ignore the requirement for [1 -1 1...] and use [1 0 1...] it can be 10 bytes.
◿2+⊙(<⇡)⊸⇡
Haskell, 45 32 27 bytes
-5 more bytes thanks to Jonathan Allan using a list comprehension:
a!b=[(-1)^x|x<-[0..a],x/=b]
This solution defines the function as an infix operator (recommended by Wheat Wizard) and uses a (-1)^ rather than a mod (recommended by xnor), saving 13 bytes:
a!b=((-1)^)<$>[2..b+1]++[b+1..a]
The original 45-byte solution (although it outputs an incorrect-length answer):
f a b=pred<$>(*2)<$>(`mod`2)<$>[1..b]++[b..a]
APL(NARS), 16 chars
{¯1*∊1⍵+⍳¨⍵,⍺-⍵}
test:
s←{¯1*∊1⍵+⍳¨⍵,⍺-⍵}
6 s 3
┌6─────────────┐
│ 1 ¯1 1 1 ¯1 1│
└~─────────────┘
7 s 6
┌7─────────────────┐
│ 1 ¯1 1 ¯1 1 ¯1 ¯1│
└~─────────────────┘
8 s 2
┌8───────────────────┐
│ 1 ¯1 ¯1 1 ¯1 1 ¯1 1│
└~───────────────────┘
Here is possible even n s 0, for example 7 s 0, that would have as result ¯1 1 ¯1 1 ¯1 1 ¯1. But in the question the 2th argument it seems >0. That would mean 1+iota 0 is zilde, and zilde is cancelled from the array from ∊.
x86-64 machine code, 14 bytes
B0 01 AA FF CA 74 02 F6 D8 FF CE 75 F5 C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes an address at which to place an array of signed 8-bit integers in RDI, n in ESI, and d in EDX.
In assembly:
f: mov al, 1 # Set AL to 1.
r: stosb # Write AL to the array, advancing the pointer.
dec edx # Subtract 1 from EDX, counting down from d.
jz s # Jump if the result is zero.
neg al # (Otherwise) Negate AL.
s: dec esi # Subtract 1 from ESI, counting down from n.
jnz r # Jump, to repeat, if the result is nonzero.
ret # Return.
Vyxal 3, 5 bytes
⑧↸ʁQ*
-6 porting Jelly but I should probably make Q act on the deeper element
-1 because Q now works on deeper elements if one is a list
Maple, 43 bytes
(a,b)->subsop(b+1=(),[seq((-1)^i,i=0..a)]);
Nibbles, 7.5 bytes
:.`<@.,$^-1$*~
Haven’t used Nibbles in a long time. Surely this can be golfed somehow…
:.`<@.,$^-1$*~
,$ Range from 1 to n
. ^-1$ Apply (-1)^x to each element
`<@ Take the first d elements
. *~ Negate each element
: Join with the rest of the list
Google Sheets, 47 bytes
=index(let(s,sequence(A1),2*isodd(s-(s>B1))-1))
Put \$n\$ in cell A1 and \$d\$ in B1.
Gets a vertical array. The formula in the screenshot uses torow() to get it horizontal instead.

APL+WIN, 23 bytes
Prompts for n followed by d:
(n↑v),¯1×(n←⎕)↓v←⎕⍴1 ¯1
JavaScript (V8), 39 bytes
Expects (n)(d).
n=>d=>{for(q=-1;n--;)print(q=d--?-q:q)}
Or the less-readable-so-that-it-looks-smarter version:
n=>q=d=>{for(;n--;q^=!!--d)print(-q|1)}
PARI/GP, 25 bytes
f(n,d)=powers(-1,n)[^d+1]
PARI/GP has the special syntax a[^n] to remove the n-th element (1-indexed) from the list a.
Charcoal, 13 bytes
NθIENX±¹⁺ι›ιθ
Try it online! Link is to verbose version of code. Takes 0-indexed d as the first input and n as the second input. Explanation:
Nθ First input as a number
N Second input as a number
E Map over implicit range
¹ Literal integer `1`
± Negated
X Raised to power
ι Current value
⁺ Plus
ι Current value
› Is greater than
θ First input
I Cast to string
Implicitly print
Dyalog APL, ⎕IO←0, 17 chars
{∊(⊂⍬)@⍵⊢¯1*⍳⍺+1}
⍳⍺+1 Get one more indices than the left arg (because we're gonna remove one)
¯1* -1 raised to those values to flop back and forth
(⊂⍬)@⍵ Put an empty array at the index specified by the right arg
∊ And flatten
Python 3.8, 39 38 bytes
-1 thanks to Adamátor!
lambda n,d:(l:=[-1,1]*n)[1:d+1]+l[d:n]
An unnamed function that accepts the length, n, and the duplicated 1-index, d, and returns the almost-alternating list.
How?
lambda n,d: # a function accepting `n` and `d` e.g. n=5, d=3
(l:= ) # inline assignment to `l` of:
[-1,1]*n # the list [1,-1] times `n` l=[-1,1,-1,1,-1,1,-1,1,-1,1]
[1:d+1] # slice `l` from 0-index 1 inclusive,
# to 0-index `d+1` exclusive [ 1,-1,1]
l[d:n] # slice `l` from 0-index `d` inclusive,
# to 0-index `n` exclusive [ 1,-1]
+ # concatenate these [1,-1,1,1,-1]
Jelly, 6 5 bytes
Żḟ⁹-*
A dyadic Link that accepts the length, n, on the left and the duplicated 1-index, d, on the right, and yields the almost-alternating list.
Try it online! Or see the test-suite.
How?
Żḟ⁹-* - Link: n; d e.g. n=5; d=3
Ż - zero-range {n} [ 0, 1, 2, 3, 4, 5]
⁹ - chain's right argument -> d 3
ḟ - filter-discard [ 0, 1, 2, 4, 5]
- - literal minus one -1
* - {-1} exponentiate {filtered} [ 1,-1, 1, 1,-1]
Or entirely tacitly (no ⁹), also in 5 bytes:
Żḟ*@-
Or (1-indexing into [-1, 1] cyclically) ...
ŻḟịØ-