{"record":{"id":"c1edab377e09fb11","repo":"TheAlgorithms/Go","slug":"character-not-available-in-charmap-at-position-v","errorCode":null,"errorMessage":"character not available in charmap at position: %v","messagePattern":"character not available in charmap at position: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"strings/genetic/genetic.go","lineNumber":107,"sourceCode":"\t\tmutationProb = .4\n\t}\n\n\tdebug := conf.Debug\n\n\t// Just a seed to improve randomness required by the algorithm\n\trnd := rand.New(rand.NewSource(time.Now().UnixNano()))\n\n\t// Verify that the target contains no genes besides the ones inside genes variable.\n\tfor position, r := range target {\n\t\tinvalid := true\n\t\tfor _, n := range charmap {\n\t\t\tif n == r {\n\t\t\t\tinvalid = false\n\t\t\t}\n\t\t}\n\t\tif invalid {\n\t\t\tmessage := fmt.Sprintf(\"character not available in charmap at position: %v\", position)\n\t\t\treturn nil, errors.New(message)\n\t\t}\n\t}\n\n\t// Generate random starting population\n\tpop := make([]PopulationItem, populationNum)\n\tfor i := 0; i < populationNum; i++ {\n\t\tkey := \"\"\n\t\tfor x := 0; x < utf8.RuneCountInString(target); x++ {\n\t\t\tchoice := rnd.Intn(len(charmap))\n\t\t\tkey += string(charmap[choice])\n\t\t}\n\t\tpop[i] = PopulationItem{key, 0}\n\t}\n\n\t// Just some logs to know what the algorithms is doing\n\tgen, generatedPop := 0, 0\n\n\t// This loop will end when we will find a perfect match for our target","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/strings/genetic/genetic.go#L89-L125","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add every character that appears in the target string to the charmap","Use a comprehensive charmap such as full ASCII printable range or a curated alphabet that covers the target","Pre-validate the target against the charmap before calling GeneticString"],"exampleFix":"// before\nGeneticString(\"hello world\", \"abcdefghijklmnopqrstuvwxyz\", 100, 0.05)\n// after (include the space)\nGeneticString(\"hello world\", \"abcdefghijklmnopqrstuvwxyz \", 100, 0.05)","handlingStrategy":"validation","validationCode":"func validTarget(target, charmap string) bool {\n    for _, r := range target {\n        if !strings.ContainsRune(charmap, r) {\n            return false\n        }\n    }\n    return true\n}\nif !validTarget(target, charmap) { /* fix charmap */ }","typeGuard":"func charmapCovers(target, charmap string) bool {\n    set := map[rune]bool{}\n    for _, r := range charmap { set[r] = true }\n    for _, r := range target {\n        if !set[r] { return false }\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Derive the charmap from the target itself (unique runes of target) plus extra symbols","Keep a canonical full-alphabet charmap constant and reuse it","Unit-test GeneticString with each production target string"],"tags":["genetic-algorithm","validation","strings"],"backgroundTag":"invalid-input-charset","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}