From 4d3ba0ba1aec2288d8f0634f3b7c424a896ee886 Mon Sep 17 00:00:00 2001 From: smtriplett Date: Sat, 11 Jan 2014 18:19:32 -0800 Subject: [PATCH] Add a check to handle input text with fewer than 25 words. --- 02-go-forth/tf-02.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/02-go-forth/tf-02.py b/02-go-forth/tf-02.py index 1e50c35..235f467 100755 --- a/02-go-forth/tf-02.py +++ b/02-go-forth/tf-02.py @@ -101,7 +101,9 @@ read_file(); filter_chars(); scan(); remove_stop_words() frequencies(); sort() stack.append(0) -while stack[-1] < 25: +# Check stack length against 1, because after we process +# the last word there will be one item left +while stack[-1] < 25 and len(stack) > 1: heap['i'] = stack.pop() (w, f) = stack.pop(); print w, ' - ', f stack.append(heap['i']); stack.append(1)