class Buffer(s: String) {
import scala.collection.mutable.StringBuilder
import scala.io.StdIn
private var buffer: StringBuilder = new StringBuilder(s)
private var cursor: Int = 0 // cursor is in between characters
private var marker: Int = 0 // marker is in between characters
private var paste: String = ""
private def end: Int = buffer.length // the end of the line
private def lwr: Int = Math.min(marker, cursor)
private def upr: Int = Math.max(marker, cursor)
/*
* Accessor methods to return aspects of the state
*/
def getCursor: Int = cursor
def getMarker: Int = marker
def getString: String = buffer.toString
def getPaste: String = paste
/**
* Delete Duplicate characters. Within the defined region, for each character,
* if it occurs once then keep it, but if it occurs multiple times then keep
* only the first occurrence. The characters to the left and right of the
* defined region remain unchanged, but within the defined region the duplicates
* are removed. This operation does not affect the paste buffer. The cursor is
* placed finally at the lower end of the defined region and the marker is placed
* finally at the upper end of the (probably reduced) defined region. For example:
*
* m i s s i s s i p p i marker = 1
* ^ ^ cursor = 10
*
* Then perform sc('a', 'X')
*
* m i s p i marker = 1
* ^ ^ cursor = 4
*/
def dd()
{
var droppedchars: Int = 0;
for (x <- lwr until upr)
{
var c = buffer.charAt(x)
for (i <- lwr until upr)
{
if (buffer.charAt(i) == c)
{
buffer.deleteCharAt(i)
droppedchars += 1
}
}
marker = lwr
cursor = upr - droppedchars
}
}
也需要一些这方面的帮助,似乎不起作用 函数需要删除它找到的任何重复字符,将标记移回新定义区域的开头,将光标移动到新定义区域的末尾,而不是要求有人为我写这个,只是指导我正确的方向
答案 0 :(得分:1)
为什么不呢:
library(tidyverse)
library(shiny)
ui <- fluidPage(
selectInput("select1", "select quarter", choices = unique(df$period)),
verbatimTextOutput(outputId = "val")
)
server <- function(input, output) {
v1 <- reactive({
i1 <- match(input$select1, df$period) # get the index
i2 <- seq(pmax(1, i1-1), i1, by = 1) # create a sequence from the previous row
df %>%
slice(i2) %>% #slice the rows
pull(value) %>% # pull the value column
diff %>% # get the difference of the two values
.[1] #to make sure that we get NA if we select Q1
})
output$val <- renderText({
v1()
})
}
shinyApp(ui = ui, server = server)
答案 1 :(得分:0)
您尝试做的事情从性能角度看O(n ^ 2)不是很好...... Imo,一个更好的解决方案是在缓冲区上只使用一个循环,并在其中使用一组字符(在for循环外声明)检查每个字符c:
val chars = scala.collection.mutable.Set[Char]()
...
for (x <- lwr until upr) {
var c = buffer.charAt(x)
if (chars contains c) buffer.deleteCharAt(x) else chars += c
}
cursor = marker + chars.size