patternpythonMinor
Pangrams python implementation
Viewed 0 times
pythonimplementationpangrams
Problem
Given
Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)
After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not. Input Format Input consists of a line containing s.
Constraints
Length of s can be at most 103 \$(1≤|s|≤103)\$ and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.
Solution 1
Solution 2
Which is better in terms of running time and space complexity?
Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)
After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not. Input Format Input consists of a line containing s.
Constraints
Length of s can be at most 103 \$(1≤|s|≤103)\$ and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.
Solution 1
from collections import defaultdict
import string
def is_pangram(astr):
lookup = defaultdict(int)
for char in astr:
lookup[char.lower()] += 1
for char in string.ascii_lowercase:
if lookup[char] == 0:
return False
return True
print "pangram" if is_pangram(raw_input()) else "not pangram"Solution 2
from collections import Counter
import string
def is_pangram(astr):
counter = Counter(astr.lower())
for char in string.ascii_lowercase:
if counter[char] == 0:
return False
return True
print "pangram" if is_pangram(raw_input()) else "not pangram"Which is better in terms of running time and space complexity?
Solution
Since you do not actually need the count of each character, it would be simpler to use a
Your two solutions are almost identical, though here
it would be faster to lowercase the whole string at once (like you already do in solution 2):
Other than that, the only difference is
set and the superset operator >=:def is_pangram(astr):
return set(astr.lower()) >= set(string.ascii_lowercase):Your two solutions are almost identical, though here
for char in astr:
lookup[char.lower()] += 1it would be faster to lowercase the whole string at once (like you already do in solution 2):
for char in astr.lower():
lookup[char] += 1Other than that, the only difference is
defaultdict vs. Counter. The latter makes your code more elegant, but since the standard library implements Counter in pure Python, there may be a speed advantage to defaultdict.Code Snippets
def is_pangram(astr):
return set(astr.lower()) >= set(string.ascii_lowercase):for char in astr:
lookup[char.lower()] += 1for char in astr.lower():
lookup[char] += 1Context
StackExchange Code Review Q#105455, answer score: 5
Revisions (0)
No revisions yet.