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.
https://go.dev/doc/effective_go#mixed-caps
https://github.com/golang/go/wiki/CodeReviewComments#mixed-caps
While golint is now deprecated, it would immediately flag use of
MORSE_CODE
as inappropriate.Your own link notes at the bottom:
I’ll take the word of
github.com/golang/go
andgo.dev
documentation itself as more authoritative than an online “learn how to program” intro page. Even if this only concerns style recommendations of what should be prefered, not what must be done.This is either wrong or argued: https://www.golangprograms.com/go-language/constants.html
You are correct
Just a bit cheeky, but demonstrates correct handling of all category
Nd
(unicode.Digit
) digits. There still isn’t really a good way to do the Caesar Cipher letter shifting for anything other than the ASCII alphabet.We would need to define a proper ring type and whatever it would be defined as, some language would say it’s the wrong order. :shrug:
This suffers from a subtle bug that
i
is the byte-offset into the string of the rune, and as such it would assume that both letters of"äa"
would be in even positions, and thus not lowercase either of them.It also does odd things like clamp non-ASCII letters into ASCII letters in a pretty weird way.
Combining both issues
"ÄAÖU"
becomes"auBC"
.The number of codepoints that satisfy
unicode.IsDigit
is also significantly bigger than0-9
and results in things like꧓
mapping to invalid codepoints.I see you are building a string through a loop of string concatenations. This is an accidentally
O(n²)
operation, since strings are immutable, and so a new backing store has to be made for every concatenation and the old data has to be copied into it.As mentioned to others,
len(string)
is the number of bytes in the slice, which can be different from the number of runes in the string. This will cause yourresultIndex
calculation to skip some indexes in the rune slice.Even if there were ever a string long enough to justify splitting up the work, spinning up a goroutine for every single character would not be an efficient way of distributing the work. While goroutines are cheap, they’re not free.
Another fun thing is that
len(string)
returns the number of bytes in the string, not the number of runes in it. This means if we have just one rune> utf8.RuneSelf
this will deadlock, as it will attempt to receivest = <-ret
for the number of bytes in string, while we will only spin off ado_work
for every rune in the string.Once we fix that, we still end up with weird behavior, where there are
\x00
values in your string because you’re indexing off the byte-length ofs
into the rune slice. https://play.golang.org/p/9WcBHj0hiQKThe
i
in the range of arange string
is actually the byte-offset of the rune in the string, so thisreverse
implementation will crash for any string with a rune> utf8.RuneSelf
. https://play.golang.org/p/29wcJFC1NDvSo,
len(string)
returns the number of bytes in the string, not the number of runes in the string. So your preallocation will almost certainly allocate too much memory, which isn’t the worst thing, but it’s important to be aware of. Though, Go is usually pretty good about amoratizing allocations anyways, so such a preallocation is almost always a premature optimizaton.But more importantly unfortunately the preallocated backing array for the
ret
slice does not ever actually get used. Because you are using a prependappend([]rune{r}, ret...)
each loop iteration will allocate a brand new backing array to put both ther
andret...
into, and it cannot reuse the existing allocation inret
. It would be better to just build the rune slice as normal withappend(ret, r)
and then reverse the slice elements.Maybe use 'A' character value syntax instead of raw ASCII values to be more readable?
The Best Practice value to return in the case of an empty string slice should be
[]string(nil)
not[]string{}
.Because for example, from the reference I shared, the code to return an empty slice should be:
not:
But the difference between the two is kind of low, which is why one should also never test for explicit values with
slice == nil
orreflect.DeepEqual(slice, []string{})
but should always test against the length of the string, because what we really care about is that there is zero (or non-zero) items in the slice.The fact that the test failed on
[]string(nil)
when expecting[]string{}
demonstrates that the test code is too strict. Both returns should work in the test, but Best Practice is still that you should be returning the one created withvar slice []string
.Maybe I didn't well understand the object of your post!
The test is (as you can see in function dotest):
Expect(ans).To(Equal(exp))
which is quite general and "github.com/onsi/ginkgo" + "github.com/onsi/gomega"
give from that, in that particular case,:
Consider the case where
array1
is 256 copies of the same string, andarray2
is also large, but does not contain that single duplicated search string.Using
strings.Contains
makes the semantics of the code clearer.Consider that
array1
could feasibly be 256 copies of the same string, so maybe we don’t want to test every single one of them?Loading more items...