# CSPT27 Lecture 1 ``` """ Create a function that returns a list of strings that are sorted alphabetically """ def sortList(someList): someList.sort() anotherList = sorted(someList) """ Create a function that takes a string, checks if it has the same number of "x"s and "o"s and returns either True or False. - Return a boolean value (True or False). - The string can contain any character. - When no x and no o are in the string, return True. Examples: - XO("ooxx") ➞ True - XO("xooxx") ➞ False - XO("ooxXm") ➞ True (Case insensitive) - XO("zpzpzpp") ➞ True (Returns True if no x and o) - XO("zzoo") ➞ False """ def XO(txt): lowerTxt = txt.lower() return lowerTxt.count('o') == lowerTxt.count('x') """ Given an unsorted list, create a function that returns the nth smallest element (the smallest element is the first smallest, the second smallest element is the second smallest, etc). Examples: - nth_smallest([7, 5, 3, 1], 1) ➞ 1 - nth_smallest([1, 3, 5, 7], 3) ➞ 5 - nth_smallest([1, 3, 5, 7], 5) ➞ None - nth_smallest([7, 3, 5, 1], 2) ➞ 3 """ def nThSmallest(someList, n): if n > len(someList): return None return sorted(someList)[n - 1] """ Write a function that creates a dictionary with each (key, value) pair being the (lower case, upper case) versions of a letter, respectively. Examples: - mapping(["p", "s"]) ➞ { "p": "P", "s": "S" } - mapping(["a", "b", "c"]) ➞ { "a": "A", "b": "B", "c": "C" } - mapping(["a", "v", "y", "z"]) ➞ { "a": "A", "v": "V", "y": "Y", "z": "Z" } Notes: - All of the letters in the input list will always be lowercase. """ def mapping(someList): myDictionary = {} for letter in someList: myDictionary[letter] = letter.upper() return myDictionary """ Create a function that applies a discount d to every number in the list. Examples: - get_discounts([2, 4, 6, 11], "50%") ➞ [1, 2, 3, 5.5] - get_discounts([10, 20, 40, 80], "75%") ➞ [7.5, 15, 30, 60] - get_discounts([100], "45%") ➞ [45] Notes: - The discount is the percentage of the original price (i.e the discount of "75%" to 12 would be 9 as opposed to taking off 75% (making 3)). - There won't be any awkward decimal numbers, only 0.5 to deal with. """ def getDiscounts(nums, percentage): discount = int(percentage[:-1]) / 100 return [n * discount for n in nums] print(getDiscounts([2, 4, 6, 11], "50%")) ```