Skip to content

Commit 3ae0e40

Browse files
committed
Add line_to_permutations. Return all permutations as tuple. Add type hints to parameters.
1 parent 4647aaf commit 3ae0e40

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

challenges/challenge.py

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ def expectation(self):
200200
# Accessing input lines
201201
# --------------------------------------------------
202202

203-
def line(self, number):
203+
def line(self, number:int):
204204
""" Return one line by the given number. """
205205
return self.lines[number]
206206

207-
def line_to_words(self, line_nr):
207+
def line_to_words(self, line_nr:int):
208208
""" Split one line into a list of words.
209209
210210
The number of the line is selected by line_nr.
@@ -213,7 +213,7 @@ def line_to_words(self, line_nr):
213213

214214
return list(re.compile(self.split_pattern).split(self.line(line_nr)))
215215

216-
def line_to_integers(self, line_nr):
216+
def line_to_integers(self, line_nr:int):
217217
""" Split one line into a list of integers.
218218
219219
The number of the line is selected by line_nr.
@@ -223,15 +223,15 @@ def line_to_integers(self, line_nr):
223223
return [int(i) for i in
224224
re.compile(self.split_pattern).split(self.line(line_nr))]
225225

226-
def line_to_integer(self, line_nr):
226+
def line_to_integer(self, line_nr:int):
227227
""" Return line as integer.
228228
229229
The number of the line is selected by line_nr.
230230
"""
231231

232232
return int(self.line(line_nr))
233233

234-
def line_to_floats(self, line_nr):
234+
def line_to_floats(self, line_nr:int):
235235
""" Split one line into a list of floats.
236236
237237
The number of the line is selected by line_nr.
@@ -240,7 +240,7 @@ def line_to_floats(self, line_nr):
240240
return [float(i) for i in
241241
re.compile(self.split_pattern).split(self.line(line_nr))]
242242

243-
def line_to_edge(self, nr):
243+
def line_to_edge(self, nr:int):
244244
"""Convert one line to an edge.
245245
246246
The number of the line is selected by line_nr.
@@ -249,18 +249,21 @@ def line_to_edge(self, nr):
249249
match = re.compile(self.edge_pattern).match(self.line(nr))
250250
return self._to_edge(match)
251251

252-
def line_to_permutation(self, nr, terminals = False):
252+
def line_to_permutation(self, nr:int, terminals:bool = False):
253253
"""Convert one line to a permutation
254254
255255
optionally surrounded by terminals
256256
257257
Example: (+1 -3, -2)
258-
Result: [1, -3, 2]
259-
If terminals is True: [0, 1, -3, 2, 4]
258+
Result: (1, -3, 2)
259+
If terminals is True: (0, 1, -3, 2, 4)
260260
261261
The number of the line is selected by line_nr.
262262
Input may be surrounded by a pair of round parenthesis.
263-
The split behaviour can be adjusted by changing self.edge_pattern.
263+
264+
:param nr: line number
265+
:param terminals: if True surrounded by 0 and n + 1
266+
:return: permutation
264267
"""
265268
line = self.line(nr)
266269
match = re.compile('^\((.*)\)$').match(line)
@@ -271,9 +274,28 @@ def line_to_permutation(self, nr, terminals = False):
271274
perm = [int(d) for d in re.compile(self.split_pattern).split(digits)]
272275
if terminals:
273276
perm = [0] + perm + [len(perm) + 1]
274-
return perm
277+
return tuple(perm)
278+
279+
def line_to_permutations(self, nr:int):
280+
"""Convert one line to multiple permutations
281+
282+
Example: (+1 -3, -2)(+4 +5)
283+
Result: [(1, -3, 2), (4, 5)]
284+
285+
The number of the line is selected by line_nr.
286+
287+
:param nr: line number
288+
:return: list of permutations (tuples)
289+
"""
290+
matches = re.findall('\(([^)]*)\)', self.line(nr))
291+
result = []
292+
for digits in matches:
293+
result.append(tuple(int(d) for d in re.compile(
294+
self.split_pattern).split(digits)))
295+
return result
296+
275297

276-
def edges(self, start=0, stop=None):
298+
def edges(self, start:int=0, stop:int=None):
277299
"""Generator to read edges from lines.
278300
279301
Reads a range of lines, one edge per line, and yields the edges.
@@ -301,7 +323,7 @@ def edges(self, start=0, stop=None):
301323
else:
302324
break # If edges end before stop, which may be infinity
303325

304-
def fasta(self, start=0, stop=None):
326+
def fasta(self, start:int=0, stop:int=None):
305327
"""Generator to read FASTA formatted samples.
306328
307329
Reads multiple fasta sequences and yields them.
@@ -338,7 +360,7 @@ def fasta(self, start=0, stop=None):
338360
# Yield final sequence
339361
yield name, sequence
340362

341-
def fasta_strands(self, start=0, stop=None):
363+
def fasta_strands(self, start:int=0, stop:int=None):
342364
""" Get the strands of a fasta read as list.
343365
344366
Takes the same arguments as self.fasta() and delegates to it.
@@ -359,14 +381,14 @@ def _to_edge(self, match):
359381
# --------------------------------------------------
360382

361383
# noinspection PyMethodMayBeStatic
362-
def format_list_of_integers(self, integers, joint=', '):
384+
def format_list_of_integers(self, integers:list, joint:str=', '):
363385
"""Join a list of integers to a string
364386
365387
Use the given joint.
366388
"""
367389
return joint.join(str(x) for x in integers)
368390

369-
def format_path(self, integers, backwards=False):
391+
def format_path(self, integers:list, backwards:bool=False):
370392
"""Join a list of integers to path of nodes.
371393
372394
The joint is -> by default. If the parameter
@@ -378,8 +400,8 @@ def format_path(self, integers, backwards=False):
378400
joint = '->'
379401
return self.format_list_of_integers(integers, joint)
380402

381-
def format_permutations(self, permutations, separator = '\n',
382-
element_separator = ' '):
403+
def format_permutations(self, permutations:list, separator:str = '\n',
404+
element_separator:str = ' '):
383405
entries = []
384406
for perm in permutations:
385407
entry = '('

tests/test_challenge.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,26 @@ def test_line_to_permuation(self):
7575
"""Show a line can be retrieved as permutation."""
7676
self.challenge.lines = ['one', '+1 -2']
7777
result = self.challenge.line_to_permutation(1)
78-
self.assertEqual(result, [1, -2])
78+
self.assertEqual((1, -2), result)
7979

8080
def test_line_to_permuation_with_parenthesis(self):
8181
"""Show a line can be retrieved as permutation."""
8282
self.challenge.lines = ['one', '(+1 -2)']
8383
result = self.challenge.line_to_permutation(1)
84-
self.assertEqual(result, [1, -2])
84+
self.assertEqual((1, -2), result)
8585

8686
def test_line_to_permuation_surrounded_with_terminals(self):
8787
"""Show a line can be retrieved as permutation."""
8888
self.challenge.lines = ['one', '+1 -2']
8989
result = self.challenge.line_to_permutation(1)
90-
self.assertEqual(result, [1, -2])
90+
self.assertEqual(result, (1, -2))
91+
92+
def test_line_to_permutations(self):
93+
"""Show a line can be retrived as genome. """
94+
self.challenge.lines = ['one', '(+1 -2)(+3 +4)']
95+
result = self.challenge.line_to_permutations(1)
96+
expect = [(1, -2), (3, 4)]
97+
self.assertEqual(expect, result)
9198

9299
def test_line_to_integers(self):
93100
"""Show a line can be retrieved as integers."""

0 commit comments

Comments
 (0)