TheAlgorithms/Go · error

character not available in charmap at position: %v

Error message

character not available in charmap at position: %v

What it means

GeneticString builds a genetic algorithm population from a charmap. For each character position of the target string it verifies that the character at that position exists in the supplied charmap; if not, it cannot ever evolve a match, so it returns this error instead of proceeding.

Source

Thrown at strings/genetic/genetic.go:107

		mutationProb = .4
	}

	debug := conf.Debug

	// Just a seed to improve randomness required by the algorithm
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

	// Verify that the target contains no genes besides the ones inside genes variable.
	for position, r := range target {
		invalid := true
		for _, n := range charmap {
			if n == r {
				invalid = false
			}
		}
		if invalid {
			message := fmt.Sprintf("character not available in charmap at position: %v", position)
			return nil, errors.New(message)
		}
	}

	// Generate random starting population
	pop := make([]PopulationItem, populationNum)
	for i := 0; i < populationNum; i++ {
		key := ""
		for x := 0; x < utf8.RuneCountInString(target); x++ {
			choice := rnd.Intn(len(charmap))
			key += string(charmap[choice])
		}
		pop[i] = PopulationItem{key, 0}
	}

	// Just some logs to know what the algorithms is doing
	gen, generatedPop := 0, 0

	// This loop will end when we will find a perfect match for our target

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Add every character that appears in the target string to the charmap
  2. Use a comprehensive charmap such as full ASCII printable range or a curated alphabet that covers the target
  3. Pre-validate the target against the charmap before calling GeneticString

Example fix

// before
GeneticString("hello world", "abcdefghijklmnopqrstuvwxyz", 100, 0.05)
// after (include the space)
GeneticString("hello world", "abcdefghijklmnopqrstuvwxyz ", 100, 0.05)
Defensive patterns

Strategy: validation

Validate before calling

func validTarget(target, charmap string) bool {
    for _, r := range target {
        if !strings.ContainsRune(charmap, r) {
            return false
        }
    }
    return true
}
if !validTarget(target, charmap) { /* fix charmap */ }

Type guard

func charmapCovers(target, charmap string) bool {
    set := map[rune]bool{}
    for _, r := range charmap { set[r] = true }
    for _, r := range target {
        if !set[r] { return false }
    }
    return true
}

Prevention

When it happens

Trigger: Calling GeneticString with a target string containing a character (or byte at a position) that is absent from the charmap parameter — e.g. target 'hello' with charmap 'abcdef' or a charmap missing lowercase letters, digits, spaces, or punctuation present in the target.

Common situations: Using a limited charmap (only uppercase, or only letters) while the target contains spaces, digits, or punctuation; copying an example that uses a short charmap but editing the target string; non-ASCII/Unicode targets with an ASCII charmap.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/c1edab377e09fb11. Report an issue: GitHub.