Documentation
¶
Overview ¶
Package numsearch finds patterns in sequences of digits.
For the github.com/keep94/sqrt package, sqrt.Sequence implements Searchable and sqrt.FiniteSequence implements both RSearchable and Searchable.
While this package is meant to be used with the data structures in the github.com/keep94/sqrt package, it will work with anything that implements the Searchable or RSearchable interface.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All(s Searchable, pattern Pattern) iter.Seq[int]
All returns all the 0 based positions in s where pattern is found.
Example ¶
package main
import (
"fmt"
"github.com/keep94/numsearch"
"github.com/keep94/sqrt"
)
func main() {
// sqrt(2) = 0.14142135... * 10^1
n := sqrt.Sqrt(2)
// '14' matches at index 0, 2, 144, ...
count := 0
for index := range numsearch.All(n, numsearch.String("14")) {
fmt.Println(index)
count++
if count == 3 {
break
}
}
}
Output: 0 2 144
func Backward ¶
func Backward(s RSearchable, pattern Pattern) iter.Seq[int]
Backward returns all the 0 based positions in s where pattern is found from last to first.
Example ¶
package main
import (
"fmt"
"github.com/keep94/numsearch"
"github.com/keep94/sqrt"
)
func main() {
n := sqrt.Sqrt(2)
count := 0
iterator := numsearch.Backward(n.WithEnd(1000), numsearch.Ints(1, 4))
for index := range iterator {
fmt.Println(index)
count++
if count == 3 {
break
}
}
}
Output: 945 916 631
func First ¶
func First(s Searchable, pattern Pattern) int
First finds the zero based index of the first match of pattern in s. First returns -1 if pattern is not found only if s has a finite number of digits. If s has an infinite number of digits and pattern is not found, First will run forever.
Example ¶
package main
import (
"fmt"
"github.com/keep94/numsearch"
"github.com/keep94/sqrt"
)
func main() {
// sqrt(3) = 0.1732050807... * 10^1
n := sqrt.Sqrt(3)
fmt.Println(numsearch.First(n, numsearch.String("0508")))
}
Output: 4
func Last ¶
func Last(s RSearchable, pattern Pattern) int
Last finds the zero based index of the last match of pattern in s. Last returns -1 if pattern is not found in s.
Example ¶
package main
import (
"fmt"
"github.com/keep94/numsearch"
"github.com/keep94/sqrt"
)
func main() {
n := sqrt.Sqrt(2)
fmt.Println(numsearch.Last(n.WithEnd(1000), numsearch.String("14")))
}
Output: 945
Types ¶
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern is a search pattern of digits between 0 and 9 inclusive. The zero value for Pattern is the empty pattern that matches everywhere.
func Ints ¶
Ints creates a pattern from a sequence of digits. Ints panics if any of the digits is outside the range of 0 and 9.
func SafeInts ¶
SafeInts works like Ints but returns an error if any of digits is outside the range of 0 and 9.
func SafeString ¶
SafeString works like String but returns an error if p contains invalid characters.
func String ¶
String creates a pattern from a string containing digits, for example "3125". If p contains characters not between '0' and '9' inclusive, String panics.
type RSearchable ¶
type RSearchable interface {
// Backward returns the 0 based position and value of each digit in this
// RSearchable from end to beginning.
Backward() iter.Seq2[int, int]
}
RSearchable represents a sequence of digits between 0-9 with contiguous positions that can be searched in reverse order.