Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Conversation

Marchitect
Copy link
Collaborator

@Marchitect Marchitect commented Jun 20, 2019

Fixes #447

Review of colleague's PR #

Changes proposed in this PR:

  • added function letters() and sortLetters(), test cases, issue raised by bot hopefully fixed

@Marchitect Marchitect changed the title 03 lab 03 updated, issue fixed Jun 20, 2019
@codecov
Copy link

codecov bot commented Jun 20, 2019

Codecov Report

Merging #457 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #457   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          40     41    +1     
  Lines         665    686   +21     
=====================================
+ Hits          665    686   +21
Impacted Files Coverage Δ
03_letters/marchitect/main.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5f8d5d...e34c677. Read the comment docs.

Copy link
Collaborator

@nicko-lee nicko-lee left a 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

@Marchitect
Copy link
Collaborator Author

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 :)

Copy link
Contributor

@juliaogris juliaogris left a 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
Copy link
Contributor

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)) {
Copy link
Contributor

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)
Copy link
Contributor

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)]))
Copy link
Contributor

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) {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lab 03 Letter Frequency
4 participants