$^//.{#}/S1//.$/
Hãy thử trực tuyến!
Định dạng đầu vào là như vậy:
string
index
Và chương trình là 1 chỉ mục.
Giải trình
Carrot có một số biến toàn cục, một biến cho mỗi loại: chuỗi, float và mảng (các biến khác sẽ được triển khai sớm). Chương trình bắt đầu ở chế độ chuỗi, trong đó tất cả các toán tử sẽ ảnh hưởng đến biến chuỗi toàn cầu. Và tôi gọi các biến này là "ngăn xếp".
(Ví dụ nhập abcdef\n3
:)
$ Get the first line of the input and set the stack-string to this value
^ Exit caret-mode
stack-string = "abcdef"
/ Operator (behaves differently depending on the argument)
/.{#}/ And the argument to this operator is a regex, so this program gets the matches of this regex into the stack-array
. Any character
{#} Pops a line from the input. So now this evaluates to # of any character where # is the second line of the input (in this case, 3)
stack-array = ["abc"]
And now we just need to get the last character in this string, but first
S1 Join the array on the number 1 and set this to the stack-string. Because the array only contains one element, the number 1 does not appear in the stack-string.
stack-string = "abc"
/ Operator; because the argument is a regex, this retrieves the matches of the regex:
/.$/ Get the last character in the string
stack-array = ["c"]
Bây giờ, điều này trả về một mảng một phần tử chứa một chuỗi có độ dài một, nhưng nó được hiển thị dưới dạng một chuỗi trong trang web.
Nếu chúng ta thực sự muốn đưa ra kết quả dưới dạng một chuỗi, chúng ta có thể dễ dàng thực hiện S","
ở cuối, nhưng điều đó không thành vấn đề vì đầu ra vẫn giống như trên trình thông dịch.