반응형
Write a function partlist that gives all the ways to divide a list (an array) of at least two elements into two non-empty parts.
- Each two non empty parts will be in a pair (or an array for languages without tuples or a structin C - C: see Examples test Cases - )
- Each part will be in a string
- Elements of a pair must be in the same order as in the original array.
Examples of returns in different languages:
a = ["az", "toto", "picaro", "zone", "kiwi"] -->
[["az", "toto picaro zone kiwi"], ["az toto", "picaro zone kiwi"], ["az toto picaro", "zone kiwi"], ["az toto picaro zone", "kiwi"]]
or
a = {"az", "toto", "picaro", "zone", "kiwi"} -->
{{"az", "toto picaro zone kiwi"}, {"az toto", "picaro zone kiwi"}, {"az toto picaro", "zone kiwi"}, {"az toto picaro zone", "kiwi"}}
or
a = ["az", "toto", "picaro", "zone", "kiwi"] -->
[("az", "toto picaro zone kiwi"), ("az toto", "picaro zone kiwi"), ("az toto picaro", "zone kiwi"), ("az toto picaro zone", "kiwi")]
or
a = [|"az", "toto", "picaro", "zone", "kiwi"|] -->
[("az", "toto picaro zone kiwi"), ("az toto", "picaro zone kiwi"), ("az toto picaro", "zone kiwi"), ("az toto picaro zone", "kiwi")]
or
a = ["az", "toto", "picaro", "zone", "kiwi"] -->
"(az, toto picaro zone kiwi)(az toto, picaro zone kiwi)(az toto picaro, zone kiwi)(az toto picaro zone, kiwi)"
Note
You can see other examples for each language in "Your test cases"
Solution:
def partlist(arr):
return [(_join_arr(arr[:i]), _join_arr(arr[i:])) for i in range(1, len(arr))]
def _join_arr(arr):
return " ".join(arr)
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1231. For UFC Fans (Total Beginners): Conor McGregor vs George Saint Pierre (0) | 2022.12.31 |
---|---|
1230. The Wide-Mouthed frog! (0) | 2022.12.30 |
1228. Kata Example Twist (0) | 2022.12.28 |
1227. CSV representation of array (0) | 2022.12.27 |
1226. Alphabet war (0) | 2022.12.27 |