@@ -0,0 +1,1953 @@
+#!/usr/bin/python -tt%0A%0A%22%22%22O(n+m) (where 'n' is size of text and 'm' is size of search string) time%0Asolution to problem 3-25 from /The Algorithm Design Manual/, 2nd ed., by Steven%0ASkiena%0A%22%22%22%0A%0Aimport sys%0A%0A%0Adef add_to_count(search_string):%0A    %22%22%22Counts occurrences of each character in specified string.%0A%0A    Args:%0A        search_string: string whose characters to count%0A%0A    Returns:%0A        Dictionary mapping characters in search string to the number of times%0A        (integer) they appear%0A    %22%22%22%0A%0A    counts = %7B%7D%0A    for character in search_string:%0A        if character not in counts:%0A            counts%5Bcharacter%5D = 1%0A        else:%0A            counts%5Bcharacter%5D += 1%0A    return counts%0A%0A%0Adef subtract_from_count(counts, magazine_string):%0A    %22%22%22Determines whether text of magazine contains sufficient occurrences of%0A    set of characters to compose certain string.%0A%0A    Args:%0A        counts: dictionary mapping characters in string to compose to the%0A                number of times they appear in string%0A        magazine_string: string representing characters in magazine%0A    %22%22%22%0A%0A    # Approach: 'counts' contains characters we need and their number; iterate%0A    # over magazine text and decrement respective count whenever we encounter%0A    # a character we need; stop early when we have them all%0A    for character in magazine_string:%0A        if character in counts:%0A            if counts%5Bcharacter%5D == 1:%0A                del counts%5Bcharacter%5D%0A                if len(counts) == 0:%0A                    return True%0A            else:%0A                counts%5Bcharacter%5D -= 1%0A    return False%0A%0A%0Adef main():%0A    if len(sys.argv) %3C 3:%0A        err = 'usage: %25s search_string magazine_text_file%5Cn' %25 (sys.argv%5B0%5D,)%0A        sys.stderr.write(err)%0A        exit(1)%0A    counts = add_to_count(sys.argv%5B1%5D)%0A    with open(sys.argv%5B2%5D, 'rU') as file_in:%0A        text = file_in.read()%0A    print subtract_from_count(counts, text)%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Create 3-25.py