This is the "Clean up the words" challenge from CodeEval:
Given a list of words mixed with extra symbols. Write a program that will clean up the words from extra numbers and symbols.
- 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.
- 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.
(--9Hello----World...--) Can 0$9 ---you~ 13What213are;11you-123+138doing7
hello world can you what are you doing
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.
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 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.