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

Exercise: Test Anagram

  • Given the following code, that checks if two strings are anagrams, write tests that verify the function.
package main

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

func main() {
	fmt.Println(is_anagram("ab", "ba"))
	fmt.Println(is_anagram("hello", "hello"))
	fmt.Println(is_anagram("hell", "ello"))
}

func is_anagram(a, b string) bool {
	aa := strings.Split(a, "")
	sort.Strings(aa)
	aaa := strings.Join(aa, "")

	bb := strings.Split(b, "")
	sort.Strings(bb)
	bbb := strings.Join(bb, "")

	return aaa == bbb
}