Avatar of artemkorsakov

artemkorsakov's solution

to Pangram in the Go Track

Published at Feb 21 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

Source

Wikipedia https://en.wikipedia.org/wiki/Pangram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

cases_test.go

package pangram

// Source: exercism/problem-specifications
// Commit: c319f15 pangram: Apply new "input" policy
// Problem Specifications Version: 1.4.0

var testCases = []struct {
	description string
	input       string
	expected    bool
}{
	{
		description: "sentence empty",
		input:       "",
		expected:    false,
	},
	{
		description: "recognizes a perfect lower case pangram",
		input:       "abcdefghijklmnopqrstuvwxyz",
		expected:    true,
	},
	{
		description: "pangram with only lower case",
		input:       "the quick brown fox jumps over the lazy dog",
		expected:    true,
	},
	{
		description: "missing character 'x'",
		input:       "a quick movement of the enemy will jeopardize five gunboats",
		expected:    false,
	},
	{
		description: "another missing character, e.g. 'h'",
		input:       "five boxing wizards jump quickly at it",
		expected:    false,
	},
	{
		description: "pangram with underscores",
		input:       "the_quick_brown_fox_jumps_over_the_lazy_dog",
		expected:    true,
	},
	{
		description: "pangram with numbers",
		input:       "the 1 quick brown fox jumps over the 2 lazy dogs",
		expected:    true,
	},
	{
		description: "missing letters replaced by numbers",
		input:       "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog",
		expected:    false,
	},
	{
		description: "pangram with mixed case and punctuation",
		input:       "\"Five quacking Zephyrs jolt my wax bed.\"",
		expected:    true,
	},
	{
		description: "upper and lower case versions of the same character should not be counted separately",
		input:       "the quick brown fox jumps over with lazy FX",
		expected:    false,
	},
}

pangram_test.go

package pangram

import (
	"testing"
)

func TestPangram(t *testing.T) {
	for _, test := range testCases {
		if actual := IsPangram(test.input); actual != test.expected {
			t.Fatalf("FAIL: %s\nInput %q expected [%t], actual [%t]", test.description, test.input, test.expected, actual)
		}
		t.Logf("PASS: %s", test.description)
	}
}

func BenchmarkPangram(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range testCases {
			IsPangram(test.input)
		}
	}
}
package pangram

import (
	"strings"
)

const alphabet = "abcdefghijklmnopqrstuvwxyz"

// IsPangram checks that input is a pangram.
func IsPangram(input string) bool {
	inputUpper := strings.ToLower(input)
	for _, c := range alphabet {
		if !strings.ContainsRune(inputUpper, c) {
			return false
		}
	}
	return true
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?