numsearch

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

README

numsearch

A package to find a pattern within a sequence of digits.

While this package is meant to work with the data structures github.com/keep94/sqrt, it will work with anything that implements the required interfaces.

Examples

package main

import (
    "fmt"

    "github.com/keep94/numsearch"
    "github.com/keep94/sqrt"
)

func main() {

    // Find the 0-based position of the first occurrence of "07" within
    // Sqrt(3). This prints 8.
    fmt.Println(numsearch.First(sqrt.Sqrt(3), numsearch.String("07")))
}

More documentation and examples can be found here.

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

func Ints(digits ...int) Pattern

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

func SafeInts(digits ...int) (Pattern, error)

SafeInts works like Ints but returns an error if any of digits is outside the range of 0 and 9.

func SafeString

func SafeString(p string) (Pattern, error)

SafeString works like String but returns an error if p contains invalid characters.

func String

func String(p string) Pattern

String creates a pattern from a string containing digits, for example "3125". If p contains characters not between '0' and '9' inclusive, String panics.

func (Pattern) Backward

func (p Pattern) Backward() []int

Backward returns this pattern in backward/reverse order.

func (Pattern) Forward

func (p Pattern) Forward() []int

Forward returns this pattern in forward order.

func (Pattern) IsZero

func (p Pattern) IsZero() bool

IsZero returns true if this pattern is the zero pattern.

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.

type Searchable

type Searchable interface {

	// All returns the 0 based position and value of each digit in this
	// Searchable from beginning to end.
	All() iter.Seq2[int, int]
}

Searchable represents a sequence of digits between 0-9 with contiguous positions that can be searched.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL