Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python3 

2"""Solution to chapter 5, exercise 20: wc""" 

3 

4 

5def wc(filename): 

6 """Accepts a filename as an argument. Prints the number of lines, 

7characters, words (separated by whitespace) and different words 

8(case sensitive) in the file.""" 

9 

10 counts = {'characters': 0, 

11 'words': 0, 

12 'lines': 0} 

13 unique_words = set() 

14 

15 for one_line in open(filename): 

16 counts['lines'] += 1 

17 counts['characters'] += len(one_line) 

18 counts['words'] += len(one_line.split()) 

19 

20 unique_words.update(one_line.split()) 

21 

22 counts['unique words'] = len(unique_words) 

23 for key, value in counts.items(): 

24 print(f'{key}: {value}')