Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: DNA Sequencing with in place filter

package main

import (
	"fmt"
	"sort"
	"strings"
)

func main() {
	dna := "ACCGXXCXXGTTACTGGGCXTTGT"
	sequences := strings.Split(dna, "X")
	fmt.Println(sequences)
	fmt.Println(len(sequences)) // 6, this contains 2 empty strings as well

	i := 0
	for j := 0; j < len(sequences); j++ {
		if sequences[j] != "" {
			sequences[i] = sequences[j]
			i++
		}
	}
	sequences = sequences[:i]
	fmt.Println(sequences)
	fmt.Printf("%v, %v\n", len(sequences), cap(sequences)) // 4, 6

	sort.Slice(sequences, func(i, j int) bool {
		return len(sequences[i]) > len(sequences[j])
	})
	fmt.Println(sequences)
	fmt.Printf("%v, %v\n", len(sequences), cap(sequences)) // 4, 6
}
[ACCG  C  GTTACTGGGC TTGT]
6
[ACCG C GTTACTGGGC TTGT]
4, 6
[GTTACTGGGC ACCG TTGT C]
4, 6