-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.py
72 lines (51 loc) · 1.83 KB
/
wordcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
"""Wordcount exercise
The main() function is already defined and complete. It calls the
print_words() and print_top() functions, which you fill in.
See the README for instructions.
Use str.split() (no arguments) to split on all whitespace.
Workflow: don't build the whole program at once. Get it to an intermediate
milestone and print your data structure. Once that's working, try for the
next milestone.
Implement the create_word_dict() helper function that has been defined in
order to avoid code duplication within print_words() and print_top(). It
should return a dictionary with words as keys, and their counts as values.
"""
# Your name, plus anyone who helped you with this assignment
# Give credit where credit is due.
__author__ = "???"
import sys
def create_word_dict(filename):
"""Returns a word/count dict for the given file."""
# Your code here
return
def print_words(filename):
"""Prints one per line '<word> : <count>', sorted
by word for the given file.
"""
# Your code here
return
def print_top(filename):
"""Prints the top count listing for the given file."""
# Your code here
return
# This basic command line argument parsing code is provided and calls
# the print_words() and print_top() functions which you must implement.
def main(args):
if len(args) != 2:
print('usage: python wordcount.py {--count | --topcount} file')
sys.exit(1)
option = args[0]
filename = args[1]
if option == '--count':
print_words(filename)
elif option == '--topcount':
print_top(filename)
else:
print('unknown option: ' + option)
if __name__ == '__main__':
main(sys.argv[1:])