| 7 | 7 | :return: tuple of (palindrome, iterable). |
| 8 | 8 | Iterable should contain both factors of the palindrome in an arbitrary order. |
| 9 | 9 | """ |
| 10 | # Edge Case: min_factor greater than max_factor |
| 11 | if min_factor > max_factor: |
| 12 | raise ValueError("min must be <= max") |
| 13 | |
| 14 | # Edge Case: negative factors |
| 15 | if max_factor < 0: |
| 16 | return (None, []) |
| 17 | |
| 18 | largest_palindrome = None |
| 19 | factors_list = [] |
| 20 | |
| 21 | # Edge Case: single digit ranges |
| 22 | for i in range(max_factor, min_factor - 1, -1): |
| 23 | for j in range(i, min_factor - 1, -1): # Start from i to avoid duplicates |
| 24 | product = i * j |
| 25 | |
| 26 | # If we already found a larger palindrome, we can break early |
| 27 | if largest_palindrome is not None and product < largest_palindrome: |
| 28 | break |
| 29 | |
| 30 | # Check if product is palindrome |
| 31 | if str(product) == str(product)[::-1]: |
| 32 | if largest_palindrome is None or product > largest_palindrome: |
| 33 | largest_palindrome = product |
| 34 | factors_list = [(j, i)] |
| 35 | elif product == largest_palindrome: |
| 36 | factors_list.append((j, i)) |
| 37 | |
| 38 | # Edge Case: no palindromes exist in range |
| 39 | if largest_palindrome is None: |
| 40 | return (None, []) |
| 41 | |
| 42 | return (largest_palindrome, factors_list) |
| 43 | |
| 44 | # Handled Edge Cases: min_factor greater than max_factor, negative factors, single digit ranges, no palindromes exist in range |
| 10 | 45 | |
| 11 | | pass |
| 12 | 46 | |
| 13 | | |
| 14 | | def smallest(min_factor, max_factor): |
| 47 | def smallest(*, max_factor, min_factor=0): |
| 15 | 48 | """Given a range of numbers, find the smallest palindromes which |
| 16 | 49 | are products of two numbers within that range. |
| 17 | 50 | |
| 20 | 53 | :return: tuple of (palindrome, iterable). |
| 21 | 54 | Iterable should contain both factors of the palindrome in an arbitrary order. |
| 22 | 55 | """ |
| 23 | | |
| 24 | | pass |
| 56 | # Edge Case: min_factor greater than max_factor |
| 57 | if min_factor > max_factor: |
| 58 | raise ValueError("min must be <= max") |
| 59 | |
| 60 | # Edge Case: negative factors |
| 61 | if max_factor < 0: |
| 62 | return (None, []) |
| 63 | |
| 64 | smallest_palindrome = None |
| 65 | factors_list = [] |
| 66 | |
| 67 | # Edge Case: single digit ranges |
| 68 | for i in range(min_factor, max_factor + 1): |
| 69 | for j in range(i, max_factor + 1): # Start from i to avoid duplicates |
| 70 | product = i * j |
| 71 | |
| 72 | # If we already found a smaller palindrome and current product is larger, we can break |
| 73 | if smallest_palindrome is not None and product > smallest_palindrome: |
| 74 | break |
| 75 | |
| 76 | # Check if product is palindrome |
| 77 | if str(product) == str(product)[::-1]: |
| 78 | if smallest_palindrome is None or product < smallest_palindrome: |
| 79 | smallest_palindrome = product |
| 80 | factors_list = [(i, j)] |
| 81 | elif product == smallest_palindrome: |
| 82 | factors_list.append((i, j)) |
| 83 | |
| 84 | # Edge Case: no palindromes exist in range |
| 85 | if smallest_palindrome is None: |
| 86 | return (None, []) |
| 87 | |
| 88 | return (smallest_palindrome, factors_list) |
| 89 | |
| 90 | # Handled Edge Cases: min_factor greater than max_factor, negative factors, single digit ranges, no palindromes exist in range |