# Week 10 practice problems, by chaynes@indiana.edu import random def odd(n): """ Return a boolean value indicating if n is an odd number. """ #... def whats_it(n): """ >>> n = 0 >>> 1/2 n #... >>> 'Holy Grail'['o' : 'r'] #... >>> whats_it(23) #... >>> """ n % 12 print n def swap(s, s1, s2): """ Returns a string that is the same as s but with all occurrances of strings s1 and s2 swapped. >>> swap('They had spam and eggs with spam', 'spam', 'eggs') 'They had eggs and spam with eggs' >>> """ # Hint: you may assume the character chr(0) (the null character) does not # occur in any of the strings. #. #. #. def add_n(lon, n): """ Return a new list of numbers obtained by adding n to each value in the given list of numbers, lon. >>> add_n([3, 2.5, 4], 2) [5, 4.5, 6] >>> """ #. #. #. #. #. #. def max_value(lon): """ Return the maximum value of the given non-empty list of numbers. >>> max_value([1, 20, 8, -3.4]) 20 >>> max_value([3]) 3 >>> """ #. #. #. #. #. #. #. #. def elementwise_less_than(lst1, lst2): """ Return a list of boolean values that indicate if the corresponding element of lst1 is less than the corresponding element of lst2. >>> elementwise_less_than([1, 2, 'a'], [2, 0, 'b']) [True, False, True] >>> """ #. #. #. #. #. #. def slice_out_char(s, index): """ Return a string like s, but with the character at index removed. >>> slice_out_char('abc', 1) 'ac' >>> slice_out_char('a', 0) '' >>> """ #... def is_scramble(s1, s2): """ Return a boolean value indicating if s1 is a scrambled version of s2, technically called a "permutation of s2"; that is, return a string with the same characters that are in s1 (with the same frequency of occurrence), but in any order. >>> is_scramble('aab', 'aba') True >>> is_scramble('aab', 'ab') False >>> is_scramble('', 'ab') False >>> """ #. #. #. #. #. #. #. #. #. def scramble(s): """ Return a permutation of string s: a string with the same characters as s, with the same frequency, but in a random order. >>> is_scramble(scramble('aab'), 'aab') True >>> is_scramble(scramble(''), '') True >>> """ #. #. #. #. #. #. #. #. def all_true(lst): """ Return a boolean value indicating if all the values in lst are true values. >>> all_true([5, 0, True]) False >>> all_true(['a', 0.1, [3]]) True >>> """ #. #. #. #. #. #. def any_true(lst): """ Return a boolean value indicating if any value in lst is a true value. >>> any_true([5, 0, True]) True >>> any_true([0, '', False]) False >>> """ #. #. #. #. #. #. def elementwise_less_than(lst1, lst2): """ Return a list of boolean values that indicate if the corresponding element of lst1 is less than the corresponding element of lst2. >>> elementwise_less_than([1, 2, 'a'], [2, 0, 'b']) [True, False, True] >>> """ #. #. #. #. #. #. def remove_duplicates(lst): """ Return a list containing every value in lst, but with each value appearing only once in the returned list. The order of values in the returned list is the order of their first appearance in lst. >>> remove_duplicates(list('adabbaacd')) ['a', 'd', 'b', 'c'] >>> """ #. #. #. #. #. #. #. #. def add_to(lst, x): """R eplace each value in the given list of numbers with the result of adding x to the original value. >>> a = [1, -3, 5] >>> add_to(a, 2) >>> a [3, -1, 7] >>> """ #. #. #. #. def rfind_char(s, c): """ Return the index of the right-most occurrence of character c in string s, or -1 if there is no occurrence of c in s. >>> rfind_char('abad', 'a') 2 >>> rfind_char('abad', 3) -1 >>> """ #. #. #. #. #. #. def every_other(lst): """ Return a list consisting of every other value in lst, starting with the first. >>> every_other(['a', 1, 'b', 2, 'c']) ['a', 'b', 'c'] >>> """ #. #. #. #. #. #. def elementwise_or(lst1, lst2): """ Return a list of boolean values that are the logical 'or' of the corresponding boolean elements of lst1 and lst2. >>> elementwise_or([True, True, False, False], [True, False, True, False]) [True, True, True, False] >>> """ #. #. #. #. #. #. def only_not_blank(los): """ Returns a list of the non-blank (whitespace-only) strings in the given list of strings. >>> only_not_blank(['spam', ' ', '\n', 'eggs']) ['spam', 'eggs'] >>> """ #. #. #. #. #. #. #. def delete_every_other(lst): """ Delete every other element from lst starting with the first. >>> lst = [1, 2, 3, 4, 5] >>> delete_every_other(lst) >>> assert lst == [2, 4] >>> """ #. #. #. #. #. #.