I have function that convert binary number to unary number based on a Markov algorithm. In what ways can I improve the code?
The algorithm goes as follows:
Replace all
0l with 0l
"|0" -> "0||"
Replace all
l0 with 0ll
"1" -> "0|"
Major Step 3: Remove the
0
"0" -> ""
public List BinaryToUnary(List value) { var temp = new List(); var temp2 = new List(); var temp3 = new List(); string hold = ""; int count = 1; TxtToDisPlay = "Major Step 1: replace All 1 with 0l userInput " + string.Join("", value) + "\n\n"; foreach (var x in value) { if (x.Equals("1")) { temp.Add("0l"); TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + x + " = " + "0l" + "\n"; } else { temp.Add(x); TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + x + " = " + x + "\n"; } count++; } TxtToDisPlay = TxtToDisPlay + "\n\nMajor Step 2: Replace all l0 with 0ll userInput = " + string.Join("", temp) + " \n\n"; foreach (var x in string.Join("", temp)) { hold = hold + x; if (hold.Equals("l0")) { temp2.Add("0ll"); TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + "0ll" + "\n"; hold = ""; } if (hold.Equals("0")) { temp2.Add(hold); TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n"; hold = ""; } count++; } if (hold.Equals("l")) { temp2.Add("l"); } hold = ""; foreach (var y in string.Join("", temp2)) { if (temp2.Count == 1) { temp3.Add("l"); break; } hold = hold + y; if (hold.Equals("l0")) { TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + "0ll" + "\n"; temp3.Add("0ll"); hold = ""; } if (hold.Equals("0") || hold.Equals("ll")) { if (hold.Equals("ll")) { TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n"; temp3.Add("l"); hold = "l"; } else { TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + hold + " = " + hold + "\n"; temp3.Add(hold); hold = ""; } } } if (value.Count == 3 && hold.Equals("l")) { temp3.Add("l"); temp3.Add("l"); } else if (value.Count == 3 || hold.Equals("l")) { temp3.Add("l"); } TxtToDisPlay = TxtToDisPlay + "\n\nMajor Step 3 : get rid of the 0 from userInput = " + string.Join("", temp3) + " \n\n"; var answer = new List(); foreach (var x in temp3) { if (x.Contains("0ll")) { foreach (var y in x.Where(y => y.Equals('l'))) { answer.Add("l"); TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + "x = l so " + "Add l To new Array result = " + string.Join("", answer) + "\n"; } } else if (x.Contains("l")) { TxtToDisPlay = TxtToDisPlay + "Step " + count + ": " + "Add l To new Array result = " + string.Join("", answer) + "\n"; answer.Add(x); } count++; } return answer; }
BinaryToUnary(new List<string> { "1", "0", "0", "0" })
returns1, 1, 1
when it should return1, 1, 1, 1, 1, 1, 1, 1
. \$\endgroup\$