# 打印Thue-Morse序列中的差异

10

Thue-Morse序列类似于01101001

``````0110100110010110
|__|_||__||_|__|
2  1 0 2 01 2          <------------Print this!
``````

6

5
xnor

2

# 果冻，9个字节

``````;¬\$‘¡TI’ḣ
``````

### 怎么运行的

``````;¬\$‘¡TI’ḣ  Main link. Argument: n

\$        Create a monadic chain that does the following to argument A (list).
¬         Negate all items of A.
;          Concatenate A with the result.
‘¡      Execute that chain n + 1 times, with initial argument n.
T     Get all indices of truthy elements (n or 1).
I    Compute the differences of successive, truthy indices.
’   Subtract 1 from each difference.
ḣ  Keep the first n results.
``````

4

# 蟒3 2，104个92 88 84字节

``````n=input()
s="2"
while len(s)<n:s="".join(`[1,20,210][int(i)]`for i in s)
print s[:n]``````

3

# 朱莉娅，56 50字节

``n->(m=1;[m=[m;1-m]for _=0:n];diff(find(m))[1:n]-1)``

3

## PowerShell，102字节

``````filter x(\$a){2*\$a+([convert]::toString(\$a,2)-replace0).Length%2}
0..(\$args[0]-1)|%{(x(\$_+1))-(x \$_)-1}
``````

`filter`计算这个数字。例如，`x 4`将给出`9`。然后我们简单地循环`0`到输入`\$args[0]`，减去，`1`因为我们索引为零，并且循环的每次迭代都会打印出下一个数字与当前数字之间的差。输出将添加到管道中，并使用换行符隐式输出。

### 例

``````PS C:\Tools\Scripts\golfing> .\print-the-difference-in-the-thue-morse.ps1 6
2
1
0
2
0
1
``````

3

``````l=2:(([[0..2],[0,2],[1]]!!)=<<l)
(`take`l)
``````

3

# Mathematica，79 68 70字节

``````(Differences[Join@@Position[Nest[#~Join~(1-#)&,{0},#+2],0]]-1)[[;;#]]&
``````

1

3

# MATL，14 11字节

``````Q:qB!Xs2\dQ
``````

``````Q:q    % take input n implicitly and generate row vector [0,1,...,n]
B!     % 2D array where columns are the binary representations of those numbers
Xs     % sum of each column. Gives a row vector of n+1 elements
2\     % parity of each sum
d      % consecutive differences. Gives a row vector of n elements
Q      % increase by 1. Display implicitly
``````

CalculatorFeline

@CatsAreFluffy您完全正确。完成
Luis Mendo

2

# 05AB1E，14 13字节

``````ÎFDSÈJJ}¥1+¹£
``````

``````Î              # Push 0 and input
F     }       # Do the following n times
DS           # Duplicate and split
È          # Check if even
JJ        # Join the list then join the stack
¥1+    # Compute the differences and add 1
¹£  # Return the [0:input] element
``````

2

## Python，69个字节

``````t=lambda n:n and n%2^t(n/2)
lambda n:[1+t(i+1)-t(i)for i in range(n)]``````

`i`序列的th项是`1+t(i+1)-t(i)`，其中`t`是Thue-Morse函数。该代码以递归方式实现，比

``t=lambda n:bin(n).count('1')%2``

1

# Mathematica，65个字节

``````SubstitutionSystem[{"0"->"012","1"->"02","2"->"1"},"0",#][[;;#]]&
``````

CalculatorFeline

1

# Mathematica，58个字节

``````Differences[Nest[Join[#,1-#]&,{0},#]~Position~0][[;;#]]-1&
``````

1

CalculatorFeline

@catsarefluffy我确实适应了您的想法以生成序列（通过剪切infix运算符对其进行搜索），但觉得此处将其转换为预期输出的方法非常不同，并且比建议的编辑更适合于新答案。

@catsarefluffy我刚刚看到了您的修改。当我这样做时，我最后看到的是它的原始形式。我将删除此答案，但您只需要相信我，它是独立的:)
Simmons

`1;;#`可以简单地代替`;;#`
LegionMammal978 '16

CalculatorFeline

1

# Perl，45 + 2 = 47字节

``````\$_=2;s/./(1,20,210)[\$&]/ge until/.{@F}/;say\$&
``````

``````\$ perl -pa morse-seq.pl <<< 22
2102012101202102012021
``````

@ Sherlock9的端口答案

Ton节省了9个字节

`-a`选项为您提供了输入的免费副本，因此`\$_=2;s/./(1,20,210)[\$&]/ge until/.{@F}/;\$_=\$&`
Ton Hospel

@TonHospel太好了，简直不敢相信我没想到:-) 如果我假设Perl> v5.18`-p``-E``say\$&`
andlrc

1

## JavaScript（ES6），73 67字节

``````f=(n,s="2")=>s[n]?s.slice(0,n):f(n,s.replace(/./g,c=>[1,20,210][c]))
``````

@ Sherlock9的答案的端口。

`!s[n]`在地方工作`s.length<n`？或者只是`s[n]``?:`倒？

1

## CJam（19个字节）

``````1ri){2b:^}%2ew::-f-
``````

``````ri_2a{{_*5*)3b~}%}@*<
``````

（警告：缓慢）。这编码了重写规则

``````0  ->  1
1  ->  20
2  ->  210
``````

``````x -> (5*x*x + 1) in base 3
``````

0

# Ruby，57个字节

xnor的Python答案端口。更改主要在于三元语句，`t`而不是`and`由于`0`在Ruby中是真实的，而使用`(1..n).map``1+t[i]-t[i-1]`保存字节与直接导入列表推导相比。

``````t=->n{n<1?n:n%2^t[n/2]}
->n{(1..n).map{|i|1+t[i]-t[i-1]}}``````

`0`是真的吗？这是如何运作的？？
CalculatorFeline

@CatsAreFluffy以我的经验，很差
Sherlock9

0

# Mathematica（几乎非语言），107110字节

``````({0}//.{n__/;+n<2#}:>{n,{n}/.x_:>(1-x)/._[x__]:>x}//.{a___,0,s:1...,0,b___}:>{a,+s/.(0->o),0,b}/.o->0)[[;;#]]&
``````

``````({\$'-\$'}//.{\$__/;+\$/#
<(\$'-\$')!+(\$'-\$')!}:>
{\$,{\$}/.\$\$_:>((\$'-\$')
!-\$\$)/.{\$\$__}:>\$\$}//.
{\$___,\$'-\$',\$\$:(\$'-\$'
)!...,\$'-\$',\$\$\$___}:>
{\$,+\$\$/.(\$'-\$'->\$\$\$\$)
,\$'-\$',\$\$\$}/.\$\$\$\$->\$'
-\$')[[;;#]]
``````

Alex A.

CalculatorFeline

Bytesave：使用`{x__}`而不是`_[x__]`
CalculatorFeline

CalculatorFeline