26
3
Let's start by defining a reflection of a character in a string:
Given a string with distinct lowercase alphabetical letters with no spaces such as
abcdefg
, define a reflection of a letter in the stringc
as moving it (without changing the order of any other letter) to a new place in the string such that the number of letters originally to the right of it is now the number of letters to the left of it.
Thus, a reflection of the letter c
in abcdefg
would be abdecfg
. Explanation: there were 4 letters to the right of c
and now, there are 4 letters to the left of c
.
Some more examples:
Reflecting the character
e
inmyfriend
would form the stringmyefrind
Reflecting the character
a
inaxyz
would form the stringxyza
.Reflecting the character
b
inabc
would form the stringabc
.Reflecting the character
d
ind
would form the stringd
.Reflecting the character
e
inef
would form the stringfe
.
For more information or to try out some test cases, here is a (somewhat long) program I wrote in C++.
The Challenge
Given a string with distinct lowercase letters, go through each character alphabetically and "reflect" it in the string.
Clarifications: The letters in the string are from a-z
, there are no spaces, the letters are unique, and the string is at least 1 letter long and at most 26 letters long.
Examples
Input:
dcba
. Output:dcba
.
Reason: First, reflect the a
as it is the character in the string that comes earliest in the alphabet. You will get adcb
. Then, reflect the b
as it comes next in the alphabet, to get badc
. Then, reflect the c
to get cbad
, and then the d
to get dcba
.
Input:
myface
. Output:fyecma
.
Hint: Go through the letters in the order a, c, e, f, m, y
.
Input:
a
. Output:a
.
Input:
acb
. Output:bac
.
Input:
cwmfjordbankglyphsvextquiz
. Output:ieabhqzugdltkfnvpjxsormycw
.
Scoring
- The input and output can be given by any convenient method.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
- Accepting ~100 hours after posting.
Current Winner
<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><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="language-list"> <h2>Shortest Solution 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> <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> <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><script>var QUESTION_ID = 162891; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 12012; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) 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); } }</script>
2Could anyone give me insight as to whether this puzzle is okay? It's my second one here (my first was closed as off-topic...whoops). I tried to make it really complete, but I'm not sure if I'm missing anything. I'd really like to improve and have fun on this SE! Thanks guys :D – NL628 – 2018-04-20T02:57:13.147
6This looks good. I might give a few longer examples (in particular, it might be good for the first examples section to present more than what are seemingly edge cases). – Esolanging Fruit – 2018-04-20T03:36:04.150
@EsolangingFruit Okay, thank you so much for your comment! I really appreciate it. Will do :D – NL628 – 2018-04-20T03:36:32.230
1Clarification request: "the letters are unique" means each letter occurs only once in input, is that correct? – GPS – 2018-04-20T05:06:22.777
4Can we take the input and output in uppercase instead of lowercase? – Kevin Cruijssen – 2018-04-20T07:05:20.813
5
@NL628 By the way, if you want to get insight on a challenge before posting it here, then post it in the Sandbox.
– Erik the Outgolfer – 2018-04-20T12:01:53.773@GPS That is correct. – NL628 – 2018-04-20T17:25:59.493
@EriktheOutgolfer Oh no! Was I supposed to post there instead? Aiyaa sorry about that...I'm really sorry :( – NL628 – 2018-04-20T17:26:19.530
@KevinCruijssen I would prefer it to be in lowercase, but if you think of a really cool one, I'll edit the question to allow either ALL uppercase or ALL lowercase. – NL628 – 2018-04-20T17:26:55.440
1@NL628 No, nobody ever has to post it there, it's just a recommendation. – Erik the Outgolfer – 2018-04-21T09:11:54.423
Okay @EriktheOutgolfer Thank you so much! – NL628 – 2018-04-21T18:19:57.917