CUSRS-完全无用的字符串重构系统!


11

介绍

我是SBU(简短而独特)挑战赛的忠实拥护者,这种挑战一直都在PPCG上出现。CUSRS是一个旨在重构字符串的系统,CUSRS函数采用2个参数并输出1个String。

挑战

产生程序,函数,lambda或可接受的替代方法以执行以下操作:

鉴于String inputString refactor(作为例子),重构input使用refactor如下:

refactor字符串将在格式((\+|\-)\w* *)+(正则表达式),例如:

+Code -Golf -lf +al

每个部分都是要在上执行的重构操作input。每个程序也都有一个指针。

+ 将在字符串的指针当前位置插入后缀(不带加号),然后将指针重置为0。

每个操作都应应用于input字符串,并应返回结果。

例:

input:
Golf +Code //pointer location: 0

output:
CodeGolf //pointer location: 0

-将通过字符串增加指针,直到找到后缀。后缀将从字符串中删除,而指针将留在删除的文本的左侧。如果找不到后缀,则指针将简单地前进到String的末尾并留在此处。

input:
Golf -lf //pointer location 0

output:
Go //pointer location 2

例子

input:
"Simple" "-impl +nip -e +er"

output:
"Sniper"

input:
"Function" "-F +Conj"

output:
"Conjunction"

input:
"Goal" "+Code -al +lf"

output:
"CodeGolf"

input:
"Chocolate" "Chocolate"

output:
"Chocolate" //Nothing happens...

input:
"Hello" "-lo+p        +Please" //Spaces are irrelevant

output:
"PleaseHelp"

input:
"Mississippi" "-s-s-i-ppi+ng" //Operations can be in any order

output:
"Missing"

input:
"abcb" "-c -b +d"

output:
"abd"

input:
"1+1=2" "-1+22-=2+=23"

outut:
"22+1=23"

范例程式码

这个例子是Java,一点儿也没有。

public static String refactor(String input, String swap) {
    int pointer = 0;
    String[] commands = swap.replace(" ", "").split("(?=[-+])");

    for (String s : commands) {
        if (s.startsWith("+")) {
            input = input.substring(0, pointer) + s.substring(1) + input.substring(pointer, input.length());
            pointer = 0;
        } else {
            if (s.startsWith("-")) {
                String remove = s.substring(1);
                for (int i = pointer; i < input.length(); i++) {
                    if (input.substring(i, i + remove.length() > input.length() ? input.length() : i + remove.length()).equals(remove)) {
                        pointer = i;
                        input = input.substring(0, i) + input.substring(i + remove.length(), input.length());
                        break;
                    }
                }
            }
        }
    }

    return input;
}

规则

  • 适用标准漏洞
  • 以字节为单位的最短代码获胜


输出应该是什么aaa -a
ETHproductions's

|aa用管道作为指针。
肖恩·怀尔德

@Emigna在看问题时,我相信我的实现会大不相同。
肖恩·怀尔德

以会发生什么-,如果后缀是没有发现?
Zgarb

Answers:


1

APL,91个 90字节

{s l←⍵0⋄s⊣{g←1↓⍵⋄'+'=⊃⍵:+s∘←(l↑s),g,l↓s⋄l∘←¯1+1⍳⍨g⍷s⋄+s∘←(l↑s),s↓⍨l+⍴g}¨{1↓¨⍵⊂⍨⍵=⊃⍵}' ',⍺}

它将字符串作为其右参数,并将命令作为其左参数,如下所示:

      '+Code -al +lf' {s l←⍵0⋄s⊣{g←1↓⍵⋄'+'=⊃⍵:+s∘←(l↑s),g,l↓s⋄l∘←¯1+1⍳⍨g⍷s⋄+s∘←(l↑s),s↓⍨l+⍴g}¨{1↓¨⍵⊂⍨⍵=⊃⍵}' ',⍺} 'Goal'
CodeGolf


1

Python 3中(164 194 186 181个 168个 165字节)

p=0
w,*x=input().split()
for i in x:
 if '-'>i:w,p=w[:p]+i[1:]+w[p:],0
 else:
  try:p=w.index(i[1:],p)
  except:p=len(w)
  w=w[:p]+w[p:].replace(i[1:],'',1)
print(w)

示例演示如果找不到子字符串,指针将移动到末尾:

Input: HelloThere -x +Friend
Output: HelloThereFriend

特别感谢Artyer为我节省了13个字节。

再次感谢Artyer通过beg参数节省了我另外3个字节index

旧答案:

p=0
x=input().split()
w=x[0]
for i in x[1:]:
 if i[0]=='+':
  w=w[:p]+i[1:]+w[p:]
  p=0
 else:
  p=w[p:].index(i[1:])+p
  w=w[:p]+w[p:].replace(i[1:],'',1)
print(w)

演示指针工作的示例(即使您不考虑指针并仅在首次出现时进行替换,Q的所有示例也可以工作):

Input: HelloThereCowboy -r -e -y +ySays +Oh
Output: OhHelloTheCowboySays

编辑:自2分钟前以来,根据提问者的评论,我的答案现在无效。

aaa -b + b将与aaab一起产生,因为指针将一直一直到结尾。

Edit2:固定。


1
w,*x=input().split(),并且if'-'>i:if i[0]=='+':和制表符代替2个缩进而不是2个空格将节省一些字节
Artyer 16'Nov

如果我尝试混合制表符和空格,我会得到TabError: inconsistent use of tabs and spaces in indentation。感谢您的建议,我不知道这些功能!我将立即开始添加它们。
redstarcoder

@redstartcoder我猜该选项卡技巧仅在Python 2中有效。我的错
Artyer

我在这里绝对是错的,但是我认为字符串有一个find方法,-1如果找不到子字符串,它将返回。由于-1指向字符串的末尾,因此您所要做的只是取模数为p,其长度w应表示您不需要try-except。
卡德,2016年

1
您可以-1%len(str)在字符串末尾获取索引。str.indexstr.find接受一个start参数,因此我假设您可以替换w[p:].index(i[1:])w.index(i[1:],p)。总体而言,它将是else:p=(w.find(i[1:],p)+p)%len(p);w=w[:p]+w[p:].replace(i[1:],'',1)
Artyer

0

JavaScript(ES6),117个字节

(s,r,t='')=>r.match(/\S\w*/g).map(r=>(q=r.slice(1),r<'-'?(s=t+q+s.t=''):([b,...a]=s.split(q),t+=b,s=a.join(q))))&&t+s

说明:我没有使用繁琐的指针,而是将字符串的左半部分保留在其中,t而将右半部分保留在中s。此外,splitjoin对执行删除操作的简便方法。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.