Created
November 9, 2023 07:31
-
-
Save arialdomartini/40b92b0661a4e5ab649582e2bdab9f5e to your computer and use it in GitHub Desktop.
TextEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private delegate IEnumerable<string> ManipulateHistory(IEnumerable<string> history); | |
ManipulateHistory append(string tail) => | |
history => history.Append(new[] { history.Last() + tail }); | |
ManipulateHistory delete(int k) => | |
history => history.Append(new[] { delete(history.Last(), k) }); | |
string delete(string last, int k) => last.Substring(0, last.Length - k); | |
ManipulateHistory print(int k) => history => | |
{ | |
Console.WriteLine(history.Last()[k - 1].ToString()); | |
return history; | |
}; | |
ManipulateHistory undo() => | |
history => history.Take(history.Length() - 1); | |
ManipulateHistory toCommand(string commandAndParameter) | |
{ | |
var parts = commandAndParameter.Split(" "); | |
var command = parts[0]; | |
return command switch | |
{ | |
"1" => append(parts[1]), | |
"2" => delete(int.Parse(parts[1])), | |
"3" => print(int.Parse(parts[1])), | |
"4" => undo(), | |
}; | |
} | |
[Fact] | |
void pass() | |
{ | |
var input = new List<string> | |
{ | |
"1 fg", // abcdefg | |
"3 6", // abcdefg | |
"2 5", // abcdefg ab | |
"4", // abcdefg | |
"3 7", // abcdefg | |
"4", // abcde | |
"3 4" // | |
}; | |
IEnumerable<string> initial = new List<string>{"abcde"}; | |
var result = input.Select(toCommand).Aggregate(initial, (h, c) => c(h)); | |
Assert.Equal("abcde", result.Last()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment