-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lab 03 updated, issue fixed #457
Conversation
Codecov Report
@@ Coverage Diff @@
## master #457 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 40 41 +1
Lines 665 686 +21
=====================================
+ Hits 665 686 +21
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting logic of implementing letters func - didn't think of approaching it this way
I wanted to say I don't even understand my logic now lolz, thank you for the feedback though :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add link to your review of colleague's PR and fix commit messages with git rebase -i HASH
|
||
func letters(s string) map[rune]int { | ||
m := make(map[rune]int) | ||
// keep a list of what characters exist in the string s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what for?
// keep a list of what characters exist in the string s | ||
chars := "" | ||
for _, c := range s { | ||
if !strings.ContainsAny(chars, string(c)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inefficient. ContainsAny is O(n)
and so it strings.Count(s, string(c))
, updating a map is O(1)
.
I accept that runtime is irrelevant for the size of strings expected in this program, but it is good practise to be aware.
for _, c := range s {
m[c]++
}
should do the trick
|
||
func sortLetters(m map[rune]int) []string { | ||
result := make([]string, 0) | ||
keys := make([]int, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the capacity to make as you know it len(m)
.
sort.Ints(keys) | ||
for _, k := range keys { | ||
unquoted, _ := strconv.Unquote(strconv.QuoteRune(rune(k))) | ||
result = append(result, unquoted+":"+strconv.Itoa(m[rune(k)])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s := fmt.Sprintf("%c:%d", rune(k), m[rune(k)])
result = append(result, s)
expected := sortLetters(letters(table.input)) | ||
actual := table.output | ||
// reference: https://yourbasic.org/golang/compare-slices/ | ||
if !reflect.DeepEqual(actual, expected) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use testify assert.Equal to turn these three lines into a oneliner.
Fixes #447
Review of colleague's PR #
Changes proposed in this PR: