Commits

Markus Tzoe committed b676843

update fibonacci

Comments (0)

Files changed (1)

fibonacci/fibonacci.go

 	return m
 }
 
-func calcmid(pattern string) (int64, int64) {
-	l0 := len(pattern)
-	m := getfib(l0)
-	b := []byte(f(m))
-	b2 := []byte(f(m + 1))
-	b = append(b[len(b)-l0+1:], b[:l0-1]...)
-	b2 = append(b2[len(b2)-l0+1:], b2[:l0-1]...)
-	l1 := len(b) - l0
-	l2 := len(b2) - l0
-	count, count2 := int64(0), int64(0)
-	for i := 0; i <= l1; i++ {
-		if string(b[i:i+l0]) == pattern {
-			count++
-			// fmt.Println("[1]: ", i, string(b[i:i+l0]))
-		}
-	}
-	for i := 0; i <= l2; i++ {
-		if string(b2[i:i+l0]) == pattern {
-			count2++
-			// fmt.Println("[2]: ", i, string(b2[i:i+l0]))
+func count(src []byte, pattern string) int64 {
+	ret := int64(0)
+	l := len(pattern)
+	k := len(src) - l
+	for i := 0; i <= k; i++ {
+		if string(src[i:i+l]) == pattern {
+			ret++
 		}
 	}
-	// fmt.Println(string(b), string(b2))
-	return count, count2
+	return ret
+}
+
+func calcmid(pattern string) (int64, int64) {
+	l := len(pattern)
+	m := getfib(l)
+	b1 := []byte(f(m))
+	b2 := []byte(f(m + 1))
+	b1 = append(b1[len(b1)-l+1:], b1[:l-1]...)
+	b2 = append(b2[len(b2)-l+1:], b2[:l-1]...)
+	return count(b1, pattern), count(b2, pattern)
 }
 
 func calcend(pattern string) (int64, int64, int) {
-	l0 := len(pattern)
-	lim := getfib(l0)
+	l := len(pattern)
+	lim := getfib(l)
 	fib1 := f(lim)
 	fib2 := f(lim + 1)
-	b := []byte(fib1)
+	b1 := []byte(fib1)
 	b2 := []byte(fib2)
-	l1 := len(b) - l0
-	l2 := len(b2) - l0
-	count, count2 := int64(0), int64(0)
-	for i := 0; i <= l1; i++ {
-		if string(b[i:i+l0]) == pattern {
-			count++
-		}
-	}
-	for i := 0; i <= l2; i++ {
-		if string(b2[i:i+l0]) == pattern {
-			count2++
-		}
-	}
-	return count2, count, lim
+	return count(b2, pattern), count(b1, pattern), lim
 }
 
 func brute(num int, pattern string) int64 {
 		return -1
 	}
 	b := []byte(f(num))
-	count := int64(0)
-	l0 := len(pattern)
-	l := len(b) - len(pattern)
-	for i := 0; i <= l; i++ {
-		if string(b[i:i+l0]) == pattern {
-			count++
-		}
-	}
-	return count
+	return count(b, pattern)
 }
 
 func Calc(num int, pattern string) int64 {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.