#! /usr/bin/python3 import re import os import urllib.request ## import urllib ## in python2 # ******************************************************** # CS 200 HW #2 DUE Monday, 10/2/2023 at 11:59 pm # via the submit system on the Zoo # ******************************************************** # Name: # Email address: # ******************************************************** # This file may be opened in python 3 # Lines beginning with hashes are comments. # If you are asked to write a procedure, please make sure it has # the specified name, and the specified number and order of arguments. # The names of the formal arguments need not be the same # as in the problem specification. # For each problem, the intended inputs to your procedures # are specified (for example, "positive integers") # and your procedures need not do anything reasonable # for other possible inputs. # You may write auxiliary procedures in addition to # the requested one(s) -- for each of your auxiliary # procedures, please include a comment explaining # what it does, and giving an example or two. # You may also use procedures you have written # elsewhere in this assignment or previous assignments. # They only need to be defined once somewhere within this file. # Reading: Google Python Class ## sections: all of them - through utilities # ******************************************************** # ** problem 0 ** (1 easy point) # Replace the number 0 in the definition below to indicate # the number of hours you spent doing this assignment # Decimal numbers (eg, 6.237) are fine. Exclude time # spent reading. def hours(): return 0 # ******************************************************** # ** problem 00 ** (1 fairly easy point) # Below is a UNIX transcript with one command replaced by XXXX transcript = ''' bash-4.4$ pwd /home/accts/sbs5/cs201/hws/new bash-4.4$ ls bash-4.4$ touch file bash-4.4$ ls -l total 0 -rw-rw-r-- 1 sbs5 cs201ta 0 Jan 26 18:27 file bash-4.4$ XXXX bash-4.4$ ls -l total 0 -rw-rw-r-- 1 sbs5 cs201ta 0 Jan 26 18:27 homework ''' # define xxxx below to return the correct UNIX command def xxxx(): return "UNIX command" # ******************************************************** # ** problem 1 ** (8 points) # Write a procedure # sumtree(tree) # that takes a nested list tree and returns the sum of integers in the leaves # you may NOT assume that all the leaves are integers # if a leaf is not an integer, ignore it. # Examples # sumtree([1, 2, 3]) => 6 # sumtree([1, [2, [3]]]) => 6 # sumtree([[[]]]) => 0 # sumtree([[[[2]]]]) => 2 # sumtree([1,2,3,4]) => 10 # sumtree([1,"2","3",4]) => 5 # sumtree([1,"2","3",4, False]) => 5 # sumtree([1,"2","3",4, False, []]) => 5 # ******************************************************** def sumtree(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 2 ** (10 points) # Write a procedure # doubletree(tree) # that takes a nested list tree and returns another tree with the same # structure, but with each element doubled # you may NOT assume that all the leaves are integers # if a leaf is not an integer, include it unmodified # Examples # doubletree([1, 2, 3]) => [2,4,6] # doubletree([1, [2, [3]]]) => [2, [4, [6]]] # doubletree([]) => [] # doubletree([[[[8, 8, 8]]]]) => [[[[16, 16, 16]]]] # doubletree([1, 2, 3, "4", "5", ["6"]]) => [2, 4, 6, '4', '5', ['6']] # doubletree([1, 2, 3, "4", "5", ["6"], True]) => [2, 4, 6, '4', '5', ['6'], True] # ******************************************************** def doubletree(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 3 ** (10 points) # Write a procedure # largesttree(tree) # which takes a nested list tree and returns the largest number among # the leaves # you may assume that all the leaves are integers # Examples # largesttree([1, 2, 3, 4, 5]) => 5 # largesttree([]) => -inf # largesttree([1, [2, [3, [4, [5]]]]]) => 5 # largesttree([[[[[3]]]]]) => 3 # ******************************************************** def largesttree(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 4 ** (10 points) # Write a procedure # allsametree(tree) # that takes a nested list tree of n elements and returns True if # the leaf elements are identical, else it returns False # Note: leaf nodes may not always be integers # Examples: # allsametree([1, 1, 1, 1]) => True # allsametree([1, 2, 3]) => False # allsametree([]) => True # allsametree([1, [1, [1, [1]]]]) => True # allsametree([1, [[[[[2]]]]]]) => False # allsametree(['a', 'a', 'a']) => True # allsametree(['a', 'b', 'c']) => False # ******************************************************** def allsametree(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 5 ** (10 points) # Write a procedure # types(tree) # that takes a nested list tree and returns a list of all # the different data types of the leaf nodes, in alphabetical order, # without duplication. # Examples: # types([1, [2.3, "a"], True, 3, 4, 5]) => ['bool', 'float', 'int', 'str'] # types([1,2,3,4]) => ['int'] # types([1,2,3,4, True, "hello"]) => ['bool', 'int', 'str'] # types([1,2,3,4, True, "hello", 1,2]) => ['bool', 'int', 'str'] # types([1,2,3,4, True, "hello", 1.2]) => ['bool', 'float', 'int', 'str'] # types([1,2,3,4, True, "hello", 1.2, {}]) => ['bool', 'dict', 'float', 'int', 'str'] # types([1,2,3,4, True, "hello", 1.2, {}, (1,2,3)]) => ['bool', 'dict', 'float', 'int', 'str', 'tuple'] # ******************************************************** def types(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 6 ** (10 points) # Write a procedure # depth(tree) # that takes a tree and returns the maximum depth of the tree. # The empty list or any non list has a depth of 0 # Every embedding increases the depth by 1. # Examples: ''' depth([]) => 0 depth([1,2,3,4,5]) => 1 depth('tree') => 0 depth([1,[2,[3,[4,[5]]]]]) => 5 depth([[[[[[[]]]]]]]) => 6 ''' # ******************************************************** def depth(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 7 ** (10 points) # Write a procedure # treeaverage(tree) # that takes a nested list tree whose leaf nodes are integers # and returns the average of those integers. # See if you can have your code traverse the data only once. # Examples: # treeaverage([1, 2, 3]) => 2 # treeaverage([1, [2, [3]]]) => 2 # treeaverage([]) => 0 # treeaverage([[[[[[[7]]]]]]]) => 7 # treeaverage([[[[[[[]]]]]]]) => 0 # ******************************************************** def treeaverage(tree): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 8 ** (10 points) # Write the procedure # countpat(file, pat) # which reads the given file and returns the number of times # that the pattern pat matches in the file. # Examples: # countpat('/c/cs200/www/lectures/linux.words', '^...$') => 6221 # countpat('/c/cs200/www/lectures/linux.words', '^.$') => 53 # countpat('/c/cs200/www/lectures/linux.words', '^[aeiou].*[aeiou]$') => 17784 # countpat('/c/cs200/www/lectures/linux.words', '^[aeiou]+$') => 29 # countpat('/c/cs200/www/lectures/linux.words', '^[^aeiou]+$') => 7843 # ******************************************************** def countpat(file, pat): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 9 ** (10 points) # Write the procedure # biggest_file(dir) # which finds the largest file in the given directory. # Examples: # biggest_file('/c/cs200/www/lectures') => 'linux.words' # biggest_file('/usr') => 'lib64' # biggest_file('/usr/share/dictxx') =>'*** error reading /usr/share/dictxx' ## Hint: the google python class uses the commands module which # is not supported in Python 3. Instead, you may use: # os.system() # and redirect its output to a tmp file (e.g., /tmp/foo) , # which you then read and remove. # there are many other ways of approaching this problem. # ******************************************************** def biggest_file(dir): pass # Replace the pass statement with your procedure(s). # ******************************************************** # ** problem 10 ** (10 points) # Write the procedure # counttags(url, nodups=False) # count the number of links in a web page # with or without duplicates. # If nodups is False (the default value) you include duplicates. # If nodups is True, you count only unique instances. # Examples: # counttags('http://www.yale.edu/') => 133 # counttags('http://www.yale.edu/', True) => 133 # counttags('http://zoo.cs.yale.edu/classes/cs200/index.html') => 22 # counttags('http://zoo.cs.yale.edu/classes/cs200/index.html', True) => 21 # ******************************************************** def counttags(url, nodups=False): pass # Replace the pass statement with your procedure(s). # ******************************************************** ### test function from google python course ### ======================================= # Provided simple test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if (hasattr(expected, '__call__')): OK = expected(got) else: OK = (got == expected) if OK: prefix = ' OK ' else: prefix = ' X ' print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected))) # Provided main() calls the above functions with interesting inputs, # using test() to check if each result is correct or not. def main(): print ('hours') print('# is it greater than 0?') test(hours(), lambda x: x > 0) print ('xxxx') test(xxxx(), "cannot test without giving it away") print ('sumtree') test(sumtree([1, 2, 3]), 6) test(sumtree([1, [2, [3]]]), 6) test(sumtree([[[]]]), 0) test(sumtree([[[[2]]]]), 2) test(sumtree([1,2,3,4]), 10) test(sumtree([1,"2","3",4]), 5) test(sumtree([1,"2","3",4, False]), 5) test(sumtree([1,"2","3",4, False, []]), 5) print ('doubletree') test(doubletree([1, 2, 3]), [2,4,6]) test(doubletree([1, [2, [3]]]), [2, [4, [6]]]) test(doubletree([]), []) test(doubletree([[[[8, 8, 8]]]]), [[[[16, 16, 16]]]]) test(doubletree([1, 2, 3, "4", "5", ["6"]]), [2, 4, 6, '4', '5', ['6']]) test(doubletree([1, 2, 3, "4", "5", ["6"], True]), [2, 4, 6, '4', '5', ['6'], True]) print ('largesttree') test(largesttree([1, 2, 3, 4, 5]), 5) test(largesttree([]), float('-inf')) test(largesttree([1, [2, [3, [4, [5]]]]]), 5) test(largesttree([[[[[3]]]]]), 3) print ('allsametree') test(allsametree([1, 1, 1, 1]), True) test(allsametree([1, 2, 3]), False) test(allsametree([]), True) test(allsametree([1, [1, [1, [1]]]]) , True) test(allsametree([1, [[[[[2]]]]]]), False) test(allsametree(['a', 'a', 'a']), True) test(allsametree(['a', 'b', 'c']), False) print ('types') test(types([1, [2.3, "a"], True, 3, 4, 5]), ['bool', 'float', 'int', 'str']) test(types([1,2,3,4]), ['int']) test(types([1,2,3,4, True, "hello"]), ['bool', 'int', 'str']) test(types([1,2,3,4, True, "hello", 1,2]), ['bool', 'int', 'str']) test(types([1,2,3,4, True, "hello", 1.2]), ['bool', 'float', 'int', 'str']) test(types([1,2,3,4, True, "hello", 1.2, {}]), ['bool', 'dict', 'float', 'int', 'str']) test(types([1,2,3,4, True, "hello", 1.2, {}, (1,2,3)]), ['bool', 'dict', 'float', 'int', 'str', 'tuple']) print ('depth') test(depth([]), 0) test(depth([1,2,3,4,5]), 1) test(depth('tree'), 0) test(depth([1,[2,[3,[4,[5]]]]]), 5) test(depth([[[[[[[]]]]]]]), 6) print ('treeaverage') test(treeaverage([1, 2, 3]), 2) test(treeaverage([1, [2, [3]]]) , 2) test(treeaverage([]), 0) test(treeaverage([[[[[[[7]]]]]]]), 7) test(treeaverage([[[[[[[]]]]]]]), 0) print ('countpat') test(countpat('/c/cs200/www/lectures/linux.words', '^...$'), 6221) test(countpat('/c/cs200/www/lectures/linux.words', '^.$'), 53) test(countpat('/c/cs200/www/lectures/linux.words', '^[aeiou].*[aeiou]$'), 17784) test(countpat('/c/cs200/www/lectures/linux.words', '^[aeiou]+$'), 29) test(countpat('/c/cs200/www/lectures/linux.words', '^[^aeiou]+$'), 7843) ## compare with: ## grep '^...$' /c/cs200/www/lectures/linux.words print ('biggest_file') test(biggest_file('/c/cs200/www/lectures'), 'linux.words') test(biggest_file('/usr'), 'lib64') test(biggest_file('/usr/share/dictx'), '*** error reading /usr/share/dictx') print ('counttags') test(counttags('http://cnn.com/'), 562) test(counttags('http://cnn.com/', True), 312) test(counttags("http://www.yale.edu/xxx"), 'problem reading url:http://www.yale.edu/xxx') test(counttags('http://zoo.cs.yale.edu/classes/cs200/index.html'), 22) test(counttags('http://zoo.cs.yale.edu/classes/cs200/index.html', True), 21) # Standard boilerplate to call the main() function. if __name__ == '__main__': main() # ******************************************************** # **** end of hw #2