Created
July 21, 2020 10:27
-
-
Save alexmon/d74caa08a1a70523e55b76ed47c6d8cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Given an array of integers greater than zero, find if it is possible to | |
| # split it in two (without reordering the elements), such that the sum | |
| # of the two resulting arrays is the same. Print the resulting arrays. | |
| # In [1]: can_partition([5, 2, 3]) | |
| # Output [1]: ([5], [2, 3]) | |
| # Return [1]: True | |
| # | |
| # In [2]: can_partition([2, 3, 2, 1, 1, 1, 2, 1, 1]) | |
| # Output [2]([2, 3, 2], [1, 1, 1, 2, 1, 1]) | |
| # Return [2]: True | |
| # | |
| # In [3]: can_partition([1, 1, 3]) | |
| # Return [3]: False | |
| # | |
| # In [4]: can_partition([1, 1, 3, 1]) | |
| # Return [4]: False | |
| import sys | |
| from typing import List, Callable | |
| cast_to_int: Callable[[str], int] = lambda x: int(x) | |
| def can_partition(elems: List[int]) -> bool: | |
| if len(elems) <= 1: | |
| return False | |
| for i in range(1, (len(elems)-1)): | |
| if sum(elems[:i]) == sum(elems[i:]): | |
| print(elems[:i], elems[i:]) | |
| return True | |
| return False | |
| if __name__ == "__main__": | |
| elems: List[int] = list(map(cast_to_int, sys.argv[1].split(","))) | |
| print(can_partition(elems)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment