GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DAT3/code/03_base_python_solutions.py at master · justmarkham/DAT3 Expand file tree/ Copy path03_base_python_solutions.pyMore file actions 116 lines (93 loc) · 2.81 KB / Copy path03_base_python_solutions.pyFile metadata and controls116 lines (93 loc) · 2.81 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 ''' SOLUTIONS: Base Python Exercises ''' ''' Exercise 1: Create a calculator function. If the operation is not specified, default to addition. If the operation is invalid, print an error message. ''' from __future__ import division def my_calc(a, b, op='add'): if op == 'multiply': return a*b elif op == 'divide': return a/b elif op == 'add': return a+b elif op == 'subtract': return a-b else: print 'error: valid operations are add, subtract, multiply, divide' my_calc(5, 6) my_calc(5, 6, 'add') my_calc(5, 6, 'subtract') my_calc(5, 6, 'divide') my_calc(5, 6, 'multiply') my_calc(5, 6, 'other') ''' Exercise 2: Given a list of numbers, return a list where ADJACENT duplicate elements have been reduced to a single element. ''' def remove_adjacent_dupes(original_list): new_list = [] new_list.append(original_list[0]) for num in original_list[1:]: if num != new_list[-1]: new_list.append(num) return new_list def remove_adjacent_dupes(original_list): new_list = [] for num in original_list: if len(new_list) == 0 or num != new_list[-1]: new_list.append(num) return new_list def remove_adjacent_dupes(original_list): i = len(original_list) - 1 while i > 0: if original_list[i] == original_list[i-1]: del original_list[i] i -= 1 return original_list remove_adjacent_dupes([1, 2, 2, 3]) remove_adjacent_dupes([1, 2, 3]) remove_adjacent_dupes([1, 2, 2, 3, 2]) remove_adjacent_dupes([1, 1, 2, 3, 2]) ''' Exercise 2 Bonus: Given a list of numbers, return a list where ALL duplicate elements have been reduced to a single element. ''' def remove_dupes(original_list): new_list = [] for num in original_list: if num not in new_list: new_list.append(num) return new_list def remove_dupes(original_list): sorted_list = sorted(original_list) new_list = [] for num in sorted_list: if len(new_list) == 0 or num != new_list[-1]: new_list.append(num) return new_list def remove_dupes(original_list): return(list(set(original_list))) remove_dupes([1, 2, 2, 3]) remove_dupes([1, 2, 3]) remove_dupes([1, 2, 2, 3, 2]) remove_dupes([1, 1, 2, 3, 2]) ''' Exercise 3: Given a string, convert it to a list of words, capitalize all words that are more than 3 characters long, and DISCARD the other words. ''' quote = "Strange women lying in ponds is no basis for government" [word.capitalize() for word in quote.split() if len(word) > 3] ''' Exercise 3 Bonus: Given a string, convert it to a list of words, capitalize all words that are more than 3 characters long, and KEEP the other words. ''' quote = "Strange women lying in ponds is no basis for government" [word.capitalize() if len(word) > 3 else word for word in quote.split()] |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |