填空


14

输入值

非负整数n,以及s仅包含字母数字字符和下划线的非空字符串_。的第一个字符s不是_。下划线s被解释为可以用其他字符填充的空格。

我们定义“无限字符串”的无限序列如下。字符串被无限次重复多次。对于所有的,字符串从得到通过用的字符填充它的空格,使得第一的被替换,所述第二带,等等。由于第一个字母不是,每个空格最终都会被填充,因此我们用无限字符串表示,其中每个字符串都已被其最终值替换。s1 = s s s...sk > 1sk+1sks1_sks1[0]s1[1]s_s_

输出量

作为字符串的前n几个字符。s

考虑输入n = 30s = ab_c_。我们有

s1 = ab_c_ab_c_ab_c_ab_c_ab_c_ab_c_ab_c_...

替代的空白,我们有s1s1

s2 = abacbab_ccab_caabbc_abcc_abacbab_cc...

我们再次用空格代替,结果是s1

s3 = abacbabaccabbcaabbc_abcccabacbab_cc...

另一种替代:

s4 = abacbabaccabbcaabbcaabcccabacbabbcc...

据此,我们已经可以推断出的前30个字符,分别是s

abacbabaccabbcaabbcaabcccabacb

这是正确的输出。

规则

您可以编写完整的程序或函数。最低字节数获胜,并且不允许出现标准漏洞。输入错误会导致崩溃。

测试用例

0  "ab__"    -> ""
1  "ab__"    -> "a"
3  "ab__"    -> "aba"
20 "ab"      -> "abababababababababab"
20 "ab__"    -> "abababababababababab"
20 "ab_"     -> "abaabbabaabaabbabbab"
30 "ab_c_"   -> "abacbabaccabbcaabbcaabcccabacb"
50 "ab_a_cc" -> "abaabccabaaaccabbacccabcaaccabbaaccabaaaccabcaccca"
50 "abc____" -> "abcabcaabcbcaaabcbcbcabcaaababccbcbabccabcabcaaaba"

我们可以按相反的顺序(在顺序重要的语言中)接受输入吗?
马丁·恩德

@MartinBüttner好的,我允许。
Zgarb 2015年

Answers:


4

佩斯,17岁

<ussC,cG\_GUQ*zQQ

输入应该在STDIN的第一行给出字符串,在第二行给出长度。例如:

abc____
50

在这里尝试。

说明:

                             Implicit:
                             z = input()              z is the string.
                             Q = eval(input())        Q is the length.

<               Q            First Q characters of
 u         UQ*zQ             Reduce, with an initial value of z repeated Q times, 
                             on the list range(len(Q)).
                             Since the reduce function doesn't use the sequence variable H
                             this function amounts to applying the inner code Q times to
                             the initial value, where the working variable is G.
  ss                         Sum from list of tuples of strings, to tuple of strings,
                             to string.
    C,                       Zip together
      cG\_                   G split on underscores
          G                  with G.
                             This inserts a character of G between every underscore
                             separated group of G, which amounts to replacing the
                             underscores with characters of G, after summation.

7

APL 29 28

{a⊣(b/a)←a↑⍨+/b←'_'=a←⍺⍴⍵}⍣≡

它的用法是这样的:

fun←{a⊣(b/a)←a↑⍨+/b←'_'=a←⍺⍴⍵}⍣≡
20 fun 'ab_c_'
abacbabaccabbcaabbca

说明:

a←⍺⍴⍵           makes vector long as left argument using repeated chars in right argument
a↑⍨+/b←'_'=a   takes a string from the beginning of string a (a↑⍨), long as the number of _'s in a (+/b←'_'=a)
(b/a)←          puts those chars in place of the _'s in the original vector
a⊣             and returns a
{}⍣≡            repeats function ( {} ) until results doesn't change anymore

Tryapl.org


⍣≡是个好主意。也许我应该尝试将其移植到J ...
FUZxxl

7

CJam,26 24 20字节

多亏了彼得,节省了4个字节。

l~:I*{_'_/[\]zsI<}I*

在这里测试。首先n在STDIN上接受字符串。

您可以通过将所有测试用例按原样粘贴到输入中(-> output如果需要,包括它们)并使用以下测试工具(颠倒代码的顺序)来运行它们:

qN/{"->"/0=S/W%S*

~:I*{_'_/[\]zsI<}I*

]oNo}/

说明

l~:I*{_'_/[\]zsI<}I*
l~                       "Read the input and evaluate.";
  :I                     "Store n in I for future use.";
    *                    "Repeat s n times to ensure it's long enough for the output.";
     {           }I*     "Repeat this block n times. This will always be enough passes.";
      _                  "Duplicate the string.";
       '_/               "Split the string on underscores.";
          [\]            "Swap with the other copy, putting both in an array.";
             z           "Zip the two arrays together, interleaving substrings from the split
                          copy with characters from the unsplit copy. Extraneous
                          characters from the unsplit copy just go at the end and
                          can be ignored.";
              s          "Convert the result into a string, flattening the array in the
                          process. This basically joins the two interleaved strings together.";
               I<        "Truncate to n characters.";

结果将在程序末尾自动打印。

关于的注释[\]原则上,[记住堆栈的当前大小,并将]所有内容收集到数组中最后记住的大小。但是,如果数组大小降到介于两者之间记住的大小以下,则将相应地调整数组的开头。现在,您可能会认为交换前两个数组元素根本不会影响数组大小,但\实际上会弹出两个值,然后以相反的顺序推送它们。这就是将数组的开始向下推二的原因。因此,这[\]是将顶部两个堆栈元素包装在数组中的最短方法。有时,以相反的顺序收集它们的副作用很烦人,但是在这种情况下,这正是我所需要的。


我想你可以替换_'_#) g使用I*。在GolfScript中为我工作。
彼得·泰勒

@PeterTaylor哦,很好的主意,谢谢!
马丁·恩德

6

Python 3,110个字节

n=int(input())
*b,=input()*n
a=b[:n]
while"_"in a:b,a=b[:],[x*(x!="_")or b.pop(0)for x in a]
print("".join(a))

需要更多的打高尔夫球,但是这里有些疯狂。n然后s从STDIN 读入。

有趣的部分是,在循环的分配中,我们进行了复制b,然后b在列表理解期间开始弹出。如果分配是另一种方式,它将无法正常工作!



4

爪哇语-162174

在Java中打高尔夫球时,并不是每天都可以使用do / while循环:D

这只是迭代并在出现空白时进行填充。它一直持续到_结果不再存在。

char[]a(int n,char[]s){char[]o=new char[n];if(n>0)do for(int i=0,j=0;i<n;i++)if(o[i]==95|o[i]<1)o[i]=s[j++%s.length];while(new String(o).contains("_"));return o;}

带换行符:

char[]a(int n,char[]s){
    char[]o=new char[n];
    if(n>0)
        do
            for(int i=0,j=0;i<n;i++)
                if(o[i]==95|o[i]<1)
                    o[i]=s[j++%s.length];
        while(new String(o).contains("_"));
    return o;
}

我不会回答这个问题,但是其他Java回答太久了,让人无法忍受;)
Geobits,2015年

3

Java 8,238

(n,s)->{int i=0,j=0;for(s=String.join("",java.util.Collections.nCopies(n,new String(s))).toCharArray();j<1;){for(i=0;i<n;i++){for(;s[++j]!=95&j<n;);if(j<n)s[j]=s[i];}for(j=1,i=0;i<n;)j=s[++i]==95?0:1;}return java.util.Arrays.copyOf(s,n);}

少打高尔夫球:

(Integer n, char[] s) -> {
    int i = 0, j = 0;
    for (s = String.join("", java.util.Collections.nCopies(n, new String(s))).toCharArray(); j < 1;) {
        for (i = 0; i < n; i++) {
            for (; s[j] != 95 & j < n; j++);
            if (j < n) {
                s[j] = s[i];
            }
        }
        for (j = 1, i = 0; i < n;) {
            j = s[++i] == 95 ? 0 : 1;
        }
    }
    return java.util.Arrays.copyOf(s, n);
}

3

Ruby,60岁

->n,s{eval"r=%1$p.chars;s.gsub!(?_){r.next};"*n%s*=n;s[0,n]}

连接s到自身n时间,然后生成n代码副本,该代码副本将下划线替换为s,评估这些副本,然后返回结果的第一个n字符。由于每个循环中至少删除了一个下划线,因此可以保证为我们提供无n下划线的字符。


什么是正确的语法来运行它?当我调用它f并运行时puts f[10,"ab_"],出现以下错误:in 'eval': undefined method 'next' for #<Array:...。但是,当字符串中没有下划线时,它似乎确实起作用。
泰奥菲尔

哦,有趣的是,看起来String#chars在Ruby 1.9.3和Ruby 2.0之间发生了改变。在Ruby 1中,当没有块时,它返回一个枚举数;在Ruby 2中,它是一个数组。通过更改charseach_char,可以使它对版本不敏感,但要花4个净字节以上的代码。
histocrat

3

Python 2,75

n,s=input()
S='';c=0
for x in s*n:b=x=='_';S+=S[c:c+b]or x;c+=b
print S[:n]

这需要像这样的输入(30,"ab_c_")

在Python中,字符串不允许赋值。因此,很难用所需的字符替换空格。可以通过转换为列表然后返回来解决此问题,但是我发现它更短,仅从头开始生成输出字符串,一次添加一个所需的字符。

正在构造的输出为S,该输出开始为空。我们遍历s多次复制的输入字符来模拟一个圆。我们通过Boolean检查是否为空b。我们检查平等x=='_'而不是比较,因为下划线位于大写字母和小写字母之间。

如果该字符不是空白,则将其添加到S。如果为空,则添加output-so-far的下一个未使用的字母S。我们通过索引指针跟踪使用过的字母c从0开始,每次遇到空白时都会递增。

最后,我们打印n结果字符串的第一个字符S

我们必须使用S[c:c+b]short代替,b*S[c]因为后者在S开始为空且c为0 时会出现越界错误。这没关系,因为我们保证of的第一个字符s是非空白的,因此S[c]永远不需要,但是代码不知道。翻转or短路也可以解决这个问题,但费用较高的字符。


Python 2,83

isaacg 解决方案的Pyth-to-Python端口,它使用splitzip执行替换:

n,s=input()
s*=n
exec"s=''.join(a+b for a,b in zip(s.split('_'),s));"*n
print s[:n]

结果变长了,因为惊奇的是,命名方法在python中很长。但它可以也许金刚砂水磨得到改善s,并s.split('_')在较短的方式在一起。


真好!没想到重建字符串会这么短!
Sp3000

3

哈斯克尔 (93) 67

我已经有一段时间没有写任何Haskell了,所以这可能会 大大缩短。但是它太好了,我们不得不缩短它并使其变得更好!

('_':b)&(d:e)=d:b&e;(a:b)&c=a:b&c
f n s=take n$q where q=cycle s&q

用法:

*Main> f 50 "ab_a_cc"
"abaabccabaaaccabbacccabcaaccabbaaccabaaaccabcaccca"

2

批次-425

我会输吗?

@echo off&setLocal enableDelayedExpansion&set s=%2
if "%3"=="" (for /l %%a in (1,1,%1)do set o=!o!%s%)else set o=%3
set o=!o:~0,%1!&set l=0
:c
if defined s set/al+=1&set "s=%s:~1%"&goto c
set s=%2&set/ap=%1-1
set y=&set c=0&for /l %%a in (0,1,%p%)do set x=!o:~%%a,1!&if !x!==_ (for %%b in (!c!)do set y=!y!!s:~%%b,1!&set/ac+=1)else (set y=!y!!x!)&if !c!==%l% set c=0
if "!y:_=!"=="!y!" echo !y!&goto :EOF
%0 %1 %2 !y!

批次有局限性-我接受这一点。例如; 由于变量解析语法的限制,我不得不使用for循环来获取可用格式的单个变量。for %%b in (!c!)do... 只是存在,所以我可以使用%%b代替,!c!这样我就可以真正地进行字符串操作!s:~%%b,1!并让变量在正确的时间扩展。

我可以做一些非常基本的事情来进一步打高尔夫球,但可能不低于400字节。我很快还会再来的。


3
除非有人发布更好的批处理答案,否则我不会称其为失败:)
Sp3000

@ Sp3000如果只有某人愿意。
2015年

2

ECMAScript 6,78

f=(s,n,i=0)=>[...s.repeat(n)].reduce((s,x)=>s+(x=='_'?s[i++]:x),'').slice(0,n)

从一个空字符串开始,并在每次出现下划线时,将其替换为当前字符串下一个索引处的字符。


1

Python 2-99 97字节


因为4个基于python的提交还不够...

n,s=input();S=s=s*n
while"_"in S:x=iter(s);S="".join(j>"_"and j or next(x)for j in S)
print S[:n]

例:

$ python2 t.py 
(50, "ab_a_cc")
abaabccabaaaccabbacccabcaaccabbaaccabaaaccabcaccca

0

的ECMAScript 6,93 91

(n,s)=>{for(x="_".repeat(n);n=0,/_/.test(x);)x=x.replace(/_/g,a=>s[n++%s.length]);return x}

从第一个版本删除了2个字符。

(n,s)=>{x="_".repeat(n);while(/_/.test(x)){n=0,x=x.replace(/_/g,a=>s[n++%s.length])}return x}

0

C#-162

我偷了Geobits解决方案并将其更改为C#

char[]p(int n,string s){var r=new char[n];if(n>0)do for(int i=0,j=0;i<n;i++)if(r[i]=='_'||r[i]<1)r[i]=s[j++%s.Length];while(r.ToList().IndexOf('_')>=0);return r;}

1个字符更好,因此您可以改善Geobits;)

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.