Skip to content

Commit 6800ae4

Browse files
committed
User defined separators for permutations formating
1 parent e0aad7e commit 6800ae4

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

challenges/challenge.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,12 @@ def format_path(self, integers, backwards=False):
378378
joint = '->'
379379
return self.format_list_of_integers(integers, joint)
380380

381-
def format_permutations(self, permutations):
381+
def format_permutations(self, permutations, separator = '\n',
382+
element_separator = ' '):
382383
output = ''
383384
for perm in permutations:
384385
output += '('
385-
output += ' '.join(('+' if i > 0 else '') + str(i) for i in perm)
386-
output += ')' + self.br
387-
return output[:-1]
386+
output += element_separator.join(
387+
('+' if i > 0 else '') + str(i) for i in perm)
388+
output += ')' + separator
389+
return output[:-len(separator)]

tests/test_challenge.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,14 @@ def test_format_permuations(self):
285285
actual = self.challenge.format_permutations(list)
286286
expect = '(+1 -2)\n(-3 +4)'
287287
self.assertEqual(expect, actual)
288+
289+
def test_format_permuations_with_user_defined_joint(self):
290+
"""Show user defined separators can be used."""
291+
list = [[1, -2], [-3, 4]]
292+
actual = self.challenge.format_permutations(
293+
list,
294+
separator = ', ',
295+
element_separator = ' | '
296+
)
297+
expect = '(+1 | -2), (-3 | +4)'
298+
self.assertEqual(expect, actual)

0 commit comments

Comments
 (0)