g | x | w | all
Bytes Lang Time Link
014Uiua250821T203208Zphidas
027Haskell250814T222140ZHayden B
016APLNARS250817T161915ZRosario
014x8664 machine code250817T134543Zm90
00605AB1E250815T012608ZLucenapo
005Vyxal 3250814T175309Zpacman25
043Maple250815T164919Zdharr
075Nibbles250815T125717ZAdam
047Google Sheets250814T184436Zdoubleun
023APL+WIN250815T090128ZGraham
039JavaScript V8250814T172924ZArnauld
025PARI/GP250815T015150Zalephalp
013Charcoal250814T204410ZNeil
017Dyalog APL250814T202402ZAaron
038Python 3.8250814T182720ZJonathan
005Jelly250814T175853ZJonathan

Uiua, 15 14 Bytes

⨬1¯1◿2+⊙(<⇡)⊸⇡

pad

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

Try it online!

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.

05AB1E, 7 6 bytes

ÝIK®sm

Try it online!

Vyxal 3, 5 bytes

⑧↸ʁQ*

Vyxal It Online!

-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$*~

Attempt This Online!

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.

screenshot

APL+WIN, 23 bytes

Prompts for n followed by d:

(n↑v),¯1×(n←⎕)↓v←⎕⍴1 ¯1

Try it online! Thanks to Dyalog Classic

JavaScript (V8), 39 bytes

Expects (n)(d).

n=>d=>{for(q=-1;n--;)print(q=d--?-q:q)}

Try it online!

Or the less-readable-so-that-it-looks-smarter version:

n=>q=d=>{for(;n--;q^=!!--d)print(-q|1)}

Try it online!

PARI/GP, 25 bytes

f(n,d)=powers(-1,n)[^d+1]

Attempt This Online!

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.

Try it online!

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:

Żḟ*@-

Try it online!


Or (1-indexing into [-1, 1] cyclically) ...

ŻḟịØ-

Try it online!