NQ peptide[2:4] -> NQL peptide[2:5...">

What is the best way to get cyclic substrings

I need something like:

var peptide = "LENQ"

peptide[2:3] -> NQ
peptide[2:4] -> NQL
peptide[2:5] -> NQLE

      

What's the best way to do this? Maybe there is a library function to get it or I need to write it myself?

+3


source to share


1 answer


For example,

package main

import (
    "fmt"
    "unicode/utf8"
)

func cyclicSubstrings(str string) []string {
    n := utf8.RuneCountInString(str)
    substrs := make([]string, 0, n*n)
    cycles := str + str
    for i := range str {
        cycle := cycles[i : i+len(str)]
        for j, r := range cycle {
            substrs = append(substrs, cycle[:j+utf8.RuneLen(r)])
        }
    }
    return substrs
}

func main() {
    peptide := "LENQ"
    fmt.Println(cyclicSubstrings(peptide))
}

      



Output:

[L LE LEN LENQ E EN ENQ ENQL N NQ NQL NQLE Q QL QLE QLEN]
+4


source







All Articles