Feat: done

This commit is contained in:
2025-10-08 19:24:13 +08:00
commit 96982b9ae9
9 changed files with 100 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "docs/template"]
path = docs/template
url = ../typst-template

16
Makefile Normal file
View File

@@ -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

BIN
docs/main.pdf Normal file

Binary file not shown.

20
docs/main.typ Normal file
View File

@@ -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"))

1
docs/template Submodule

Submodule docs/template added at 5a290913a2

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module reverse
go 1.25.1

5
result Normal file
View File

@@ -0,0 +1,5 @@
> go test -v
=== RUN TestReverse
--- PASS: TestReverse (0.00s)
PASS
ok reverse 0.001s

31
reverse.go Normal file
View File

@@ -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--
}
}

21
reverse_test.go Normal file
View File

@@ -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)
}
}
}