32
Write a program or function that duplicates letters in a word, so that all the duplicated letters arranged from left to right in the word would form the input array.
For example:
input: chameleon, [c,a,l,n]
output: cchaamelleonn
Input
- The starting word (e.g.
chameleon
) - An array of characters (
[c,a,l,n]
) or a string to represent an array (caln
), or something similar - Input can be through function parameters, STDIN or language equivalents
- All inputs will be lower-case letters (a-z)
Output
The changed word
If there are multiple solutions, any can be printed
input: banana [n,a] possible outputs: bannaana, banannaa |-|---------|-|--->[n,a]
You may assume that the input word (not necessarily the array) will have the letters in the array (in order)
You may also assume that the inputs have no consecutive letters which are the same (NOT apple, geek, green, glass, door...)
Examples
input: abcdefghij, [a,b,c]
output: aabbccdefghij
input: lizard, [i,a,r,d]
output: liizaarrdd
input: coconut, [c,o]
ouput: ccooconut or coccoonut or ccocoonut
input: onomatopoeia, [o,o,a,o,o]
output: oonoomaatoopooeia
input: onomatopoeia, [o,a,o]
output: oonomaatoopoeia or onoomaatoopoeia or oonomaatopooeia etc.
Shortest program wins!
Leaderboard (thanks to Martin Büttner for the snippet)
/* Configuration */
var QUESTION_ID = 51984; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
/* App */
var answers = [], page = 1;
function answersUrl(index) {
return "http://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
answers.push.apply(answers, data.items);
if (data.has_more) getAnswers();
else process();
}
});
}
getAnswers();
var SIZE_REG = /\d+(?=[^\d&]*(?:<(?:s>[^&]*<\/s>|[^&]+>)[^\d&]*)*$)/;
var NUMBER_REG = /\d+/;
var LANGUAGE_REG = /^#*\s*([^,]+)/;
function shouldHaveHeading(a) {
var pass = false;
var lines = a.body_markdown.split("\n");
try {
pass |= /^#/.test(a.body_markdown);
pass |= ["-", "="]
.indexOf(lines[1][0]) > -1;
pass &= LANGUAGE_REG.test(a.body_markdown);
} catch (ex) {}
return pass;
}
function shouldHaveScore(a) {
var pass = false;
try {
pass |= SIZE_REG.test(a.body_markdown.split("\n")[0]);
} catch (ex) {}
return pass;
}
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
answers = answers.filter(shouldHaveScore)
.filter(shouldHaveHeading);
answers.sort(function (a, b) {
var aB = +(a.body_markdown.split("\n")[0].match(SIZE_REG) || [Infinity])[0],
bB = +(b.body_markdown.split("\n")[0].match(SIZE_REG) || [Infinity])[0];
return aB - bB
});
var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
answers.forEach(function (a) {
var headline = a.body_markdown.split("\n")[0];
//console.log(a);
var answer = jQuery("#answer-template").html();
var num = headline.match(NUMBER_REG)[0];
var size = (headline.match(SIZE_REG)||[0])[0];
var language = headline.match(LANGUAGE_REG)[1];
var user = getAuthorName(a);
if (size != lastSize)
lastPlace = place;
lastSize = size;
++place;
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", user)
.replace("{{LANGUAGE}}", language)
.replace("{{SIZE}}", size)
.replace("{{LINK}}", a.share_link);
answer = jQuery(answer)
jQuery("#answers").append(answer);
languages[language] = languages[language] || {lang: language, user: user, size: size, link: a.share_link};
});
var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);
langs.sort(function (a, b) {
if (a.lang > b.lang) return 1;
if (a.lang < b.lang) return -1;
return 0;
});
for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body { text-align: left !important}
#answer-list {
padding: 10px;
width: 50%;
float: left;
}
#language-list {
padding: 10px;
width: 50%px;
float: left;
}
table thead {
font-weight: bold;
}
table td {
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
<h2>Leaderboard</h2>
<table class="answer-list">
<thead>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
</thead>
<tbody id="answers">
</tbody>
</table>
</div>
<div id="language-list">
<h2>Winners by Language</h2>
<table class="language-list">
<thead>
<tr><td>Language</td><td>User</td><td>Score</td></tr>
</thead>
<tbody id="languages">
</tbody>
</table>
</div>
<table style="display: none">
<tbody id="answer-template">
<tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
<tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
</tbody>
</table>
@AlexA. only one instance because otherwise the array formed by the duplicate letters would be
[c,o,c,o]
, rather than[c,o]
. – Stretch Maniac – 2015-06-22T16:03:22.067Yeah sorry, reading it again that's obvious. Thanks. – Alex A. – 2015-06-22T16:08:44.880
2
Seeing this got quite a lot of answers, and many in the same languages, would you be interested in adding the leaderboard snippet? If so, I'm happy to edit it in and amend the answers that don't use the required header format.
– Martin Ender – 2015-06-23T13:12:47.043@MartinBüttner I forgot about that! Added. I had to change
#answer-list
and#language-list
width to50%
to avoid overlapping columns in your snippet. – Stretch Maniac – 2015-06-23T20:49:52.597@StretchManiac That's because some of the answers contain links in their headers which they shouldn't. I'll fix them. – Martin Ender – 2015-06-23T20:50:59.107
"You may also assume that the inputs have no consecutive letters which are the same (NOT apple, geek, green, glass, door...)" both inputs or just the first one? There's an example with "ooaoo" as second input and my code breaks in this case. – aragaer – 2015-06-24T11:33:39.553
1Clarification (see my
bash
+sed
answer): Is it illegal forbanana, na
=>baannana
? I believed that "You may assume that all inputs will have the letters in the array (in order)" is meant to permit, but not require, answers to process both lists sequentially, but @manatwork interpreted it differently. – Toby Speight – 2015-06-24T12:48:54.967Is there a maximum length on either piece of input? – Sparr – 2015-06-24T16:49:42.563
@aragaer that is only the first input, not the array, I'll clarify that. – Stretch Maniac – 2015-06-24T22:41:54.317
@TobySpeight that sentence was meant to merely state that the input possible, for example there would be no
banana ,[t,x]
. In your example, the duplicated letters would form[a,n]
, not[n,a]
. I'll clarify that in the question. – Stretch Maniac – 2015-06-24T22:45:18.197I'm still not completely sure - is
banana, na
=>baannana
allowed? Or am I only allowedbannaana
,bannanaa
orbanannaa
for that input? – Toby Speight – 2015-06-25T08:19:56.103@TobySpeight No. That is not allowed (your first sentence). – Stretch Maniac – 2015-06-25T21:57:28.777