无效的Invali无效


27

这个想法大致基于@TùxCräftîñg的聊天消息。

看一下下面的示例序列:

INVALID0INVALID1INVALID2 INVALID3INVALID4...INVALID9

之后INVALID9,它像这样继续:

INVALI0INVALI1INVALI2INVALI3...INVALI9

之后INVALI9,是这样的:

INVAL0INVAL1INVAL2INVAL3...INVAL9

之后,INVAL9是这样的:

INVA0INVA1INVA2INVA3,...INVA9

注意我们如何INVALID每次都从单词中删除字母。

您将不断重复此操作,直到到达单个字母,即字母I

I0I1I2I3I4...I9

现在,您的任务是,像上例一样,输入一个单词,并从中产生一个序列。您的代码还必须使用单个字母,在这种情况下,结果序列将更短。

您可以选择自己喜欢的任何输入和输出格式(根据需要选择带或不带分隔符),但是必须指定选择的格式。

顺序必须严格按照指定的顺序进行。

成功完成此挑战的最短代码(以字节为单位)将赢得挑战。

上例中的完整序列:

INVALID0, INVALID1, INVALID2, INVALID3, INVALID4, INVALID5, INVALID6, INVALID7, INVALID8, INVALID9, INVALI0, INVALI1, INVALI2, INVALI3, INVALI4, INVALI5, INVALI6, INVALI7, INVALI8, INVALI9, INVAL0, INVAL1, INVAL2, INVAL3, INVAL4, INVAL5, INVAL6, INVAL7, INVAL8, INVAL9, INVA0, INVA1, INVA2, INVA3, INVA4, INVA5, INVA6, INVA7, INVA8, INVA9, INV0, INV1, INV2, INV3, INV4, INV5, INV6, INV7, INV8, INV9, IN0, IN1, IN2, IN3, IN4, IN5, IN6, IN7, IN8, IN9, I0, I1, I2, I3, I4, I5, I6, I7, I8, I9

其他例子:

输入:(MAYBE大写和小写无关紧要)

输出:

MAYBE0, MAYBE1, MAYBE2, MAYBE3, MAYBE4, MAYBE5, MAYBE6, MAYBE7, MAYBE8, MAYBE9, MAYB0, MAYB1, MAYB2, MAYB3, MAYB4, MAYB5, MAYB6, MAYB7, MAYB8, MAYB9, MAY0, MAY1, MAY2, MAY3, MAY4, MAY5, MAY6, MAY7, MAY8, MAY9, MA0, MA1, MA2, MA3, MA4, MA5, MA6, MA7, MA8, MA9, M0, M1, M2, M3, M4, M5, M6, M7, M8, M9


输入: AFTER

输出:

AFTER0, AFTER1, AFTER2, AFTER3, AFTER4, AFTER5, AFTER6, AFTER7, AFTER8, AFTER9, AFTE0, AFTE1, AFTE2, AFTE3, AFTE4, AFTE5, AFTE6, AFTE7, AFTE8, AFTE9, AFT0, AFT1, AFT2, AFT3, AFT4, AFT5, AFT6, AFT7, AFT8, AFT9, AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7, AF8, AF9, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9


输入: WHAT ARE YOU DOING

WHAT ARE YOU DOING0, WHAT ARE YOU DOING1, WHAT ARE YOU DOING2, WHAT ARE YOU DOING3, WHAT ARE YOU DOING4, WHAT ARE YOU DOING5, WHAT ARE YOU DOING6, WHAT ARE YOU DOING7, WHAT ARE YOU DOING8, WHAT ARE YOU DOING9, WHAT ARE YOU DOIN0, WHAT ARE YOU DOIN1, WHAT ARE YOU DOIN2, WHAT ARE YOU DOIN3, WHAT ARE YOU DOIN4, WHAT ARE YOU DOIN5, WHAT ARE YOU DOIN6, WHAT ARE YOU DOIN7, WHAT ARE YOU DOIN8, WHAT ARE YOU DOIN9, WHAT ARE YOU DOI0, WHAT ARE YOU DOI1, WHAT ARE YOU DOI2, WHAT ARE YOU DOI3, WHAT ARE YOU DOI4, WHAT ARE YOU DOI5, WHAT ARE YOU DOI6, WHAT ARE YOU DOI7, WHAT ARE YOU DOI8, WHAT ARE YOU DOI9, WHAT ARE YOU DO0, WHAT ARE YOU DO1, WHAT ARE YOU DO2, WHAT ARE YOU DO3, WHAT ARE YOU DO4, WHAT ARE YOU DO5, WHAT ARE YOU DO6, WHAT ARE YOU DO7, WHAT ARE YOU DO8, WHAT ARE YOU DO9, WHAT ARE YOU D0, WHAT ARE YOU D1, WHAT ARE YOU D2, WHAT ARE YOU D3, WHAT ARE YOU D4, WHAT ARE YOU D5, WHAT ARE YOU D6, WHAT ARE YOU D7, WHAT ARE YOU D8, WHAT ARE YOU D9, WHAT ARE YOU 0, WHAT ARE YOU 1, WHAT ARE YOU 2, WHAT ARE YOU 3, WHAT ARE YOU 4, WHAT ARE YOU 5, WHAT ARE YOU 6, WHAT ARE YOU 7, WHAT ARE YOU 8, WHAT ARE YOU 9, WHAT ARE YOU0, WHAT ARE YOU1, WHAT ARE YOU2, WHAT ARE YOU3, WHAT ARE YOU4, WHAT ARE YOU5, WHAT ARE YOU6, WHAT ARE YOU7, WHAT ARE YOU8, WHAT ARE YOU9, WHAT ARE YO0, WHAT ARE YO1, WHAT ARE YO2, WHAT ARE YO3, WHAT ARE YO4, WHAT ARE YO5, WHAT ARE YO6, WHAT ARE YO7, WHAT ARE YO8, WHAT ARE YO9, WHAT ARE Y0, WHAT ARE Y1, WHAT ARE Y2, WHAT ARE Y3, WHAT ARE Y4, WHAT ARE Y5, WHAT ARE Y6, WHAT ARE Y7, WHAT ARE Y8, WHAT ARE Y9, WHAT ARE 0, WHAT ARE 1, WHAT ARE 2, WHAT ARE 3, WHAT ARE 4, WHAT ARE 5, WHAT ARE 6, WHAT ARE 7, WHAT ARE 8, WHAT ARE 9, WHAT ARE0, WHAT ARE1, WHAT ARE2, WHAT ARE3, WHAT ARE4, WHAT ARE5, WHAT ARE6, WHAT ARE7, WHAT ARE8, WHAT ARE9, WHAT AR0, WHAT AR1, WHAT AR2, WHAT AR3, WHAT AR4, WHAT AR5, WHAT AR6, WHAT AR7, WHAT AR8, WHAT AR9, WHAT A0, WHAT A1, WHAT A2, WHAT A3, WHAT A4, WHAT A5, WHAT A6, WHAT A7, WHAT A8, WHAT A9, WHAT 0, WHAT 1, WHAT 2, WHAT 3, WHAT 4, WHAT 5, WHAT 6, WHAT 7, WHAT 8, WHAT 9, WHAT0, WHAT1, WHAT2, WHAT3, WHAT4, WHAT5, WHAT6, WHAT7, WHAT8, WHAT9, WHA0, WHA1, WHA2, WHA3, WHA4, WHA5, WHA6, WHA7, WHA8, WHA9, WH0, WH1, WH2, WH3, WH4, WH5, WH6, WH7, WH8, WH9, W0, W1, W2, W3, W4, W5, W6, W7, W8, W9

排行榜


1
您可以一次发布全部序列吗?可能还有更多样本?另外,输入内容可能包含什么?
DJMcMayhem

1
缺少分隔符(例如INVALID0INVALID1INVALID2)是否是有效的输出格式?
DLosc

@DLosc是的。
缓冲读取

3
众所周知,通常不鼓励在发布挑战后这么快地接受答案。过早接受可能会阻止用户发布新答案。这并不是说您不能保留已接受的答案,但是我鼓励您下次再等待更长的时间。
DJMcMayhem

@DJMcMayhem好的!
缓冲读取

Answers:


5

果冻,7 个字节

ḣJṚp⁵Ḷ¤

在线尝试!

怎么运行的

ḣJṚp⁵Ḷ¤  Main link. Argument: s (string)

 J       Yield all (1-based) indices of s.
ḣ        Head; for each index k, take the first k characters of s.
  Ṛ      Reverse the result.
      ¤  Combine the two links to the left into a niladic chain.
    ⁵      Yield 10.
     Ḷ     Unlength; yield [0, ..., 9].
   p     Return the Cartesian product of the prefixes and the range.
         (implicit) Print the Cartesian product without separators.

6
7个字节。我只是想知道有人是如何想到使此精确代码与代码高尔夫一起工作的。
haykam

8

05AB1E10 8字节

.pžmâ€JR

说明

.p        # get prefixes of input
  žmâ     # cartesian product with [9..0]
     €J   # join each
       R  # reverse

在线尝试!

感谢Adnan,节省了2个字节


1
.p等效于Œ¹g£:)。
阿德南

1
@Adnan:严重的是,我怎么又忘记了这一点!谢谢!似乎我应该休息一下:P
Emigna '16

8

Javascript(ES6),53 47字节

f=(s,n=0)=>s&&s+n+f(n-9?s:s.slice(0,-1),++n%10)

由于花生和尼尔节省了6个字节

输出:所有单词作为单个字符串,没有分隔符。

var f=(s,n=0)=>s&&s+n+f(n-9?s:s.slice(0,-1),++n%10)

document.getElementsByTagName('div')[0].innerHTML = f('INVALID')
<div style="word-wrap:break-word"></div>


为您节省一个字节,将其减少到52个字节,通过在模式中的项目之间不添加空格来不使用分隔符(允许使用分隔符在这里尝试!
haykam

您能s&&代替s?... 使用:''吗?
尼尔

另外,您可以通过删除我上次发布的代码中的部分,将其减少到49个字节+''在这里尝试!
haykam '16

我使用的是Firefox,并且文字之间没有空格。这个问题不是必需的,但我想我会让你知道。
缓冲读取

1
@TheBitByte-我的坏。不再有分隔符(如Peanut所建议),但我忘了相应地更新我的答案。感谢您的关注!
Arnauld

7

Perl,29个字节

包括+1的 -n

使用STDIN上的输入运行:

perl -nE '/^.+(?{map{say$&.$_}0..9})^/' <<< PERL

只是代码:

/^.+(?{map{say$&.$_}0..9})^/

非常好的代码。我最后一点都不明白^...看起来它的工作与相同(*FAIL),但我不明白为什么。你能解释一下吗?
达达

@Dada是的,强迫失败正是它的作用。由于它从字符串开头至少匹配了1个字符,因此不再位于开头,因此^导致匹配失败,这迫使正则表达式回溯
Ton Hospel

好,谢谢。我希望它可以与输入中未包含的任何字符一起使用,但似乎只能与^... 一起使用...我的意思是说您的示例,为什么,/不起作用,但^/可以呢?
达达

这是正则表达式优化器的实现细节。如果您在字符串中未放置特定字符,则足够聪明地知道正则表达式永远不会匹配,甚至真正的正则表达式匹配都不会开始。^超出了当前对优化器的理解。无论是这两种行为可能会改变未来..
吨Hospel

好的,我明白了,我以为是那样,但不确定。非常感谢
Dada's

6

Haskell,47 43个字节

f""=[]
f x=map((x++).show)[0..9]++f(init x)

用法示例:f "IN"-> ["IN0","IN1","IN2","IN3","IN4","IN5","IN6","IN7","IN8","IN9","I0","I1","I2","I3","I4","I5","I6","I7","I8","I9"]

简单的递归方法。将每个数字附加到单词上,并附加删除了最后一个字母的递归调用。


6

Pyth,9个字节

sM*_._QUT

一个程序,该程序在STDIN上输入带引号的字符串并输出字符串列表。

在线尝试

怎么运行的

sM*_._QUT  Program. Input: Q
    ._     List of prefixes of Q
   _       Reverse
       UT  Unary range up to 10, yielding [0, 1, 2, ..., 9]
  *        Cartesian product of the above two
sM         Map concatenate over the above
           Implicitly print

5

12 11字节

将单词作为cmdline参数。没有分隔符的输出。

Wa&Oa.,tDQa

在线尝试!

说明:

             Implicit: a = 1st cmdline arg, t = 10
Wa           While a (i.e. while it's not the empty string)
   Oa.,t     Concatenate range(10) to a and output
               (Pip concatenates a string to a range itemwise)
  &          The output operation is &-ed to the loop condition to save on curly braces
        DQa  Dequeue from a, removing the final character on each iteration

4

V,20字节

A0òYpó.10/0/e
$hòd

在线尝试!

由于其中包含不可打印的字符,因此以下是可读格式:

A0<esc>òYp<C-a>ó.10/0/e
$hòd

这是一个十六进制转储:

0000000: 4130 1bf2 5970 01f3 2e31 302f 302f 650a  A0..Yp...10/0/e.
0000010: 2468 f264                                $h.d

说明:

A0<esc>                 "Append a '0' to the input
       ò                "Recursively:
        Yp              "  Yank this line and paste it
          <C-a>         "  Increment the first number on this line
               ó        "  Substitute:
                .10     "    Any single character followed by '10'
                   /0   "    Replace it with a '0'
                     /e "    Ignore errors if this is not found
$h                      "  Move to the end of the end of this line than back one.
                        "  This makes it so the loop ends once there is only one
                        "  character on this line.
  ò                     "End the loop
   d                    "Delete a line (since we create one too many)  

4

Bash + coreutils,54个字节:

for i in `seq ${#1} 1`;{ printf "${1:0:i}%s " {0..9};}

简单地循环遍历一个序列,[Length of Input,1]并在每次迭代期间,将输入字输出到当前迭代值9时间的长度,并将每个数字[0,9]附加到9该字的每个副本之后。在文件中加上引号中的一个或多个单词来执行它bash A.sh "blah blah blah"


4

Floroid - 50 47 31字节

f=Ba:aM[a+b KbIhd]+f(a[:-1])H[]

当前使用与@JonathanAllan第二种递归方法相似的方法。

如果我可以使用以下语言更仔细地实现笛卡尔积,那就可能是这样Bc:ca([c]+[c[:-a-1]KaIw(Z(c)-1)],hd)

测试用例

Input: ABC
Output: ['ABC0', 'ABC1', 'ABC2', 'ABC3', 'ABC4', 'ABC5', 'ABC6', 'ABC7', 'ABC8', 'ABC9', 'AB0', 'AB1', 'AB2', 'AB3', 'AB4', 'AB5', 'AB6', 'AB7', 'AB8', 'AB9', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9']

Input: M
Output: ['M0', 'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9']

3

(lambdabot)Haskell-49个字节

f q=[x++show n|x<-reverse.tail$inits q,n<-[0..9]]

Lambdabot是#haskell上的IRC机器人;自动导入了一堆模块,包括Data.List这就是inits生活。而且由于一种语言是由其实现定义的,所以我可以称其为lambdabot haskell而不用为导入支付字节。

常规Haskell:

import Data.List
f q=[x++show n|x<-reverse.tail$inits q,n<-[0..9]]

您确定tails可行吗?
Bergi

@Bergi,完全忘记了进口,感谢您指出:)
BlackCap

我不是说进口,而是说它会产生错误的输出:INVALID, NVALID, VALID, ALID, LID, ID, D,
Bergi

@ Bergi,Yikes!你是对的。然后再为我提供8个字节
BlackCap 2013年

3

脑气肿34 33 31 28字节

在目前的状态下,脑力激荡只是一些激动人心的(例如3?)。我一直在花费大部分开发时间来尽可能地使其“企业化”,而不是实际添加功能...

无论如何,以下代码应与最新的开发快照配合使用。它从stdin(如)接收不需要换行符的输入$ echo -n INVALID | braingasm invalid.bg,然后打印到stdout。

,[>,]#[48+10[#<[.>]<+]0,<0,]

说明:

,[>,]                 lay down the input on the tape
#[                    (length of input - 1) times do
  48+                   add '0' at the end of the tape
  10[                   10 times do
     #<[.>]               move to start of tape, then print the tape
     <+                   increase the number at the end of the tape
  ]                     done printing current word with 0 through 9
  0,                    erase the number by writing 0 onto it
  <0,                   likewise, remove one character
]                     done

编辑:显然可以跳过使用空字符串作为定界符


2

Python 2 2,53 55字节

+2个字节:声明f必须进行递归(如@Destructible Watermelon所指出)

f=lambda s:s and[s+`n`for n in range(10)]+f(s[:-1])or[]

递归到空字符串(产生一个空列表),一次砍掉一个字符,并以当前字符串的十个列表开头,每个字符串后面附加数字0-9。

测试ideone

Python 3,54 56字节

f=lambda s:s and[s+n for n in'0123456789']+f(s[:-1])or[]

测试ideone


2
我非常确定,如果您的lambda包含对自身的调用,则需要使用该f=部件(有点像您不能假定变量具有值的方式)
Destructible Lemon

2

Swift 3,150字节

并不是最短的解决方案,但对于Swift来说并不可怕

func a(s: String){var c=s.characters,r="";while(c.count>0){var k = "";for d in c{k+=String(d)};for i in 0...9{r+="\(k)\(i) "};c.removeLast()};print(r);}

在IBM Swift Sandbox中在线测试

不打高尔夫球

func a(s s: String){
    var c = s.characters, r = ""
    while(c.count > 0){
        var k = ""
        for d in c{
            k+=String(d)
        }
        for i in 0...9{
            r+="\(k)\(i) "
        }
        c.removeLast()
    }
    print(r)
}

2

红宝石,51岁

不使用分隔符。

->s{(10*n=s.size).times{|i|print s[0,n-i/10],i%10}}

i%10分隔符之后添加以下内容:

,$/用于换行符,,?|用于|(类似于任何可打印字符),,' '用于空格。

在测试程序中

f=->s{(10*n=s.size).times{|i|print s[0,n-i/10],i%10}}

f[gets.chomp]

2

PHP,64 56字节

for($i=9;$a=substr($argv[1].a,0,-++$i/10);)echo$a.$i%10;

for(;$a=substr($argv[1].a,$i=0,-++$l);)for(;$i<10;)echo $a.$i++;


防止发生另一个for循环的好方法。您可以通过删除回声后的空间节省字节
aross

2

Haskell,49个 46字节

f=(>>=(<$>['0'..'9']).snoc).reverse.tail.inits

您可以通过插入地图f=(>>=(map 来保存字节['0'..'9']).snoc).tail.reverse.inits。3通过使用fmap:f=(>>=(<$>['0'..'9']).snoc).tail.reverse.inits
BlackCap

哦,如果您不这样做reverse.tail.initstail.reverse.inits那么您还会获得正确的输出;)
BlackCap 2013年

@BlackCap:谢谢,我真的很想知道为什么标准库中没有翻转的(f)贴图,但是却没有想到节。关于tail,我想我的意思是init,但是将其与反向交换也可以::)
Bergi

2

C#,107102字节

string f(string i){var o="";while(i!=""){for(int k=0;k<=9;)o+=i+k++;i=i.Remove(i.Length-1);}return o;}

不打高尔夫球

string f(string i)
{
   string o = "";
   while(i != "")
   {
      for (int k = 0; k <= 9;)
         o += i + k++;
      i = i.Remove(i.Length - 1);
   }
   return o;
}

1
您可以通过删除k++for循环并++在使用后添加来进行打法k,如下所示:string f(string i){var o="";while(i!=""){for(int k=0;k<=9;)o+=i+k+++",";i=i.Remove(i.Length-1);}return o;}另外,OP的挑战也不要求使用逗号,尽管您当然可以保留它们。如果没有它,那就是:string f(string i){var o="";while(i!=""){for(int k=0;k<=9;)o+=i+k++;i=i.Remove(i.Length-1);}return o;}
Kevin Cruijssen

2

Ruby,90 85字节

f=->s{if s=="";return[];end;(0..9).map{|i|s+i.to_s}+f[s.chars.take(s.length-1).join]}

如果字符串为空,则返回一个空数组。否则,生成字符串+每个数字(从0到9)中的数字,并f使用不带最后一个字符的字符串进行调用。

@LevelRiverSt节省了5个字节


我认为您以前从未玩过Ruby。检查我对这个问题的回答(或本网站上的许多其他Ruby回答),以了解定义函数的简便方法,而不必包括那些多余的defend。您可以创建一个lambda,以便您甚至不必给它起一个名字,只要您分配一个变量并使用方括号中的参数调用它即可。
Level River St

@LevelRiverSt使用lambda的时间增加了1个字节
TuxCrafting

好的,我错过了您确实需要命名的事实,因为它是递归的。但是仍然f=->s{if s=="";return[];end;(0..9).map{|i|s+i.to_s}+f[s.chars.take(s.length-1).join]}要短5个字节。
水平河圣

@LevelRiverSt噢,我不知道->语法
TuxCrafting

f=->s{s==""&&(return[]);(0..9).map{|i|s+i.to_s}+f[s.chars.take(s.length-1).join]}保存另外4个字节。Ruby从左到右评估布尔表达式,除非确定结果需要,否则不评估后续项。在C语言中可以使用相同的高尔夫技术。我不知道为什么在这种情况下需要()周围环境return[]
水平河圣

2

Perl 6,32 = 31字节+1 -p

我对Perl 6并不十分精通,因此可能有更多方法可以减少它。

$_= ~((~$_,*.chop...^!*)X~ ^10)

它用于-p为每个输入行评估一次。将该行放入$_程序中并在程序运行后打印$_

(~$_,*.chop...^!*)列表是第一个元素被字符串化(~)输入的列表,每个后续元素都是通过将前一个字符(*.chop)切掉的最后一个字符获得的,并且一直持续到字符串为空(!*)为止,不包括空字符串大小写(^ in ...^) 。

X~ 使用指定的操作(在这种情况下,字符串串联(~它们上))。^10是0、1,... 9的列表。

最后,再次使用列表对列表进行字符串化~,以空格作为分隔符,以提供所需的单词。


2

PowerShell v2 +,60字节

param($n)$n.length..1|%{$i=$_-1;0..9|%{-join$n[0..$i]+"$_"}}

从输入字符串的长度向下循环到1。每次迭代时,将helper设置为$i等于当前数减1。这是必需的,因为这是.length字符总数,但是索引字符串是基于0的。然后,我们从循环09。每个内部循环都$n根据外部循环的值将输入字符串切成片,将其切-join回字符串,然后根据内部循环计数将字符串串联起来。每个单独的循环结果都放置在管道上,并且在程序完成时隐式输出。

PS C:\Tools\Scripts\golfing> .\invalid-invali-inval.ps1 'foo'
foo0
foo1
foo2
foo3
foo4
foo5
foo6
foo7
foo8
foo9
fo0
fo1
fo2
fo3
fo4
fo5
fo6
fo7
fo8
fo9
f0
f1
f2
f3
f4
f5
f6
f7
f8
f9

2

Dyalog APL14 11 字节

返回字符串列表。

,⎕D∘.,⍨⌽,\⍞

, listify(将表放入列表)

⎕D 所有数字

∘.,⍨ 附加到所有(即与之进行所有组合)

的反向列表

,\ 的累积串联

文字输入

在线尝试APL!


固定。我使用样板,忘了填写。
Adám16年


2

批处理,85 83字节

@for /l %%i in (0,1,9)do @echo %1%%i
@set s=%1
@if not "%s:~,-1%"=="" %0 %s:~,-1%

2

Java 7,105 98字节

void c(String s){for(int x=0,l=s.length();x<l*10;)System.out.print(s.substring(0,l-x/10)+x++%10);}

-7个字节,感谢@Poke

Ungolfed

void c(String s){
  for(int x = 0, l = s.length(); x < l*10; ){
    System.out.print(s.substring(0, l - x/10) + x++ % 10);
  }
}

测试代码:

在这里尝试。

class M{
  static void c(String s){for(int x=0,l=s.length();x<l*10;)System.out.print(s.substring(0,l-x/10)+x++%10);}

  public static void main(String[] a){
    c("INVALID");
    System.out.println();
    c("MAYBE");
    System.out.println();
    c("AFTER");
    System.out.println();
    c("WHAT ARE YOU DOING");
  }
}

输出:

INVALID0INVALID1INVALID2INVALID3INVALID4INVALID5INVALID6INVALID7INVALID8INVALID9INVALID0INVALID1INVALID2INVALID3INVALID4INVALID5INVALID6INVALID7INVALID8INVALID9INVALI0INVALI1INVALI2INVALI3INVALI4INVALI5INVALI6INVALI7INVALI8INVALI9INVAL0INVAL1INVAL2INVAL3INVAL4INVAL5INVAL6INVAL7INVAL8INVAL9INVA0INVA1INVA2INVA3INVA4INVA5INVA6INVA7INVA8INVA9INV0INV1INV2INV3INV4INV5INV6INV7INV8INV9IN0IN1IN2IN3IN4IN5IN6IN7IN8IN9I0I1I2I3I4I5I6I7I8I9
MAYBE0MAYBE1MAYBE2MAYBE3MAYBE4MAYBE5MAYBE6MAYBE7MAYBE8MAYBE9MAYBE0MAYBE1MAYBE2MAYBE3MAYBE4MAYBE5MAYBE6MAYBE7MAYBE8MAYBE9MAYB0MAYB1MAYB2MAYB3MAYB4MAYB5MAYB6MAYB7MAYB8MAYB9MAY0MAY1MAY2MAY3MAY4MAY5MAY6MAY7MAY8MAY9MA0MA1MA2MA3MA4MA5MA6MA7MA8MA9M0M1M2M3M4M5M6M7M8M9
AFTER0AFTER1AFTER2AFTER3AFTER4AFTER5AFTER6AFTER7AFTER8AFTER9AFTER0AFTER1AFTER2AFTER3AFTER4AFTER5AFTER6AFTER7AFTER8AFTER9AFTE0AFTE1AFTE2AFTE3AFTE4AFTE5AFTE6AFTE7AFTE8AFTE9AFT0AFT1AFT2AFT3AFT4AFT5AFT6AFT7AFT8AFT9AF0AF1AF2AF3AF4AF5AF6AF7AF8AF9A0A1A2A3A4A5A6A7A8A9
WHAT ARE YOU DOING0WHAT ARE YOU DOING1WHAT ARE YOU DOING2WHAT ARE YOU DOING3WHAT ARE YOU DOING4WHAT ARE YOU DOING5WHAT ARE YOU DOING6WHAT ARE YOU DOING7WHAT ARE YOU DOING8WHAT ARE YOU DOING9WHAT ARE YOU DOING0WHAT ARE YOU DOING1WHAT ARE YOU DOING2WHAT ARE YOU DOING3WHAT ARE YOU DOING4WHAT ARE YOU DOING5WHAT ARE YOU DOING6WHAT ARE YOU DOING7WHAT ARE YOU DOING8WHAT ARE YOU DOING9WHAT ARE YOU DOIN0WHAT ARE YOU DOIN1WHAT ARE YOU DOIN2WHAT ARE YOU DOIN3WHAT ARE YOU DOIN4WHAT ARE YOU DOIN5WHAT ARE YOU DOIN6WHAT ARE YOU DOIN7WHAT ARE YOU DOIN8WHAT ARE YOU DOIN9WHAT ARE YOU DOI0WHAT ARE YOU DOI1WHAT ARE YOU DOI2WHAT ARE YOU DOI3WHAT ARE YOU DOI4WHAT ARE YOU DOI5WHAT ARE YOU DOI6WHAT ARE YOU DOI7WHAT ARE YOU DOI8WHAT ARE YOU DOI9WHAT ARE YOU DO0WHAT ARE YOU DO1WHAT ARE YOU DO2WHAT ARE YOU DO3WHAT ARE YOU DO4WHAT ARE YOU DO5WHAT ARE YOU DO6WHAT ARE YOU DO7WHAT ARE YOU DO8WHAT ARE YOU DO9WHAT ARE YOU D0WHAT ARE YOU D1WHAT ARE YOU D2WHAT ARE YOU D3WHAT ARE YOU D4WHAT ARE YOU D5WHAT ARE YOU D6WHAT ARE YOU D7WHAT ARE YOU D8WHAT ARE YOU D9WHAT ARE YOU 0WHAT ARE YOU 1WHAT ARE YOU 2WHAT ARE YOU 3WHAT ARE YOU 4WHAT ARE YOU 5WHAT ARE YOU 6WHAT ARE YOU 7WHAT ARE YOU 8WHAT ARE YOU 9WHAT ARE YOU0WHAT ARE YOU1WHAT ARE YOU2WHAT ARE YOU3WHAT ARE YOU4WHAT ARE YOU5WHAT ARE YOU6WHAT ARE YOU7WHAT ARE YOU8WHAT ARE YOU9WHAT ARE YO0WHAT ARE YO1WHAT ARE YO2WHAT ARE YO3WHAT ARE YO4WHAT ARE YO5WHAT ARE YO6WHAT ARE YO7WHAT ARE YO8WHAT ARE YO9WHAT ARE Y0WHAT ARE Y1WHAT ARE Y2WHAT ARE Y3WHAT ARE Y4WHAT ARE Y5WHAT ARE Y6WHAT ARE Y7WHAT ARE Y8WHAT ARE Y9WHAT ARE 0WHAT ARE 1WHAT ARE 2WHAT ARE 3WHAT ARE 4WHAT ARE 5WHAT ARE 6WHAT ARE 7WHAT ARE 8WHAT ARE 9WHAT ARE0WHAT ARE1WHAT ARE2WHAT ARE3WHAT ARE4WHAT ARE5WHAT ARE6WHAT ARE7WHAT ARE8WHAT ARE9WHAT AR0WHAT AR1WHAT AR2WHAT AR3WHAT AR4WHAT AR5WHAT AR6WHAT AR7WHAT AR8WHAT AR9WHAT A0WHAT A1WHAT A2WHAT A3WHAT A4WHAT A5WHAT A6WHAT A7WHAT A8WHAT A9WHAT 0WHAT 1WHAT 2WHAT 3WHAT 4WHAT 5WHAT 6WHAT 7WHAT 8WHAT 9WHAT0WHAT1WHAT2WHAT3WHAT4WHAT5WHAT6WHAT7WHAT8WHAT9WHA0WHA1WHA2WHA3WHA4WHA5WHA6WHA7WHA8WHA9WH0WH1WH2WH3WH4WH5WH6WH7WH8WH9W0W1W2W3W4W5W6W7W8W9

1
通过组合for循环并执行一些额外的逻辑来隐式确定后缀和子字符串,可以节省7个字节。void c(String s){for(int x=0,l=s.length();x<l*10;)System.out.print(s.substring(0,l-x/10)+x++%10);}

1

Python 3,62个字节

lambda x:[(x+" ")[:~i//10]+str(i%10)for i in range(len(x)*10)]

不像其他答案那样使用递归。

“”所在的原因x+" "是:-0仍为零,因此我们不能使用减号来获取所有字符串,因此我们可以使用的最高值是减一,因此“”要填充字符串,


1

C,72、70字节

j;F(char*s,int l){while(l--)for(j=0;j<10;)printf("%.*s%d",l+1,s,j++);}

将字符串作为指针/大小对。测试主体:

int main() {
  F("INVALID", 7); putchar('\n');
  F("MAYBE", 5); putchar('\n');
  F("AFTER", 5); putchar('\n');
  F("WHAT ARE YOU DOING", 18); putchar('\n');
}

1

视网膜,37字节

字节数假定为ISO 8859-1编码。

M&!r`.+
m`$
0
%{`$
¶$%`
T`w`d`.$
G10`

在线尝试!

说明

M&!r`.+

通过匹配并从右侧打印所有重叠的匹配项来获取输入的所有前缀。

m`$
0

将a附加0到每行。

%{`$
¶$%`

{表示余下的三个阶段在一个循环,直到他们无法改变字符串执行。要求将%它们分别应用于每行。

该阶段本身只是复制最后一行(最初只是该行在其上运行,但是三个阶段的每次迭代都会添加另一行)。

T`w`d`.$

通过执行以下字符替换来增加最后一行中的数字:

from: _0123456789AB...
to:   0123456789

最后:

G10`

仅保留前10行,以便我们删除刚刚添加的行INPUT9


1

Scala,73 70字节

def g(s:String):String=if(s=="")""else(0 to 9 flatMap(s+_))++g(s.init)

称它为f("INVALID")。返回一个字符序列。

说明

def g(s:String):String= //defines a method g taking a String as a parameter
                        //and returning a String
if(s=="")""             //guard to prevent infinite recursion
else
    (0 to 9             //create a Range from 0 to 9 (inclusive)
    flatMap(            //map:
        s+_                 //append each number to the string
    ))                  //and flatten
    ++ g(s.init)        //concatenate with g applied to everything but the last element of s

替代解决方案,73字节

(s:String)=>s.scanLeft("")(_+_).tail.reverse.flatMap(x=>(0 to 9)map(x+_))

定义一个匿名函数。要调用它,写

val f = ...

并这样称呼它

f("INVALID")

它返回一个字符串序列,在打印时将如下所示:

Vector(INVALID0, INVALID1, INVALID2, INVALID3, INVALID4, INVALID5, INVALID6, INVALID7, INVALID8, INVALID9, INVALI0, INVALI1, INVALI2, INVALI3, INVALI4, INVALI5, INVALI6, INVALI7, INVALI8, INVALI9, INVAL0, INVAL1, INVAL2, INVAL3, INVAL4, INVAL5, INVAL6, INVAL7, INVAL8, INVAL9, INVA0, INVA1, INVA2, INVA3, INVA4, INVA5, INVA6, INVA7, INVA8, INVA9, INV0, INV1, INV2, INV3, INV4, INV5, INV6, INV7, INV8, INV9, IN0, IN1, IN2, IN3, IN4, IN5, IN6, IN7, IN8, IN9, I0, I1, I2, I3, I4, I5, I6, I7, I8, I9)

说明

s.scanLeft("")(_+_)    //accumulate letters from left to right -> Vector("", "I", "IN", "INV", "INVA", "INVAL", "INVALI", "INVALID")
.tail                  //drop the first element
.reverse               //reverse it
.flatMap(x =>          //map each element called x
    (0 to 9)           //create a Range from 0 to 9 (inclusive)
    map(x+_)           //append each number to x
)                      //and flatten

您的递归解决方案比迭代解决方案少3个字节
TuxCrafting

没错,我必须在数完之后对其进行优化。
corvus_192 2016年

1

CJam,29 28字节

ls_,,:)W%]~{_[X<aA*A,]zo}fX;

说明:

ls                              read input as string
  _                             duplicate input
   ,,                           create range of length input
      :)W%]                     add 1 to all elements and reverse
           ~                    dump array on stack
            {            }fX    for loop
             _                  duplicate input string
              [X<aA*            slice input string and multiply by 10
                    A,]         range(10)
                       zo       zip array and print (no separator)
                            ;   clear stack

在线尝试

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.