递归二进制描述


14

递归二进制描述

最近,我对OEIS做出了第一个贡献,方法是将b文件扩展并添加到序列A049064中。该序列以开头0,然后通过给出最后一项的“二进制描述”来得出下一个值。

例如,第二项将是10,因为0第一个元素中有一个。第三项是1110,因为有一个1和一个0。第四个是11110。因为有三个11二进制!) 1和一个0。以下是第五个术语的细目分类,以使该过程更加清晰:

> 11110
> 1111 0    (split into groups of each number)
> 4*1 1*0   (get count of each number in each group)
> 100*1 1*0 (convert counts to binary)
> 100110    (join each group back together)

这是一个从第六学期到第七学期的示例:

> 1110010110
> 111 00 1 0 11 0
> 3*1 2*0 1*1 1*0 2*1 1*0
> 11*1 10*0 1*1 1*0 10*1 1*0
> 111100111010110

您可以签出参考程序φI来计算这些项。

你的工作

您需要创建一个程序或函数,该程序或函数n通过标准输入或函数参数接收一个数字,并打印出该1st术语到该(n+1)th术语的序列,并用换行符分隔。如果您想查看较低的数字,可以参考OEIS页面中的b文件。但是,您的程序/功能应支持0 <= n <= 30,即直至31日。这是一项不小的壮举,因为δA049064(30)超过140,000位数长。如果您想知道第31个名词应该是什么,我已将其放在Pastebin上

示例I / O

func(10)
0
10
1110
11110
100110
1110010110
111100111010110
100110011110111010110
1110010110010011011110111010110
1111001110101100111001011010011011110111010110
1001100111101110101100111100111010110111001011010011011110111010110

func(0)
0

func(3)
0
10
1110
11110

只有一个规则:没有标准漏洞!

这是 ,因此最低字节数为准。


φ-可以找到要点 这里和ideone演示是在这里

δ-万一您想知道,我对第100个学期的估计大约为3.28x10 250字符长,对于任何人来说,这都是很大的。


以列表形式输出?赞[0]\n[1, 0]\n[1, 1, 1, 0]\n...
Jakube

@Jakube不,您需要在其上进行字符串连接。
卡德,2015年

5
恭喜您为OEIS做出了贡献!
Alex A.

Answers:


8

CJam,18个 17字节

0{sN1$e`2af.b}ri*

感谢@MartinBüttner打高尔夫球一个字节!

CJam解释器中在线尝试。

怎么运行的

0                 e# Push 0.
 {           }ri* e# Repeat int(input)) times:
  s               e#   Stringify the element on top of the stack.
                       EXAMPLE: [[[1 1] '1] [[1] '0]] -> "11110"
   N              e#   Push a linefeed.
    1$            e#   Copy the last stack.
      e`          e#   Perform run-length encoding.
                  e#   EXAMPLE: "100110" -> [[1 '1] [2 '0] [2 '1] [1 '0]]
        2a        e#   Push [2].
          f.b     e#   For each pair [x 'y], execute: [x 'y][2].b
                  e#   This pushes [x2b 'y], where b is base conversion.

4

Pyth,18 17字节

J]0VQjk~JsjR2srJ8

在线试用:演示

感谢@isaacg节省了一个字节。

说明:

                     implicit: Q = input number
 ]0                  create an initial list [0]
J                    and store in J
   VQ                for loop, repeat Q times:
              rJ8       run-length-encoding of J
             s          sum, unfolds lists
          jR2           convert each value to base 2
         s              sum, unfolds lists
       ~J               store the result in J

                        but return the old list,
     jk                 join it and print it

这利用了一个事实,即二进制中的0和1也是0和1。


使用V而不是来缩短1个字节.uJ]0VQjk~JsjR2srJ8
isaacg

2

Python 2中,125个 116 110字节

from itertools import*
v='0'
exec"print v;v=''.join(bin(len(list(g)))[2:]+k for k,g in groupby(v));"*-~input()

@ Sp3000节省了1个字节,删除冗余int调用节省了5个字节。

旧版本:

import itertools as t
v='0'
exec"print v;v=''.join(bin(int(len(list(g))))[2:]+k for k,g in t.groupby(v));"*-~input()

感谢@ Vioz-,节省了很多字节!

原始版本:

import itertools as t
v='0'
for n in range(input()+1):print v;v=''.join(bin(int(len(list(g))))[2:]+k for k,g in t.groupby(v))

1

Ruby,80 72 69字节

作为功​​能:

f=->m{l=?0;0.upto(m){puts l;l.gsub!(/1+|0+/){$&.size.to_s(2)+$&[0]}}}

调用它例如: f[6]


如果将输入作为函数参数,则可以节省一些字节:->m{l=?0;0.upto(m){puts l;l.gsub!(/1+|0+/){$&.size.to_s(2)+$&[0]}}}
blutorange

@blutorange不错!完全忘记了upto! -谢谢:)
达尼罗

1

Python 2,102字节

import re
o='0'
exec"print o;o=''.join(bin(len(x))[2:]+x[0]for x in re.findall('0+|1+',o));"*-~input()

以某种方式itertools,长于对象regroupby返回grouper对象的组合意味着使用正则表达式会更短...


0

眼镜蛇-128

do(i)=if(i-=1,(r=RegularExpressions).Regex.replace(f(i),'1+|0+',do(m=r.Match())=Convert.toString('[m]'.length,2)+'[m]'[:1]),'0')

0

Haskell,136130字节

import Text.Printf
import Data.List
f n=putStr.unlines.take(n+1).iterate(concatMap(\n->(printf"%b"$length n)++[head n]).group)$"0"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.