请填写空白!


11

(不,不是这个也不是任何这些

给定一个字符串和一个字符串列表,用相应的字符串填充输入字符串中的所有空格。

输入输出

输入字符串仅包含字母字符,空格和下划线。它是非空的,并且不以下划线开头。换句话说,输入字符串与正则表达式匹配^[a-z A-Z]([a-z A-Z_]*[a-z A-Z])?$

输入列表中的每个字符串都是非空的,并且仅包含字母数字字符和空格。换句话说,它们匹配正则表达式^[a-z A-Z]+$

空白是下划线(_)的连续序列,下划线()之前或之后都没有。

输入字符串包含n一些正整数的空格n,字符串列表恰好包含n字符串。

通过将k输入字符串中的每个-th空格替换为输入字符串k列表中的-th字符串来获得输出。

给定一个输入字符串"I like _____ because _______ _____ing"和一个字符串列表["ice cream", "it is", "satisfy"],我们可以找到如下输出:

  • 第一个空格紧随其后"like "。我们"ice cream"用来填充它"I like ice cream because ______ _____ing"
  • 第二个空格紧随其后"because "。我们"it is"用来填充它"I like ice cream because it is _____ing"
  • 第三个空格紧随其后"is "。我们"satisfy"用来填充它"I like ice cream because it is satisfying"

我们输出最后一个字符串"I like ice cream because it is satisfying"

测试用例

input string, input list => output
"Things _____ for those who ____ of how things work out _ Wooden",["work out best","make the best","John"] => "Things work out best for those who make the best of how things work out John Wooden"
"I like _____ because _______ _____ing",["ice cream","it is","satisfy"] => "I like ice cream because it is satisfying"
"If you are ___ willing to risk _____ you will ha_o settle for the ordi_____Jim ______n",["not","the usual","ve t","nary ","Roh"] => "If you are not willing to risk the usual you will have to settle for the ordinary Jim Rohn"
"S____ is walking from ____ to ____ with n_oss of ___ W_____ Churchill",["uccess","failure","failure","o l","enthusiasm","inston"] => "Success is walking from failure to failure with no loss of enthusiasm Winston Churchill"
"If_everyone_is_thinking ____ ____ somebody_isnt_thinking G____e P____n",[" "," "," ","alike","then"," "," ","eorg","atto"] => "If everyone is thinking alike then somebody isnt thinking George Patton"
"Pe_________e __say ____motivation does__ last Well___her doe_ bathing____thats why we rec____nd it daily _ __________lar",["opl","often ","that ","nt"," neit","s","  ","omme","Zig","Zig"] => "People often say that motivation doesnt last Well neither does bathing  thats why we recommend it daily Zig Ziglar"

5
琐碎任务的很多解释。

Answers:


5

,5个字节

'_%.\

在线尝试!

Convex是基于CJam的语言,此答案与我的CJam答案几乎相同,但l~此处不需要,因为Convex在程序开始时会自动进行参数评估。

说明:

'_%.\ e# Full program only
'_    e# Push '_'
  %   e# Split and remove empty chunks
   .\ e# Vectorize by Swap

4

Japt,8字节

r"_+"@Vv

在线测试!

我觉得我错过了规则中的一些隐藏问题,因为这非常简单:“将字符串中的每个下划线替换为数组中的下一项。”


3

JavaScript,35个字节

a=>b=>a.replace(/_+/g,a=>b.shift())

在线尝试


1
我也有 另外,a=>b=>String.raw({raw:a.split(/_+/)},...b)
ETHproductions's


2

MATL,9字节

'_+'i1&YX

在线尝试!

说明

'_+'   % Push this string: regexp pattern
i      % Input cell array of replacement strings
1      % Push 1
&YX    % Four-input regexp replacement. This implicitly inputs the original
       % string, and consecutively replaces each first occurrence of the 
       % regexp pattern in that string by one of the replacement strings.
       % Implicitly display

2

CJam,7个字节

l~'_%.\

在线尝试!

-1感谢Martin Ender的巧妙End俩

说明:

l~'_%.\ e# Full program only
l       e# Input line
 ~      e# Eval
  '_    e# Push '_'
    %   e# Split and remove empty chunks
     .\ e# Vectorize by Swap

.\代替\]z
Martin Ender

@MartinEnder,这对o_o起作用
Erik the Outgolfer

当然,\是二进制运算符。:)
Martin Ender

@MartinEnder好像我对CJam中的映射如何工作有点了解。
Erik the Outgolfer

@MartinEnder第二个想法还好,我怎么没想到呢?不喜欢,我不知道映射的行为,例如[1 2 3]:_-> [1 1 2 2 3 3]同样适用于.
Erik the Outgolfer

2

果冻8 7字节

ṣ”_¬Ðfż

在线尝试!编辑:感谢@Erik the Outgolfer保存了1个字节。说明:

ṣ”_         Split on underscores
   ¬Ðf      Discard empty values
      ż     Zip with second input
            Implicit concatenated output


1

Pyth,10个字节

s.i:E"_+"3

在这里尝试!

这个怎么运作?

si:E“ _ +” 3完整程序。

   :E 3在正则表达式的匹配项上分割第二个输入...
     “ _ +”正则表达式“ _ +”(匹配1个或多个下划线)
 .i将拆分列表的元素与输入交织。
s连接到字符串。

1

RProgN 2,11字节

x='_+'³[x‘r

在堆栈顶部获取一个字符串和一堆字符串。

堆栈的第一个(顶部)元素在右侧,因此输入从右到左。

解释

x='_+'³[x‘r
x=          # Set the stack of replacements to x
  '_+'³   r # Replace each chunk of _'s with the function...
       [    # Pop off the group of _'s
        x‘  # Pop the top element off x. Use that.

在线尝试!


1

Java 8,57字节

a->s->{for(String i:a)s=s.replaceFirst("_+",i);return s;}

当我阅读挑战时,我首先想到我们应该用一个随机顺序的单词组成一个语法正确的句子,但是用每个连续单词替换行会更容易。;)

说明:

在这里尝试。

a->s->{            // Method with String-array and String parameter and String return-type
  for(String i:a)  //  Loop over the input-array
    s=s.replaceFirst("_+",i);
                   //   Replace the first line (1 or more adjacent "_") with the substring
                   //  End of loop (implicit / single-line body)
  return s;        //  Return the result
}                  // End of method




0

Python 2,61个字节

import re
s,l=input()
for i in l:s=re.sub('_+',i,s,1)
print s

在线尝试!

该死的。

Python 2,63个字节

import re
f=lambda s,l:l and f(re.sub('_+',l[0],s,1),l[1:])or s

在线尝试!


通过re.sub函数传递60个字节
Stephen

@Stephen Eh ...太接近您的解决方案了。我将坚持下去,并找到更多晦涩的解决方案。:P
完全人类的,2017年

通过使用函数来改进@Stephen的建议,从而获得53个字节
乔纳森·弗雷希

@JonathanFrech是的,但这已经是我的答案了:P
Stephen

@斯蒂芬哦; 没有看到您的答案...:d
乔纳森·弗雷奇


0

SOGL V0.12,7 个字节

lΔ╔*№≤ŗ

在这里尝试!

说明:

l        get the strings length
 Δ       range
  ╔*     multiply an underscore with each of the numbers
    №    reverse vertically (so the longest underscores get replaced first)
     ≤   put the array on top - order parameters for ŗ correctly
      ŗ  replace in the original string each any occurence of the underscores with its corresponding item in the array

0

Python 2,49个字节

s,l=input();import re;print re.sub("_+","%s",s)%l

在线尝试!

列表输入被视为元组。允许实际列表作为输入将增加另外七个字节,因为需要进行转换(tuple(...))。

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.