2023-04-16

 16 Apr 2023

Check if a sentence is a Pangram

Taken from Exercism

Your task is to figure out if a sentence is a pangram.

A pangram is a sentence using every letter of the alphabet at least once. It is case insensitive, so it doesn’t matter if a letter is lower-case (e.g. k) or upper-case (e.g. K).

For this exercise we only use the basic letters used in the English alphabet: a to z.

Solution

This is the answer I came up with:

function isPangram(sentence: string): boolean {
  // Create a Set from the alphabet
  let alphabet: string = 'abcdefghijklmnopqrstuvwxyz'
  let alphaSet: Set<string> = new Set([...alphabet])

  // Convert the sentence to lowercase, filter against the alphabet Set
  let rawSentenceArray: Array<string> = [...sentence.toLowerCase()]
  let filteredSentenceArray: Array<string> = []

  rawSentenceArray.forEach(element => {
    if (alphaSet.has(element)) {
      filteredSentenceArray.push(element)
    }
  })

  // Convert the filtered sentence Array into a Set, 
  let sentenceSet: Set<string> = new Set([...filteredSentenceArray])

  // Compare the two Sets via number of elements (since 26 chars in alphabet)
  return (sentenceSet.size === alphaSet.size) ? true : false
}

The tests checked for underscores, numbers, and punctuation but the implementation covered all edge cases since filtering against a Set is discriminative.

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABDAzgBQIZgOYCcMC2AFCgKZhTkSkBciKUuMOAlHQEZxwA2pWiAbwBQiRAHoxiAMK4+lRBkQBlUlETBccAoigALUgu4AHXRnaqRiXmozHT5qHQZMciALyIA5GYgATUsDYujAAVgDW3ARgcEYAjrgMIABuAO4AHgCeAF6eltaGJhgqjsqqADzOzNgAfO6IYKQppVBEANoAdJ22hQ4AuixClhLSCEmkuGp6BmQUVAZQcFZwKeMQGGQANOow3JS4CtgYzAw6+gX2qs15l-gpKrOQpACCuPgZdC9vFYxVtR4dnRmlEe7QWABlluMpOtSEQWL1rmpgDs9qRfPdgdRPhh3ohsRlvi4anVWgjLLcMXN8e1gHBcABRDAQXREUi8AjkNRuWrCUSiGDARBEbqmYrtUwoVnszksFiCSx87a7cZoymPalGEAoFls0gcigDRUAX0sRoGQ0kUlG40mZ2RytkvnonLmeNeOOQFEWimKW0Rzoe1GKdGKhN+dQaTWKbU6NJRKvRLvV7oy-UGomGVoIRgwslO8xSi2KKEQSRgijAIAI5n2cEFuv1UBLJGY1EQACYAGyIZm5kvMc5mVSGxCyKAgXBIEhJoOqdooGBZAxuFeDsULpdygD8OlwIAMdGAtjIQhNQggCBQPFI7W4cGwRFQmBw+GIngAKvoAPqxEAwCBhF+7CaCkYBfrSaRfiEVZGCgX5wGMuBflMX7cBgWQZF+vj3p4sriJIjD7uel7Xre96PugWB4IQRAfmcv7-mEiDAcsSAQYg0HZiWCHjPmVjoRkiDYdguFysMhGkIMF5gFevBkQ+T5Ua+tGfgYDEAcxIFsYsnGwYgPH7FM-GCcJon4eox6SdJsk3neCmUS+NGeAA7LoADMiC-gAjBpLGgeocBpBxMHcUkbn7K5HloRhQk4XhwxHtwJ7ETJpF2RRz7UW+ABEABiMBjJ5IBMmEVSIAAWqQJgZAkHE8GoBCCSkGBBeYvjtNlZniXuVkkXJ6WKY5b5GV5RWMZprEBUFumhbxRntvxMXCSgXUET1QhAA

Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated October 9, 2024 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems