Ad
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Thanks, I'm glad you enjoyed it.

  • Custom User Avatar

    Hi, Thanks for the comment. Yes, the named return value here is just for reducing the number of lines, and make the code enjoyable, since it s just 9 lines of code. I did enjoy writing it. Right now I am thinking about n-queens on 1000x1000 chess board to solve it under 10 seconds. let me know if you have a 10-second solution :)

  • Custom User Avatar

    Yes, I would only use make([]int, 0, n) when the n is known beforehand, otherwise let the append manage allocation. And in the Go production code you may use profiling: https://golang.org/doc/diagnostics#profiling

  • Custom User Avatar

    What about this: Solve([]int{1, 1, 1, 1, 1, 1, 1})

  • Custom User Avatar

    Hi Thanks for the comment. There is one big difference: unicode support. I did this for the unicode support, this way the input string supports unicode input. If you use string directly it is an immutable byte slice and fails for unicode. If you are this hungry for the code performance you should use C or assembly language! The Go code should be clean and reusable , I believe. Repent, repent, premature optimization is a sin :)

  • Custom User Avatar

    Hey,
    Imagine a = []int{1, 2, 5, 100} and s = "abcdef" so
    by r := []rune(s) we convert the input string to its runes (characters),
    so we have slice of characters in r, e.g. r[0]='a' and r[1]='b' and so on.
    Using for _, v := range a { iterate over indices e.g. 1, 2, 5, 100 using v.
    here if v >= 0 && v < len(r) we make sure that index v is valid index inside the slice.
    Then we convert the rune to upper case and save it in place: r[v] = unicode.ToUpper(r[v]).
    So now r = []rune{'a','B','C','d','e','F'}.
    Finally convert to string: return string(r).
    I hope this helps.