diff --git a/algs4/frequency_counter.py b/algs4/frequency_counter.py index 60d141d..202a191 100644 --- a/algs4/frequency_counter.py +++ b/algs4/frequency_counter.py @@ -1,6 +1,6 @@ """ Execution: - python frequency_counter L < input.txt + python frequency_counter.py L < input.txt Data files: https://algs4.cs.princeton.edu/31elementary/tnyTale.txt https://algs4.cs.princeton.edu/31elementary/tale.txt @@ -12,40 +12,35 @@ the most frequently occurring word that has length greater than a given threshold. - % python frequency_counter 1 < tinyTale.txt + % python frequency_counter.py 1 < tinyTale.txt it 10 - % python frequency_counter 8 < tale.txt + % python frequency_counter.py 8 < tale.txt business 122 - % python frequency_counter 10 < leipzig1M.txt + % python frequency_counter.py 10 < leipzig1M.txt government 24763 """ -#from algs4.st import ST +import sys from algs4.sequential_search_st import SequentialSearchST +minlen = int(sys.argv[1]) +st = SequentialSearchST() -class FrequencyCounter: - pass +for line in sys.stdin: + words = line.split() + for word in words: + if len(word) < minlen: + continue + if not st.contains(word): + st.put(word, 1) + else: + st.put(word, st.get(word) + 1) -if __name__ == "__main__": - import sys - minlen = int(sys.argv[1]) - st = SequentialSearchST() - for line in sys.stdin: - words = line.split() - for word in words: - - if len(word) < minlen: - continue - if not st.contains(word): - st.put(word, 1) - else: - st.put(word, st.get(word) + 1) - maxstr = "" - st.put(maxstr, 0) - for word in st.keys(): - if st.get(word) > st.get(maxstr): - maxstr = word - print(maxstr, " ", st.get(maxstr)) +maxstr = "" +st.put(maxstr, 0) +for word in st.keys(): + if st.get(word) > st.get(maxstr): + maxstr = word +print(maxstr, " ", st.get(maxstr)) diff --git a/algs4/heap.py b/algs4/heap.py index 7720cc0..e4e7962 100644 --- a/algs4/heap.py +++ b/algs4/heap.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python heap < tiny.txt +% python heap.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python heap < words3.txt +% python heap.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -52,6 +52,7 @@ def sort(cls, arr): return arr + if __name__ == '__main__': import sys diff --git a/algs4/insertion.py b/algs4/insertion.py index 243f2ba..5382837 100644 --- a/algs4/insertion.py +++ b/algs4/insertion.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python insertion < tiny.txt +% python insertion.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python insertion < words3.txt +% python insertion.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -37,6 +37,7 @@ def sort(cls, arr): j -= 1 return arr + if __name__ == '__main__': import sys diff --git a/algs4/merge.py b/algs4/merge.py index 799ea97..317aeb6 100644 --- a/algs4/merge.py +++ b/algs4/merge.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python merge < tiny.txt +% python merge.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python merge < words3.txt +% python merge.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -61,6 +61,7 @@ def mergesort(cls, arr, lo, hi): def sort(cls, arr): return cls.mergesort(arr, 0, len(arr) - 1) + if __name__ == '__main__': import sys diff --git a/algs4/queue.py b/algs4/queue.py index 98ea357..0536014 100644 --- a/algs4/queue.py +++ b/algs4/queue.py @@ -1,10 +1,10 @@ """ -Execution: python queue < input.txt +Execution: python queue.py < input.txt % more tobe.txt to be or not to - be - - that - - - is -% python queue < tobe.txt +% python queue.py < tobe.txt to be or not to be (2 left on queue) """ diff --git a/algs4/quick.py b/algs4/quick.py index c3e7e29..43325a9 100644 --- a/algs4/quick.py +++ b/algs4/quick.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python quick < tiny.txt +% python quick.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python quick < words3.txt +% python quick.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -55,6 +55,7 @@ def quicksort(cls, arr, lo, hi): def sort(cls, arr): return cls.quicksort(arr, 0, len(arr) - 1) + if __name__ == '__main__': import sys diff --git a/algs4/quick_3way.py b/algs4/quick_3way.py index b7ba1f6..d7887cb 100644 --- a/algs4/quick_3way.py +++ b/algs4/quick_3way.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python quick 3-way < tiny.txt +% python quick_3way.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python quick_3way < words3.txt +% python quick_3way.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -51,6 +51,7 @@ def quicksort(cls, arr, lo, hi): def sort(cls, arr): return cls.quicksort(arr, 0, len(arr) - 1) + if __name__ == '__main__': import sys diff --git a/algs4/selection.py b/algs4/selection.py index 2a088c9..3ba0f0b 100644 --- a/algs4/selection.py +++ b/algs4/selection.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python selection < tiny.txt +% python selection.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python selection < words3.txt +% python selection.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -36,6 +36,7 @@ def sort(cls, arr): arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr + if __name__ == '__main__': import sys diff --git a/algs4/shell.py b/algs4/shell.py index 369287b..c761647 100644 --- a/algs4/shell.py +++ b/algs4/shell.py @@ -6,7 +6,7 @@ S O R T E X A M P L E -% python shell < tiny.txt +% python shell.py < tiny.txt A E E L M O P R S T X [ one string per line ] @@ -17,7 +17,7 @@ bed bug dad yes zoo ... all bad yet -% python shell < words3.txt +% python shell.py < words3.txt all bad bed bug dad ... yes yet zoo [ one string per line ] """ @@ -43,6 +43,7 @@ def sort(cls, arr): h /= 3 return arr + if __name__ == '__main__': import sys diff --git a/algs4/stopwatch.py b/algs4/stopwatch.py index 319b7d7..bb753a3 100644 --- a/algs4/stopwatch.py +++ b/algs4/stopwatch.py @@ -1,8 +1,8 @@ """ - Execution: python stopwatch n + Execution: python stopwatch.py n utility class to measure the running time (wall clock) of a program. - % python stopwatch 10000000 + % python stopwatch.py 10000000 2.108185e+10 3.60 seconds 2.108185e+10 4.53 seconds @@ -18,6 +18,7 @@ def __init__(self): def elapsed_time(self): return round(time.time() - self.start, 2) + if __name__ == '__main__': import sys import math