Exercise_go_unit_test_是否回文
编写一个回文检测函数,并为其编写单元测试和基准测试,根据测试的结果逐步对其进行优化。(回文:一个字符串正序和逆序一样,如“Madam,I’mAdam”、“油灯少灯油”等。) 来源:李文周blog tip: 要考虑中文则使用 rune // palindrome/palindrome.go package palindrome func IsPalindrome(s string) bool { // 转成 rune, 应对中文等特殊字符 r := []rune(s) l := len(r) for i := 0; i < l/2; i++ { if r[i] != r[l-1-i] { return false } } return true } // palindrome/palindrome_test.go package palindrome import ( "fmt" "testing" ) func TestIsPalindrome(t *testing.T) { type test struct { text string want bool } arr := []test{ {"", true}, {"a", true}, {"aa", true}, {"ab", false}, {"aba", true}, {"abcba", true}, {"abccba", true}, {"abcdabcd", false}, {"Madam,I’mAdam", false}, {"油灯少灯油", true}, } for _, tc := range arr { t.
Recent Posts
Tags
- apache 4
- axios 1
- benchmark 1
- c 1
- canvas 1
- centos 3
- channel 1
- crontab 1
- css 2
- docker 4
- fail2ban 1
- frp 1
- gin 1
- github 1
- go 26
- goaccess 1
- goroutine 1
- http 1
- https 1
- jetbrains 1
- jquery 1
- js 2
- linux 20
- mermaid 1
- mysql 10
- nginx 3
- node 1
- php 43
- prisma 1
- react 8
- server 1
- ssh 2
- tarojs 1
- tcp/ip 1
- token 1
- ubuntu 1
- ufw 1
- unit-test 1
- vmware 1
- vscode 1
- vue 12
- yum 1
- 域名 3
- 安全 2
- 微信 3
- 算法 3