บทนำ
ฉันเป็นแฟนตัวยงของ SBU (สั้น แต่ไม่เหมือนใคร) ที่ท้าทายความสามารถในการเพาะปลูก PPCG ตลอดเวลา CUSRS เป็นระบบที่ออกแบบมาเพื่อ refactor สตริงฟังก์ชั่น CUSRS ใช้เวลา 2 พารามิเตอร์และเอาท์พุท 1 สตริง
ท้าทาย
จัดทำโปรแกรมฟังก์ชั่นแลมบ์ดาหรือทางเลือกที่ยอมรับได้เพื่อทำสิ่งต่อไปนี้:
รับString inputและString refactor(เป็นตัวอย่าง) refactor inputใช้refactorดังนี้:
refactorString จะอยู่ในรูปแบบของ((\+|\-)\w* *)+(regex) ตัวอย่างเช่น:
+Code -Golf -lf +al
แต่ละส่วนคือการกระทำที่ refactoring inputเพื่อดำเนินการเกี่ยวกับ แต่ละโปรแกรมยังมีตัวชี้
+ จะแทรกคำต่อท้าย (โดยไม่มีเครื่องหมายบวก) ที่ตัวชี้ตำแหน่งปัจจุบันในสตริงแล้วรีเซ็ตตัวชี้เป็น 0
แต่ละการดำเนินการควรถูกนำไปใช้กับinputString และควรส่งคืนผลลัพธ์
ตัวอย่าง:
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?
|aaด้วยท่อที่เป็นตัวชี้
-หากไม่พบคำต่อท้าย