36
\$\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\$
10
  • 18
    \$\begingroup\$ I wished everyone would change the bytes in their headers to byteth. \$\endgroup\$
    – Leaky Nun
    Jun 17, 2016 at 17:14
  • 7
    \$\begingroup\$ There should be bonus points if the program doesn't use s nor S anywhere in it. \$\endgroup\$ Jun 17, 2016 at 18:41
  • 5
    \$\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, 2016 at 3:06
  • 4
    \$\begingroup\$ @fluffy after writing the question I did realise a case like that would fail, but it is too late to change now \$\endgroup\$
    – george
    Jun 18, 2016 at 7:37
  • 3
    \$\begingroup\$ Welcome to Programming Puzzleth & Code Golf! That Ith a Good quethtion and thothe help the community a lot! \$\endgroup\$
    – user54200
    Jul 5, 2016 at 10:15

58 Answers 58

1
2
2
\$\begingroup\$

Matlab, 39 byteth

A straitforward approach:

@(t)strrep(strrep(t,'s','th'),'S','Th')
\$\endgroup\$
2
\$\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\$
2
\$\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\$
2
  • \$\begingroup\$ Doesn't p parameter need +1 byte? \$\endgroup\$ Jul 4, 2016 at 18:36
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Oh.. it is now "!" instead of "!". I'll fix that \$\endgroup\$
    – MCMastery
    Jul 9, 2016 at 22:58
2
\$\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\$
2
\$\begingroup\$

Lexurgy, 18 byteth

s:
{S,s}=>{Th,th}
\$\endgroup\$
2
\$\begingroup\$

Arturo, 30 29 byteth

$=>[translate&#[S:"Th"s:'th]]

Try it

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

SpecBAS ThpecBATh - 53 bytes

1 INPUT a$: ?REPLACE$(REPLACE$(a$,"S","Th"),"s","th")
\$\endgroup\$
0
1
\$\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\$
3
  • \$\begingroup\$ Is the language Thue or Sue? \$\endgroup\$
    – clismique
    Jul 10, 2016 at 16:44
  • 1
    \$\begingroup\$ @DerpfacePython The tranthformationth are only s => th and S => Th, tho it'th Thue. \$\endgroup\$ Jul 10, 2016 at 16:50
  • \$\begingroup\$ Ah, I underthtand now. Thankth! \$\endgroup\$
    – clismique
    Jul 10, 2016 at 16:51
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\$
0
1
\$\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
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\$
0
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\$
2
  • \$\begingroup\$ Hello, and welcome to PPCG! \$\endgroup\$ Jun 18, 2016 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, 2016 at 7:57
1
\$\begingroup\$

J, 47 bytes

Without using the strings library.

[:;(]`('h',~[:u:1+3&u:)@.([:+/'sS'-:"0]))&.>@;/
\$\endgroup\$
0
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\$
3
  • \$\begingroup\$ I think straightforward approach is shorter: codegolf.stackexchange.com/a/83190/55372 \$\endgroup\$
    – pajonk
    Jun 17, 2016 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, 2016 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, 2016 at 13:43
1
\$\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\$

Go, 87 bytes

import."strings"
func f(s string)string{R:=ReplaceAll
return R(R(s,"s","th"),"S","Th")}

Attempt This Online!

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

Thunno, \$ 16 \log_{256}(96) \approx \$ 13.17 bytes

's"th"AR'S"Th"AR

Attempt This Online!

Obviouth anthwer.

Explanation

's"th"AR'S"Th"AR  # Implicit input
's                # Character 's'
  "th"            # String "th"
      AR          # Replace
        'S        # Character 'S'
          "Th"    # String "Th"
              AR  # Replace
                  # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 11 bytes

`Ss"Thth"½Ṇ

Try it online!

Explanation

`Ss"Thth"½Ṇ  # Implicit input
`Ss          # Push "Ss"
   "Thth"    # Push "Thth"
         ½   # Halve it
          Ṇ  # Transliterate
             # Implicit output
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 18 bytes

"Th"/"S"\"th"/"s"\

Try it online!

  • "s"\ split (implicit) input on "s"s
  • "th"/ join with "th"
  • "S"\ split on "S"s
  • "Th"/ join with "Th"
\$\endgroup\$
0
\$\begingroup\$

Pyke, 14 byteth

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

Try it here!

Thith anthwer ith very thimple and boring.

\$\endgroup\$
0
\$\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\$
0
0
\$\begingroup\$

Rattle, 50 byteth

\&Th&th|Ig0Ig0I^P3[g["S"b1]["s"b2][^"S"[^"s"b]]>]`

Try it Online!

Explanation

\&Th&th|                          pass [(user input), "Th", "th"] to program
        I                         place list items in consecutive memory slots, move pointer to next free slot
         g0                       get item 0 (user input)
           I                      place list items in consecutive memory slots, move pointer to next free slot
            g0                    get item 0 (user input)
              I^                  set top of stack to the length of this item
                P3                move the pointer to slot 3

[ ... ]`                          loop n times, where n is the top of the stack
 
g                                 get the value at the pointer
 ["S"  ]                          if the value is "S",
     b1                           add the item in storage at pointer 1 ("Th") to print buffer
        ["s"  ]                   if the value is "s",
            b2                    add the item in storage at pointer 2 ("th") to print buffer
               [^"S"       ]      if the value is not "S",
                    [^"s" ]       if the value is not "s",
                         b        add the item to the print buffer
                            >     move the pointer right
                                  the buffer is printed implicitly

Rattle, 50 byteth

Here's an alternative approach using ASCII values that turned out to be the same byte count:

\&Th&th|Ig0Ig0I^P3[gn[83b1][115b2][^83[^115gb]]>]`

Try it Online!

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

Raku, 23 bytes

*.trans(<s S>=><th Th>)

Try it online!

Just calls the built-in string translate function.

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

Racketh -- 58 bytes

#!racket
(regexp-replaces(read-line)'(["s""th"]["S""Th"]))

Try it online!


Explanation

On line one we have the required language expression for Racket.

On line two we pass user input to a function that uses regular expressions to replace certain characters ("s" becomes "th" and "S" becomes "Th").

#lang racket
(regexp-replaces (read-line) '(["s" "th"] ["S" "Th"]))
\$\endgroup\$
0
\$\begingroup\$

sed 4.2.2, 17 bytes

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

Try it online!

Pretty thimple. It'th what thed it made for.

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

05AB1E, 11 10 byteth

„sS„thD™‚‡

Originally thuggethted ath a golf for the exithting 05AB1E anthwer, but I decided to potht it ath a theparated anthwer after there wath no rethpondth for halve a year. :)

Try it online.

Explanation:

„sS         # Push string "sS"
   „th      # Push string "th"
      D     # Duplicate it
       ™    # Titlecase the copy
        ‚   # Pair the two together: ["th","Th"]
         ‡  # Transliterate all "s" to "th" and "S" to "Th" in the (implicit) input
            # (after which the result is output implicitly)

PTh: Altho workth in 2thable:
Try it online (Hello. Table for two, pleathe. ;p)

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

brainfuck, 183 byteth

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

Try it online!

Explanation

[
* https://esolangs.org/wiki/Brainfuck_constants
* https://esolangs.org/wiki/Brainfuck_algorithms
*
* Cell #    Name    Explanation
* 0         tc0     Unused except right at the start for making 83 ('S')
* 1         tc1     Stores 'S'
* 2         tc1p5   Temp cell & used for outputting input char
* 3         tc2     Temp cell & holds input char - 'S' 
* 4         tc3     Temp cell & holds input char - 's'
* 5         tc4     Temp cell & input initially starts here
* 6         tc4p5   Temp cell
* 7         tc5     Stores 's'
* 8         tc6     Stores 'h'
*
* Algorithm:
*   initialization
*
*   while (ch = input()) {
*       if ((ch - 's') != 0) {
*           if ((ch - 'S') != 0) {
*               output(ch)
*           } else {
*               output(ch + 1)
*               output('h')
*           }
*       } else {
*           output(ch + 1)
*           output('h')
*       }
*   }
]

-[>+<---]>-- make 83 ('S') on tc1 end tc1

>> make space for tc1p5 and tc4p5

>+[->-[<]>--]>- make 104 ('h') on tc6 end tc6
<<<+[-->++[<]>-]>+ make 115 ('s') on tc5 end tc5

<<,[ tc4 begin input loop

handling:
    'S': output Th
    's': output th
    else: output char

[-<+<+<+>>>] copy tc4 to tc3 tc2 tc1p5 end tc4

>> tc5 s
[<<+<->>>-] move tc5 to tc4 and sub from tc3 end tc5
<<[->>+<<] tc4 move back to tc5 end tc4

+ tc4 is 1
<[
    tc3 is not 's'
    [-] zero tc3 
    >-< tc4 to zero   

    <<< tc1
    [->>->+<<<] tc1 to sub tc2 and add tc3 end tc1
    >>>[-<<<+>>>] tc3 to tc1 end tc3
    
    + tc3 one
    <[
        tc2 is not 'S'
        <. output tc1p5
        
        > tc2
        >-
    ]>
    [<
        tc2 is 'S'
        <+. tc1p5 output 'T'
        >>->>>>. tc6 output 'h' and dec tc3 along the way

        <<< tc4
    ] end tc4

]>
[< tc3 and tc3 is 's'
    <<+. tc1p5 make 't' output
    >>>->>>. tc6 output 'h' and dec tc4 along the way
    << tc4p5
] end tc4p5  tc4 0 tc4p5 0  

<<<[-] tc2 zero
<[-] tc1p5 zero

>>> tc4

,] end tc4
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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