debugpythonMinor
Snakes and Letters
Viewed 0 times
andsnakesletters
Problem
This is the "Clean up the words" challenge from CodeEval:
Challenge
Given a list of words mixed with extra symbols. Write a program that will clean up the words from extra numbers and symbols.
Specifications
Constraints
Input Sample
Output Sample
In a previous question someone joked the program would be much shorter / simpler in Python. I accepted this as a challenge and excuse to practice Python.
Solution:
It's so succinct I'm unsure there's enough for a review, but this site surprised me in the past.
Challenge
Given a list of words mixed with extra symbols. Write a program that will clean up the words from extra numbers and symbols.
Specifications
- The first argument is a path to a file.
- Each line includes a test case.
- Each test case is a list of words.
- Letters are both lowercase and uppercase, and mixed with extra symbols.
- Print the words separated by spaces in lowercase letters.
Constraints
- The length of a test case together with extra symbols can be in a range from 10 to 100 symbols.
- The number of test cases is 40.
Input Sample
(--9Hello----World...--)
Can 0$9 ---you~
13What213are;11you-123+138doing7Output Sample
hello world
can you
what are you doingIn a previous question someone joked the program would be much shorter / simpler in Python. I accepted this as a challenge and excuse to practice Python.
Solution:
import sys
import re
def sanitized(line):
sanitized_line = re.sub("[^a-zA-Z]+", " ", line)
return sanitized_line.lower().strip()
def main(file):
with open(file, 'r') as input_file:
for line in input_file:
print(sanitized(line))
if __name__ == "__main__":
try:
file = sys.argv[1]
main(file)
except:
print("No argument provided.")It's so succinct I'm unsure there's enough for a review, but this site surprised me in the past.
Solution
The code is pretty clear and clean. I will have to do some nit-picking, but here goes an attempt:
-
Change it to
-
You're printing the error message to stdout. You could consider exiting using
-
'r'ead mode is the default opening mode. While perhaps clearer with the extra argument,
-
In case of Python 2: don't use
Nits:
-
Do you want it to be Python 3 only? Otherwise, include a
-
doc-strings to the program and functions, if you want to go all-out. (A linter would complain about that, even if it's pretty unnecessary here, and not to the point of the exercise.)
-
try - except without the actual exception(s). Here, you print an error when the exception raised is an IndexError (to sys.argv), but consider that you get the same error message if the file can't be properly opened (or is simply non-existent).Change it to
except IndexError, and let any IO/OSError just bubble up, since the corresponding error message is often clear enough (e.g., 'Is a directory', 'File does not exist', etc).-
You're printing the error message to stdout. You could consider exiting using
sys.exit("No argument provided."), which will exit with an exit code of 1, and print the message to stderr. Or raise an equivalent exception to the one you're catching.-
'r'ead mode is the default opening mode. While perhaps clearer with the extra argument,
with open(file) as ... is more standard.-
In case of Python 2: don't use
file as a variable name, since it shadows the built-in file function. Use e.g. filename instead.Nits:
-
Do you want it to be Python 3 only? Otherwise, include a
from __future__ import print_function at the top. print will still work as it is now in Python 2, but once you use more arguments, you're printing a tuple in Python 2, instead of a series of concatenated str-ified elements. (Thanks to JoeWallis in the comments for pointing out the mistake that I was thinking "tuple"-wise where it was just a parenthesized string.)-
doc-strings to the program and functions, if you want to go all-out. (A linter would complain about that, even if it's pretty unnecessary here, and not to the point of the exercise.)
Context
StackExchange Code Review Q#131804, answer score: 7
Revisions (0)
No revisions yet.