Einführung
Ich bin ein großer Fan der SBU-Herausforderungen (Short But Unique), die ständig bei PPCG auftauchen. Das CUSRS ist ein System zur Umgestaltung von Zeichenfolgen. Eine CUSRS-Funktion verwendet 2 Parameter und gibt 1 Zeichenfolge aus.
Herausforderung
Erstellen Sie ein Programm, eine Funktion, ein Lambda oder eine akzeptable Alternative, um Folgendes zu tun:
Gegeben String inputund String refactor(als Beispiele), refactor inputVerwendung refactorwie folgt:
Der refactorString hat das Format ((\+|\-)\w* *)+(Regex), zum Beispiel:
+Code -Golf -lf +al
Jeder Abschnitt ist eine Refactoring-Aktion, die ausgeführt werden muss input. Jedes Programm hat auch einen Zeiger.
+ Fügt das Suffix (ohne Pluszeichen) an der aktuellen Position des Zeigers im String ein und setzt den Zeiger dann auf 0 zurück.
Jede Operation sollte auf den inputString angewendet und das Ergebnis zurückgegeben werden.
Beispiel:
input:
Golf +Code //pointer location: 0
output:
CodeGolf //pointer location: 0
-Inkrementiert den Zeiger durch den String, bis er das Suffix findet. Das Suffix wird aus dem String entfernt und der Zeiger bleibt auf der linken Seite des entfernten Textes. Wenn kein Suffix gefunden wird, bewegt sich der Zeiger einfach zum Ende des Strings und bleibt dort.
input:
Golf -lf //pointer location 0
output:
Go //pointer location 2
Beispiele
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"
Beispielcode
Das Beispiel ist Java, es wird überhaupt nicht gespielt.
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;
}
Regeln
- Standardschlupflöcher gelten
- Der kürzeste Code in Bytes gewinnt
aaa -a?
|aamit der Pipe als Zeiger.
-wenn das Suffix nicht gefunden wird?