Related to: Make a ;# interpreter
In the above linked challenge the task was to create an interpreter for the esoteric language ;#
.
The ;#
language
The language has exactly two commands: ;
and #
(all other characters are ignored by the interpreter):
;
: Increment the accumulator
#
: Modulo the accumulator by 127, print the corresponding ASCII character and reset the accumulator to 0.
Challenge
Because I am lazy but still want to test some more testcases, I need a program or function which converts plain text to ;#
code.
Input
The input is a string, taken either as argument or through stdin. It will only contain printable ASCII characters and newlines.
Output
The output is the generated ;#
program by returning, or printing to stdout. As long as the program is valid, it may contain excess characters other than #
and ;
as all other characters are ignored.
Examples
Input: Hello, World!
Output: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
Input: ABC
Output: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
Input: ;#
Output: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#
Leaderboard
var QUESTION_ID=122139,OVERRIDE_USER=73772;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}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>
87 Answers 87
;#+, 61 bytes
Outgolfed by Conor O'Brien
;;;;;;;(~;;;;;~-;-)~>:~;;;;(~;;;;;;~-;-)~>~-*((;~<#~):<#-:-*)
Note that the input has a trailing null byte.
-
14\$\begingroup\$ Points for style. \$\endgroup\$Chowlett– Chowlett2017年05月23日 08:19:55 +00:00Commented May 23, 2017 at 8:19
-
2\$\begingroup\$ I approve of this answer :D definitely the right language for the job \$\endgroup\$Conor O'Brien– Conor O'Brien2017年05月23日 13:41:50 +00:00Commented May 23, 2017 at 13:41
;#+, 40 bytes
;;;;;~+++++++>~;~++++:>*(-(;~<#~):<#-*:)
Try it online! Input is terminated with a null byte.
Explanation
The code is split into two parts: generation and iteration.
Generation
;;;;;~+++++++>~;~++++:>
This puts the constants ;
and #
into memory as such:
;;;;;~+++++++>~;~++++:>
;;;;; set A to 5
~ swap A and B
+++++++ add B to A 7 times
(A, B) = (5*7, 5) = (35, 5)
> write to cell 0
~ swap A and B
; increment A
~ swap A and B
(A, B) = (35, 6)
++++ add B to A 4 times
(A, B) = (59, 6)
: increment cell pointer
> write to cell 1
Iteration
*(-(;~<#~):<#-*:)
* read a character into A
( * ) while input is not a null byte:
- flip Δ
( ) while A != 0
; decrement
~ swap A and B
< read ";" into A
# output it
~ swap A and B
: decrement cell pointer
< read "#" into A
# output it
- flip Δ
* take another character from input
: increment cell pointer
-
1\$\begingroup\$ All this from a joke language I made when I was bored. I'm flattered. \$\endgroup\$2017年05月23日 14:11:21 +00:00Commented May 23, 2017 at 14:11
-
\$\begingroup\$ @RandomUser :D it's a fun concept to play around with \$\endgroup\$Conor O'Brien– Conor O'Brien2017年05月23日 14:59:35 +00:00Commented May 23, 2017 at 14:59
-
\$\begingroup\$ huh. What if I want the program to print out a null byte in ;#? \$\endgroup\$tuskiomi– tuskiomi2017年08月15日 13:44:08 +00:00Commented Aug 15, 2017 at 13:44
-
\$\begingroup\$
#
@tuskiomi. Try it online! \$\endgroup\$Conor O'Brien– Conor O'Brien2017年08月15日 18:06:55 +00:00Commented Aug 15, 2017 at 18:06 -
\$\begingroup\$ @ConorO'Brien how would I input that into your program? \$\endgroup\$tuskiomi– tuskiomi2017年08月15日 18:10:01 +00:00Commented Aug 15, 2017 at 18:10
Jelly, (削除) 10 (削除ここまで) (削除) 8 (削除ここまで) 7 bytes
O";ẋp"#
O";ẋp"# Main Link
O Map over `ord` which gets the codepoint of every character
";ẋ Repeat ';' the required number of times
"# '#'
p Cartesian Product; this puts a '#' at the end of each element in the array
Implicit Output shows as a single string
-2 bytes thanks to @Emigna
-1 byte thanks to @Dennis
-
\$\begingroup\$ Could you do
O";ẋ;€"#
instead? \$\endgroup\$Emigna– Emigna2017年05月23日 06:38:08 +00:00Commented May 23, 2017 at 6:38 -
\$\begingroup\$ @Emigna Ah, yes, thanks. I don't quite get how that works but I kind of understand it. Thanks! \$\endgroup\$2017年05月23日 06:40:04 +00:00Commented May 23, 2017 at 6:40
-
4\$\begingroup\$
;€
can becomep
. \$\endgroup\$Dennis– Dennis2017年05月23日 07:10:13 +00:00Commented May 23, 2017 at 7:10 -
\$\begingroup\$ @Dennis Oh now I understand how that works. Thanks! :) \$\endgroup\$2017年05月23日 12:11:00 +00:00Commented May 23, 2017 at 12:11
GS2, 6 bytes
■しかく•;2•#
Reversible hexdump (xxd)
0000000: ff 07 3b 32 07 23 ■しかく•;2•#
How it works
■しかく Map the rest of the program over all code points C of the input.
•; Push ';'.
2 Multiply; repeat ';' C times.
•# Push '#'.
-
2\$\begingroup\$
■しかく
= what the heck? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月23日 08:47:33 +00:00Commented May 23, 2017 at 8:47 -
2\$\begingroup\$
2
is the multiplication command? GS2 is weird :P \$\endgroup\$ETHproductions– ETHproductions2017年05月23日 10:34:13 +00:00Commented May 23, 2017 at 10:34 -
1\$\begingroup\$ @EriktheOutgolfer executes the code for each of the input's character's code point o_O \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年05月23日 12:15:52 +00:00Commented May 23, 2017 at 12:15
-
\$\begingroup\$ @EriktheOutgolfer That sounds fancier than it is.
■しかく
is just map, and GS2 implements strings as lists of integers. \$\endgroup\$Dennis– Dennis2017年05月23日 15:43:32 +00:00Commented May 23, 2017 at 15:43 -
\$\begingroup\$ @ETHproductions GS2 isn't character-based; it interprets the source code as a raw byte stream, and there's usually no connection between the instruction and the CP-437 character the byte encodes. In x86_64 byte code,
2
is XOR... \$\endgroup\$Dennis– Dennis2017年05月23日 15:48:33 +00:00Commented May 23, 2017 at 15:48
Taxi, 779 bytes
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.[c]Switch to plan "e" if no one is waiting.Pickup a passenger going to Charboil Grill.Go to Charboil Grill:n 1 l 3 l 3 l.Pickup a passenger going to The Underground.Go to Writer's Depot:w 1 r.[p]; is waiting at Writer's Depot.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.Go to The Underground:n 1 r 1 l.Switch to plan "n" if no one is waiting.Pickup a passenger going to The Underground.Go to Zoom Zoom:n 3 l 2 r.Go to Writer's Depot:w.Switch to plan "p".[n]# is waiting at Writer's Depot.Go to Writer's Depot:n 3 l 2 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Switch to plan "c".[e]
Ungolfed:
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
[c]
Switch to plan "e" if no one is waiting.
Pickup a passenger going to Charboil Grill.
Go to Charboil Grill: north 1st left 3rd left 3rd left.
Pickup a passenger going to The Underground.
Go to Writer's Depot: west 1st right.
[p]
; is waiting at Writer's Depot.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.
Go to The Underground: north 1st right 1st left.
Switch to plan "n" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Zoom Zoom: north 3rd left 2nd right.
Go to Writer's Depot: west.
Switch to plan "p".
[n]
# is waiting at Writer's Depot.
Go to Writer's Depot: north 3rd left 2nd left.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
Switch to plan "c".
[e]
Explanation:
Pick up stdin and split it into characters.
Covert each character to ASCII.
Print ";" as you count down from that ASCII to zero.
Print "#".
Pickup the next character and repeat until done.
-
\$\begingroup\$ +1 I just love languages such as this and Mornington Crescent, the code is just so beautiful! \$\endgroup\$Karl-Johan Sjögren– Karl-Johan Sjögren2017年05月26日 10:55:55 +00:00Commented May 26, 2017 at 10:55
Brainfuck, 43 bytes
+[+[<]>->++]--[>--<+++++++]>-<,[[<.>-]>.<,]
Null byte terminates the program.
Explanation
+[+[<]>->++] 59 (semicolon) location 5
--[>--<+++++++]>- 35 (hash) location 7
<,[ input location 6
[ while input byte not 0
<.> print semicolon
- decrement input byte
]
>.< print hash
,] loop while input not null
-
\$\begingroup\$ That's impressively small for Brainf*ck. \$\endgroup\$MD XF– MD XF2017年05月23日 21:48:44 +00:00Commented May 23, 2017 at 21:48
-
\$\begingroup\$ almost competes with the python answer. Impressive. \$\endgroup\$raddish0– raddish02017年06月09日 16:07:28 +00:00Commented Jun 9, 2017 at 16:07
05AB1E, 8 bytes
×ばつ'#«J
Explanation
Ç # convert each input char to its ascii value
×ばつ # repeat ";" those many times
'#« # append a "#" to each run of semi-colons
J # join to string
-
\$\begingroup\$ -1 without
Ç
by taking input as a list of codepoints. \$\endgroup\$Makonede– Makonede2021年04月21日 19:44:42 +00:00Commented Apr 21, 2021 at 19:44
-
3\$\begingroup\$
for s in input():print(";"*ord(s)+"#")
is one byte shorter. \$\endgroup\$ovs– ovs2017年05月23日 07:01:22 +00:00Commented May 23, 2017 at 7:01 -
3\$\begingroup\$ @ovs that moment when you assume a list comprehension is shorter. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年05月23日 12:09:36 +00:00Commented May 23, 2017 at 12:09
-
2\$\begingroup\$ That doesn't accept a string with a newline does it? \$\endgroup\$Tim– Tim2017年05月24日 12:34:05 +00:00Commented May 24, 2017 at 12:34
><>, 22 bytes
i:0(?;\"#"o
o1-:?!\";"
Try it online, or at the fish playground
Input is STDIN, output is STDOUT. In><>, characters and ASCII codes are the same thing, so all we need to do is read a character, print ";"
and decrement the character until it's 0, then print "#"
and loop until there's no more input left.
F#, 79 bytes
let c i=System.String.Join("#",Seq.map(fun c->String.replicate(int c)";")i)+"#"
Expanded
// string -> string
let convert input =
System.String.Join(
"#", // join the following char seq with "#"
input // replicate ";" n times where n = ASCII value of char c
|> Seq.map (fun c-> String.replicate (int c) ";")
) + "#" // and add the last "#" to the output
convert takes the input string and outputs a ;# program
Usage
convert "Hello, World!" |> printfn "%s"
convert "ABC" |> printfn "%s"
convert ";#" |> printfn "%s"
PowerShell, (削除) 29 (削除ここまで) (削除) 27 (削除ここまで) 25 bytes
$args|% t*y|%{';'*$_+'#'}
Pretty straightforward. Takes input as command-line argument. Output is a valid ;# program that prints the requested text.
-
\$\begingroup\$ It need to join result strings. \$\endgroup\$mazzy– mazzy2018年09月07日 08:55:36 +00:00Commented Sep 7, 2018 at 8:55
-
\$\begingroup\$ @mazzy: From the task description: »As long as the program is valid, it may contain excess characters other than
#
and;
as all other characters are ignored.« \$\endgroup\$Joey– Joey2018年09月07日 10:39:29 +00:00Commented Sep 7, 2018 at 10:39 -
\$\begingroup\$ as you wish :-) \$\endgroup\$mazzy– mazzy2018年09月07日 11:10:58 +00:00Commented Sep 7, 2018 at 11:10
-
\$\begingroup\$ quotation marks can be removed.
$args
is enough. \$\endgroup\$mazzy– mazzy2018年09月07日 13:49:25 +00:00Commented Sep 7, 2018 at 13:49 -
\$\begingroup\$ Unless the argument is numeric. \$\endgroup\$Joey– Joey2018年09月07日 14:28:18 +00:00Commented Sep 7, 2018 at 14:28
brainfuck, 47 bytes
+++++++[->++++++++>+++++<<]>+++<,[[>.<-]>>.<<,]
See also: ovs's answer, which takes a similar approach, but with a different method of generating constants and a different cell layout.
Explanation:
This challenge lines up with the brainfuck spec pretty well, which means the solution is essentially trivial. Brainfuck takes input as ASCII values, which is exactly what ;# need to output as.
The schematic for transpiling is simple: Generate the ASCII value for ;
and #
, print ;
equal to the ASCII value of the input character, print #
, repeat for every input.
+++++++[- 7
>++++++++ * 8 = 56
>+++++<< * 5 = 35 (#)
]>+++< 56 + 3 = 59 (;)
,[ Input into first cell
[>.<-] Print ;'s equal to ASCII input
>>.<<, Print one #
] End on EOF
Mathematica, 49 bytes
StringRepeat[";",#]<>"#"&/@ToCharacterCode@#<>""&
Explanation
Converts the input string to a list of character codes, then Map
s the function StringRepeat[";",#]<>"#"&
over the list, then StringJoin
s the result with the empty string.
-
\$\begingroup\$ Why do you need the
<>""
? \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月19日 16:01:12 +00:00Commented Jun 19, 2017 at 16:01 -
\$\begingroup\$ @CalculatorFeline Without it I would be left with a list of strings for each character.
StringJoin
ing (<>
) the empty string concatenates each string. \$\endgroup\$user61980– user619802017年06月20日 16:18:06 +00:00Commented Jun 20, 2017 at 16:18 -
\$\begingroup\$ Forgot about that :P \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月20日 16:32:23 +00:00Commented Jun 20, 2017 at 16:32
JavaScript, (削除) 55 (削除ここまで) (削除) 54 (削除ここまで) (削除) 51 (削除ここまで) (削除) 50 (削除ここまで) 48 bytes
s=>1+[...s].map(c=>";".repeat(Buffer(c)[0])+"#")
- 1 byte saved thanks to Neil.
Alternatives
If we can take input as an array of individual characters then 5 bytes can be saved.
a=>1+a.map(c=>";".repeat(Buffer(c)[0])+"#")
If we can also output as an array then 2 more bytes can be saved.
a=>a.map(c=>";".repeat(Buffer(c)[0])+"#")
-
\$\begingroup\$
\n
should becomes;;;;;;;;;;#
. \$\endgroup\$Neil– Neil2017年05月23日 09:13:37 +00:00Commented May 23, 2017 at 9:13 -
\$\begingroup\$ Hmm ... that's strange. Guess I'll have to roll back to the longer solution, so. Thank, @Neil. \$\endgroup\$Shaggy– Shaggy2017年05月23日 09:17:10 +00:00Commented May 23, 2017 at 9:17
-
2\$\begingroup\$ I think you could change
.
to[^]
, which would still leave it a byte shorter thanmap/join
? \$\endgroup\$Neil– Neil2017年05月23日 09:18:50 +00:00Commented May 23, 2017 at 9:18 -
\$\begingroup\$ Yup, that did the job, @Neil :) \$\endgroup\$Shaggy– Shaggy2017年05月23日 09:23:24 +00:00Commented May 23, 2017 at 9:23
-
\$\begingroup\$ Just a heads up, the
join()
in your previous answer was unnecessary given the specification for;#
, and you can also declare that the input for your function is an array of characters, though the second suggestion is a bit of a stretch. Either way, that brings you down to at most 48 bytes. \$\endgroup\$Patrick Roberts– Patrick Roberts2017年05月23日 21:55:10 +00:00Commented May 23, 2017 at 21:55
Aceto, 19 bytes
Since there's an interpreter in Aceto, I thought there outta be an Aceto answer to this challenge as well. It fits neatly in a 2rd order Hilbert curve:
\n;*
'o'p
`!#'
,dpO
First of all, we read a single character (,
) and duplicate and negate it to test whether it is a newline (d!
, when reading a newline, an empty character is normally pushed on the stack). I then use what I think is a pretty clever trick to handle the newline case compactly:
`'\n
If the value on the stack is True
(we read a newline), that code means: do (`
) put a character literal on the stack ('
), which is a newline: \n
.
If the value on the stack is False
(we didn't read a newline), that code means: don't (`
) read a character literal ('
). That means the next character is executed as a command. Fortunately, a backslash escapes the next command (it makes it so that it doesn't get executed), so n
doesn't print a newline (which is what n
usually does).
The rest of the code is straightforward; we convert the character on the stack to the integer of its unicode codepoint (o
), we push a literal semicolon (';
), multiply the number with the string (*
, like in Python), p
rint the result, push a literal ('
) #
, p
rint it too, and go back to the O
rigin.
Run with -F
if you want to see immediate results (because buffering), but it works without, too.
Perl, 24 bytes
s/./";"x(ord$&)."#"/ges
Run with perl -pe
.
Alternative solution:
say";"x ord,"#"for/./gs
Run with perl -nE
.
Solace, 11 bytes
Yay, new languages.
';@jx{'#}Ep
Explanation
'; Push the code point of ';' (59).
@j Push the entire input as a list of code points.
x For each code point in the input, repeat 59 that many times.
{ }E For each resulting list of 59s:
'# Push the code point of '#' (35).
p Flatten and print as unicode characters.
Fourier, 19 bytes
$(I(`;`&j)`#`0~j&i)
To run, you must enclose the input string in quotation marks.
Explanation pseudocode
While i != Input length
temp = pop first char of Input
While j != Char code of temp
Print ";"
Increment j
End While
Print "#"
j = 0
Increment i
End While
R, (削除) 59 (削除ここまで) 56 bytes
for(x in utf8ToInt(scan(,'')))cat(rep(';',x),'#',sep="")
-
\$\begingroup\$ After 3 years, I've finally managed to find a shorter approach... \$\endgroup\$Dominic van Essen– Dominic van Essen2020年09月27日 16:33:52 +00:00Commented Sep 27, 2020 at 16:33
Befunge-98 (FBBI), (削除) 23 (削除ここまで) (削除) 17 (削除ここまで) 10 bytes
-5 bytes thanks to Jo King.
"#@~k:*k,;
-
1\$\begingroup\$ @JoKing Thanks a lot. I was able to golf two bytes more off. This now on par with Pyth, Husk and CJam! \$\endgroup\$ovs– ovs2018年09月07日 17:13:08 +00:00Commented Sep 7, 2018 at 17:13
Actually, 11 bytes
O⌠';*'#o⌡MΣ
Explanation:
O⌠';*'#o⌡MΣ
O convert string to list of ASCII ordinals
⌠';*'#o⌡M for each ordinal:
';* repeat ";" that many times
'#o append "#"
Σ concatenate
APL (Dyalog), 18 bytes
'#', ̈⍨';'⍴ ̈⍨⎕UCS
⎕UCS
Convert to Unicode code points
';'⍴ ̈⍨
use each code point to reshape (⍴ = Rho ≈ R; Reshape) a semicolon
#', ̈⍨
append a hash to each string
Ruby, (削除) 28 (削除ここまで) 25 bytes
24 bytes, plus the -n
command line switch to repeatedly operate on stdin
.
$_.bytes{|b|$><<?;*b+?#}
3 bytes saved (and output corrected on newlines!) thanks to manatwork.
-
\$\begingroup\$ You could avoid the use of
.ord
by working directly with character codes:$_.bytes{|b|$><<?;*b+?#}
. There is difference: this one also encodes the newline in the input. Not sure what the question owner intends to say by "It will only contain printable ASCII characters and newlines.", but to me sounds like newlines should be encoded too. \$\endgroup\$manatwork– manatwork2017年05月23日 08:19:24 +00:00Commented May 23, 2017 at 8:19 -
\$\begingroup\$ Your Ruby-fu exceeds mine, @manatwork - I'd forgotten about
bytes
. I've asked OP about newlines up top and will edit this afterwards. \$\endgroup\$Chowlett– Chowlett2017年05月23日 08:24:22 +00:00Commented May 23, 2017 at 8:24
Alice, 12 bytes
'#I.h%&';d&O
Explanation
'# Push 35, the code point of '#'.
I Read a code point C from STDIN. Pushes -1 at EOF.
.h% Compute C%(C+1). For C == -1, this terminates the program due to division
by zero. For C > -1, this just gives back C, so it does nothing.
&'; Pop C and push that many 59s (the code point of ';').
d Push the stack depth, which is C+1.
&O Print that many code points from the top of the stack.
The IP wraps around to the beginning and another iteration of this
loop processes the next character.
PHP, 48 bytes
for(;$c?:~$c=~ord($argn[$i++]);)echo";#"[!++$c];
jq, 30 characters
(26 characters code + 4 characters command line options)
explode|map(";"*.+"#")|add
Sample run:
bash-4.4$ jq -Rr 'explode|map(";"*.+"#")|add' <<< 'Hello, World!' | jq -Rrj '[scan(".*?#")|gsub("[^;]";"")|length%127]|implode'
Hello, World!
;#
ignores all other characters, so the generated program would still work. \$\endgroup\$;
. Secondly, 127 is correct, as stated in the linked question that contains the specification of the ;# language. \$\endgroup\$