[
https://issues.apache.org/jira/browse/GROOVY-11655?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17949865#comment-17949865
]
Paul King edited comment on GROOVY-11655 at 5/8/25 12:37 AM:
-------------------------------------------------------------
Here is how I believe that leetcode example would change:
{code:groovy}
class TextEditor {
private left = new StringBuilder(),
right = new StringBuilder()
void addText(String text) {
left << text // could be replaced with: left.push(text)
}
int deleteText(int k) {
[k, left.size()].min().tap{ left.length -= it }
}
String cursorLeft(int k) {
while (k > 0 && left) {
right << left[-1] // these two lines could be replaced with:
left.length -= 1 // right << left.pop()
k--
}
text()
}
String cursorRight(int k) {
while (k > 0 && right) {
left << right[-1] // these two lines could be replaced with:
right.length -= 1 // left << right.pop()
k--
}
text()
}
private String text() {
left.takeRight(10)
}
}
new TextEditor().with {
addText('leetcode') // leetcode|
assert deleteText(4) == 4 // leet|
addText('practice') // leetpractice|
assert cursorRight(3) == 'etpractice' // leetpractice|
assert cursorLeft(8) == 'leet' // leet|practice
assert deleteText(10) == 4 // |practice
assert cursorLeft(2) == '' // |practice
assert cursorRight(6) == 'practi' // practi|ce
}
{code}
was (Author: paulk):
Here is how I believe that leetcode example would change:
{code:groovy}
class TextEditor {
private left = new StringBuilder(),
right = new StringBuilder()
void addText(String text) {
left << text // could be replaced with:
left.push(text)
}
int deleteText(int k) {
[k, left.size()].min().tap{ left.length = left.size() - it }
}
String cursorLeft(int k) {
while (k > 0 && left) {
right << left[-1] // these two lines could be
replaced with:
left.length = left.size() - 1 // right << left.pop()
k--
}
text()
}
String cursorRight(int k) {
while (k > 0 && right) {
left << right[-1] // these two lines could be
replaced with:
right.length = right.size() - 1 // left << right.pop()
k--
}
text()
}
private String text() {
left.takeRight(10)
}
}
new TextEditor().with {
addText('leetcode') // leetcode|
assert deleteText(4) == 4 // leet|
addText('practice') // leetpractice|
assert cursorRight(3) == 'etpractice' // leetpractice|
assert cursorLeft(8) == 'leet' // leet|practice
assert deleteText(10) == 4 // |practice
assert cursorLeft(2) == '' // |practice
assert cursorRight(6) == 'practi' // practi|ce
}
{code}
> Create extension method to make StringBuilder like a stack
> ----------------------------------------------------------
>
> Key: GROOVY-11655
> URL: https://issues.apache.org/jira/browse/GROOVY-11655
> Project: Groovy
> Issue Type: New Feature
> Reporter: John
> Priority: Major
>
> for leetcode problem:
> [https://leetcode.com/problems/design-a-text-editor/description/]
> the java code below, suppose StringBuilder to be a contain of chars ,
> meanwhile as a stack.
>
> {code:java}
> class TextEditor {
> private final StringBuilder left = new StringBuilder();
> private final StringBuilder right = new StringBuilder();
> public void addText(String text) {
> left.append(text);
> }
> public int deleteText(int k) {
> k = Math.min(k, left.length());
> left.setLength(left.length() - k);
> return k;
> }
> public String cursorLeft(int k) {
> while (k > 0 && !left.isEmpty()) {
> right.append(left.charAt(left.length() - 1));
> left.setLength(left.length() - 1);
> k--;
> }
> return text();
> }
> public String cursorRight(int k) {
> while (k > 0 && !right.isEmpty()) {
> left.append(right.charAt(right.length() - 1));
> right.setLength(right.length() - 1);
> k--;
> }
> return text();
> }
> private String text() {
>
> return left.substring(Math.max(left.length() - 10, 0));
> }
> }
> {code}
>
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)