| 2 | 2 | |
| 3 | 3 | |
| 4 | 4 | def append(list1: list, list2: list) -> list: |
| 5 | | pass |
| 5 | # Edge Case: Both lists are empty |
| 6 | if not list1 and not list2: |
| 7 | return [] |
| 8 | # Edge Case: First list is empty |
| 9 | if not list1: |
| 10 | return list2[:] |
| 11 | # Edge Case: Second list is empty |
| 12 | if not list2: |
| 13 | return list1[:] |
| 14 | |
| 15 | result = list1[:] |
| 16 | for item in list2: |
| 17 | result.append(item) |
| 18 | return result |
| 19 | # Handled Edge Cases: Both lists empty, first list empty, second list empty |
| 6 | 20 | |
| 7 | 21 | |
| 8 | 22 | def concat(lists: list[list]) -> list: |
| 9 | | pass |
| 23 | # Edge Case: Empty list of lists |
| 24 | if not lists: |
| 25 | return [] |
| 26 | # Edge Case: List containing empty lists |
| 27 | result = [] |
| 28 | for lst in lists: |
| 29 | for item in lst: |
| 30 | result.append(item) |
| 31 | return result |
| 32 | # Handled Edge Cases: Empty list of lists, list containing empty lists |
| 10 | 33 | |
| 11 | 34 | |
| 12 | 35 | def filter(function: Callable[[Any], bool], list: list) -> list: |
| 13 | | pass |
| 36 | # Edge Case: Empty list |
| 37 | if not list: |
| 38 | return [] |
| 39 | # Edge Case: All elements filtered out |
| 40 | result = [] |
| 41 | for item in list: |
| 42 | if function(item): |
| 43 | result.append(item) |
| 44 | return result |
| 45 | # Handled Edge Cases: Empty list, all elements filtered out |
| 14 | 46 | |
| 15 | 47 | |
| 16 | 48 | def length(list: list) -> int: |
| 17 | | pass |
| 49 | # Edge Case: Empty list |
| 50 | count = 0 |
| 51 | for _ in list: |
| 52 | count += 1 |
| 53 | return count |
| 54 | # Handled Edge Cases: Empty list |
| 18 | 55 | |
| 19 | 56 | |
| 20 | 57 | def map(function: Callable[[Any], Any], list: list) -> list: |
| 21 | | pass |
| 22 | | |
| 23 | | |
| 24 | | def foldl(function: Callable[[Any, Any], list], list: list, initial: Any) -> Any: # function(acc, el) |
| 25 | | pass |
| 26 | | |
| 27 | | |
| 28 | | def foldr(function: Callable[[Any, Any], list], list: list, initial: Any) -> Any: # function(acc, el) |
| 29 | | pass |
| 58 | # Edge Case: Empty list |
| 59 | if not list: |
| 60 | return [] |
| 61 | result = [] |
| 62 | for item in list: |
| 63 | result.append(function(item)) |
| 64 | return result |
| 65 | # Handled Edge Cases: Empty list |
| 66 | |
| 67 | |
| 68 | def foldl(function: Callable[[Any, Any], Any], list: list, initial: Any) -> Any: |
| 69 | # Edge Case: Empty list |
| 70 | accumulator = initial |
| 71 | for item in list: |
| 72 | accumulator = function(accumulator, item) |
| 73 | return accumulator |
| 74 | # Handled Edge Cases: Empty list |
| 75 | |
| 76 | |
| 77 | def foldr(function: Callable[[Any, Any], Any], list: list, initial: Any) -> Any: |
| 78 | # Edge Case: Empty list |
| 79 | accumulator = initial |
| 80 | # Process list from right to left |
| 81 | for i in range(len(list) - 1, -1, -1): |
| 82 | accumulator = function(list[i], accumulator) |
| 83 | return accumulator |
| 84 | # Handled Edge Cases: Empty list |
| 30 | 85 | |
| 31 | 86 | |
| 32 | 87 | def reverse(list: list) -> list: |
| 33 | | pass |
| 88 | # Edge Case: Empty list |
| 89 | if not list: |
| 90 | return [] |
| 91 | # Edge Case: Single element list |
| 92 | result = [] |
| 93 | for i in range(len(list) - 1, -1, -1): |
| 94 | result.append(list[i]) |
| 95 | return result |
| 96 | # Handled Edge Cases: Empty list, single element list |