Created
August 22, 2019 23:34
-
-
Save bradleyyin/6b3bf110bf6db74888c42c79b279e95e to your computer and use it in GitHub Desktop.
code challenge AH 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//"Challenge 9: Find pangrams | |
//Difficulty: Tricky | |
// | |
//Write a function that returns true if it is given a string that is an English pangram, ignoring letter case. | |
// | |
//Tip: A pangram is a string that contains every letter of the alphabet at least once. | |
// | |
//Sample input and output | |
//The string "The quick brown fox jumps over the lazy dog" should return true. | |
//The string "The quick brown fox jumped over the lazy dog" should return false, because it's missing the S. | |
//" | |
// | |
//Excerpt From: Paul Hudson. "Swift Coding Challenges." Apple Books. | |
import Foundation | |
func checkPangram(string: String) -> Bool { | |
let lowerCasedString = string.lowercased() | |
var dictionary : [Character: Int] = [:] | |
let alphabet = "abcdefghijklmnopqrstuvwxyz" | |
for character in lowerCasedString { | |
guard alphabet.contains(character) else { continue } | |
if dictionary[character] != nil { | |
dictionary[character]! += 1 | |
} else { | |
dictionary[character] = 1 | |
} | |
} | |
print(dictionary.count) | |
if dictionary.count == 26 { | |
return true | |
} else { | |
return false | |
} | |
} | |
checkPangram(string: "The quick brown fox jumps over the lazy dog") | |
checkPangram(string: "The quick brown fox jumped over the lazy dog") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment