Vecchio transmogrifier codice C *


13

* Che cos'è un transmogrifier?

Nel linguaggio di programmazione C , ci sono formazioni chiamate digrafi e trigrafi che sono sequenze di due e tre caratteri che valutano caratteri meno comuni. Ad esempio, puoi usarlo ??-se la tua tastiera non ha ~.

Dato il testo, sostituisci tutte le istanze dei seguenti digrafi e trigrafi (lato sinistro) con il carattere corretto, più corto, con il golf (lato destro).

??=  #
??/  \
??'  ^
??(  [
??)  ]
??!  |
??<  {
??>  }
??-  ~
<:   [
:>   ]
<%   {
%>   }
%:   #

fonte

Ingresso

L'input è un testo ASCII. Newline finale consentita. Non deve essere un codice C valido.

Produzione

L'output è lo stesso testo, con tutte le istanze dei precedenti digrafi e trigrafi sostituiti con la versione abbreviata, valutata da sinistra a destra. Newline finale consentita. Non deve essere un codice C valido.

Casi test

=> separa input e output.

if (true ??!??! false) { => if (true || false) {

??-arr.indexOf(n) => ~arr.indexOf(n)

function f(??) { console.log('test??'); } => function f(] { console.log('test^); }

/* comment :> :) *??/ => /* comment ] :) *\

%:What am I doing??!!??` => `#What am I doing|!??

??(??)??(??) <:-- not a palindrome => [][] [-- not a palindrome

?????????? => ??????????

int f(int??(??) a) ??< return a??(0??)??'a??(1??) + "??/n"; ??> => int f(int[] a) { return a[0]^a[1] + "\n"; }

??<:>??<% => {]{%

<:> => [>

<::> => []

:>> => ]>

#\^[]|{}~ => #\^[]|{}~

: > => : >

??=%: => ##

3
Rimuovi gli `s dagli esempi. Li rende così difficili da leggere.
caird coinheringaahing

4
"??=%:"è un altro banco di prova rilevanti: in C, questo mezzo "#%:"in cui %:non è speciale, ma credo che l'output previsto è "##".
hvd,

@ Satan'sSon lo farà, inizialmente non lo avevo in un blocco di codice per rendere più leggibile l'input / output ma Riker lo ha cambiato. Sentiti libero di modificare qualcosa come formattarti la prossima volta :)
Stephen,

1
Quindi, stai chiedendo il codice golf al codice golf. Bonus golf :-)
Albero

@Mast questa è l'idea
Stephen

Risposte:



7

C, 206 205 bytes

(-1 thanks to ceilingcat)

The newlines are just here for readability.

c,d,q;f(char*s){for(char*S,*T,*t=s;c-63?q=0:q++,d=c<<8|*s,*s?
q>1&&(T=index(S="=/'()!<>-",*s))?t-=2,*s="#\\^[]|{}~"[T-S]:
d>*s&&(T=strstr(S=">:<>%<:%",&d))&&(c="][ }{ # "[T-S])&1?--t,*s=c:0:
0,*t++=c=*s++;);}

Modifies s in place. Tested with GCC and clang on Fedora Workstation, x86, in 32-bit and 64-bit mode.

C is not exactly the best language for golfing here.


C is not exactly the best language for golfing here. no kidding. Looks good :) Thinking back, I should have forced all questions to add +1 or +2 bytes if they used one of the characters that a digraph or trigraph makes xD
Stephen

1
You could make it even worse: +1 or +2 for each character that can be part of a di-/trigraph would really hurt :)
hvd

5

JavaScript (ES6), 106 bytes

s=>[...'#\\^[]|{}~[]{}#'].map((c,i)=>s=s.split('<:<%%'[i-9]+':>%>:'[i-9]||'??'+"=/'()!<>-"[i]).join(c))&&s

How?

This is pretty straightforward.

We should note however that:

  • When i is less than 9, the expression '<:<%%'[i-9] + ':>%>:'[i-9] evaluates to undefined + undefined which equals NaN (falsy as expected).

  • When i is greater than or equal to 9, the expression '??' + "=/'()!<>-"[i] evaluates to "??" + undefined which is coerced to the string "??undefined" (truthy when we expect a falsy result).

That's why we must process the test in this order.

Test cases


2

Ruby, 104+1 = 105 bytes

Uses the -p flag for +1 byte.

"=#/\\'^([)]!|<{>}-~".scan(/(.)(.)/){|k,v|gsub'??'+k,v}
"<:[:>]<%{%>}%:#".scan(/(..)(.)/){|k,v|gsub k,v}

Try it online!


2

Javascript (ES6), 131 123 bytes

f=
s=>"#??= \\??/ ^??' [??( ]??) |??! {??< {??> ~??- [<: ]:> {<% }%> #%:".split` `.map(x=>s=s.split(x.slice(1)).join(x[0]))&&s
<input oninput=console.log(f(this.value))>


2

PHP , 112 Bytes

<?=str_replace(explode(_,strtr("<:_:>_<%_%>_%:0=0/0'0(0)0!0<0>0-",["_??"])),str_split("[]{}##\\^[]|{}~"),$argn);

Try it online!

PHP , 115 Bytes

<?=str_replace(explode(_,"??=_??/_??'_??(_??)_??!_??<_??>_??-_<:_:>_<%_%>_%:"),str_split("#\\^[]|{}~[]{}#"),$argn);

Try it online!

PHP , 124 Bytes

Regex solution

foreach(explode(_,"=|%:_/_'_\(|<:_\)|:>_!_<|<%_>|%>_-")as$v)$a=preg_replace("#\?\?$v#","#\\^[]|{}~"[$k++],$a=&$argn);echo$a;

Try it online!


1

JavaScript (ES6), 113 bytes

s=>s.replace(/\?\?[^:%?]|[<:%]./g,c=>"#\\^[]|{}~"["=/'()!<>-".indexOf(c[2])]||"[] {} #"["<:><%>%:".indexOf(c)]||c)

Not the shortest, but I wanted to try a different approach.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.