title: May the first be with you

text:
# Question
Title: May the first be with you
Write a program to replace all occurrences of "force" with "first" and all occurrences of "first" with "force", keeping the original case for all character positions:

```
"ForcefoRcefOrcE" -> "FirstfiRstfIrsT"
"FirstfiRstfIrsT" -> "ForcefoRcefOrcE"

```

The rest of the string must stay unchanged, and so running your program twice shall return the original string:

```
"thirst of forces" -> "thirst of firsts" -> "thirst of forces"

```

Your program should work on any initial string. So as a hint, you better avoid using magic characters as intermediate representation, because if you try a three pass replacement (`"force" -> "zzzzz", "first" -> "force", "zzzzz" -> "first"`), it will fail on strings containing `"zzzzz"`.

You should support the full range of characters allowed in a definition of a String by your programming language (in most cases, it's Unicode). Example, using JSON-style representation for non-printable characters (\u + 4 digits):

```
"\u0000\u0001\u0002\u0003the Force of the firsT"
                     |
                     V
"\u0000\u0001\u0002\u0003the First of the forcE"

```

# Answer
# Retina, 33 bytes

```
iT`\OC\E\ocetsiTSI`Ro`first|force

```

Try it online!

Edit: Saved 5 bytes thanks to @MartinEnder for pointing out what `Ro` does.
> 8 votes

# Answer
## JavaScript (ES6), ~~93~~ 88 bytes

```
f=
s=>s.replace(/force|first/gi,s=>s.replace(/./g,c=>s[s.search(c)^1]||c,s="oicsetOICSET"))
```

```
<textarea oninput=o.textContent=f(this.value)></textarea><pre id=o>
```

Edit: Saved 5 bytes by optimising the unchanged letter case.
> 10 votes

# Answer
# Perl 5, 52 bytes

51 bytes of code + `-p` flag.

```
s%first|force%$&=~y/oceOCEistIST/istISToceOCE/r%eig

```

Try it online!

Nothing too crazy going on. Find the occurrences of `force` and `first` non-case-sensitive (`s%force|first%%gi`), and then transliterates the characters to convert one to the other.
> 8 votes

# Answer
# PHP, 88 Bytes

Online Versions

```
<?=preg_replace_callback("#first|force#i",function($t){return$t[0]^first^force;},$argn);

```

# PHP, 110 Bytes

```
<?=preg_replace_callback("#first|force#i",function($t){return strtr($t[0],iIsStToOcCeE,oOcCeEiIsStT);},$argn);

```
> 7 votes

# Answer
# APL (Dyalog), 61 bytes

Requires `⎕IO←0` which is default on many systems. Can be four *characters* shorter using the Unicode symbol  instead of `⎕OPT` .

```
(t←'force' 'first')⎕R{(m∊⎕A)c¨t⊃⍨~t⍳(c←819⌶)⊂m←⍵.Match}⎕OPT 1

```

Try it online!
> 7 votes

# Answer
# Java 10, ~~318~~ ~~310~~ ~~284~~ ~~279~~ ~~277~~ ~~271~~ 256 bytes

```
String c(String s){var x=s.toLowerCase();int i=x.indexOf("force")+1,j=x.indexOf("first")+1,t=i>0&j>i|j<1?1:-1;var y=s.toCharArray();return-j<i?s.substring(0,i=t<0?j:i)+(y[i++]-=t*6)+y[i++]+(y[i++]+=t*16)+(y[i++]+=t*15)+c(s.length()>i?s.substring(i):""):s;}

```

-34 bytes thanks to *@ceilingcat*.

Try it online.

**Explanation:**

```
String c(String s){                     // Recursive method with String as both parameter and return-type
  var x=s.toLowerCase();                //  Temp String as lowercase of the input
  int i=x.indexOf("force")+1,           //  Index of "force" + 1 (becomes 0 if NOT present; >=1 if it is present)
      j=x.indexOf("first")+1,           //  Index of "first" + 1 (becomes 0 if NOT present; >=1 if it is present)
      t=i>0&j>i|j<1?1:-1;               //  Temp integer: -1 if "force" is found first; 1 if "first" is found first
  var y=s.toCharArray();                //  Convert the string to a character-array
  return-j<i?                           //  If either "force" or "first" is found:
    s.substring(0,i=t<0?j:i)            //   Return the substring before that (if any) + ('f' or 'F')
     +(y[i++]-=t*6)                     //   + 'i'↔'o' or 'I'↔'O'
     +y[i++]                            //   + 'r' or 'R'
     +(y[i++]+=t*16)                    //   + 's'↔'c' or 'S'↔'C'
     +(y[i++]+=t*15)                    //   + 't'↔'e' or 'T'↔'E'
     +c(s.length()>i?s.substring(i):"") //   + a recursive call for the rest of the input-String (if any)
   :                                    //  Else:
    s;}                                 //   Return the input-String as is

```
> 4 votes

# Answer
## CJam, 66 bytes

```
qY5m*_"force"{f{_eu}3/:z{~?}f%}:K~\"first"K.{[\]:P~@\/\f/P~@\f*\*}

```

Goes through every case variation of "first" and "force" and tries to split on it. If it can, it then joins it back with the reverse words.

Pseudocode:

```
input_chars = list(read_all_input()) # CJam: q
power = cartesian_power(2, 5) # CJam: Y4m*_
def case_variations(s): # CJam: {...}:K
    temp0 = [[i, j, upper(j)] for i, j in zip(power, s)] # CJam: f{_eu}3/
    temp1 = map(transpose, temp0) # CJam: :z
    ret = []
    for i in ret:
        for j in i: # CJam: {...}f%
            ret.append(j[1] if j[0] else j[2]) # CJam: ~?
    return ret
force_var = K("force") # CJam: "force"{...}:K~
first_var = K("first") # CJam: \"first"K
for force, first in zip(force_var, first_var): # CJam: .{...}
    current = [force, first] # CJam: [\]:P~
    input_chars = list_split(input_chars, force) # CJam: @\/
    input_chars = [list_split(i, first) for i in input_chars] # CJam: \f/
    input_chars = [list_join(i, force) for i in input_chars] # CJam: P~@\f*
    input_chars = list_split(input_chars, first) # CJam: \*

```
> 3 votes

# Answer
# Jelly, ~~37~~ 36 bytes

*Is there is a way to use a reduce across slices of length 5 instead?*

```
®‘©ị“Ɓu“¡Ḣƭ»
Œlœṣ¢œṣ€¢j€¢j¢Œu⁸=ŒuT¤¦

```

**Try it online!**

### How?

```
®‘©ị“Ɓu“¡Ḣƭ» - Link 1 helper that fetches the next word to use: no arguments
®            - recall value from register (initially zero)
 ‘           - increment
  ©          - place the result into the register
    “Ɓu“¡Ḣƭ» - literal dictionary compressed string list ["first","force"]
   ị         - index into (1-indexed and modular)
             - so this link first yields "first", then "force", then "first" and so on.

Œlœṣ¢œṣ€¢j€¢j¢Œu⁸=ŒuT¤¦ - Main link: list of characters, S
Œl                      - convert S to lower case
  œṣ                    - split on sublists equal to:
    ¢                   -   call the last link (1) as a nilad ("first")
     œṣ€                - split €ach on sublists equal to:
        ¢               -   call the last link (1) as a nilad ("force")
         j€             - join €ach with:
           ¢            -   call the last link (1) as a nilad ("first")
            j           - join with:
             ¢          -   call the last link (1) as a nilad ("force")
                      ¦ - apply a link to sparse indices:
              Œu        -   convert to upper case
                     ¤  -   nilad followed by link(s) as a nilad:
                ⁸       -     chain's left argument, S
                  Œu    -     convert to upper case
                 =      -     equal to S? (vectorises)
                    T   -     truthy indexes (indexes at which input is upper case)

```
> 3 votes

# Answer
# MATL, 47 bytes

```
5W:qB!"o['first';'force']@!32*-cZ}_Zt5M_6MoZt|c

```

Try it online!

This uses negative values as the intermediate step, and after the two passes it takes the absolute value.
> 2 votes

# Answer
# Pyth, ~~36~~ 35 bytes

```
K"first"srVjJ"force"mjKcdJcr0QKqVr1

```

Try it online!

Pyth is not especially good at string manipulations.
> 2 votes

# Answer
# C (clang), ~~201~~ ~~183~~ ~~226~~ 214 bytes

Had some bugs... Still needs to be golfed down quite a lot

(saved 12 thanks to ceilingcat)

```
char*s,*p,*q;main(i,v)char**v;{puts(s=v[1]);do{p=strcasestr(s,"first");q=strcasestr(s,"force");if(p&&(!q|p<q))p[1]+=6,p[3]-=16,p[4]-=15;else if(q)q[1]-=6,q[3]+=16,q[4]+=15;s=p&&(!q|p<q)?p:q;}while(s++);puts(v[1]);}

```

Try it online!
> 2 votes

# Answer
# Python 2.7, ~~173~~ 165 bytes

8 bytes saved by quintopia

This one got gross:

```
lambda S:`[(t[0],t[0].upper())[t[1]]for t in zip("".join("first".join(s.replace("first","force")for s in S.lower().split("force"))),[l.isupper() for l in S])]`[2::5]

```

Try it online

Breaking it down step by step:

1. `S.lower().split("force")`: take the string, unify to lowercase, split into substrings separated by `"force"`
2. `s.replace("first","force")for s in <STEP 1>`: Replace all `"first"`'s with `"force"`
3. `_`.join("first".join(<STEP 2>)`[2::5]`_`: replace all `"force"`'s with `"first"` by recombining the `"force"` delineated substrings with `"first"` and rejoin into single string (underscores added to get tick marks correct)
4. `zip(<STEP 3>,[(2,1)[l.isupper()]for l in S])`: zip each character of replaced phrase with case encoding of original string (2 for lowercase, 1 for uppercase)
5. `_`[(t[0],t[0].upper())[t[1]==1]for t in <STEP 4>]`[2::5]`_`: Restore original casing, converts list to string (underscores added to get tick marks correct)
> 2 votes

# Answer
# Flex (lexer), 72 bytes

```
%%
 #define x(a) yytext[a]^=
(?i:first|force) x(1)6;x(3)16;x(4)17;ECHO;

```

To compile and run:

```
flex first.l
gcc lex.yy.c -lfl # -ll on Macs, apparently
./a.out

```
> 2 votes

# Answer
# Python 2, 171 bytes

I wanted to try to do this using built-ins, but it can't beat the messy method with all the splitting and zipping.

```
import re,string as g
def f(s):f="istISTECOeco";l=re.split("(first|force)",s,0,re.IGNORECASE);l[1::2]=[t.translate(g.maketrans(f,f[::-1]))for t in l[1::2]];print"".join(l)

```

I think it's pretty clear what I'm doing here. Split the string on instances of first and force (case-insensitive), replace those instances with versions translated using str.translate, and join it back into a string again.

Try it online!
> 2 votes

# Answer
# Python 3.8 (pre-release), ~~113~~ 102 bytes

*-11 bytes thanx to naffetS*

```
lambda s:re.sub("(?i:first|force)",lambda m:m[0].translate(' tf Io RCE i  rceS TF  Os'*9),s)
import re

```

Try it online!

*Inspired by quintopias answer.*
> 2 votes

# Answer
# Vyxal `r`, ~~21 20 19~~ 18 bytes

```
⇩1∆o‛°⇩"JḂǓ"ƛ÷V;•F

```

Try it Online!

Neat. String compression helps here but honestly the hard part was figuring out how to set up the lists with minimal repetition.

-1 because I never actually use the register in my final answer

-1 by switching the last two elements to remove the duplication

-1 again for redoing how the lists are created

```
⇩1∆o‛°⇩"JḂǓ"ƛ÷V;•F
 1∆o                # "first"
    ‛°⇩             # "force"
⇩      "J           # pair the two and append the lowercase input
         Ḃ          # push ["force", "first", input] and its reverse
          Ǔ         # rotate the reverse so its in the order ["first", "force", input]
           "        # pair the two
            ƛ  ;    # map over each list
             ÷V     # replacement of first/force
                •F  # copy capitalization of input and remove the one identical to the input

```

Created with the help of Luminespire.
> 2 votes

# Answer
# C# 273 bytes

```
string c(string s){var x=s.ToLower();int i=x.IndexOf("force")+1,j=x.IndexOf("first")+1,t=i>0&j>i?0:j>0?1:0;return i>0|j>0?s.Substring(0,t>0?(i=j):i)+(char)(s[i++]-(t>0?-6:6))+s[i++]+(char)(s[i++]+(t>0?-16:16))+(char)(s[i++]+(t>0?-15:15))+c(s.Length>i?s.Substring(i):""):s;}

```

Try it online!

Direct port of Kevin Cruijssen's Java answer, turns out when it comes to getting the char in a string at a given index, C# is much golfier than java (`s[i++]` instead of `s.charAt(i++)`)
> 1 votes

# Answer
# Japt, 41 bytes

```
r"first|force"_d"i1o s1c t1e"¸m²®+ZuÃq}'i

```

Try it online!

This would be considerably shorter if Japt had a sane transliterate function...

Alternate version:

```
r"first|force"_d"io sc te"¸®¬¸²Ã®+ZuÃq}'i

```

Try it online!
> 1 votes

# Answer
# C#, 234 bytes

```
string a(string s){var l=s.ToLower();int f=l.IndexOf("first"),F=l.IndexOf("force"),m=f<F&f>-1?f:F>-1?F:f;return ++m>0?s.Substring(0,m)+(char)(s[m]^6)+s[m+1]+(char)(s[m+2]^16)+(char)(s[m+3]^17)+(s.Length-m>5?a(s.Substring(m+4)):""):s;}

```

Try it online!
> 1 votes

# Answer
# Ruby, 55 bytes

```
gsub(/first|force/i){$&.tr(s="iIsStTEeCcOo",s.reverse)}

```

Try it online!
> 1 votes

# Answer
# 05AB1E, ~~21~~ 20 bytes

```
„€Û‹›#`UVlY¡XY:Xý¹.Ï

```

Try it online!

I could not figure out a way of doing this shorter without using variables, so here we are. I'm sure someone more versed with stack manipulation can figure it out

How?

```
„€Û‹›#`UVlY¡XY:Xý¹.Ï
„€Û‹›#`UV               X = "first" Y="force"
         l              Lower case the input
          Y¡            Split on "force"
            XY:         Replace "first" with "force"
               Xý       Join on "first"
                 ¹.Ï    Apply capitalization of input

```
> 1 votes

# Answer
# Swift, ~~127~~ ~~247~~ ~~225~~ ~~219~~ 208 bytes

```
var b="force",c="first",a=b,d=a,x={(a=$0,d=$3,($1+"").lowercased().ranges(of:$2+"").map{a.replaceSubrange($0,with:zip(d,a[$0]).map{$1.isLowercase ?$0:.init($0.uppercased())})},a).3},f={x(x($0,$0,b,c),$0,c,b)}

```

Built and tested with Swift 5.9, so TIO is out of the window. Here's a JDoodle link instead.

---

**The following explanation is outdated, and refers to this similar, yet slightly less complicated version:**

```
let f={let b="force",c="first",s=$0+"",x={var a=$0+"",d=$2+""
s.lowercased().ranges(of:$1+"").map{a.replaceSubrange($0,with:zip(d,a[$0]).map{$1.isLowercase ?$0:.init($0.uppercased())})}
return a}
return x(x(s,b,c),c,b)}

```

The current revision isn't much different in principle; the major differences are:

* Tuples are exploited to allow for one-line closures, which lets us use implicit `return` statements.
* All declarations are merged into a single `var` statement.
* The `s` variable is no longer used, and has been replaced with an extra parameter to `x`.
* Empty strings used for type inference have been replaced with references to other strings; this saves a byte wherever it's used.

---

With whitespace restored:

```
let f = {
  let b = "force", c = "first", s = $0 + "", x = {
    var a = $0 + "", d = $2 + ""
    s.lowercased().ranges(of: $1 + "").map {
      a.replaceSubrange($0, with: zip(d, a[$0]).map {
        $1.isLowercase ? $0 : .init($0.uppercased())
      })
    }
    return a
  }
  return x(x(s, b, c), c, b)}

```

## Explanation

There's a lot to follow here, so I'll try and step you through the least Swifty Swift code I've ever written. Knowledge of Swift's intricacies is invaluable in understanding this gibberish.

> Fair warning: Most of the identifiers used here are completely arbitrary.

### Line 1

```
let f = { /* ... */ }

```

Since function declarations are generally not small, I've opted for a closure instead, so that the compiler can infer the parameter and return types. Plus, this means we don't have any pesky argument labels to deal with.

### Line 2

```
let b = "force", c = "first", s = $0 + "", x = { /* ... */ }

```

`b` and `c` are simply there to shave off a few bytes, since I use each of those strings twice.

`s` serves a dual purpose. It lets me use the closure parameter (the original string) inside of more closures, since implicit closure parameters can only be used one level deep; and concatenating `""` to it lets the type checker know that it's a `String`, meaning I don't have to specify that directly.

`x` is simply another closure definition. It takes three parameters:

* the string to replace characters in
* the search string
* the replacement string

### Line 3

```
var a = $0 + "", d = $2 + ""

```

`a` is there so I have a mutable copy of the string to work with. I also use the trick I used with `s` above to help out the type checker.

`d` serves the same purpose as `s` above (except for it being used differently, of course).

### Line 4

```
s.lowercased().ranges(of: $1 + "").map { /* ... */ }

```

This lowercases the original string (to effectively make the substring search case insensitive) before finding all ranges containing the search string (`$1`, with `""` appended to assist the type checker). These ranges allow us to refer to discovered words in the string even after we start replacing them.

The call to `map` here serves the same purpose as a `for`-`in` loop, just with less bytes (and a warning since we don't use the result -- if you don't want a warning, replace `map` with `forEach`).

### Line 5

```
a.replaceSubrange($0, with: zip(d, a[$0]).map { /* ... */ }

```

This line does the actual replacing. In this method call, `$0` is one of the ranges containing a word we want to replace. `a[$0]` gets the actual contents of the string in that range.

> `replaceSubrange(_:with:)` unfortunately does not have a non-`mutating` counterpart, which is why we declare `a` above as a `var` instead of a `let`.

The call to `zip` allows us to iterate over the original string and the replacement string simultaneously, allowing us to keep case unchanged. Speaking of which...

### Line 6

```
$1.isLowercase ? $0 : .init($0.uppercased())

```

Here, `$1` is a character from the original string, and `$0` is the corresponding character from the replacement string.

* If `$1` is lowercase, we return `$0` unchanged (since I hardcoded the replacement strings to be lowercase).
* If `$1` is uppercase (or if it isn't cased at all), we return the result of uppercasing `$0` (if it isn't cased, this call does nothing).

> We have to wrap the call to `uppercased()` in a call to `Character.init(_:)` since `uppercased()` on `Character` returns a `String` (blame Swift's Unicode support).

### Line 9

```
return a

```

The only line of this program that actually makes sense.

### Line 11

```
return x(x(s, b, c), c, b)

```

> Remember: `b == "force"`, and `c == "first"`.

* We call our closure with the original string first, telling it to replace `"force"` with `"first"`.
* We then call the closure again with the result, and replace `"first"` with `"force"`.
> We avoid replacing words we've already replaced by referencing the original, unmodified string when finding ranges.
* Finally, we return the result of *that* and leave the miniature catastrophe that is the `f` closure.

## But why, tho?

I've probably overcomplicated a good chunk of this program, but if we're being honest here, Swift was not designed with code golfing in mind. Still, that's what makes golfing fun in the first place!
> 1 votes

# Answer
**C# (269 Bytes)**

```
string s(string z){var u=z.ToUpper();var a=new[]{"FIRST","FORCE"};return String.Join("",u.Split(a,StringSplitOptions.None).Aggregate((c,n)=>c+(u.Substring(c.Length,5)==a[0]?a[1]:a[0])+n).Select((c,i)=>Char.IsLower(z[i])?Char.ToLower(c):c));}

```

yet another c# solution, only the second-smallest because I declared two variables and so can't use lambda syntax. oh well, I had fun. :)

explanation:

* upshift the original string, then split on "FORCE" and "FIRST".
* aggregate the results and on every split, find the five-character substring that was used to split the original string using the length so far of the string being aggregated. if it was "force" make it "first" and vice versa
* select all the characters of the newly created all caps string and check if the original string was lowercase at the same index. if yes, return lowercased character at that index in the new string, otherwise return the uppercase character
> 0 votes

---
Tags: code-golf, string
---


title: Complete a sequence using its distances

text:
# Question
Title: Complete a sequence using its distances
Given \\$A = (a\_1,\dots,a\_k)\ k\ge2 \\$ a sequence of positive integers, in which all elements are different.

Starting from \\$i=2\\$, while \\$a\_i\in A:\\$ *(until the last element)*

* If \\$d=|a\_i-a\_{i-1}|\\$ is not already in \\$A\\$, append \\$d\\$ to \\$A\\$
* Increase \\$i\\$

**Output** the completed sequence.

This is

## Example

```
In:  16 21 11 2

     16 21 11 2 5
      --^
     16 21 11 2 5 10
         --^
     16 21 11 2 5 10 9
            --^
     16 21 11 2 5 10 9 3
              --^
     16 21 11 2 5 10 9 3
                --^
     16 21 11 2 5 10 9 3 1
                   --^
     16 21 11 2 5 10 9 3 1 6
                     --^
     16 21 11 2 5 10 9 3 1 6
                       --^
     16 21 11 2 5 10 9 3 1 6
                         --^
Out: 16 21 11 2 5 10 9 3 1 6

```

# Answer
> 8 votes
# 05AB1E, ~~6~~ 5 bytes

```
Δ¥Ä«Ù

```

Try it online!

```
Δ         until the output doesn't change:
 ¥Ä           absolute differences
   «          concatenate to the original input
    Ù         only keep unique values

```

# Answer
> 6 votes
# Python 2, 57 bytes

```
l=input()
d=0
for i in l:l+={abs(d-i)}-set(l);d=i
print l

```

Try it online!

# Answer
> 6 votes
# Ruby, 40 bytes

```
->a,i=0{a|=[(p(a[i])-a[i+=1]).abs];redo}

```

Try it online!

Takes an array `a` as input and adds unique new elements to it using the set append operator `|=`. Terminates by throwing a rescuable TypeError (which is now allowed) when attempting to subtract beyond the last element of the array. By that point all of the required output has already been printed to STDOUT.

---

# Ruby, 48 bytes

```
->a,i=0{a|=[(p(A).-a[i+=1]||0).abs]while A=a[i]}

```

Try it online!

This version avoids the TypeError at a cost of 8 bytes.

# Answer
> 6 votes
# , ~~17~~ 16 bytes

```
(~.@,2|@-/\])^:_

```

Try it online!

### How it works

```
(~.@,2|@-/\])^:_
(           )^:_   until output does not change
     2   /\]       for each neighboring pair
      |@-          get the absolute difference
    ,              append the result to the list
 ~.@               and remove duplicates

```

# Answer
> 4 votes
# , ~~68~~ 57 bytes

```
v=scan();while(any(F-(F=v)))v=unique(c(v,abs(diff(v))));v

```

Try it online!

*Edit: -9 bytes thanks to Giuseppe, and -2 more from `(F-(F=v))` (had to be tested to see whether that would work...)*

Boringly follows the instructions in the question, in the same order...

# Answer
> 4 votes
# APL (Dyalog Unicode), 14 bytes SBCS

```
{⍵,⍵~⍨|2-/⍵}⍣≡

```

Try it online!

Same method as the J answer; I came up with this on my own :) also ended up being 3 bytes shorter, at least atm.

# Answer
> 3 votes
# Perl 5 -pl, 58 bytes

```
$d=abs$a[$;]-$a[++$;],/\b$d\b/||s/$/ $d/ while(@a=split)>$

```

Try it online!

Ungolfed:

```
# with -pl the special var $_ is initialized with the input line string
while(
       @a = split    # array @a = the numbers currently in the $_ special var
   and $i++ < @a     # and the $i index (counter) is less than the length of @a
                     # in the golfed version $; is used instead of $i
){
  $d = abs $a[$i] - $a[$i-1] # $d = non-zero absolute diff btw element $i and $i-1
  and !/\b$d\b/              # and $d unseen before? \b is "borders" around digits
  and s/$/ $d/               # and if so append space and $d to $_
}
# with -pl the current $_ and \n is printed

```

Run:

```
$ echo 16 21 11 2 | perl -pl program_58_bytes.pl
16 21 11 2 5 10 9 3 1 6

```

# Answer
> 3 votes
# Gaia, 9 bytes

```
e⟨:ọȯ¦|⟩°

```

Try it online!

Note that the documentation states that `ȯ` is `sign of z` when `z` is a numeric type, but it's actually `abs`...

```
e		# eval input as Gaia code (push as a list)
 ⟨	⟩°	# until there's no change:
  :		# dup the list
   ọ		# take the differences
    ȯ¦		# take the absolute values
      |		# and set union

```

# Answer
> 2 votes
# Jelly, ~~8~~ 7 bytes

```
;IAQƲÐL

```

-1 byte thanks to *@JonathanAllan*.

Try it online.

**Explanation:**

```
         # Full program taking a single list argument
     ÐL  # Repeat until the result no longer changes,
    Ʋ    # using the previous four links as monads:
 I       #  Get the forward differences of the current list
  A      #  Take their absolute values
;        #  Merge it to the current list
   Q     #  And uniquify it
         # (after which it is output implicitly as result)

```

# Answer
> 2 votes
# perl -M5.010 -a, 79 bytes

```
@F{@F}=@F;{$d=abs($F[0]-$F[1]);$F{$d}++or$F[@F]=$d;say shift@F;@F>1&&redo}say@F

```

Try it online!

Reads in a line of input, with the integers space separated. Outputs the sequence with each number on a different line.

How does it work? It gets the input in the array `@F` (due to the `-a` command line argument). In the hash `%F` it stores the numbers already in `@F` (here we use the given that all numbers in sequence are positive integers). Then, in a loop, we find the distance between the first two elements of `@F`; if not seen before, we add it to `@F` (and `%F`). We then remove the first element of `@F` and print it. We exit the loop if only one element is left, which is printed just before exiting the program.

The sequence can never contain a zero, as that requires two subsequent elements to be the same, but that is not allowed.

# Answer
> 2 votes
# Charcoal, 21 bytes

```
Ｗ⁻Ｅθ↔⁻κ∧λ§θ⊖λθＦι⊞θκＩθ

```

Try it online! Link is to verbose version of code. Explanation:

```
Ｗ⁻Ｅθ↔⁻κ∧λ§θ⊖λθ

```

Generate the absolute differences between adjacent elements of the input and filter out those appearing in the input.

```
Ｆι⊞θκ

```

While there were any new values, push them to the input and repeat.

```
Ｉθ

```

Output the result.

# Answer
> 2 votes
# K (Kona), 14 bytes

```
{?x,_abs-':x}/

```

Try it online!

Similar to J and APL answers.

# Answer
> 2 votes
# Japt `-h`, ~~10~~ 6 bytes

```
£=âUäa

```

Try it

```
£=âUäa     :Implicit input of array U
£          :Map
 =         :Reassign to U, for the next iteration
  â        :Setwise union of U and
   Uä      :Consecutive pairs of U
     a     :  Reduced by absolute difference

```

# Answer
> 2 votes
# Wolfram Language (Mathematica), ~~57~~ ~~51~~ 46 bytes

*-11 bytes thanks to att!*

```
#//.a_:>Keys@Counts@Join[a,Abs@Differences@a]&

```

Try it online! Pure function. Takes a list as input and returns another list as output. It just repeatedly appends the absolute differences to the end of the list and removes duplicates, until the list no longer changes.

# Answer
> 2 votes
# Pyth, 10 bytes

```
u{+GaM.:G2

```

Try it online!

```
u{+GaM.:G2
u            Apply inner function until a repeat is found, current value G, starting with input
      .:G2     Find all sublists of length 2
    aM         Absolute difference between each pair
  +G           Append to G
 {             Deduplicate
             Implicit print

```

# Answer
> 2 votes
# Wolfram Language (Mathematica), 50 bytes

```
#//.l:_[___,b_,a_,___]:>l<>d/;FreeQ[l,d=Abs[b-a]]&

```

Try it online!

Takes a sequence with any head. If the sequence is already complete, returns it unchanged. Otherwise, returns the completed sequence in a `StringJoin`.

`<>` (`StringJoin`) is short, flattens `List`s, does not operate on integer (non-string) arguments, and is `Flat`. It's also not `Orderless`, so can store ordered data.

```
      _[             ]                           (* match a nonatomic expression with any head, *)
    l:                                           (*  and name it l. it contains: *)
            b_,a_,                               (*   two adjacent numbers, b,a, *)
        ___,      ___                            (*   as close to the front as possible, *)
                            /;                   (* subject to the condition that *)
                              FreeQ[l,d=Abs[b-a] (*  |b-a| is not in l. *)
#//.                  :>l<>d                     (* while the condition holds, append |b-a|. *)

```

# Answer
> 2 votes
# Raku, 45 bytes

```
{|$_,{|keys abs([-] @_[++$,$++])∖@_}...^!*}

```

Try it online!

### Explanation

```
{                                         }    # Anonymous codeblock
                                    ...        # Generating a sequence
 |$_,                                          # Starting with the input
     {                             }           # Where we add to the sequence
            abs([-]            )               # The absolute difference
                    @_[++$,$++]                # Of the next pair of elements
      |keys                     ∖@_            # Set subtracting the sequence
                                    ...!*      # Continue until we run out of pairs

```

# Answer
> 2 votes
# APL(NARS), 22 chars, 44 bytes

```
{⍬≡k←∪⍵∼⍨∣2-/⍵:⍵⋄∇⍵,k}

```

test:

```
  f←{⍬≡k←∪⍵∼⍨∣2-/⍵:⍵⋄∇⍵,k}
  f 16 21 11 2
16 21 11 2 5 10 9 3 1 6
  f 2 3 4 5
2 3 4 5 1

```

# Answer
> 2 votes
# Factor \+ `math.unicode`, 47 bytes

```
[ [ dup differences vabs ∪ ] to-fixed-point ]

```

Try it online!

## Explanation

* `[ ... ] to-fixed-point` Do `[ ... ]` to the input until it stops changing.

```
               ! { 16 21 11 2 }
dup            ! { 16 21 11 2 } { 16 21 11 2 }
differences    ! { 16 21 11 2 } { 5 -10 -9 }
vabs           ! { 16 21 11 2 } { 5 10 9 }
∪              ! { 16 21 11 2 5 10 9 }   (set union)
               ! (etc...)

```

# Answer
> 2 votes
# Uiua SBCS, 14 bytes

```
⍥(◴⊂:⌵↘1°\+.)∞

```

Try it!

```
⍥(◴⊂:⌵↘1°\+.)∞
⍥(          )∞  # to fixed point
           .    # duplicate
      ↘1°\+     # differences
     ⌵          # abs
   ⊂:           # append
  ◴             # deduplicate

```

# Answer
> 2 votes
# J-uby, 36 bytes

```
:!~&:|%(~:each_cons&2|:*&(+:-|:abs))

```

Attempt This Online!

## Explanation

```
:!~ & :| % (~:each_cons & 2 | :* & (+:- | :abs))
:!~ &                                             # Until fixed point...
      :| % (                                   )  #   Union of input and...
                                    +:- | :abs    #     absolute differences of...
            ~:each_cons & 2 | :* & (          )   #     consecutive pairs

```

# Answer
> 2 votes
# PowerShell Core, 73 bytes

```
for($t=$args;$t[$i+2]){$t+=,($d=$t[$i++]-$t[$i]-replace'-')*!($d-in$t)}$t

```

Try it online!

# Answer
> 1 votes
# JavaScript (ES6), 63 bytes

```
f=(a,i=0)=>(v=a[i]-a[++i])?f([...new Set(a).add(v>0?v:-v)],i):a

```

Try it online!

# Answer
> 1 votes
# Io, 59 bytes

Port of the Python 2 answer.

```
method(x,a :=0;x foreach(i,x=x push((i-a)abs)unique;a=i);x)

```

Try it online!

# Answer
> 1 votes
# Retina, ~~84~~ ~~68~~ 66 bytes

*Edit: Saved ~~16~~ 18 bytes thanks to @Neil !! This answer is now mildly competitive :P*

```
\d+
*
^
#
{`#( _+)(_*)(\1(_*).*)
$1$2#$3 $2$4
)D` _+
^ |#

_+
$.&

```

Try it online!

Not the shortest answer to this question, but it was fun to golf anyway :)

**Explanation**

```
\d+
*

```

Convert each input number to unary (using `_`'s)

```
^
#

```

Insert a `#` at the start of the input

```
{`
)`

```

Run the following 2 stages in a loop until the input stops changing:

```
#( _+)(_*)(\1(_*).*)
$1$2#$3 $2$4

```

Take the two numbers to the right of the `#` and remove the maximal number of `_`'s from each. This results in the absolute difference between the two numbers. Append this result to the end of the list.

```
D` _+

```

Deduplicate. If the number that was just added matches a number already in the list, remove it.

```
^ |#

_+
$.&

```

Once the loop breaks, remove the `#` and leading space, and convert back to decimal

# Answer
> 1 votes
# Java (JDK), 90 bytes

```
l->{for(int i=0,v;i+1<l.size();)if(!l.contains(v=Math.abs(l.get(i++)-l.get(i))))l.add(v);}

```

Try it online!

# Answer
> 1 votes
# PHP, ~~96~~ 78 bytes

```
for($a=$argv;$n=$a[++$i];print"$n ")in_array($v=abs($n-$a[$i+1]),$a)?:$a[]=$v;

```

Try it online!

EDIT: 18 bytes saved with the help of Domenico Modica, thanks!

---
Tags: code-golf, sequence, integer
---