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