查找序列化整数


16

任务

编写一个将(作为输入)正整数的程序。然后,它将从开始计数0,将每个整数附加到String,仅在的长度String小于输入值时才继续。

序列整数被定义为具有最大值属于完全形成的整数String。通过“完全形成”,该整数应没有丢失的数字(如果满足的长度约束,则将出现此数字String)。

程序的输出应为其相应的正输入的序列化整数。


规则

  • 这是代码高尔夫球,所以最短的答案(以字节为单位)获胜!
  • 输入将始终为正。
  • 输出必须是以10为底的整数(十进制)。
  • 该程序必须为0索引。

输入示例| 输出量

   5 | 4   (0 1 2 3 4              - Length of 5)
  11 | 9   (0 1 2 3 4 5 6 7 8 9 1  - Length of 11)
  12 | 10  (0 1 2 3 4 5 6 7 8 9 10 - Length of 12)
1024 | 377 (0 1 2 3 4 5 6 7 8 ...  - Length of 1024)

笔记)


6
建议的测试用例:11
Rod

@Rod添加了它,希望它可以更容易理解!
Jacob G.

在示例中为字符串添加引号可能会使您更容易理解它是字符串。
isaacg

那么Champernowne常数的前N-1几位带有前缀?0
Mego

Answers:


8

JavaScript(ES6),40 37字节

f=(n,i=s='0')=>(s+=++i)[n]?i-1:f(n,i)
<input type=number min=1 value=1 oninput=o.textContent=f(this.value)><pre id=o>0

编辑:在@Arnauld的帮助下保存了3个字节。




5

Japt,13个字节

1n@P±X l >U}a

在线测试!

说明

1n@ P± X l >U}a
1nX{P+=X l >U}a
                   Implicit: U = input integer, P = empty string
  X{         }a    Return the first integer X in [0, 1, 2, ...] that returns a truthy value:
    P+=X             Append X to P.
         l >U        Return P.length > U.
                   This returns the first integer that can't fit into the U-char string.
1n                 Subtract 1 from the result.
                   Implicit: output result of last expression




4

果冻 11 10  9 字节

RD;\L€<⁸S

单链链接,采用正整数并返回非负整数。

在线尝试!

怎么样?

编辑中...

RD;\L€<⁸S - link: number n
R         - range -> [1,2,...10,11,...,n-1]
 D        - convert to decimal (vectorises) -> [[1],[2],...,[1,0],[1,1],...D(n-1)]
   \      - cumulative reduce by:
  ;       -   concatenation -> prefixes i.e.: [[1],[1,2],...,[1,2,...,1,0],[1,2,...,1,0,1,1],[1,2,...,1,0,1,1,...Flattened(D(n))]]
    L€    - length of €ach -> [1,2,3,...,11,13,...,length([1,2,...,1,0,1,1,...Flattened(D(n))])]
       ⁸  - chain's left argument, n
      <   - less than? (vectorises)
        S - sum (yields the number of prefixes that are less than or equal in length to n)
          -   Note: `0` is excluded from the range and all the prefixes, but including
          -         it would mean comparing to n+1 AND decrementing at the end (for a
          -         total cost of a byte)


3

Perl 6,36个字节

{(0...^{([~] 0..$^a).comb>$_})[*-1]}

在线尝试!

  • 0 ...^ {...}是从零到小于括号中代码块返回true的数字的数字序列。(...如果没有插入符号,则将返回该块返回true的第一个数字。)
  • [~] 0 .. $^a是数字0到当前数字$^a(代码块的参数)的串联。
  • .comb是串联字符串中所有字符(数字)的列表。解释为数字,它计算字符串的长度。 .chars在这里使用它会更自然,因为它直接计算字符串的长度,但是名称长一个字符。
  • $_ 是顶级函数的参数。
  • [*-1] 选择生成列表的最后一个元素。

2

QBIC,34个字节

{A=!p$~_lB+A|>:|_xp-1|\B=B+A]p=p+1

说明

{           DO infinitely
A=!p$       Set A$ to p cast to num
            Note that p starts out as 0.
~      >:   IF the input number is exceeded by
 _l   |     the length of
   B+A      A$ and B$ combined
_xp-1|      THEN QUIT, printing the last int successfully added to B$
            The _X operator quits, (possibly) printing something if followed by a-zA-Z
            _x is slightly different, it prints the expression between the operator _x and |
\B=B+A      ELSE add A$ to B$
]           END IF
p=p+1       Raise p, and rerun


2

J,26个字节

(>i:1:)([:+/\[:>.10^.1+i.)

((>i:1:)([:+/\[:>.10^.1+i.))"0 ] 5 11 12 1024 2000 20000 100000 1000000
4 9 10 377 702 5276 22221 185184


0

WendyScript,42个字节

<<f=>(x){<<n=""#i:0->x{n+=i?n.size>=x/>i}}

f(1024) // returns 377

在线尝试!

取消高尔夫:

let f => (x) {
  let n = ""
  for i : 0->x { 
    n+=i
    if n.size >= x 
    ret i
  }
  ret
}


0

Java 8,64字节

n->{int i=0;for(String t="0";;t+=++i)if(t.length()>n)return~-i;}

或具有相同字节数的轻微替代方案:

n->{int i=0;for(String t="";;t+=i++)if(t.length()>n)return i-2;}
n->{int i=-1;for(String t="";;t+=++i)if(t.length()>n)return~-i;}

说明:

在这里尝试。

n->{                  // Method with integer as both parameter and return-type
  int i=0;            //  Integer `i`, starting at 0
  for(String t="0";   //  String, starting at "0"
      ;               //  Loop indefinitely
       t+=++i)        //    After every iteration: append the String with `i+1`
                      //    by first increasing `i` by 1 with `++i`
    if(t.length()>n)  //   If the length of the String is larger than the input:
      return~-i;      //    Return `i-1`
                      //  End of loop (implicit / single-line body)
}                     // End of method


0

Ruby,44个字节

受到Kevin Cruijssen的JAVA答案的启发。-G 4个字节。

->n{i,t=0,'';t+="#{i+=1}"while t.size<n;i-1}

第(i + = 1; T + = i.to_s)相同为t + = “#{I + = 1}”,只有4个字节
GB

如果你这样做,你不需要变量t了,你可从N减去的尺寸,然后用0比较
GB

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.