注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Your task is to implement a simplified version of text editor.
All operations that should be supported are listed below. Partial credit will be given for each implemented operation. Please submit often to ensure partial credits are captured.
Implementation tips
Implement operations and provided steps one by one, and not all together, keeping in mind that you will need to make refactors to support each additional step. In the first three levels you can assume that only one text document is modified.
Note
After every operation, add the current state of the text to the resulting array. The returning array should consist of all the states after each operation is applied and have the same length as the # of input queries.
Level 1
The editor starts with a blank text document, with the cursor at initial position 0.
1. APPEND <text> should append the inputted string text to the document starting from the current position of the cursor. After the operation, the cursor should be at the end of the added string.
queries = [
["APPEND", "Hey"], | "" -> "Hey"
["APPEND", " there"], | "Hey" -> "Hey there"
["APPEND", "!"] | "Hey there" -> "Hey there!"
]
// returns: [ "Hey",
// "Hey there",
// "Hey there!" ]
2. MOVE <position> should move the cursor to the specified position. The cursor should be positioned between document characters, and are enumerated sequentially starting from 0. If the specified position is out of bounds, then move the cursor to the nearest available position.
queries = [
["APPEND", "Hey you"], | "" -> "Hey you"
["MOVE", "3"], | moves the cursor after the first "y"
["APPEND", ","] | "Hey you" -> "Hey, you"
]
// returns: [ "Hey you",
// "Hey you",
// "Hey, you" ]
3. FORWARD_DELETE should remove the character right after the cursor, if any.
queries = [
["APPEND", "Hello! world!"], | "" -> "Hello! world!"
["MOVE", "5"], | moves the cursor before the first "!"
["FORWARD_DELETE"], | "Hello! world!" -> "Hello world!"
["APPEND", ","] | "Hello world!" -> "Hello, world!"
]
// returns: [ "Hello! world!",
// "Hello! world!",
// "Hello world!",
// "Hello, world!" ]
and
queries = [
["APPEND", "!"], | "" -> "!"
["FORWARD_DELETE"], | "!" -> "!"
["MOVE", "0"], | moves the cursor before the first symbol
["FORWARD_DELETE"], | "! | switches to document2
["PASTE"], | document2: "" -> "world"
["SWITCH", "document1"], | switches to document1
["FORWARD_DELETE"] | document1: "Hello, !" -> "Hello,!"
]
// returns: [ "",
"",
"",
"",
"Hello, world!",
"Hello, world!",
"Hello, !",
"",
"world",
"Hello, !",
"Hello,!" ]
Example
For
queries = [
["APPEND", "Hey"],
["APPEND", " you"],
["APPEND", ", don't"],
["APPEND", " "],
["APPEND", "let me down"]
]
the output should be
solution(queries) = [
"Hey",
"Hey you",
"Hey you, don't",
"Hey you, don't ",
"Hey you, don't let me down"
]
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.array.string queries
An array of operations need to be applied to the text editor. It is guaranteed that each operation is one of the operations described in the description, all operation parameters are given in correct format, and the text editor will never be in an incorrect state that is not described in the description.
Guaranteed constraints:
1 ≤ queries.length ≤ 250.
[output] array.string
After every operation, add the current state of the text to the resulting array. The returning array should consist of all the states after each operation is applied and have the same length as the # of input queries.
[Python 3] Syntax Tips
# Prints help message to the console
# Returns a string
def helloWorld(name):
print("This prints to the console when you Run Tests")
return "Hello, " + name
|