""" A module to show off loop invariants in a function Authors: Walker M. White (wmw2), Lillian Lee (LJL2), Steve Marschner (srm2), Anne Bracy (awb93) Date: April 25, 2018 """ def num_adj_pairs(s): """ Returns: number of adjacent equals pairs in s An adjacent pair has the form s[k-1], s[k]. Example: num_adj_pairs('ebeee') is 2 Parameter s: the string to search Precondition: s is a string """ # Set n_pair to # adjacent equal pairs in s[0..len(s)-1] n_pair = 0 k = 1 # inv: n_pair = # adjacent equal pairs in s[0..k-1] while k < len(s): # Process k print('loop:', n_pair, 'pairs in', s[0:k]) if (s[k-1] == s[k]): n_pair = n_pair + 1 k = k + 1 # n_pair = # adjacent equal pairs in s[0..len(s)-1] print('post:', n_pair, 'pairs in', s) return n_pair s = sys.argv[1] # get string from command line x = num_adj_pairs(s) s = "eeeeebeee" n = num_adj_pairs(s) print('The number of matching adjacent pairs in ' + s + ' is '+str(n))