# Fill in the Blanks, Please!

(No, not this nor any of these)

Given a string and a list of strings, fill in the all blanks in the input string with corresponding strings.

## Input/Output

The input string contains only alphabetic characters, spaces, and underscores. It is nonempty and does not start with an underscore. In other words, the input string matches the regex ^[a-z A-Z]([a-z A-Z_]*[a-z A-Z])?$ Every string in the input list is nonempty and contains only alphanumeric characters and spaces. In other words, they match the regex ^[a-z A-Z]+$.

A blank is a contiguous sequence of underscores (_) which is neither preceded nor proceeded by an underscore.

The input string contains n blanks for some positive integer n, and the list of strings contains exactly n strings.

The output is obtained by replacing each k-th blank in the input string by the k-th string in the input list of strings.

## Example

Given an input string "I like _____ because _______ _____ing" and a list of strings ["ice cream", "it is", "satisfy"], we can find the output as follows:

• The first blank comes directly after "like ". We fill that in with "ice cream" to get "I like ice cream because ______ _____ing".
• The second blank comes directly after "because ". We fill that in with "it is" to get "I like ice cream because it is _____ing".
• The third blank comes directly after "is ". We fill that in with "satisfy" to get "I like ice cream because it is satisfying".

We output the final string "I like ice cream because it is satisfying".

## Test Cases

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"

• A lot of explanations for trivial task. – user72349 Sep 21 '17 at 3:07

# Convex, 5 bytes

'_%.\


Try it online!

Convex is a CJam-based language, and this answer is almost the same as my CJam answer, except for l~ which is unneeded here, since Convex does automatic argument evaluation at the start of the program.

Explanation:

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


# Japt, 8 bytes

r"_+"@Vv


Test it online!

I feel I missed some hidden catch in the rules because this is extremely simple: "replace each run of underscores in the string with the next item in the array."

# JavaScript, 35 bytes

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


Try it online

• Same thing I had. Alternatively, a=>b=>String.raw({raw:a.split(/_+/)},...b) – ETHproductions Sep 21 '17 at 3:03

# Proton, 42 bytes

a=>b=>re.sub("_+",_=>b.pop(0),a)
import re


Try it online!

Stupid imports...

Same thing in python:

# Python 3, 53 bytes

lambda a,b:re.sub("_+",lambda _:b.pop(0),a)
import re


Try it online!

• ...How did I frigging miss that. ._. – totallyhuman Sep 21 '17 at 3:15

# MATL, 9 bytes

'_+'i1&YX


Try it online!

### Explanation

'_+'   % 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


# CJam, 7 bytes

l~'_%.\


Try it online!

-1 thanks to a clever trick by Martin Ender.

Explanation:

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

• .\ instead of \]z. – Martin Ender Sep 21 '17 at 12:20
• @MartinEnder That works o_o – Erik the Outgolfer Sep 21 '17 at 12:22
• Sure, \ is a binary operator. :) – Martin Ender Sep 21 '17 at 12:23
• @MartinEnder Seems like I have gotten a bit too much in how mapping works in CJam then. – Erik the Outgolfer Sep 21 '17 at 12:32
• @MartinEnder OK on second thought how didn't I think of that? Not like I didn't know how mapping behaves e.g. [1 2 3]:_ -> [1 1 2 2 3 3] likewise for .... – Erik the Outgolfer Sep 21 '17 at 16:22

# Jelly, 8 7 bytes

ṣ”_¬Ðfż


Try it online! Edit: Saved 1 byte thanks to @Erik the Outgolfer. Explanation:

ṣ”_         Split on underscores
ż     Zip with second input
Implicit concatenated output


# Perl 5, 25 + 1 (-p) = 26 bytes

@a=eval<>;s|_+|shift@a|eg


Try it online!

• Came up with basically the same, except using <> to negate the eval and shift: Try it online!. There's got to be a way to avoid the replacing the newlines... – Dom Hastings Sep 21 '17 at 7:35
• This subroutine also has 26 bytes: sub{shift=~s|_+|shift|egr}. If you reversed the arguments, you could use pop and get it down to 22 bytes. – nwellnhof Sep 21 '17 at 14:18

# Pyth, 10 bytes

s.i:E"_+"3


Try it here!

# How it works?

s.i:E"_+"3   Full program.

:E    3   Split the second input on matches of the regex...
"_+"    The regex "_+" (matches 1 or more underscores)
.i          Interleave the elements of the split list with the input.
s            Join to a string.


# RProgN 2, 11 bytes

x='_+'³[x‘r


Takes a string and a stack of strings on the top of the stack.

The first (top) element of a stack is to the right, hence input is right to left.

## Explained

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.


Try it online!

# Java 8, 57 bytes

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


When I read the challenge I at first thought we should make a grammatically correct sentence with the words being in random order, but just replacing lines with each sequential word is easier. ;)

Explanation:

Try it here.

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


# 05AB1E, 12 bytes

„__¤:sv'_y.;


Try it online!

Explanation

„__¤:          # replace all runs of multiple "_" with a single "_"
sv        # for each y in the list of replacements
'_y.;   # replace the first instance of "_" with y

• – Erik the Outgolfer Sep 21 '17 at 12:14
• @EriktheOutgolfer: Thanks for noticing. I rolled back to my previous version which handled that. – Emigna Sep 21 '17 at 12:20

# C# (.NET Core), 97 96 + 18 bytes

Saved 1 byte thanks to Kevin Cruijssen!

The 18 bytes are for using System.Linq.

s=>l=>string.Concat(s.Split('_').Where(x=>x!="").Zip(l.Concat(new[]{""}),(x,y)=>x+y).ToArray());


Try it online!

• You can change (s,l)=> to s=>l=> to save a byte. Try it online! – Kevin Cruijssen Sep 21 '17 at 12:53

# Ruby, 28 + 1 = 29 bytes

\$_=gsub(/_+/){|i|gets[/.*/]}


Uses -p

The first line is the format string, the rest of the lines are the array.

Try it online!

# Python 2, 61 bytes

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


Try it online!

Damn.

## Python 2, 63 bytes

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


Try it online!

• 60 bytes by passing re.sub a function – Stephen Sep 21 '17 at 3:14
• @Stephen Eh... That's just too close to your solution. I'll just stick to this and find more obscure solutions. :P – totallyhuman Sep 21 '17 at 3:17
• 53 bytes by using a function to improve @Stephen's proposal. – Jonathan Frech Sep 21 '17 at 7:27
• @JonathanFrech yeah, but that's exactly my answer already :P – Stephen Sep 21 '17 at 15:27
• @Stephen Oh; did not see your answer... :d – Jonathan Frech Sep 21 '17 at 15:37

# Stacked, 21 bytes

[@y'_+'[y shift]repl]


Try it online!

Replaces all runs of _ with y shift. Nothing original, as it seems.

# SOGL V0.12, 7 bytes

lΔ╔*№≤ŗ


Try it Here!

Explanation:

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


# Python 2, 49 bytes

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


Try it online!

The list input is taken as a tuple. Allowing actual lists as input would add another seven bytes, as a conversion would be required (tuple(...)).