Skip to content

Commit d60545c

Browse files
Create Refactored ArrayCombination Class
1 parent d868982 commit d60545c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Refactored ArrayCombination Class

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Refactored ArrayCombination Class
2+
from itertools import combinations
3+
4+
class ArrayCombination:
5+
def __init__(self, arr):
6+
if not isinstance(arr, list):
7+
raise ValueError("Input should be a list.")
8+
self.arr = arr
9+
10+
def get_combinations(self, length=None):
11+
"""
12+
Generate all combinations of the array.
13+
If length is provided, generate combinations of that length.
14+
"""
15+
if length is None:
16+
# Generate combinations of all lengths from 1 to len(arr)
17+
return [list(combo) for r in range(1, len(self.arr) + 1) for combo in combinations(self.arr, r)]
18+
elif isinstance(length, int) and 0 < length <= len(self.arr):
19+
# Generate combinations of specified length
20+
return [list(combo) for combo in combinations(self.arr, length)]
21+
else:
22+
raise ValueError("Invalid combination length")

0 commit comments

Comments
 (0)