python-ds-practice-solution/27_titleize/titleize.py def titleize(phrase): """Return phrase in title case (each word capitalized). >>> titleize('this is awesome') 'This Is Awesome' >>> titleize('oNLy...

https://curric.rithmschool.com/springboard/exercises/python-ds-practice/solution/


python-ds-practice-solution/27_titleize/titleize.py def titleize(phrase): """Return phrase in title case (each word capitalized). >>> titleize('this is awesome') 'This Is Awesome' >>> titleize('oNLy cAPITALIZe fIRSt') 'Only Capitalize First' """ # there's a built-in method for this! return phrase.title() # or, if you didn't know that, could capitalize each word by hand # return ' '.join([s.capitalize() for s in phrase.split(' ')]) python-ds-practice-solution/09_is_palindrome/is_palindrome.py def is_palindrome(phrase): """Is phrase a palindrome? Return True/False if phrase is a palindrome (same read backwards and forwards). >>> is_palindrome('tacocat') True >>> is_palindrome('noon') True >>> is_palindrome('robert') False Should ignore capitalization/spaces when deciding: >>> is_palindrome('taco cat') True >>> is_palindrome('Noon') True """ normalized = phrase.lower().replace(' ', '') return normalized == normalized[::-1] python-ds-practice-solution/37_sum_up_diagonals/sum_up_diagonals.py def sum_up_diagonals(matrix): """Given a matrix [square list of lists], return sum of diagonals. Sum of TL-to-BR diagonal along with BL-to-TR diagonal: >>> m1 = [ ... [1, 2], ... [30, 40], ... ] >>> sum_up_diagonals(m1) 73 >>> m2 = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> sum_up_diagonals(m2) 30 """ total = 0 for i in range(len(matrix)): total += matrix[i][i] total += matrix[i][-1 - i] return total # or, probably too tersely: # # return sum([matrix[i][i] + matrix[i][-1 - i] for i in range(len(matrix))]) python-ds-practice-solution/fs_4_reverse_vowels/reverse_vowels.py def reverse_vowels(s): """Reverse vowels in a string. Characters which re not vowels do not change position in string, but all vowels (y is not a vowel), should reverse their order. >>> reverse_vowels("Hello!") 'Holle!' >>> reverse_vowels("Tomatoes") 'Temotaos' >>> reverse_vowels("Reverse Vowels In A String") 'RivArsI Vewols en e Streng' reverse_vowels("aeiou") 'uoiea' reverse_vowels("why try, shy fly?") 'why try, shy fly?'' """ vowels = set("aeiou") string = list(s) i = 0 j = len(s) - 1 while i < j:="" if="" string[i].lower()="" not="" in="" vowels:="" i="" +="1" elif="" string[j].lower()="" not="" in="" vowels:="" j="" -="1" else:="" string[i],="" string[j]="string[j]," string[i]="" i="" +="1" j="" -="1" return="" "".join(string)="" python-ds-practice-solution/38_min_max_key_in_dictionary/min_max_key_in_dictionary.py="" def="" min_max_keys(d):="" """return="" tuple="" (min-keys,="" max-keys)="" in="" d.="">>> min_max_keys({2: 'a', 7: 'b', 1: 'c', 10: 'd', 4: 'e'}) (1, 10) Works with any kind of key that can be compared, like strings: >>> min_max_keys({"apple": "red", "cherry": "red", "berry": "blue"}) ('apple', 'cherry') """ keys = d.keys() return (min(keys), max(keys)) python-ds-practice-solution/03_last_element/last_element.py def last_element(lst): """Return last item in list (None if list is empty. >>> last_element([1, 2, 3]) 3 >>> last_element([]) is None True """ if lst: return lst[-1] # we don't need to do anything else; functions # return None by default python-ds-practice-solution/fs_5_read_file_list/dogs Fido Whiskey Dr. Sniffle python-ds-practice-solution/fs_5_read_file_list/cats Auden Ezra Fluffy Meowsley python-ds-practice-solution/fs_5_read_file_list/read_file_list.py def read_file_list(filename): """Read file and print out each line separately with a "-" before it. For example, if we have a file, `dogs`, containing: Fido Whiskey Dr. Sniffle This should work: >>> read_file_list("dogs") - Fido - Whiskey - Dr. Sniffle It will raise an error if the file cannot be found. """ with open(filename) as f: for line in f: # remove newline at end of line! line = line.strip() print(f"- {line}") python-ds-practice-solution/34_same_frequency/same_frequency.py def freq_counter(coll): """Returns frequency counter mapping of coll.""" counts = {} for x in coll: counts[x] = counts.get(x, 0) + 1 return counts def same_frequency(num1, num2): """Do these nums have same frequencies of digits? >>> same_frequency(551122, 221515) True >>> same_frequency(321142, 3212215) False >>> same_frequency(1212, 2211) True """ return freq_counter(str(num1)) == freq_counter(str(num2)) python-ds-practice-solution/11_flip_case/flip_case.py def flip_case(phrase, to_swap): """Flip [to_swap] case each time it appears in phrase. >>> flip_case('Aaaahhh', 'a') 'aAAAhhh' >>> flip_case('Aaaahhh', 'A') 'aAAAhhh' >>> flip_case('Aaaahhh', 'h') 'AaaaHHH' """ to_swap = to_swap.lower() out = "" for ltr in phrase: if ltr.lower() == to_swap: ltr = ltr.swapcase() out += ltr return out # Alternate phrasing: a bit clever, same runtime, and harder to # read: # to_swap = to_swap.lower() # # fixed = [ # (char.swapcase() if char.lower() == to_swap else char) # for char in phrase # ] # # return "".join(fixed) python-ds-practice-solution/39_find_greater_numbers/find_greater_numbers.py def find_greater_numbers(nums): """Return # of times a number is followed by a greater number. For example, for [1, 2, 3], the answer is 3: - the 1 is followed by the 2 *and* the 3 - the 2 is followed by the 3 Examples: >>> find_greater_numbers([1, 2, 3]) 3 >>> find_greater_numbers([6, 1, 2, 7]) 4 >>> find_greater_numbers([5, 4, 3, 2, 1]) 0 >>> find_greater_numbers([]) 0 """ count = 0 for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[j] > nums[i]: count += 1 return count python-ds-practice-solution/01_product/product.py # Write a function called product which takes in two numbers # and returns the product of the numbers. # Examples: # product(2, 2) # 4 # product(2, -2) # -4 def product(a, b): """Return product of a and b
Jul 31, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here