Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Thanks, I'm glad you enjoyed it.
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 :)
Yes, I would only use
make([]int, 0, n)
when then
is known beforehand, otherwise let theappend
manage allocation. And in the Go production code you may use profiling: https://golang.org/doc/diagnostics#profilingWhat about this:
Solve([]int{1, 1, 1, 1, 1, 1, 1})
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 :)
Hey,
Imagine
a = []int{1, 2, 5, 100}
ands = "abcdef"
soby
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'
andr[1]='b'
and so on.Using
for _, v := range a {
iterate over indices e.g.1, 2, 5, 100
usingv
.here
if v >= 0 && v < len(r)
we make sure that indexv
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.