# pr8.py, by chaynes@indiana.edu import random def predicate1(a, b, c): """ Write one line that is as simple as possible and equivalent to: if a < b: if c != 3: return False else: return True else: return False """ # FYI: Calling a function a "predicate" indicates that it returns a # truth value. #. def predicate2(a, b): """ Return a boolean value indicating if a == 3 and b == 3 or a < b without using the boolean operators and or or. """ #. #. #. #. #. #. #. def whats_it_print(): """ >>> whats_it_print() #... >>> """ i = 0 s = '01234567890123456789' line = '' while i <= 10: i = i + 1 if i < 5: i = i + 3 else: line = line + s[i] + ' ' print line UPPER_CASE_SHIFT = ord('a') - ord('A') def lower(string): """ Return a string that is like the given string, but with all upper-case letters converted to lower-case. >>> lower("The Knights of Ni") 'the knights of ni' >>> """ # FYI: this is similar to the string lower() method. # # Hint: use a loop that accumulates the string to be returned. # The since lower-case letters immediately follow the upper-case letters, # conversion of upper-case letters can be done by converting to ordinal # values, adding the difference between the ordinals of 'a' and 'A', # via the above constant, and converting back. #. #. #. #. #. #. #. #. #. #. def balanced_parens(string): """ Returns a boolean value indicating if the string contains balanced parenthesis. >>> balanced_parens('(a ()(b d) + d) e') True >>> balanced_parens('(a b))') # too many right parens False >>> balanced_parens(')(') # open paren not followed by close paren False >>> """ # Algorithm: scan the characters from left to right, adding one to a count # (which starts with zero) each time a left parenthesis is spotted, # subtracting one from the count each time # a right parenthesis is spotted, and return True if the count # ends with zero and never goes negative. #. #. #. #. #. #. #. #. #. #. #. def emphasize(string): """ Return a string with the characters in the given string separated by spaces and all upper case. >>> emphasize('The Knights said Ni!') 'T H E K N I G H T S S A I D N I !' >>> """ # Hint: recall the string method upper() #. #. #. #. #. #. def shuffle(s1, s2): """Return the perfect shuffle of strings s1 and s2, starting with s1. That is, return a string that alternates the characters of s1 and s2, starting with the first character of s1. If one string is shorter, the extra characters of the longer one are appended to the result. >>> shuffle('abc', 'def') 'adbecf' >>> shuffle('abc', 'd') 'adbc' >>> shuffle('', 'a') 'a' >>> """ #. #. #. #. #. #. #. #. #. #. def digits_encode(string): """ Assume all the characters in the string are spaces or printing keyboard characters (all of which have ordinal numbers between 32 and 126). Return a string obtained by concatinating the string representations of the ordinals in the given string. >>> ord('c') 99 >>> digits_encode('cd') '99100' >>> digits_encode('Silly walk.') '83105108108121321199710810746' >>> """ #. #. #. #. #. #. def digits_decode(string): """ If s is any string of printing keyboard characters, including space, digits_decode(digits_encode(s)) returns s. >>> digits_decode(digits_encode('Silly walk.')) 'Silly walk.' >>> """ #. #. #. #. #. #. #. #. #. #. #. def random_letters(n): """ Return a string of n randomly generated lower-case letters. >>> random_letters(5) 'kvztf' >>> """ # Hint: random letters were generated in the skip_cipher function # presented in class. #. #. #. #. #. #. #. def every_nth(s, n): """ Return a string composed of every n_th character of s, starting from the first; that is, those characters with indices 0, 1*n, 2*n, ... >>> every_nth('abcdefg', 2) 'aceg' >>> every_nth('abcdefg', 3) 'adg' >>> every_nth('abcdefg', 1) 'abcdefg' >>> every_nth('abcdefg', 20) 'a' >>> """ #. #. #. #. #. #. def skip_cipher(s, n): """ Return the result of encrypting string s by inserting n randomly chosen letters before every letter of s. >>> skip_cipher('abc', 3) 'vqtavxfbcbac' >>> every_nth(skip_cipher('abc', 3)[3:], 4) 'abc' >>> """ # Hint: this is a generalization of the version of skip_cipher presented in # class. use an indexed accumulating loop. #. #. #. #. #. #. def is_lower_case(string): """ Returns true if all the cased letters (A-Z and a-z) in string are lower case and there is at least one cased letter in string. >>> is_lower_case('aB3') False >>> is_lower_case('ab3') True >>> is_lower_case('123') False >>> """ # FYI: this is similar to the string method islower(). # # Hint: Use a while loop, with an index counter, and a boolean variable # indicating if a lower case letter has been seen. #. #. #. #. #. #. #. #. #. #. def is_palindrome(s): """Return a boolean value indicating if s is a palindrome; that is if it is equal to its reverse. >>> is_palindrome('abc') False >>> is_palindrome('aba') True >>> is_palindrome('aa') True >>> """ # Hint: use a while loop #. #. #. #. #. #. #. def balanced_parens(string): """ Returns a boolean value indicating if the string contains balanced parenthesis. >>> balanced_parens('(a ()(b d) + d) e') True >>> balanced_parens('(a b))') # too many right parens False >>> balanced_parens(')(') # open paren not followed by close paren False >>> """ # Algorithm: scan the characters from left to right, adding one to a count # (which starts with zero) each time a left parenthesis is spotted, # subtracting one from the count each time # a right parenthesis is spotted, and return True if the count # ends with zero and never goes negative. #. #. #. #. #. #. #. #. #. #. #. def main(): """In this function add a few tests for each function in the file.""" main()