Skip to main content
edited body
Source Link
emanresu A
  • 46.2k
  • 27
  • 43

Convert prefix to infixConvert prefix to infix

Infix notation is a method of printing mathematical expressions where each operator sits between its two arguments, such as \$ \left(5 \cdot 4\right) + 3 \$.

Prefix notation is a method of printing expressions where operators sit before their arguments. The equivalent of the above is +*543. It's a bit harder to understand than infix, but here's a sort of explanation:

+*543 # Expression + # Adding *54 # Expression * # The product of 5 # 5 and 4 # 4 3 # And 3 

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

digit := 0-9 operator := a-z expression := digit | (expression operator expression) 

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Example

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9))) And that's the result! Spaces are optional. 

Testcases

As usual, these are manually created, so comment if I've stuffed these up.

a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 

Convert prefix to infix

Infix notation is a method of printing mathematical expressions where each operator sits between its two arguments, such as \$ \left(5 \cdot 4\right) + 3 \$.

Prefix notation is a method of printing expressions where operators sit before their arguments. The equivalent of the above is +*543. It's a bit harder to understand than infix, but here's a sort of explanation:

+*543 # Expression + # Adding *54 # Expression * # The product of 5 # 5 and 4 # 4 3 # And 3 

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

digit := 0-9 operator := a-z expression := digit | (expression operator expression) 

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Example

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9))) And that's the result! Spaces are optional. 

Testcases

As usual, these are manually created, so comment if I've stuffed these up.

a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 
edited body
Source Link
emanresu A
  • 46.2k
  • 27
  • 43

Convert prefix to infix

Infix notation is a method of printing mathematical expressions where each operator sits between its two arguments, such as \$ \left(5 \cdot 4\right) + 3 \$.

Prefix notation is a method of printing expressions where operators sit before their arguments. The equivalent of the above is +3*54+*543. It's a bit harder to understand than infix, but here's a sort of explanation:

+3*54+*543 # Expression + # Adding 3 # 3 and   *54  # Expression   *  # The product of   5  # 5 and   4  # 4 3 # And 3 

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

digit := 0-9 operator := a-z expression := digit | (expression operator expression) 

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Example

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9))) And that's the result! Spaces are optional. 

Testcases

As usual, these are manually created, so comment if I've stuffed these up.

a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 

Convert prefix to infix

Infix notation is a method of printing mathematical expressions where each operator sits between its two arguments, such as \$ \left(5 \cdot 4\right) + 3 \$.

Prefix notation is a method of printing expressions where operators sit before their arguments. The equivalent of the above is +3*54. It's a bit harder to understand than infix, but here's a sort of explanation:

+3*54 # Expression + # Adding 3 # 3 and   *54 # Expression   * # The product of   5 # 5 and   4 # 4 

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

digit := 0-9 operator := a-z expression := digit | (expression operator expression) 

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Example

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9))) And that's the result! Spaces are optional. 

Testcases

As usual, these are manually created, so comment if I've stuffed these up.

a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 

Convert prefix to infix

Infix notation is a method of printing mathematical expressions where each operator sits between its two arguments, such as \$ \left(5 \cdot 4\right) + 3 \$.

Prefix notation is a method of printing expressions where operators sit before their arguments. The equivalent of the above is +*543. It's a bit harder to understand than infix, but here's a sort of explanation:

+*543 # Expression + # Adding *54  # Expression *  # The product of 5  # 5 and 4  # 4 3 # And 3 

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

digit := 0-9 operator := a-z expression := digit | (expression operator expression) 

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Example

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9))) And that's the result! Spaces are optional. 

Testcases

As usual, these are manually created, so comment if I've stuffed these up.

a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 
deleted 162 characters in body
Source Link
emanresu A
  • 46.2k
  • 27
  • 43

Convert infix to prefix to infix

Your challenge is to, given an expression in infix forprefix, convert it to prefixinfix notation.

The expressioninput will contain digits, lowercase letters and digits, and parentheses. It can be assumed to be a valid expression - that is, each operator (sortaletter) has exactly two operands and there is only one value left at the end

The output should be described bya valid expression in infix - that is, it should be an expression in the following recursive grammar:

That is, lowercase letters are used as operators and put between other expressions to combine them in some way. Expressions caneach expression should be digitsa digit, or two expressions combined withjoined by an operator and wrapped in parentheses.

For example, this is a valid expression: (1a2)
And so is this: (3x((4f3)u(5h9)))

You can assume the input will always be a valid expression. Extraneous parentheses, like (((...))), will not be included; parentheses are only used to group expressions that are combined with operatorsfor unambiguity.

Only single digits will occur

Note: Spaces are for clarity and should not be includedare optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3x3, u(f(4f34, 3)u, h(5h95, 9))) We haveor "x"something. The calledx withis taking 3, and thatthe sub-expression with a u as operands: Result: x (3 x ...) Expression: ((4f3)u(5h9)) We havef "u"4 called3 withh two5 expressions:9 Result:The xu 3is utaking ...the ... Expression:expression (4f3) Wewith havea "f"f calledand the expression with 4an andh 3:as operands. Result: x (3 u fx 4((...) 3u (...))) Expression: (5h9)f 4 3 We haveThe "h"f calledis withtaking 54 and 93 as operands. Result: x (3 u fx ((4 f 3) u (...))) Expression: h 5 9 Without spaces: x3uf43h59  AndThe thish is our result! You couldtaking also5 readand this9 as a series of function calls:operands. xExpression: (3, ux (f(4, f 3), hu (5, h 9)))   And that's the result! Spaces are optional. 
a34 -> (3a4) ba567 -> a34 ((5a6)b7) cba1234 -> ba567 (((1a2)b3)c4) a1b23 -> cba1234 (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> a1b23 (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) -> a1fcb46d4e95ig6h42kj32l68 

Convert infix to prefix

Your challenge is to, given an expression in infix for, convert it to prefix.

The expression will contain digits, lowercase letters, and parentheses. It can (sorta) be described by the following recursive grammar:

That is, lowercase letters are used as operators and put between other expressions to combine them in some way. Expressions can be digits, or two expressions combined with an operator and wrapped in parentheses.

For example, this is a valid expression: (1a2)
And so is this: (3x((4f3)u(5h9)))

You can assume the input will always be a valid expression. Extraneous parentheses, like (((...))), will not be included; parentheses are only used to group expressions that are combined with operators.

Only single digits will occur

Note: Spaces are for clarity and should not be included in the output.

Expression: (3x((4f3)u(5h9))) We have "x" called with 3, and that sub-expression: Result: x 3 ... Expression: ((4f3)u(5h9)) We have "u" called with two expressions: Result: x 3 u ... ... Expression: (4f3) We have "f" called with 4 and 3: Result: x 3 u f 4 3 ... Expression: (5h9) We have "h" called with 5 and 9 Result: x 3 u f 4 3 h 5 9 Without spaces: x3uf43h59  And this is our result! You could also read this as a series of function calls: x(3, u(f(4, 3), h(5, 9))) 
(3a4) -> a34 ((5a6)b7) -> ba567 (((1a2)b3)c4) -> cba1234 (1a(2b3)) -> a1b23 (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) -> a1fcb46d4e95ig6h42kj32l68 

Convert prefix to infix

Your challenge is to, given an expression in prefix, convert it to infix notation.

The input will contain lowercase letters and digits, and can be assumed to be a valid expression - that is, each operator (letter) has exactly two operands and there is only one value left at the end

The output should be a valid expression in infix - that is, it should be an expression in the following recursive grammar:

That is, each expression should be a digit, or two expressions joined by an operator and wrapped in parentheses for unambiguity.

Note: Spaces are for clarity and are optional in the input and output.

Expression: x 3 u f 4 3 h 5 9 You could read this as x(3, u(f(4, 3), h(5, 9))) or something. The x is taking 3 and the expression with a u as operands: Result: (3 x ...) Expression: u f 4 3 h 5 9 The u is taking the expression with a f and the expression with an h as operands. Result: (3 x ((...) u (...))) Expression: f 4 3 The f is taking 4 and 3 as operands. Result: (3 x ((4 f 3) u (...))) Expression: h 5 9 The h is taking 5 and 9 as operands. Expression: (3 x ((4 f 3) u (5 h 9)))   And that's the result! Spaces are optional. 
a34 -> (3a4) ba567 -> ((5a6)b7) cba1234 -> (((1a2)b3)c4) a1b23 -> (1a(2b3)) a1fcb46d4e95ig6h42kj32l68 -> (1a(((4b6)c(4d(9e5)))f((6g(4h2))i((3j2)k(6l8))))) 
Source Link
emanresu A
  • 46.2k
  • 27
  • 43
Loading