commit 96982b9ae9fe79b480c13d17853c07b27c17624d Author: Yi-Ting Shih Date: Wed Oct 8 19:24:13 2025 +0800 Feat: done diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2aa7d37 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "docs/template"] + path = docs/template + url = ../typst-template diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bcd1473 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +.PHONY: all watch clean + +SOURCE := $(shell find docs/ -type f -name '*.typ') +TARGET := docs/main.pdf +TYPST_ARGS += --root . + +all: $(TARGET) + +docs/main.pdf: docs/main.typ $(SOURCE) + typst compile $(TYPST_ARGS) $< + +watch: + typst watch --root . docs/main.typ + +clean: + -rm docs/main.pdf diff --git a/docs/main.pdf b/docs/main.pdf new file mode 100644 index 0000000..a1482ee Binary files /dev/null and b/docs/main.pdf differ diff --git a/docs/main.typ b/docs/main.typ new file mode 100644 index 0000000..683b908 --- /dev/null +++ b/docs/main.typ @@ -0,0 +1,20 @@ +#import "template/module.typ": * + +#show: default.with( + title: "Golang Homework 5", + authors: (( + name: "Yi-Ting Shih (111550013)", + affiliation: "National Yang Ming Chaio Tung University", + email: "ytshih@cs.nycu.edu.tw", + ),), +) + +== Code + +#code(read("../reverse.go")) + +This code doesn't allocate new memory except array subscript `i, l, r`. + +== Result + +#code(read("../result")) diff --git a/docs/template b/docs/template new file mode 160000 index 0000000..5a29091 --- /dev/null +++ b/docs/template @@ -0,0 +1 @@ +Subproject commit 5a290913a2b076125191ce8c1c46457e726b893d diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b97b025 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module reverse + +go 1.25.1 diff --git a/result b/result new file mode 100644 index 0000000..657b981 --- /dev/null +++ b/result @@ -0,0 +1,5 @@ +> go test -v +=== RUN TestReverse +--- PASS: TestReverse (0.00s) +PASS +ok reverse 0.001s diff --git a/reverse.go b/reverse.go new file mode 100644 index 0000000..e3b5a0d --- /dev/null +++ b/reverse.go @@ -0,0 +1,31 @@ +package reverse + +import "unicode/utf8" + +func swap(a *byte, b *byte) { + *a ^= *b + *b ^= *a + *a ^= *b +} + +func reverse(b []byte) { + for i := 0; i < len(b); { + _, sz := utf8.DecodeRune(b[i:]) + l := i + r := i + sz - 1 + for l < r { + swap(&b[l], &b[r]) + l++ + r-- + } + i += sz + } + + l := 0 + r := len(b) - 1 + for l < r { + swap(&b[l], &b[r]) + l++ + r-- + } +} diff --git a/reverse_test.go b/reverse_test.go new file mode 100644 index 0000000..9e2eae2 --- /dev/null +++ b/reverse_test.go @@ -0,0 +1,21 @@ +package reverse + +import "testing" + +func TestReverse(t *testing.T) { + tcs := []struct { + input string + expects string + }{ + {"Hello 世界", "界世 olleH"}, + } + + for _, tc := range tcs { + b := []byte(tc.input) + reverse(b) + ret := string(b) + if ret != tc.expects { + t.Errorf("Failed to remove unicode space. Input: %s, expects: %s, results: %s", tc.input, tc.expects, ret) + } + } +}