Course Notes - Reading Material

  • Upload
    daniel

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 7/24/2019 Course Notes - Reading Material

    1/78

    http://creativecommons.org/licenses/by-nc-nd/3.0/

  • 7/24/2019 Course Notes - Reading Material

    2/78

  • 7/24/2019 Course Notes - Reading Material

    3/78

  • 7/24/2019 Course Notes - Reading Material

    4/78

  • 7/24/2019 Course Notes - Reading Material

    5/78

  • 7/24/2019 Course Notes - Reading Material

    6/78

  • 7/24/2019 Course Notes - Reading Material

    7/78

    /** Sorts a in ascending order */

    public void sort(int[] a) ...

    // sets a to 1

    a = 1 ;

  • 7/24/2019 Course Notes - Reading Material

    8/78

  • 7/24/2019 Course Notes - Reading Material

    9/78

  • 7/24/2019 Course Notes - Reading Material

    10/78

  • 7/24/2019 Course Notes - Reading Material

    11/78

  • 7/24/2019 Course Notes - Reading Material

    12/78

  • 7/24/2019 Course Notes - Reading Material

    13/78

    a a a

    a

  • 7/24/2019 Course Notes - Reading Material

    14/78

  • 7/24/2019 Course Notes - Reading Material

    15/78

  • 7/24/2019 Course Notes - Reading Material

    16/78

  • 7/24/2019 Course Notes - Reading Material

    17/78

    Flo

    Al

    F

    E

    L

    Doug

    Carlos

    Betty

    Ellen

  • 7/24/2019 Course Notes - Reading Material

    18/78

  • 7/24/2019 Course Notes - Reading Material

    19/78

  • 7/24/2019 Course Notes - Reading Material

    20/78

  • 7/24/2019 Course Notes - Reading Material

    21/78

  • 7/24/2019 Course Notes - Reading Material

    22/78

  • 7/24/2019 Course Notes - Reading Material

    23/78

  • 7/24/2019 Course Notes - Reading Material

    24/78

  • 7/24/2019 Course Notes - Reading Material

    25/78

  • 7/24/2019 Course Notes - Reading Material

    26/78

  • 7/24/2019 Course Notes - Reading Material

    27/78

  • 7/24/2019 Course Notes - Reading Material

    28/78

  • 7/24/2019 Course Notes - Reading Material

    29/78

  • 7/24/2019 Course Notes - Reading Material

    30/78

    sm

    f

    f

    sm

    s

    f

    s

    f

    s

    f s

  • 7/24/2019 Course Notes - Reading Material

    31/78

  • 7/24/2019 Course Notes - Reading Material

    32/78

  • 7/24/2019 Course Notes - Reading Material

    33/78

  • 7/24/2019 Course Notes - Reading Material

    34/78

  • 7/24/2019 Course Notes - Reading Material

    35/78

  • 7/24/2019 Course Notes - Reading Material

    36/78

  • 7/24/2019 Course Notes - Reading Material

    37/78

  • 7/24/2019 Course Notes - Reading Material

    38/78

  • 7/24/2019 Course Notes - Reading Material

    39/78

  • 7/24/2019 Course Notes - Reading Material

    40/78

  • 7/24/2019 Course Notes - Reading Material

    41/78

  • 7/24/2019 Course Notes - Reading Material

    42/78

  • 7/24/2019 Course Notes - Reading Material

    43/78

  • 7/24/2019 Course Notes - Reading Material

    44/78

  • 7/24/2019 Course Notes - Reading Material

    45/78

  • 7/24/2019 Course Notes - Reading Material

    46/78

  • 7/24/2019 Course Notes - Reading Material

    47/78

  • 7/24/2019 Course Notes - Reading Material

    48/78

  • 7/24/2019 Course Notes - Reading Material

    49/78

  • 7/24/2019 Course Notes - Reading Material

    50/78

  • 7/24/2019 Course Notes - Reading Material

    51/78

    def mult(m,n):

    """ Multiply integers m and n. """

    # Precondition: m >= 0

    x = m

    y = n

    z = 0

    # loop invariant: z = mn - xy

    while x != 0:

    if x % 2 == 1: z = z + y # x is odd

    x = x > > 1 # x = x / 2 (right s hift)

    y = y < < 1 # y = y * 2 (left s hift)

    # post condition: z = mn

    return z

  • 7/24/2019 Course Notes - Reading Material

    52/78

    def LS(A,x):

    """ Return an index i such that x == L[i]. Otherwise, return -1. """

    i = 0 # (line 1)

    while i < len(A): # (line 2)

    if A[i] == x: # (line 3)

    return i # (line 4)

    i = i + 1 # (line 5)

    return -1 # (line 6)

    LS([2,4,6,8],4)

    i=0

    0 < 4

    A[0] == 4

    i = 1

  • 7/24/2019 Course Notes - Reading Material

    53/78

    1 < 4

    A[1] == 4

    return 1

  • 7/24/2019 Course Notes - Reading Material

    54/78

  • 7/24/2019 Course Notes - Reading Material

    55/78

  • 7/24/2019 Course Notes - Reading Material

    56/78

  • 7/24/2019 Course Notes - Reading Material

    57/78

  • 7/24/2019 Course Notes - Reading Material

    58/78

  • 7/24/2019 Course Notes - Reading Material

    59/78

  • 7/24/2019 Course Notes - Reading Material

    60/78

    def IS(A):

    """ Sort the elements of A in non-decreasing order. """

    i = 1 # (line 1)

    while i < len(A): # (line 2)

    t = A[i] # (line 3)

    j = i # (line 4)

    while j > 0 and A[j-1] > t: # (line 5)

    A[j] = A[j-1] # (line 6)

    j = j-1 # (line 7)

    A[j] = t # (line 8)

    i = i+1 # (line 9)

  • 7/24/2019 Course Notes - Reading Material

    61/78

    for while sum

    def max_sum(L):max = 0 # line 1

    # To generate all non-empty slices [i:j] for list L, i must take on values

    # from 0 to len(L)-1, and j must take on values from i+1 to len(L).

    i = 0 # line 2

    while i < len(L): # line 3

    j = i + 1 # line 4

    while j max: # line 11

    max = sum # line 12

    j = j + 1 # line 13

    i = i + 1 # line 14

    # At this point, weve examined every slice.

    return max # line 15

  • 7/24/2019 Course Notes - Reading Material

    62/78

    L

    max 0

    max_sum len(L)

    L

    L len(L) =

    L

  • 7/24/2019 Course Notes - Reading Material

    63/78

    max_sum(L)

    def faster_max_sum(L):

    max = 0 # line 1

    # Generate all non-empty slices [i:j+1] for list L, where i takes on values

    # from 0 to len(L)-1, and j takes on values from i to len(L)-1.

    i = 0 # line 2

    while i < len(L): # line 3

    sum = 0 # line 4

    j = i # line 5while j < len(L): # line 6

    sum = sum + L[j] # line 7

    if sum > max: # line 8

    max = sum # line 9

    j = j + 1 # line 10

    i = i + 1 # line 11

    # At this point, weve examined every slice.

    return max # line 12

  • 7/24/2019 Course Notes - Reading Material

    64/78

  • 7/24/2019 Course Notes - Reading Material

    65/78

    faster_max_sum

    def mystery1(L):

    """ L is a non-empty list of length len(L) = n. """

    tot = 0

    i = 0

    while i < len(L):

    if L[i] > 0:

    tot = tot + L[i]

    i = i + 1

    return tot

    def mystery2(L):

    """ L is a non-empty list of length len(L) = n. """

    i = 1

    while i < len(L) - 1:

    j = i - 1

    while j

  • 7/24/2019 Course Notes - Reading Material

    66/78

  • 7/24/2019 Course Notes - Reading Material

    67/78

  • 7/24/2019 Course Notes - Reading Material

    68/78

  • 7/24/2019 Course Notes - Reading Material

    69/78

    def halt(f,i):

    """Return True iff f(i) eventually halts."""

    return True # replace this stub with correct code

    halt halt f

    i True f(i) False

    f(i)

    f(i) halt

    f

    halt(blah,5) halt(blah,8) blah

    def blah(x):

    i f x % 2 = = 0 :

    while True: pass

    else

    return x

    halt

    halt halt

  • 7/24/2019 Course Notes - Reading Material

    70/78

    halt

    def confused(f):

    def halt(f,i):

    ...copy/paste the full code for halt here...

    if halt(f,f): # line 1

    while True: pass # line 2

    else:

    return False # line 3

    halt

    confused(confused)

    confused(confused) confused(confused)

    confused(confused)

    halt(confused,confused) True halt

    confused(confused)

    confused(confused) confused(confused)

    confused(confused)

    halt(confused,confused) False halt

    confused(confused) False

    confused(confused) confused(confused)

    confused(confused) confused(confused)

    halt

    halt halt(f,i)

    halt

    halt

  • 7/24/2019 Course Notes - Reading Material

    71/78

    f f(a)

    halt

    halt

    halt

    initialized(f,v) True v

    f

    f initialize(f,v) False i f(i)

    v

    initialized(f,v) True v

    f f(i)

    i

    def f1(x): def f2(x):

    return x + 1 return x + y + 1

    print y

    f1 y

    initialized(f1,y) True y

    f2 initialized(f2,y) False

    initialized

    initialized

    halt

    def halt(f,i):

    def initialized(g,v):

    ...code for initialized goes here...

    # Put some code here to scan the code for f and figure out

    # a variable name that doesnt occur in f, and store it in v

    def f_prime(x):

    # Ignore the argument x, call f with the fixed argument i

    # (the one passed in to halt).

    f(i)

    exec("print " + v) # treat string v as an identifier

    return not initialized(f_prime,v)

  • 7/24/2019 Course Notes - Reading Material

    72/78

    initialized

    f(i) f_prime(i) print v initialized(f_prime,v)

    False halt(f,i) True

    f(i) f_prime(x) print v x

    initialized(f_prime,v) True halt(f,i) False

    halt

    initialized

    initialized

    halt

    initialized

    halt

    halt

    halt

    halt initialized

    f_prime

    f(i)

    v f_prime

    f(i)

    halt

    initialized

    f(i) v

    f_prime

    f

    h

    halt

    confused halt

  • 7/24/2019 Course Notes - Reading Material

    73/78

  • 7/24/2019 Course Notes - Reading Material

    74/78

  • 7/24/2019 Course Notes - Reading Material

    75/78

  • 7/24/2019 Course Notes - Reading Material

    76/78

  • 7/24/2019 Course Notes - Reading Material

    77/78

    halt

    halt f i f

    f

  • 7/24/2019 Course Notes - Reading Material

    78/78

    halt

    halt confused

    halt

    confused halt

    confused confused

    halt

    confused