Skip to content

Commit

Permalink
change document for run python files
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Jun 9, 2019
1 parent fa54aea commit e4a9b3d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 45 deletions.
49 changes: 22 additions & 27 deletions algs4/frequency_counter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
5 changes: 3 additions & 2 deletions algs4/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand Down Expand Up @@ -52,6 +52,7 @@ def sort(cls, arr):

return arr


if __name__ == '__main__':
import sys

Expand Down
5 changes: 3 additions & 2 deletions algs4/insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand All @@ -37,6 +37,7 @@ def sort(cls, arr):
j -= 1
return arr


if __name__ == '__main__':
import sys

Expand Down
5 changes: 3 additions & 2 deletions algs4/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions algs4/queue.py
Original file line number Diff line number Diff line change
@@ -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)
"""

Expand Down
5 changes: 3 additions & 2 deletions algs4/quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions algs4/quick_3way.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions algs4/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand All @@ -36,6 +36,7 @@ def sort(cls, arr):
arr[i], arr[minIndex] = arr[minIndex], arr[i]
return arr


if __name__ == '__main__':
import sys

Expand Down
5 changes: 3 additions & 2 deletions algs4/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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 ]
"""
Expand All @@ -43,6 +43,7 @@ def sort(cls, arr):
h /= 3
return arr


if __name__ == '__main__':
import sys

Expand Down
5 changes: 3 additions & 2 deletions algs4/stopwatch.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit e4a9b3d

Please sign in to comment.