在stack看到HENNGE公司的招聘信息,于是去参加了一次线上笔试。对方法发了三道题,此为第一道题——使用Golang处理文本。
下为要求:
仔细思考后,发现一个规律:
第1行指定总行数;偶数行n指定下一行奇数行n+1行的个数;全部数据喂完后出结果,意味着最后是扔进数组,放到最后遍历。提示:
要求不能使用for;只能使用基本库。因为Golang的循环语句出来了for,只剩下goto和递归。因为用goto会下地狱,因此我用递归。因为没学过Golang,只看了几个小时的语句介绍,可能写的比较菜,见谅。
package main import ( "fmt" "io/ioutil" "regexp" "strconv" "strings" ) var output []int func main() { var err1 error var testfile0 []string // That's the test file name // this demo get data from the test.txt file, err1 := ioutil.ReadFile("./test.txt") if err1 != nil { fmt.Println(err1) } testfile0 = strings.Split(string(file), "\r\n") total, _ := strconv.Atoi(testfile0[0]) count := 0 m := 0 list(m, testfile0, count, total) // fmt.Println(output) outPrint(0, output) } func squareSum(n int, arr []string) int { // fmt.Println("传进来的参数,代表个数:", n) // fmt.Println("此时指定奇数行的值:", arr[n-1]) num, _ := strconv.Atoi(arr[n-1]) if n == 1 { num1, _ := strconv.Atoi(arr[0]) // fmt.Println("最后一个的平方:", num1*num1) return num1 * num1 } if num >= 0 { // fmt.Println("平方:", num*num) return num*num + squareSum(n-1, arr) } return squareSum(n-1, arr) } func outPrint(i int, output []int) { if i+1 > len(output) { return } // fmt.Println("i", i) // fmt.Println("output[i]", output[i]) fmt.Println(output[i]) i++ outPrint(i, output) return } func list(m int, testfile0 []string, count int, total int) { if m+1 >= len(testfile0) { // fmt.Println(count) if count == total { fmt.Println("success, the first line number equal the test line ") return } fmt.Println("error, the first line number not equal the test line") return } if (m+1)%2 == 0 { var sum int = 0 // fmt.Println("m:", m) // fmt.Println("testfile0[m+1]:", testfile0[m+1]) pretext := regexp.MustCompile(`\s`) text := pretext.ReplaceAllString(testfile0[m+1], `,`) // fmt.Println("得到的数组:", text) // fmt.Println("上一行,指定的数字个数:", testfile0[m]) var arr []string = strings.Split(text, ",") num, _ := strconv.Atoi(testfile0[m]) sum = squareSum(num, arr) count++ // fmt.Println("total:", sum) output = append(output, sum) // fmt.Println(output) // outPrint(0, output) // return output } m++ list(m, testfile0, count, total) // fmt.Println(output) return }
