Ad
  • Default User Avatar

    Replace seen := make(map[string]int, len(gloves)) to seen := make(map[string]int) in my solution, and you will get identical benchmark.

    If you think what I have done is considered "clever" I have bad news for you..

  • Custom User Avatar

    These solutions that try and be clever and test for % 2 inside the loop are doing way too many tests. Here's the results:

    ubuntu@dev:~/codewars/kata/59cfc09a86a6fdf6df0000f1$ go test -bench=. ./...
    ?       codewarrior     [no test files]
    goos: linux
    goarch: amd64
    pkg: codewarrior/kata
    cpu: AMD Ryzen 9 5950X 16-Core Processor            
    BenchmarkKata1-4        10970066               109.5 ns/op
    BenchmarkKata2-4         6148126               195.9 ns/op
    PASS
    ok      codewarrior/kata        2.720s
    

    BenchmarkKata2 is for this code. BenchmarkKata1 is for code that does not check inside the for loop, and has a separate for loop over the map to add "seen" / 2 to the total number of pairs.

    "Clever" in codewars seems to be a positive mark. In reality, you don't want to be clever in your code. Especially with Go, one of the key values of the language is that it is straight-forward and simple. In this case, clever is not even faster, it is almost 50% slower.

  • Custom User Avatar

    What if the string contains runes that are neither uppercase or lowercase (not a letter, but a number or symbol)? unicode considers them neither upper or lowercase. len(str) also returns the number of bytes in the string, not the number of runes.