def problem_A16(grades): tests1 = [ ("""(define (square x) (* x x)) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))) (f 3 4)""", 456), ] tests2 = [ ("""(define x 3) (define y 4) (let ((x (+ y 2)) (y (+ x 1))) (cons x y))""", Pair(6, 4)), ("""(let ((x 'hello)) x)""", 'hello'), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem4(): if check_doctest('analyze_tweet_sentiment', trends): return True # Change the representation of sentiments to validate abstraction barrier. original_make_sentiment = trends.make_sentiment original_sentiment_value = trends.sentiment_value original_has_sentiment = trends.has_sentiment trends.make_sentiment = lambda s: lambda : s trends.sentiment_value = lambda s: s() trends.has_sentiment = lambda s: s() != None sentiment_tests = ( ((trends.make_tweet('Help, I\'m trapped in an autograder factory and I can\'t get out!'.lower(), None, 0, 0),), -0.416666667), ((trends.make_tweet('The thing that I love about hating things that I love is that I hate loving that I hate doing it.'.lower(), None, 0, 0),), 0.075), ) no_sentiment_tests = ( ((trends.make_tweet('Peter Piper picked a peck of pickled peppers'.lower(), None, 0, 0),), None), ) def analyze(tweet): return trends.sentiment_value(trends.analyze_tweet_sentiment(tweet)) if check_func(analyze, sentiment_tests, comp=comp_float): return True if check_func(analyze, no_sentiment_tests): return True trends.make_sentiment = original_make_sentiment trends.sentiment_value = original_sentiment_value trends.has_sentiment = original_has_sentiment
def problem_A16(grades): tests1 = [ ( """(define (square x) (* x x)) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))) (f 3 4)""", 456, ), ] tests2 = [ ( """(define x 3) (define y 4) (let ((x (+ y 2)) (y (+ x 1))) (cons x y))""", Pair(6, 4), ), ("""(let ((x 'hello)) x)""", "hello"), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_12(grades): tests1 = [ ('(define (square x) (* x x)) (square 21)', 441), ('(define square (lambda (x) (* x x))) (square (square 21))', 194481), ] tests2 = [ ("""(define square (lambda (x) (* x x))) (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4)""", 25) , ("""(define double (lambda (x) (* 2 x))) (define compose (lambda (f g) (lambda (x) (f (g x))))) (define apply-twice (lambda (f) (compose f f))) ((apply-twice double) 5)""", 20), ("""(define (outer x y) (define (inner z x) (list x y z)) (inner x 10)) (outer 1 2)""", pairify([10, 2, 1])), ("""(define (outer-func x y) (define (inner z x) (list x y z)) inner) ((outer-func 1 2) 1 10)""", pairify([10, 2, 1])), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem4(): if check_doctest('analyze_tweet_sentiment', trends): return True # Change the representation of sentiments to validate abstraction barrier. original_make_sentiment = trends.make_sentiment original_sentiment_value = trends.sentiment_value original_has_sentiment = trends.has_sentiment trends.make_sentiment = lambda s: lambda: s trends.sentiment_value = lambda s: s() trends.has_sentiment = lambda s: s() != None sentiment_tests = ( ((trends.make_tweet( 'Help, I\'m trapped in an autograder factory and I can\'t get out!' .lower(), None, 0, 0), ), -0.416666667), ((trends.make_tweet( 'The thing that I love about hating things that I love is that I hate loving that I hate doing it.' .lower(), None, 0, 0), ), 0.075), ) no_sentiment_tests = (((trends.make_tweet( 'Peter Piper picked a peck of pickled peppers'.lower(), None, 0, 0), ), None), ) def analyze(tweet): return trends.sentiment_value(trends.analyze_tweet_sentiment(tweet)) if check_func(analyze, sentiment_tests, comp=comp_float): return True if check_func(analyze, no_sentiment_tests): return True trends.make_sentiment = original_make_sentiment trends.sentiment_value = original_sentiment_value trends.has_sentiment = original_has_sentiment
def problem_B17(grades): tests1 = [ ( """(define f (mu (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7)""", 13, ), ( """(define g (mu () x)) (define (high f x) (f)) (high g 2)""", 2, ), ] tests2 = [ ( """(define (f x) (mu () (lambda (y) (+ x y)))) (define (g x) (((f (+ x 1))) (+ x 2))) (g 3)""", 8, ), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_19(grades): tests1 = [ ( """(sol-same-lols (list-partitions 5 2 4) '((4 1) (3 2)))""", True, ), ( """(sol-same-lols (list-partitions 7 3 5) '((5 1 1) (4 2 1) (3 3 1) (3 2 2) (5 2) (4 3)))""", True, ), ] tests2 = [ ( """(sol-same-lols (list-partitions 7 2 4) '((4 3)))""", True, ), ( """(sol-same-lols (list-partitions 7 7 1) '((1 1 1 1 1 1 1)))""", True, ), ] check = lambda snippet: check_scheme(snippet, problem_19_preamble) if check_func(check, tests1, comp=scheme_equal): return True if check_func(check, tests2, comp=scheme_equal): return True
def problem_22(grades): scheme.scheme_eval = scheme.scheme_optimized_eval tests1 = [ ( """(define (sum n total) (if (zero? n) total (sum (- n 1) (+ n total)))) (sum 1001 0)""", 501501, ), ] tests2 = [ ( """(define (sum n total) (if (zero? n) total (if #f 42 (sum (- n 1) (+ n total))))) (sum 1001 0)""", 501501, ), ] tests3 = [ ( """(define (sum n total) (cond ((zero? n) total) ((zero? 0) (sum (- n 1) (+ n total))) (else 42))) (sum 1001 0)""", 501501, ), ] tests4 = [ ( """(define (sum n total) (if (zero? n) total (add n (+ n total)))) (define add (lambda (x+1 y) (sum (- x+1 1) y))) (sum 1001 0)""", 501501, ), ] tests5 = [ ( """(define (sum n total) (if (zero? n) total (let ((n-1 (- n 1))) (sum n-1 (+ n total))))) (sum 1001 0)""", 501501, ), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True if check_func(scheme_eval, tests3, comp=scheme_equal): return True if check_func(scheme_eval, tests4, comp=scheme_equal): return True if check_func(scheme_eval, tests5, comp=scheme_equal): return True
def problem_12(grades): tests1 = [ ('(define (square x) (* x x)) (square 21)', 441), ('(define square (lambda (x) (* x x))) (square (square 21))', 194481), ] tests2 = [ ("""(define square (lambda (x) (* x x))) (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4)""", 25), ("""(define double (lambda (x) (* 2 x))) (define compose (lambda (f g) (lambda (x) (f (g x))))) (define apply-twice (lambda (f) (compose f f))) ((apply-twice double) 5)""", 20), ("""(define (outer x y) (define (inner z x) (list x y z)) (inner x 10)) (outer 1 2)""", pairify([10, 2, 1])), ("""(define (outer-func x y) (define (inner z x) (list x y z)) inner) ((outer-func 1 2) 1 10)""", pairify([10, 2, 1])), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_18(grades): tests1 = [ ("(merge < '(1 5 7 9) '(4 8 10))", read_line('(1 4 5 7 8 9 10)')), ("(merge > '(9 7 5 1) '(10 8 4 3))", read_line('(10 9 8 7 5 4 3 1)')), ] tests2 = [ ("(merge < '(1 2 3) '(4))", read_line('(1 2 3 4)')), ("(merge < () '(1 2))", read_line('(1 2)')), ] if check_func(check_scheme, tests1, comp=scheme_equal): return True if check_func(check_scheme, tests2, comp=scheme_equal): return True
def problem1(grades): """Test roll_dice.""" counted_dice = make_test_dice(4, 1, 2) test_suite1 = [((2, make_test_dice(4, 6, 1)), 10), ((3, make_test_dice(4, 6, 1)), 1), ((3, make_test_dice(1, 2, 3)), 1), ((3, counted_dice), 1), ((1, counted_dice), 4)] test_suite2 = [((5, make_test_dice(4, 2, 3, 3, 4, 1)), 16), ((2, make_test_dice(1)), 1)] if check_func(hog.roll_dice, test_suite1): return True if check_func(hog.roll_dice, test_suite2): return True
def problem_22(grades): scheme.scheme_eval = scheme.scheme_optimized_eval tests1 = [ ("""(define (sum n total) (if (zero? n) total (sum (- n 1) (+ n total)))) (sum 1001 0)""", 501501), ] tests2 = [ ("""(define (sum n total) (if (zero? n) total (if #f 42 (sum (- n 1) (+ n total))))) (sum 1001 0)""", 501501), ] tests3 = [ ("""(define (sum n total) (cond ((zero? n) total) ((zero? 0) (sum (- n 1) (+ n total))) (else 42))) (sum 1001 0)""", 501501), ] tests4 = [ ("""(define (sum n total) (if (zero? n) total (add n (+ n total)))) (define add (lambda (x+1 y) (sum (- x+1 1) y))) (sum 1001 0)""", 501501), ] tests5 = [ ("""(define (sum n total) (if (zero? n) total (let ((n-1 (- n 1))) (sum n-1 (+ n total))))) (sum 1001 0)""", 501501), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True if check_func(scheme_eval, tests3, comp=scheme_equal): return True if check_func(scheme_eval, tests4, comp=scheme_equal): return True if check_func(scheme_eval, tests5, comp=scheme_equal): return True
def problem_20(grades): tests1 = [ ('(tree-sums (make-tree 3 nil))', read_line('(3)')), ('(tree-sums tree)', read_line('(20 19 13 16 11)')), ] tests2 = [ ("(tree-sums '(9 (4 (3 (8)) (2)) (5) (1 (2 (6)) (5))))", read_line('(24 15 14 18 15)')), ("(tree-sums '(-3 (-2) (-4)))", read_line('(-5 -7)')), ] if check_func(check_scheme, tests1, comp=scheme_equal): return True if check_func(check_scheme, tests2, comp=scheme_equal): return True
def problem_7(grades): tests1 = [ ('(begin (+ 2 3) (+ 5 6))', 11), ('(begin (display 3) (newline) (+ 2 3))', 5), ] tests2 = [ ('(begin (define x 3) x)', 3), ('(define 0 1)', 'SchemeError'), ("(begin 30 'hello)", 'hello'), ("(begin (define x 3) (cons x '(y z)))", pairify([3, 'y', 'z'])), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem2(grades): """Test take_turn.""" test_suite1 = [ ((2, 0, make_test_dice(4, 6, 1)), 10), ((3, 20, make_test_dice(4, 6, 1)), 1), ((2, 0, make_test_dice(6)), 12), ((0, 34), 5), # Free bacon ((0, 71), 8), ((0, 7), 8) ] test_suite2 = [((0, 99), 10), ((0, 0), 1), ((0, 50), 6)] if check_func(hog.take_turn, test_suite1): return True if check_func(hog.take_turn, test_suite2): return True
def problem2(grades): """Test take_turn.""" test_suite1 = [((2, 0, make_test_dice(4, 6, 1)), 10), ((3, 20, make_test_dice(4, 6, 1)), 1), ((2, 0, make_test_dice(6)), 12), ((0, 34), 5),# Free bacon ((0, 71), 8), ((0, 7), 8)] test_suite2 = [((0, 99), 10), ((0, 0), 1), ((0, 50), 6)] if check_func(hog.take_turn, test_suite1): return True if check_func(hog.take_turn, test_suite2): return True
def problem_7(grades): tests1 = [ ("(begin (+ 2 3) (+ 5 6))", 11), ("(begin (display 3) (newline) (+ 2 3))", 5), ] tests2 = [ ("(begin (define x 3) x)", 3), ("(define 0 1)", "SchemeError"), ("(begin 30 'hello)", "hello"), ("(begin (define x 3) (cons x '(y z)))", pairify([3, "y", "z"])), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem1(grades): """Test roll_dice.""" counted_dice = make_test_dice(4, 1, 2) test_suite1 = [((2, make_test_dice(4, 6, 1)), 10), ((3, make_test_dice(4, 6, 1)), 1), ((3, make_test_dice(1, 2, 3)), 1), ((3, counted_dice), 1), ((1, counted_dice), 2)] test_suite2 = [((5, make_test_dice(4, 2, 3, 3, 4, 1)), 16), ((2, make_test_dice(1)), 1)] if check_func(hog.roll_dice, test_suite1): return True if check_func(hog.roll_dice, test_suite2): return True
def problem_A15(grades): tests1 = [ ("""(cond ((> 2 3) 5) ((> 2 4) 6) ((< 2 5) 7) (else 8))""", 7), ("""(cond ((> 2 3) 5) ((> 2 4) 6) ((< 2 5) 7))""", 7), ("""(cond ((> 2 3) 5) ((> 2 4) 6) (else 8))""", 8), ("""(cond ((> 2 3) 4 5) ((> 2 4) 5 6) ((< 2 5) 6 7) (else 7 8))""", 7), ("""(cond ((> 2 3) (display 'oops) (newline)) (else 9))""", 9), ("""(cond ((< 2 1)) ((> 3 2)) (else 5))""", True), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True
def problem2(grades): """Test extract_words.""" if check_doctest('extract_words', trends): return True import string tests = [ ( 'You! Shall! Not!...Pass!', ['You', 'Shall', 'Not', 'Pass'], ), ('This.is`separated!by@only#non$letter%characters^so&you*need(to)use-white+listing{instead}of\\black/listing:or"else<you\'ll>get~the wrong answer', [ 'This', 'is', 'separated', 'by', 'only', 'non', 'letter', 'characters', 'so', 'you', 'need', 'to', 'use', 'white', 'listing', 'instead', 'of', 'black', 'listing', 'or', 'else', 'you', 'll', 'get', 'the', 'wrong', 'answer' ]), ['', []], # This test is constructed below. ] pathological_test = tests[-1] for i in range(32, 128): c = chr(i) if (c in string.ascii_letters or not c in string.printable): continue pathological_test[0] += chr(i) + 'a' pathological_test[1].append('a') if check_func(trends.extract_words, tests): print('Failed test(s) in extract_words.') return True
def problem_A9(grades): tests1 = [ ( "(begin (define (f x y) (+ x y)) f)", LambdaProcedure(pairify(["x", "y"]), pairify(["+", "x", "y"]), create_global_frame()), ), ( "(begin (define (f) (+ 2 2)) f)", LambdaProcedure(nil, pairify(["+", 2, 2]), create_global_frame()), ), ( "(begin (define (f x) (* x x)) f)", LambdaProcedure(pairify(["x"]), pairify(["*", "x", "x"]), create_global_frame()), ), ( "(begin (define (f x) 1 2) f)", LambdaProcedure(pairify(["x"]), pairify(["begin", 1, 2]), create_global_frame()), ), ("(define (0 x) (* x x))", "SchemeError"), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True
def problem4(grades): """Test play using fixed dice.""" always = hog.always_roll hog.four_sided = make_test_dice(1) hog.six_sided = make_test_dice(3) test_suite = [ ((always(5), always(5)), (92, 106)), ((always(2), always(2)), (17, 102)), ((always(2), always(10)), (19, 120)), ((always(0), always(0)), (91, 103)), # always roll 0 ((always(0), always(2)), (106, 56)) ] try: failure = False if check_func(hog.play, test_suite): failure = True finally: # Revert dice hog.four_sided = four_sided hog.six_sided = six_sided print('Note: Not all tests have been released for problem4.', 'Submit your project to the actual autograder to get more results!', sep='\n', end='\n') return failure
def problem_8(grades): tests1 = [('(lambda (x y) (+ x y))', LambdaProcedure(pairify(['x', 'y']), pairify(['+', 'x', 'y']), create_global_frame()))] tests2 = [ ('(lambda (x) (+ x) (+ x x))', LambdaProcedure(pairify(['x']), pairify(['begin', ['+', 'x'], ['+', 'x', 'x']]), create_global_frame())), ('(begin (define x (lambda () 2)) x)', LambdaProcedure(nil, 2, create_global_frame())), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_2(grades): tests1 = [('(a . b)', Pair('a', 'b')), ('(a)', Pair('a', nil)), ('(a b . c)', Pair('a', Pair('b', 'c')))] tests2 = [ ('(a b . c d)', 'Error'), ('((a b) (c (d (e f))))', pairify([['a', 'b'], ['c', ['d', ['e', 'f']]]])), ('(a . (b . (c . (d . ()))))', pairify(['a', 'b', 'c', 'd'])), ('(. . 2)', 'Error'), ('(2 . 3 4 . 5)', 'Error'), ('(2 (3 . 4) 5)', Pair(2, Pair(Pair(3, 4), Pair(5, nil)))), ] if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal): return True if check_func(catch_syntax_error(read_line), tests2, comp=scheme_equal): return True
def problem6(grades): """Test find_state_center.""" if check_doctest('find_state_center', trends): return True from geo import make_position as mp import geo def center_as_tuple(state): center = trends.find_state_center(state) return (geo.latitude(center), geo.longitude(center)) def make_tests(): return ( (([[mp(49, -17), mp(83, -18), mp(-33, -54), mp(27, 82), mp(15, -97), mp(10, 97), mp(-37, -68), mp(26, 66), mp(49, -17)], [mp(-98, 55), mp(84, 27), mp(-81, 4), mp(94, -25), mp(-26, 42), mp(-98, 55)], [mp(60, -90), mp(59.5117046837911, -96.9829483518188), mp(59.3721917363029, -98.9780764523384), mp(60.2790258949765, -86.0097437989607), mp(60.4185388424647, -84.014615698441), mp(60, -90)], ],), (18.65154401154401, -14.746118326118323)), (([[mp(-53, 68), mp(-52, -23), mp(-74, -65), mp(-44, 46), mp(-61, 68), mp(-14, 12), mp(33, 58), mp(-53, 68)], [mp(-30, -70), mp(-91, 40), mp(46, -93), mp(-4, -35), mp(29, 28), mp(-30,-70)], [mp(90, 50), mp(88, 46.5358983848623), mp(83.5, 38.7416697508023), mp(86, 43.0717967697245), mp(87, 44.8038475772934), mp(84.5, 40.4737205583712), mp(90, 50)], ],), (-29.056049940231105, 26.2892371718245)), ) tests = make_tests() if check_func(center_as_tuple, tests, comp=comp_tuple): return True print("Testing abstraction barriers.") try: original_geo = trends.make_position, trends.latitude, trends.longitude trends.make_position = geo.make_position = lambda lat,long: lambda z: z*lat+(1-z)*long trends.latitude = geo.latitude = lambda p: p(1) trends.longitude = geo.longitude = lambda p: p(0) mp = geo.make_position tests = make_tests() if check_func(center_as_tuple, tests, comp=comp_tuple): return True finally: geo.make_position = trends.make_position = original_geo[0] geo.latitude = trends.latitude = original_geo[1] geo.longitude = trends.longitude = original_geo[2]
def problem_B6(grades): tests1 = [ ("(list 'a 'b)", pairify(['a', 'b'])), ("(define a 1) (list a 'b)", pairify([1, 'b'])), ("(car '(a b c))", 'a'), ("(car (car '((a))))", 'a'), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True
def problem_A5(grades): tests1 = [ ('(define size 2) size', 2), ('(define size 2) (* 5 size)', 10), ('(define pi 3.14159) (define radius 10) (* pi (* radius radius))', 314.159), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True
def problem_19(grades): tests1 = [ ("""(sol-same-lols (list-partitions 5 2 4) '((4 1) (3 2)))""", True), ("""(sol-same-lols (list-partitions 7 3 5) '((5 1 1) (4 2 1) (3 3 1) (3 2 2) (5 2) (4 3)))""", True), ] tests2 = [ ("""(sol-same-lols (list-partitions 7 2 4) '((4 3)))""", True), ("""(sol-same-lols (list-partitions 7 7 1) '((1 1 1 1 1 1 1)))""", True), ] check = lambda snippet: check_scheme(snippet, problem_19_preamble) if check_func(check, tests1, comp=scheme_equal): return True if check_func(check, tests2, comp=scheme_equal): return True
def problem_2(grades): tests1 = [ ("(a . b)", Pair("a", "b")), ("(a)", Pair("a", nil)), ("(a b . c)", Pair("a", Pair("b", "c"))), ] tests2 = [ ("(a b . c d)", "Error"), ("((a b) (c (d (e f))))", pairify([["a", "b"], ["c", ["d", ["e", "f"]]]])), ("(a . (b . (c . (d . ()))))", pairify(["a", "b", "c", "d"])), ("(. . 2)", "Error"), ("(2 . 3 4 . 5)", "Error"), ("(2 (3 . 4) 5)", Pair(2, Pair(Pair(3, 4), Pair(5, nil)))), ] if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal): return True if check_func(catch_syntax_error(read_line), tests2, comp=scheme_equal): return True
def problem_2(grades): tests1 = [ ('(a . b)', Pair('a', 'b')), ('(a)', Pair('a', nil)), ('(a b . c)', Pair('a', Pair('b', 'c'))) ] tests2 = [ ('(a b . c d)', 'Error'), ('((a b) (c (d (e f))))', pairify([['a', 'b'], ['c', ['d', ['e', 'f']]]])), ('(a . (b . (c . (d . ()))))', pairify(['a', 'b', 'c', 'd'])), ('(. . 2)', 'Error'), ('(2 . 3 4 . 5)', 'Error'), ] if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal): return True if check_func(catch_syntax_error(read_line), tests2, comp=scheme_equal): return True
def problem3(): if check_doctest('get_word_sentiment', trends): return True has_sentiment_tests = ( ((trends.make_sentiment(0.3),), True), ((trends.make_sentiment(None),), False), ((trends.make_sentiment(-1),), True), ) sentiment_value_tests = ( ((trends.make_sentiment(-0.3),), -0.3), ((trends.make_sentiment(1),), 1), ((trends.make_sentiment(-1),), -1), ) if check_func(trends.has_sentiment, has_sentiment_tests): return True elif check_func(trends.sentiment_value, sentiment_value_tests): return True
def problem7(grades): """Test bacon_strategy.""" if check_doctest('bacon_strategy', hog): return True old_bacon = hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS = 5, 4 test_suite = [((32, 34), 0), ((20, 23), 4), ((20, 4), 0), ((20, 99), 0)] failed = check_func(hog.bacon_strategy, test_suite) hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS = old_bacon return failed
def problem_4(grades): tests1 = [ ('(+ 2 3)', 5), ('(+ 2 3 4 5 6 7)', 27), ('(+ 2)', 2), ('(+)', 0), ('(* (+ 3 2) (+ 1 7))', 40), ('(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))', 57), ] tests2 = [ ('(odd? 13)', True), ('(car (list 1 2 3 4))', 1), ('hello', 'SchemeError'), ('(car car)', 'SchemeError'), ('(odd? 1 2 3)', 'SchemeError'), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def test_groups(): tweets = pirate_tweets(trends.make_tweet) expected = { 'MI': [tweets[0], tweets[4]], 'MT': [tweets[1], tweets[5]], 'ND': [tweets[2], tweets[6]], 'FL': [tweets[3], tweets[7]], } tests = (((tweets, ), expected), ) if check_func(trends.group_tweets_by_state, tests, comp=comp_group): return True
def problem_4(grades): tests1 = [ ("(+ 2 3)", 5), ("(+ 2 3 4 5 6 7)", 27), ("(+ 2)", 2), ("(+)", 0), ("(* (+ 3 2) (+ 1 7))", 40), ("(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))", 57), ] tests2 = [ ("(odd? 13)", True), ("(car (list 1 2 3 4))", 1), ("hello", "SchemeError"), ("(car car)", "SchemeError"), ("(odd? 1 2 3)", "SchemeError"), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def test_groups(): tweets = pirate_tweets(trends.make_tweet) expected = { 'MI': [tweets[0], tweets[4]], 'MT': [tweets[1], tweets[5]], 'ND': [tweets[2], tweets[6]], 'FL': [tweets[3], tweets[7]], } tests = ( ((tweets,), expected), ) if check_func(trends.group_tweets_by_state, tests, comp=comp_group): return True
def problem_1(grades): tests1 = [('3', 3), ('-123', -123), ('1.25', 1.25), ('true', True), (')', 'Error'), ("'x", pairify(['quote', 'x'])), ('(quote x)', pairify(['quote', 'x'])), ("'(a b)", pairify(['quote', ['a', 'b']])), ("'(a (b c))", pairify(['quote', ['a', ['b', 'c']]])), ("(a (b 'c))", pairify(['a', ['b', ['quote', 'c']]])), ("(a (b '(c d)))", pairify(['a', ['b', ['quote', ['c', 'd']]]])), ("')", 'Error')] if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal): return True
def problem5(grades): """Test find_centroid.""" if check_doctest('find_centroid', trends): return True from geo import make_position as mp import geo def make_tests(): return ( ([mp(49, -17), mp(83, -18), mp(-33, -54), mp(27, 82), mp(15, -97), mp(10, 97), mp(-37, -68), mp(26, 66), mp(49, -17)], (24.031793687451884, -10.597882986913008, 4330.0)), ([mp(-98, 55), mp(84, 27), mp(-81, 4), mp(94, -25), mp(-26, 42), mp(-98, 55)], (2.5294117647058822, -27.17647058823529, 1445.0)), ([mp(-53, 68), mp(-52, -23), mp(-74, -65), mp(-44, 46), mp(-61, 68), mp(-14, 12), mp(33, 58), mp(-53, 68)], (-7.561094365870623, 59.29600432800061, 2156.5)), ([mp(60, -90), mp(59.5117046837911, -96.9829483518188), mp(59.3721917363029, -98.9780764523384), mp(60.2790258949765, -86.0097437989607), mp(60.4185388424647, -84.014615698441), mp(60, -90)], (60, -90, 0)), ([mp(90, 50), mp(88, 46.5358983848623), mp(83.5, 38.7416697508023), mp(86, 43.0717967697245), mp(87, 44.8038475772934), mp(84.5, 40.4737205583712), mp(90, 50)], (90, 50, 0)), ) tests = make_tests() if check_func(trends.find_centroid, tests, comp=comp_tuple): return True print("Testing abstraction barriers.") try: original_geo = trends.make_position, trends.latitude, trends.longitude trends.make_position = geo.make_position = lambda lat,long: lambda z: z*lat+(1-z)*long trends.latitude = geo.latitude = lambda p: p(1) trends.longitude = geo.longitude = lambda p: p(0) mp = geo.make_position tests = make_tests() if check_func(trends.find_centroid, tests, comp=comp_tuple): return True finally: geo.make_position = trends.make_position = original_geo[0] geo.latitude = trends.latitude = original_geo[1] geo.longitude = trends.longitude = original_geo[2]
def problem_B11(grades): # Note: Doesn't check well-formed but unrequired list matching. # E.g., (lambda (a . b) 2) and (lambda x 2) tests1 = [ ('(lambda (x y z) x)', LambdaProcedure(pairify(['x', 'y', 'z']), 'x', create_global_frame())), ('(lambda (0 y z) x)', 'SchemeError'), ('(lambda (x y nil) x)', 'SchemeError'), ('(lambda (x y (and z)) x)', 'SchemeError'), ('(lambda (x #t z) x)', 'SchemeError'), ] tests2 = [ ("(lambda (h e l l o) 'world)", 'SchemeError'), ("(lambda (c s 6 1 a) 'yay)", 'SchemeError'), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_8(grades): tests1 = [ ('(lambda (x y) (+ x y))', LambdaProcedure(pairify(['x', 'y']), pairify(['+', 'x', 'y']), create_global_frame())) ] tests2 = [ ('(lambda (x) (+ x) (+ x x))', LambdaProcedure(pairify(['x']), pairify(['begin', ['+', 'x'], ['+', 'x', 'x']]), create_global_frame())), ('(begin (define x (lambda () 2)) x)', LambdaProcedure(nil, 2, create_global_frame())), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_A13(grades): tests1 = [ ('(if #t 1 0)', 1), ('(if #f 1 0)', 0), ('(if 1 1 0)', 1), ("(if 'a 1 0)", 1), ('(if (cons 1 2) 1 0)', 1), ('(if #t 1)', 1), ('(if #f 1)', scheme.okay), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True
def problem3(): if check_doctest('make_sentiment', trends): return True if check_doctest('get_word_sentiment', trends): return True has_sentiment_tests = ( ((trends.make_sentiment(0.3), ), True), ((trends.make_sentiment(None), ), False), ((trends.make_sentiment(-1), ), True), ) sentiment_value_tests = ( ((trends.make_sentiment(-0.3), ), -0.3), ((trends.make_sentiment(1), ), 1), ((trends.make_sentiment(-1), ), -1), ) if check_func(trends.has_sentiment, has_sentiment_tests): return True elif check_func(trends.sentiment_value, sentiment_value_tests): return True
def problem3(grades): """Test sentiment abstract data type.""" if check_doctest('make_sentiment', trends): return True if check_doctest('get_word_sentiment', trends): return True has_sentiment_tests = ( ((trends.make_sentiment(0.3),), True), ((trends.make_sentiment(None),), False), ((trends.make_sentiment(-1),), True), ) sentiment_value_tests = ( ((trends.make_sentiment(-0.3),), -0.3), ((trends.make_sentiment(1),), 1), ((trends.make_sentiment(-1),), -1), ) if check_func(trends.has_sentiment, has_sentiment_tests): return True elif check_func(trends.sentiment_value, sentiment_value_tests): return True
def problem8(grades): """Test swap_strategy.""" if check_doctest('swap_strategy', hog): return True old_bacon = hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS = 5, 4 test_suite = [((12, 34), 0), # beneficial swap ((8, 9), 4), # harmful swap ((32, 43), 0), # lots of free bacon ((20, 32), 4)] # baseline failed = check_func(hog.swap_strategy, test_suite) hog.BACON_MARGIN, hog.BASELINE_NUM_ROLLS = old_bacon return failed
def problem_B17(grades): tests1 = [ ("""(define f (mu (x) (+ x y))) (define g (lambda (x y) (f (+ x x)))) (g 3 7)""", 13), ("""(define g (mu () x)) (define (high f x) (f)) (high g 2)""", 2), ] tests2 = [ ("""(define (f x) (mu () (lambda (y) (+ x y)))) (define (g x) (((f (+ x 1))) (+ x 2))) (g 3)""", 8), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem_10(grades): gf = create_global_frame() formals, vals = read_line('(a b c)'), read_line('(1 2 3)') call_frame = gf.make_call_frame(formals, vals) doctest = [ (set(call_frame.bindings), {'a', 'b', 'c'}), (len(call_frame.bindings), 3), (call_frame.bindings['a'], 1), (call_frame.bindings['b'], 2), (call_frame.bindings['c'], 3), (call_frame.parent, gf), ] if check_func(lambda x: x, doctest, comp=scheme_equal): return True formals = read_line('(a b c)') args = read_line('(2 #t a)') lf = gf.make_call_frame(formals, args) tests1 = [ (lf.bindings['a'], 2), (lf.bindings['b'], True), (lf.bindings['c'], 'a'), (lf.parent, gf), ] if check_func(lambda x: x, tests1, comp=scheme_equal): return True formals = read_line('(a)') args = read_line('(seven)') lf2 = lf.make_call_frame(formals, args) tests2 = [ (lf.bindings['a'], 2), (lf.parent, gf), ] if check_func(lambda x: x, tests2, comp=scheme_equal): return True
def problem_B14(grades): tests1 = [ ('(and)', True), ('(and 1 #f)', False), ('(and 2 1)', 1), ('(and #f 5)', False), ('(and 3 2 #f)', False), ('(and 3 2 1)', 1), ('(and 3 #f 5)', False), ] tests2 = [ ('(or)', False), ('(or 1)', 1), ('(or #f)', False), ("(or 0 1 2 'a)", 0), ('(or #f #f)', False), ("(or 'a #f)", 'a'), ("(or (< 2 3) (> 2 3) 2 'a)", True), ('(or (< 2 3) 2)', True), ] if check_func(scheme_eval, tests1, comp=scheme_equal): return True if check_func(scheme_eval, tests2, comp=scheme_equal): return True
def problem5(grades): """Test make_averaged.""" # hundred_dice cycle from 1 to 99 repeatedly hundred_range = range(1, 100) hundred_dice = make_test_dice(*hundred_range) averaged_hundred_dice = test_eval(hog.make_averaged, (hundred_dice, 5 * len(hundred_range))) correct_average = sum(range(1, 100)) / len(hundred_range) test_suite = [((), correct_average)] * 2 if check_doctest('make_averaged', hog): return True if check_func(averaged_hundred_dice, test_suite): return True
def problem_1(grades): tests1 = [ ('3', 3), ('-123', -123), ('1.25', 1.25), ('true', True), (')', 'Error'), ("'x", pairify(['quote', 'x'])), ('(quote x)', pairify(['quote', 'x'])), ("'(a b)", pairify(['quote', ['a', 'b']])), ("'(a (b c))", pairify(['quote', ['a', ['b', 'c']]])), ("(a (b 'c))", pairify(['a', ['b', ['quote', 'c']]])), ("(a (b '(c d)))", pairify(['a', ['b', ['quote', ['c', 'd']]]])), ("')", 'Error') ] if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal): return True