Skip to content

Commit ce27047

Browse files
committed
Adding permutations
1 parent 098712b commit ce27047

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

challenges/challenge.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
This module holds the base class of all challenges.
44
"""
55

6-
import math
76
import re
8-
7+
import math
98
import types
109

11-
1210
class Challenge:
1311
"""Base class of all challenges
1412
@@ -251,6 +249,30 @@ def line_to_edge(self, nr):
251249
match = re.compile(self.edge_pattern).match(self.line(nr))
252250
return self._to_edge(match)
253251

252+
def line_to_permutation(self, nr, terminals = False):
253+
"""Convert one line to a permutation
254+
255+
optionally surrounded by terminals
256+
257+
Example: (+1 -3, -2)
258+
Result: [1, -3, 2]
259+
If terminals is True: [0, 1, -3, 2, 4]
260+
261+
The number of the line is selected by line_nr.
262+
Input may be surrounded by a pair of round parenthesis.
263+
The split behaviour can be adjusted by changing self.edge_pattern.
264+
"""
265+
line = self.line(nr)
266+
match = re.compile('^\((.*)\)$').match(line)
267+
if match:
268+
digits = match.group(1)
269+
else:
270+
digits = line
271+
perm = [int(d) for d in re.compile(self.split_pattern).split(digits)]
272+
if terminals:
273+
perm = [0] + perm + [len(perm) + 1]
274+
return perm
275+
254276
def edges(self, start=0, stop=None):
255277
"""Generator to read edges from lines.
256278
@@ -355,3 +377,11 @@ def format_path(self, integers, backwards=False):
355377
else:
356378
joint = '->'
357379
return self.format_list_of_integers(integers, joint)
380+
381+
def format_permutations(self, permutations):
382+
output = ''
383+
for perm in permutations:
384+
output += '('
385+
output += ' '.join(('+' if i > 0 else '') + str(i) for i in perm)
386+
output += ')' + self.br
387+
return output[:-1]

tests/test_challenge.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ def test_line(self):
7171
self.challenge.lines = ['one', 'two', 'three']
7272
self.assertEqual(self.challenge.line(1), 'two')
7373

74+
def test_line_to_permuation(self):
75+
"""Show a line can be retrieved as permutation."""
76+
self.challenge.lines = ['one', '+1 -2']
77+
result = self.challenge.line_to_permutation(1)
78+
self.assertEqual(result, [1, -2])
79+
80+
def test_line_to_permuation_with_parenthesis(self):
81+
"""Show a line can be retrieved as permutation."""
82+
self.challenge.lines = ['one', '(+1 -2)']
83+
result = self.challenge.line_to_permutation(1)
84+
self.assertEqual(result, [1, -2])
85+
86+
def test_line_to_permuation_surrounded_with_terminals(self):
87+
"""Show a line can be retrieved as permutation."""
88+
self.challenge.lines = ['one', '+1 -2']
89+
result = self.challenge.line_to_permutation(1)
90+
self.assertEqual(result, [1, -2])
91+
7492
def test_line_to_integers(self):
7593
"""Show a line can be retrieved as integers."""
7694
self.challenge.lines = ['one', '1, 2, 3']
@@ -260,3 +278,10 @@ def test_format_path(self):
260278
self.assertEqual(result, '11->22->33')
261279
result = self.challenge.format_path([11, 22, 33], backwards=True)
262280
self.assertEqual(result, '11<-22<-33')
281+
282+
def test_format_permuations(self):
283+
"""Show a list of permuations can be formatted."""
284+
list = [[1, -2], [-3, 4]]
285+
actual = self.challenge.format_permutations(list)
286+
expect = '(+1 -2)\n(-3 +4)'
287+
self.assertEqual(expect, actual)

0 commit comments

Comments
 (0)