28
\$\begingroup\$

My friend made a lisp translator the other day, that is to say it took a string and converted s=>th and S=>Th. It was quite long and I thought that it could be golfed.

So the task is to make a program/function that takes an input string, translates it in to lisp and outputs the string

Test case

Sam and Sally like Sheep        Tham and Thally like Thheep
Sally likes sausages            Thally liketh thauthageth
Sally sells seashells           Thally thellth theathhellth

Note that it doesn't matter that h gets repeated all the time

This is code golf so shortest answer wins

\$\endgroup\$
  • 13
    \$\begingroup\$ I wished everyone would change the bytes in their headers to byteth. \$\endgroup\$ – Leaky Nun Jun 17 '16 at 17:14
  • 6
    \$\begingroup\$ There should be bonus points if the program doesn't use s nor S anywhere in it. \$\endgroup\$ – Nate Diamond Jun 17 '16 at 18:41
  • 1
    \$\begingroup\$ I think the language names should be made clear. Without the replacement. Some languages contain "th" already, so it's ambiguous. And who's to say that someone won't create a different language actually called "Common Lithp" in the future? \$\endgroup\$ – mbomb007 Jun 17 '16 at 21:02
  • 1
    \$\begingroup\$ @mbomb007 I wouldn't worry about it too much. I'm not sure if there is actually a default way to title your answer. Most question I see if they do state how to do it it is normally the same way. As I haven't explain how the answer should be set out, then users are free to title them how they want to. Being a pedant, I didn't even ask for a language, so I could argue against them even writing them. But I do understand your point. I just don't think it is of concern \$\endgroup\$ – george Jun 17 '16 at 21:09
  • 2
    \$\begingroup\$ A thing that would have made this challenge more interesting would be overall case preservation, e.g. LOOK OUT A SNAKE!!! -> LOOK OUT A THNAKE!!! \$\endgroup\$ – fluffy Jun 18 '16 at 3:06

44 Answers 44

1
\$\begingroup\$

Perl 5, 18 byteth

The obviouth anthwer; I don't thee anything thhorter. 17 byteth, pluth 1 for -pe inthtead of -e.

s/s/th/g;s/S/Th/g

I wathn't thure whether, tho ath to follow the thandard for anthwerth of thith quethtion, I thhould lithpify my code ath well ath my prothe. Jutht in cathe, here'th the lithpy verthion:

th/th/th/g;th/Th/Th/g
\$\endgroup\$
1
\$\begingroup\$

J, 38 bytes

load'strings'
rplc&('s';'th';'S';'Th')

In J, rplc is the string replace verb in the strings addon, and it takes a list of boxes where each pair [x, y] represents the string x to be replaced with y.

Usage

   f =: rplc&('s';'th';'S';'Th')
   f 'Sam and Sally like Sheep'
Tham and Thally like Thheep
   f 'Sally likes sausages'
Thally liketh thauthageth
   f 'Sally sells seashells'
Thally thellth theathhellth
\$\endgroup\$
1
\$\begingroup\$

C, 69 byteth

Reads from stdin, writes to stdout. Nothing terrific, but somebody had to do it!

c;main(){while((c=getchar())>0)printf(c-'s'?c-'S'?"%c":"Th":"th",c);}
\$\endgroup\$
1
\$\begingroup\$

C, 101 bytes

Note this is a complete C program, unlike the previous C answer.

It takes the first command line argument as input. Compile w/ gcc and run ./a.out "Sally sells seashells"

#include<stdio.h>
main(int d,char**c){for(d=**++c;d;putchar(d&223^83?d:d+(*0[c]--=104,1)),d=*++*c);}
\$\endgroup\$
  • \$\begingroup\$ Hello, and welcome to PPCG! \$\endgroup\$ – NoOneIsHere Jun 18 '16 at 1:27
  • 2
    \$\begingroup\$ Erm, whether the program is complete doesn't matter. On PPCG we accept both full programs and functions. \$\endgroup\$ – user48538 Jun 18 '16 at 7:57
1
\$\begingroup\$

J, 47 bytes

Without using the strings library.

[:;(]`('h',~[:u:1+3&u:)@.([:+/'sS'-:"0]))&.>@;/
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 235 byteth

(Newlines included only for readability)

,[[>+>+>+<<<-]>>>>-[>+<---]>--[-<<->>]+<<[>>-<<[-]]>>[>-[--->+<]>-.[---->+++++<]>-.<<<-]>>>>
[-]<[>+<[-]]+>[<->-]<[<<<<<-[>++<+++++++++]>+[-<<->>]+<<[>>-<<[-]]>>[>--------[-->+++<]>.---
---------.>]<>[-]<[>+<[-]]+>[<->-]<[<<.>]]>>>>>>>,]

Requires 8-bit cells, and 0 on EOF. Try it online! I thought I could golf tt using some "if = s OR = S" wizardry, but it turns out that this just confounds the system.

Readable

,
[
 [>+>+>+<<<-]>>>
 >-[>+<---]>--
 [-<<->>]+<<[>>-<<[-]]>>
 [ if char = "S"
  >-[--->+<]>-.[---->+++++<]>-.<<<-  output Th
 ]
 >>>

 >[-]
 <[>+<[-]]+
 >[<->-]<    logical negation

 [ else
  <<<<<
  -[>++<+++++++++]>+
  [-<<->>]+<<[>>-<<[-]]>>

   [ if char = "s"
    >--------[-->+++<]>.------------.>  output th
   ]

  <

  >[-]
  <[>+<[-]]+
  >[<->-]<    logical negation

  [<<.>] otherwise output and move to zero cell
 ]
 >>>>>>> we're going to move over some so we can use new cells
 , take more input
]
\$\endgroup\$
1
\$\begingroup\$

Matlab, 38 42 bytes

@(t)regexprep(t,{'s','S'},{'th','Th'})

Shout out to @LuisMendo for the tip about cell arrays!

For 'Miss Sissy' yields ans = Mithth Thiththy.

\$\endgroup\$
  • \$\begingroup\$ I think straightforward approach is shorter: codegolf.stackexchange.com/a/83190/55372 \$\endgroup\$ – pajonk Jun 17 '16 at 18:37
  • 1
    \$\begingroup\$ You can use a single call to regexprep with cell arrays: @(t)regexprep(t,{'s' 'S'},{'th' 'Th'}), 38 bytes \$\endgroup\$ – Luis Mendo Jun 18 '16 at 15:26
  • \$\begingroup\$ @LuisMendo I had had problems using cell arrays in regexprep before, but they seem to have straightened that out. Edited! \$\endgroup\$ – sintax Jun 20 '16 at 13:43
1
\$\begingroup\$

s-lang, 16 15 8 bytes (non-competing)

Saved one byte because s-lang no longer requires last argument bracket

Saved seven bytes because of ! paramater

Try it online!

t![s][th

s-lang (or, th-lang) is a string manipulation language I am working on for fun. I am still optimizing argument brackets...

Explantion

  • t replace function with search argument and replace argument.
  • ! "preserve case" parameter
  • [s] the first search argument (searches for the character "s")
  • [th the first replace argument (now this will replace each "s" with a "th"). End bracket is omitted because it is unnecessary.
\$\endgroup\$
  • \$\begingroup\$ Doesn't p parameter need +1 byte? \$\endgroup\$ – Erik the Outgolfer Jul 4 '16 at 18:36
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Oh.. it is now "!" instead of "!". I'll fix that \$\endgroup\$ – MCMastery Jul 9 '16 at 22:58
1
\$\begingroup\$

Javathcript, 27 Byteth

b=a=>a.replace(/s/gi,'th');

Although it is case insensitive it is the smallest JS version so far :)

\$\endgroup\$
0
\$\begingroup\$

Pyke, 14 byteth

\s"th":\S"Th":

Try it here!

Thith anthwer ith very thimple and boring.

\$\endgroup\$
0
\$\begingroup\$

Thue, Full program: 27 byteth, function: 13 byteth

s::=th
S::=Th

Thince Thue can read input, a correct input routine will be too long. Tho i potht it ath a "function"

EDIT: I have forgotten, Thue read lineth from thtdin, not characterth

Tho, here ith a full program:

s::=th
S::=Th
I::=:::
::=
I
\$\endgroup\$
  • \$\begingroup\$ Is the language Thue or Sue? \$\endgroup\$ – Qwerp-Derp Jul 10 '16 at 16:44
  • 1
    \$\begingroup\$ @DerpfacePython The tranthformationth are only s => th and S => Th, tho it'th Thue. \$\endgroup\$ – TuxCrafting Jul 10 '16 at 16:50
  • \$\begingroup\$ Ah, I underthtand now. Thankth! \$\endgroup\$ – Qwerp-Derp Jul 10 '16 at 16:51
0
\$\begingroup\$

PowerThhell, 41 bytes

$args.replace('S','Th').replace('s','th')

I can't beat this simple approach; there is a string.translate buried in XPath and there are map style approaches in the Linq library - but they're wordy. Can't easily do a dictionary lookup per character because PS is case insensitive and thinks 's' and 'S' are duplicate keys.

Best novelty alternative so far:

$OFS='Th';''+$args.split('S')-replace's','th'
\$\endgroup\$
0
\$\begingroup\$

Thilicon, 13 byteth

i`s|th``S|Th`

Pretty simple. i to get input, and the rest of it is replacing stuff. Output is implicit.

\$\endgroup\$
-1
\$\begingroup\$

TCC - 28 bytes

=SO;i;{'s';'th';{'S';'Th';SO

Try it online!

Explanation:

=SO;i;                       - Load input (i) into SO (string operations)
      {'s';'th';             - Replace 's' with 'th' in SO
                {'S';'Th';   - Replace 'S' with 'Th' in SO
                          SO - Implicit print of SO
\$\endgroup\$
  • \$\begingroup\$ Did this work with a version of tcc.lua before the challenge was posted? Given that you've recently added commands to solve several other challenges, I assume it didn't. If your answer requires a version of the language that postdates the challenge, you must label it as non-competing in the header. I'll remove my downvote when you add the label or provide proof that your code worked in an earlier version. \$\endgroup\$ – Dennis Jul 31 '16 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.