This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""With dictionary d, of format key: word from text, value: word frequency | |
Sort and display the key value pairs in the following ways: | |
1. DISPLAY TOP 20 WORDS & THEIR FREQ - SORTED: MOST FREQUENT FIRST | |
2. DISPLAY ALL WORDS THAT APPEAR MORE THAN 25 TIMES & THEIR COUNT - SORTED: MOST FREQ FIRST | |
3. DISPLAY ALL WORDS - SORTED ALPHABETICALLY | |
""" | |
d = make_wordcount_dict_from(filename) | |
top_twenty_count = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Python allows putting multiple open() statements in a single with. | |
comma-separate multiple open() statement | |
thank you steveha! | |
https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement | |
answered Feb 14 '12 at 19:33 | |
""" | |
def filter(txt, oldfile, newfile): | |
'''\ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## The writable | |
with open('output.txt', 'w') as f: | |
f.write('Hi there!') | |
## we sure about that 'r'??? | |
with open('readthis.txt', 'r') as f: | |
f.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This basic command line argument parsing code is provided and | |
# calls the handle_this() and review_that() functions | |
# inspired by google's python class! thanks! | |
def main(): | |
if len(sys.argv) != 3: | |
print 'usage: ./myscript.py {--option_1 | --option_2} file_to_get' | |
sys.exit(1) | |
option = sys.argv[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" SORT options with lambda function key | |
Given a list of non-empty tuples, return a list sorted in increasing | |
order by the last element in each tuple. | |
e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields | |
[(2, 2), (1, 3), (3, 4, 5), (1, 7)] | |
""" | |
data = [(1, 7), (1, 3), (3, 4, 5), (2, 2)] | |
sorted_by_last = sorted(data, key=lambda tup: tup[::-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python -tt | |
"""simple function tester""" | |
def do(x): | |
return x | |
def test(got, expected): | |
"""Compare what you got with what you expected. | |
Report back please |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python -tt | |
"""test runner | |
test_suite: list of test cases for each function | |
test case: 2-value-tuple with 1) string for function name and 2) a list of test_values | |
test_values: a list of got,expected value pairs saved as tuples | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" FORMAT THE format() and UNPACK THE *args """ | |
res = ['Homie McHomeboy', 35, 24.798] # print this | |
args = (10, 5, 2.2) # with these dimensions | |
print('{0[0]:{1}s} {0[1]:{2}d} {0[2]:{3}f}'.format(res, *args)) | |
## where: | |
## 's' # format a STRING with n spaces, left justified by defau | |
## 'd' # format an INT reserving n spaces, right justified by default | |
## 'f' # format a float, reserving n spaces, and .n spaces after the decimal point, right justfied by default. | |
## AND UNPACK THE *args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python -tt | |
"""Function tester adapted from exercies in Google's Python class""" | |
def donuts(x): | |
return 'Number of donuts: {}'.format(x) | |
# Provided simple test() function used in main() to print | |
# what each function returns vs. what it's supposed to return. | |
def test(got, expected): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## assuming function: test_check_this() | |
## Generator runs each test | |
T = [10, -10, 'h', [], {}, '', 1.0, None, check_this('16'), '\n'] | |
G = (test_check_this(test_val) for test_val in T) | |
for testable_item in T: | |
# run test on that testable_item | |
print next(G) |
NewerOlder