Skip to main content
deleted 720 characters in body
Source Link
Adnan
  • 44.5k
  • 6
  • 82
  • 246

Prolog (SWI), 9595 81 bytes

Haven't programmed in Prolog for a while, so this was a fun challenge to do.

Prologs ability to handle strings and atoms do not make it easy. Probably suboptimal, but here we go:

f(ZR,[H|T]):-atom(H)T=[],appendH<97,R=[H];append(LX,RY,T),f(XC,LX),f(YD,RY),atomics_to_string(['flatten('[40,XC,H,Y,')']D,Z).
f(X41],[X]R).

Takes two parameters. The first being the variable in which the result will be stored and the second being the input split out as a list of atomsInput and numbersoutput are both codepoint lists.

Try it online!

Ungolfed Version

prefix_to_infix(X, [X]) :- number(X).
prefix_to_infix(Result, [X | Xs]) :-
    append(Ls, Rs, Xs),
    prefix_to_infix(Left, Ls),
    prefix_to_infix(Right, Rs),
    atomics_to_string(['(', Left, X, Right, ')'], Result).

Try it online!Try it online!

Prolog (SWI), 95 bytes

Haven't programmed in Prolog for a while, so this was a fun challenge to do.

Prologs ability to handle strings and atoms do not make it easy. Probably suboptimal, but here we go:

f(Z,[H|T]):-atom(H),append(L,R,T),f(X,L),f(Y,R),atomics_to_string(['(',X,H,Y,')'],Z).
f(X,[X]).

Takes two parameters. The first being the variable in which the result will be stored and the second being the input split out as a list of atoms and numbers.

Try it online!

Ungolfed Version

prefix_to_infix(X, [X]) :- number(X).
prefix_to_infix(Result, [X | Xs]) :-
    append(Ls, Rs, Xs),
    prefix_to_infix(Left, Ls),
    prefix_to_infix(Right, Rs),
    atomics_to_string(['(', Left, X, Right, ')'], Result).

Try it online!

Prolog (SWI), 95 81 bytes

Haven't programmed in Prolog for a while, so this was a fun challenge to do.

Prologs ability to handle strings and atoms do not make it easy. Probably suboptimal, but here we go:

f(R,[H|T]):-T=[],H<97,R=[H];append(X,Y,T),f(C,X),f(D,Y),flatten([40,C,H,D,41],R).

Input and output are both codepoint lists.

Try it online!

Source Link
Adnan
  • 44.5k
  • 6
  • 82
  • 246

Prolog (SWI), 95 bytes

Haven't programmed in Prolog for a while, so this was a fun challenge to do.

Prologs ability to handle strings and atoms do not make it easy. Probably suboptimal, but here we go:

f(Z,[H|T]):-atom(H),append(L,R,T),f(X,L),f(Y,R),atomics_to_string(['(',X,H,Y,')'],Z).
f(X,[X]).

Takes two parameters. The first being the variable in which the result will be stored and the second being the input split out as a list of atoms and numbers.

Try it online!

Ungolfed Version

prefix_to_infix(X, [X]) :- number(X).
prefix_to_infix(Result, [X | Xs]) :-
    append(Ls, Rs, Xs),
    prefix_to_infix(Left, Ls),
    prefix_to_infix(Right, Rs),
    atomics_to_string(['(', Left, X, Right, ')'], Result).

Try it online!