Stan.Belkin
BAN USERusing System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Prefix2Postfix
{
public class Prefix2PostfixShould
{
[Theory]
[InlineData("*ab", "ab*")] // a*b
[InlineData("+a*bc", "abc*+")] // a+b*c
[InlineData("+*abc", "ab*c+")] // a*b+c
[InlineData("*+ab+cd", "ab+cd+*")] // (a + b)*(c + d)
public void Convert(string prefix, string expected)
{
var postfix = Expressions.Convert2Postfix(prefix);
Assert.Equal(expected, postfix);
}
}
public class Expressions
{
public static string Convert2Postfix(string prefix)
{
var stack = new Stack<string>();
for (int i=prefix.Length-1; i>=0; i--)
{
if (IsOperator(prefix[i]))
{
var left = stack.Pop();
var right = stack.Pop();
var res = left + right + prefix[i];
stack.Push(res);
}
else
{
stack.Push(prefix[i].ToString());
}
}
string postfix = string.Empty;
while(stack.TryPop(out var item))
{
postfix += item;
}
return postfix;
}
private static bool IsOperator(char value)
{
const string operators = "*/+-";
return operators.Contains(value);
}
}
}
Just summarize values of all elements. If there is no cycles the sum should be equal to arithmetic progression of array size.
- Stan.Belkin September 13, 2017
Repamyfreynold, abc at ADP
Hi Everyone, I'm Ammy. I love to build props...everything from a casket to pneumatic monsters.My current pet ...
Repewasam940, Backend Developer at Absolute Softech Ltd
I am a 29-year-old Investment Advisor from New York. I help people make the right investment decision. I met many ...
Repzk6354367, Financial Software Developer at Agilent Technologies
Hi , I am Zessie from the USA , working as Dien for the last three years. Previously I have served as ...
Repfredlhenry, Android Engineer at Digital Merkating
Hi, I am Fred, 27 years old, I have done bachelor’s in IT.I have been working for a ...
RepJaneBraun, Associate at ASAPInfosystemsPvtLtd
Hi, I am Jane From York,PA.My goal is to create a life that I don’t want to ...
Replarrymmapp, Android test engineer at Software AG
Hello, I am name and I live in a city, USA. I am a professional's Podiatric doctor. I am ...
Replestermauldin, Assignment editor at Royal Gas
I am Lester Assignment editor . I am responsible for organizing the assignment desk to operate around the clock. I often ...
Repmakaylaangwin, Backend Developer at Abs india pvt. ltd.
I'm part owner of Bloom Floral Land, an Australian floral design company. Life is really short, try to smile ...
Interesting what was the point of that question. To check if you know and remember advanced multiplication algorithms or your ability to implement very boring but well known one?
School algorithm. Its really ugly:
- Stan.Belkin September 16, 2017