loading...

re: Daily Challenge #60 - Find the Missing Letter VIEW POST

FULL DISCUSSION
 

Time to Go find the missing letter!

missing_letter.go

package find

// MissingLetter indicates what the missing character is in a ordered sequence of characters
func MissingLetter(chars []rune) rune {
    var last int

    for _, r := range chars {
        if last != 0 && int(r)-last > 1 {
            return rune(r - 1)
        }
        last = int(r)
    }

    return rune(last)
}

missing_letter_test.go

package find

import "testing"

type testCase struct {
    description string
    input       []rune
    expected    rune
}

func TestMissingLetter(t *testing.T) {
    testCases := []testCase{
        {
            "two characters",
            []rune{'A', 'C'},
            'B',
        },
        {
            "dev-to example one",
            []rune{'a', 'b', 'c', 'd', 'f'},
            'e',
        },
        {
            "dev-to example two",
            []rune{'O', 'Q', 'R', 'S'},
            'P',
        },
    }

    for _, test := range testCases {
        if result := MissingLetter(test.input); result != test.expected {
            t.Fatalf("FAIL: %s - MissingLetter(%+v): %v - expected %v", test.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.description)
    }
}

 

Nice, but I think there are a few edge cases you might consider handling:

  • Empty inputs return 0 (I guess that could work).
  • Inputs that don't actually miss a letter will return the ASCII code of the last element.
  • One element inputs return the ASCII code of the only letter (just a special case of the previous point really).

In other words, is returning the character representation of last really a sensible default if the input does not have a missing letter, either due to length or contents?

 

Its not but I didn't really have much time for this at the time. So I implemented it within the context of the challenge as specified

 

Meaning, slice is always valid, always has length of at least 2, always has exactly one character missing, and all the characters will be of the same case.

My solution includes no consideration for these edge cases since within the context of this challenge, those edge cases don't exist.

Sure, was just trying to have a discussion. Generally the problems in these challenges are not particularly interesting by themselves, but that doesn't mean one can't turn them into a learning experience by e.g. doing more than what was specified.

Oh no of course, you are right and I agree with all the points you made.

code of conduct - report abuse