30
0
Given a string x
, output the characters in x
sorted according to the order of appearance in your source code.
Examples
Source: ThisIs A Test
Input: Is it a Test?
Output: TissI etta?
Source: Harry - yer a wizard.
Input: I'm a what?
Output: aa wh'?Imt
Source: Mr. H. Potter, The Floor, Hut-on-the-Rock, The Sea
Input:
Output:
Rules
- Standard loopholes & i/o rules apply
- Input & output can be either a string, a list of characters, or a list of bytes.
- If a character is used multiple times in the source, use the first occurrence.
- If one or more characters does not appear in the source, they should be at the end; their order does not matter, nor does it have to be consistent.
- Source must be non-empty
- Newlines are treated the same as other characters.
- The order in which the code is executed doesn't matter; just the raw string.
- The input is in the same encoding as the code.
- The input is sorted by characters, not by bytes.
- Sorting is case sensitive
- This is code-golf, so shortest answer in bytes for each language wins!
let f = (s, i, o) => o.split("").map(x => [x, s.indexOf(x) == -1 ? s.length + 100 : s.indexOf(x)]).sort(([, a], [, b]) => a - b).map(([x]) => x).join("") === o && i.split("").sort().join("") === o.split("").sort().join("");let g = (s, i) => i.split("").map(x => [x, s.indexOf(x) == -1 ? s.length + 100 + Math.random() : s.indexOf(x)]).sort(([, a], [, b]) => a - b).map(([a]) => a).join("");$(() => $("button").click(() => $("#result").text(f($("#source").val(), $("#input").val(), $("#output").val()) ? "Valid" : `Invalid; example output: \`${g($("#source").val(), $("#input").val())}\``)));
body,label{display: flex;flex-direction: column;}*{white-space: pre;font-family: Inconsolata, Consolas, mono-space;font-size: 14px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><label>Source: <textarea id="source">ThisIs A Test</textarea></label><label>Input: <textarea id="input">Is it a Test?</textarea></label><label>Output: <textarea id="output">TissI etta?</textarea></label><div><button>Validate</button> <span id="result"></span></div>
Do unprintable characters need to be accounted for? And if not, what about whitespace? – negative seven – 2019-06-27T15:40:43.740
2@negativeseven Any characters from the source code encoding need to be accounted for, including whitespace. – tjjfvi – 2019-06-27T15:46:37.570
2related – Rod – 2019-06-27T17:46:36.107
3You should probably make explicit mention in the spec that matching is case sensitive. – Shaggy – 2019-06-27T23:36:45.363
1Can we get
x
encoded in UTF-8 or UTF-16 if our solution isn't encoded in Unicode at all, and the Unicode characters inx
represent characters in the solution's code page? For example, some golfing languages use custom code pages to reduce their byte count but still be readable with the naked eye. – Erik the Outgolfer – 2019-06-29T07:03:20.517@Erik No, the input is in whatever encoding the source code is in, including custom code pages. – tjjfvi – 2019-06-29T23:39:32.117
If that was allowed, a golfing language answer could probably be a no-op, because none of the characters in the source are in UTF-8. – tjjfvi – 2019-06-29T23:46:12.260
1@tjjfvi I'm not talking about no-ops here (which would be certainly invalid); at least a few of the answers below, instead of taking input in plain bytes (0x00-0xFF), take the input as the UTF-8 string that the byte sequence would represent (e.g. when we post an answer, we usually post the UTF-8 version of our code, not the hexdump), and then use the UTF-8 representation of the source code to sort the input. Is this allowed? I feel like it should be an option too (alongside the plain byte stream), because otherwise solutions in golfing languages with custom codepages would be hindered greatly. – Erik the Outgolfer – 2019-06-30T19:19:14.710
@tjjfvi This is my understanding: We should write some source-code (which is some bytes) and we may use a code-page (which encodes those bytes as some string of characters - whether that be UTF-8 or UTF-16 or ASCII or ...) and running our code must sort an input composed solely of characters that appear on the aforementioned code-page according to their position in the encoded source-code and output that string of characters. We then score our submission by the byte length of the source-code (in bytes, not in whatever encoding is used to display the characters). Is my understanding correct? – Jonathan Allan – 2019-07-01T10:23:38.330
1@Jonathan Yes, that is correct. – tjjfvi – 2019-07-02T03:20:02.160
1@Erik If the source is valid in encoding X, and is given in encoding X, then it may only handle characters in encoding X. The encoding used by the interpreter etc. is irrelevant. – tjjfvi – 2019-07-02T03:22:53.233
1+1 for "Harry - yer a wizard." – HighlyRadioactive – 2019-10-09T10:01:31.923