# CSPT17 Lecture 1 - Python Basics ## Sort List ``` """ Create a function that returns a list of strings that are sorted alphabetically """ def sort_list(lst): return sorted(lst, reverse=True) ``` ## XO ``` """ 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): lowerCaseText = txt.lower() return lowerCaseText.count('x') == lowerCaseText.count('o') # numberOfOs = 0 # numberOfXs = 0 # for char in txt: # if char == 'x' or char == 'X': # numberOfXs += 1 # elif char == 'o' or char == 'O': # numberOfOs += 1 # return numberOfOs == numberOfXs ``` ## Get Discounts ``` """ 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 get_discounts(nums, percentage): discount = float(percentage[:-1]) / 100 # "50%" --> "50" 50.0 return [n * discount for n in nums] # res = [] # for n in nums: # res.append(n * discount) # return res print(get_discounts([2, 4, 6, 11], "50%")) ```