# lec19.py
# D. Gries, L. Lee, S. Marschner, W. White
# April 8, 2014

"""Demos for lecture 19"""

def countslash(s):
    """Returns: number of slashes in s.
    Pre: s a string"""

    # invariant: count is number of / in s[0..i-1]
    # (so i marks start of undone)

    # BEGIN REMOVE
    i = 0 # start of undone
    count = 0 # number of / in s[0..i-1]
    while i < len(s):
        count += (1 if s[i] == '/' else 0)
        i+=1
    return count
   # END REMOVE

def sumrecip(n):
    """Returns: sum of 1.0/i for i in 1..n.
    Pre: n a non-negative int"""

    # invariant: v is sum of 1.0/j for j in 1..i-1
    # (so i marks start of undone)

    # BEGIN REMOVE

    v = 0 
    i = 1 

    while i <= n:
        v = v + 1.0 / i
        i += 1

    return v

    # END REMOVE

if __name__ == '__main__':
    for s in ['//', 'a/b', 'a']:
        print "count of / in ", s, ':',  countslash(s)

    for n in [1, 2, 3]:
        print "sum of recips up to n for n=", n, " is ", sumrecip(n)