| 1 | 1 | def append(list1, list2): |
| 2 | | pass |
| 2 | # Edge Case: Handle None inputs |
| 3 | if list1 is None: |
| 4 | list1 = [] |
| 5 | if list2 is None: |
| 6 | list2 = [] |
| 7 | |
| 8 | result = [] |
| 9 | for item in list1: |
| 10 | result.append(item) |
| 11 | for item in list2: |
| 12 | result.append(item) |
| 13 | return result |
| 14 | # Handled Edge Cases: None inputs |
| 3 | 15 | |
| 4 | 16 | |
| 5 | 17 | def concat(lists): |
| 6 | | pass |
| 18 | # Edge Case: Handle None input |
| 19 | if lists is None: |
| 20 | return [] |
| 21 | |
| 22 | # Edge Case: Handle empty lists input |
| 23 | if len(lists) == 0: |
| 24 | return [] |
| 25 | |
| 26 | result = [] |
| 27 | for lst in lists: |
| 28 | # Edge Case: Handle None in nested lists |
| 29 | if lst is None: |
| 30 | continue |
| 31 | for item in lst: |
| 32 | result.append(item) |
| 33 | return result |
| 34 | # Handled Edge Cases: None input, empty lists input, None in nested lists |
| 7 | 35 | |
| 8 | 36 | |
| 9 | 37 | def filter(function, list): |
| 10 | | pass |
| 38 | # Edge Case: Handle None inputs |
| 39 | if function is None or list is None: |
| 40 | return [] |
| 41 | |
| 42 | result = [] |
| 43 | for item in list: |
| 44 | # Edge Case: Handle function that raises exception |
| 45 | try: |
| 46 | if function(item): |
| 47 | result.append(item) |
| 48 | except: |
| 49 | # If function raises exception, we skip the item |
| 50 | continue |
| 51 | return result |
| 52 | # Handled Edge Cases: None inputs, function that raises exception |
| 11 | 53 | |
| 12 | 54 | |
| 13 | 55 | def length(list): |
| 14 | | pass |
| 56 | # Edge Case: Handle None input |
| 57 | if list is None: |
| 58 | return 0 |
| 59 | |
| 60 | count = 0 |
| 61 | for _ in list: |
| 62 | count += 1 |
| 63 | return count |
| 64 | # Handled Edge Cases: None input |
| 15 | 65 | |
| 16 | 66 | |
| 17 | 67 | def map(function, list): |
| 18 | | pass |
| 68 | # Edge Case: Handle None inputs |
| 69 | if function is None or list is None: |
| 70 | return [] |
| 71 | |
| 72 | result = [] |
| 73 | for item in list: |
| 74 | # Edge Case: Handle function that raises exception |
| 75 | try: |
| 76 | result.append(function(item)) |
| 77 | except: |
| 78 | # If function raises exception, we append the original item |
| 79 | result.append(item) |
| 80 | return result |
| 81 | # Handled Edge Cases: None inputs, function that raises exception |
| 19 | 82 | |
| 20 | 83 | |
| 21 | 84 | def foldl(function, list, initial): |
| 22 | | pass |
| 85 | # Edge Case: Handle None inputs |
| 86 | if function is None or list is None: |
| 87 | return initial |
| 88 | |
| 89 | accumulator = initial |
| 90 | for item in list: |
| 91 | # Edge Case: Handle function that raises exception |
| 92 | try: |
| 93 | accumulator = function(accumulator, item) |
| 94 | except: |
| 95 | # If function raises exception, we keep the accumulator unchanged |
| 96 | continue |
| 97 | return accumulator |
| 98 | # Handled Edge Cases: None inputs, function that raises exception |
| 23 | 99 | |
| 24 | 100 | |
| 25 | 101 | def foldr(function, list, initial): |
| 26 | | pass |
| 102 | # Edge Case: Handle None inputs |
| 103 | if function is None or list is None: |
| 104 | return initial |
| 105 | |
| 106 | accumulator = initial |
| 107 | # Process list from right to left |
| 108 | for i in range(len(list) - 1, -1, -1): |
| 109 | # Edge Case: Handle function that raises exception |
| 110 | try: |
| 111 | accumulator = function(accumulator, list[i]) |
| 112 | except: |
| 113 | # If function raises exception, we keep the accumulator unchanged |
| 114 | continue |
| 115 | return accumulator |
| 116 | # Handled Edge Cases: None inputs, function that raises exception |
| 27 | 117 | |
| 28 | 118 | |
| 29 | 119 | def reverse(list): |
| 30 | | pass |
| 120 | # Edge Case: Handle None input |
| 121 | if list is None: |
| 122 | return [] |
| 123 | |
| 124 | result = [] |
| 125 | for i in range(len(list) - 1, -1, -1): |
| 126 | result.append(list[i]) |
| 127 | return result |
| 128 | # Handled Edge Cases: None input |