patternpythonModerate
Pangrams CodeEval challenge
Viewed 0 times
codeevalpangramschallenge
Problem
I took a challenge on CodeEval. Although the code seems to work for the examples taken from the site, I feel it is not really pretty and must be more complicated than it should be.
Description:
The sentence 'A quick brown fox jumps over the lazy dog' contains
every single letter in the alphabet. Such sentences are called
pangrams. You are to write a program, which takes a sentence, and
returns all the letters it is missing (which prevent it from being a
pangram). You should ignore the case of the letters in sentence, and
your return should be all lower case letters, in alphabetical order.
You should also ignore all non US-ASCII characters.In case the input
sentence is already a pangram, print out the string NULL.
Description:
The sentence 'A quick brown fox jumps over the lazy dog' contains
every single letter in the alphabet. Such sentences are called
pangrams. You are to write a program, which takes a sentence, and
returns all the letters it is missing (which prevent it from being a
pangram). You should ignore the case of the letters in sentence, and
your return should be all lower case letters, in alphabetical order.
You should also ignore all non US-ASCII characters.In case the input
sentence is already a pangram, print out the string NULL.
import sys
filepath = sys.argv[1]
f = open(filepath)
wholealphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z')
for line in f:
sortedletters = list(set(line.lower()))
i = 0
while i != len(sortedletters):
if wholealphabet.count(sortedletters[i]) != 0:
i = i + 1
else:
sortedletters.pop(i)
missingletters = ""
for letter in wholealphabet:
if sortedletters.count(letter) == 0:
missingletters +=letter
if len(missingletters) == 0:
print("NULL")
else:
print(missingletters)Solution
One of Python's greatest strengths is its built-in capability to use sets directly. I don't feel you've used sets to their fullest extent here. I'd also like to point out the
That's really all you need. Unless you want to reconsider the definition of a sentence. :)
with statement, which you should probably use to handle file handles.from __future__ import with_statement
import sys
from string import ascii_lowercase
filepath = sys.argv[1]
wholealphabet = frozenset(ascii_lowercase)
# Use with to handle file … handles
with open(filepath) as f:
for line in f: # assume a line is a sentence, not exactly per spec?
# sortedletters = list(set(line.lower())) # guaranteed to be *unsorted*
missingletters = wholealphabet.difference(line.lower())
if missingletters:
print ''.join(sorted(missingletters))
else:
print 'NULL'That's really all you need. Unless you want to reconsider the definition of a sentence. :)
Code Snippets
from __future__ import with_statement
import sys
from string import ascii_lowercase
filepath = sys.argv[1]
wholealphabet = frozenset(ascii_lowercase)
# Use with to handle file … handles
with open(filepath) as f:
for line in f: # assume a line is a sentence, not exactly per spec?
# sortedletters = list(set(line.lower())) # guaranteed to be *unsorted*
missingletters = wholealphabet.difference(line.lower())
if missingletters:
print ''.join(sorted(missingletters))
else:
print 'NULL'Context
StackExchange Code Review Q#6323, answer score: 13
Revisions (0)
No revisions yet.