Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def main():
- print('sys.argv', sys.argv)
- args = sys.argv[1:]
- if len(args) == 1:
- arg = args[0]
- words = word_array(arg)
- print(repr(words))
- else:
- sys.stderr.write("Expecting zero or one command-line argument. Got {!r}\n".format(args))
- raise sys.exit(1)
- def word_array(line):
- words = []
- i = 0
- start_word_index = -1
- while True:
- if i >= len(line):
- break
- curr_is_space = line[i] == ' ' and (line[i-1] != '.') and (line[i-1] != ',') and (line[i-1] != '!') and (line[i-1] != '?')
- curr_is_letter = (line[i] != ' ') and (line[i] != '.') and (line[i] != ',') and (line[i] != '!') and (line[i] != '?')
- curr_is_puncuation = (line[i] == '.') or (line[i] == ',') or (line[i] == '!') or (line[i] == '?')
- if curr_is_letter and start_word_index == -1:
- start_word_index = i
- elif curr_is_space and start_word_index != -1:
- words.append(line[start_word_index:i])
- start_word_index = -1
- elif curr_is_puncuation and start_word_index != -1:
- words.append(line[start_word_index:(i)])
- start_word_index = -1
- i = i + 1
- if start_word_index != -1:
- words.append(line[start_word_index:])
- return words
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement