Skip to content

Commit

Permalink
Add line_to_permutations. Return all permutations as tuple. Add type …
Browse files Browse the repository at this point in the history
…hints to parameters.
  • Loading branch information
elmar-hinz committed Jan 20, 2018
1 parent 4647aaf commit 3ae0e40
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
58 changes: 40 additions & 18 deletions challenges/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def expectation(self):
# Accessing input lines
# --------------------------------------------------

def line(self, number):
def line(self, number:int):
""" Return one line by the given number. """
return self.lines[number]

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

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

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

def line_to_integer(self, line_nr):
def line_to_integer(self, line_nr:int):
""" Return line as integer.
The number of the line is selected by line_nr.
"""

return int(self.line(line_nr))

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

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

def line_to_permutation(self, nr, terminals = False):
def line_to_permutation(self, nr:int, terminals:bool = False):
"""Convert one line to a permutation
optionally surrounded by terminals
Example: (+1 -3, -2)
Result: [1, -3, 2]
If terminals is True: [0, 1, -3, 2, 4]
Result: (1, -3, 2)
If terminals is True: (0, 1, -3, 2, 4)
The number of the line is selected by line_nr.
Input may be surrounded by a pair of round parenthesis.
The split behaviour can be adjusted by changing self.edge_pattern.
:param nr: line number
:param terminals: if True surrounded by 0 and n + 1
:return: permutation
"""
line = self.line(nr)
match = re.compile('^\((.*)\)$').match(line)
Expand All @@ -271,9 +274,28 @@ def line_to_permutation(self, nr, terminals = False):
perm = [int(d) for d in re.compile(self.split_pattern).split(digits)]
if terminals:
perm = [0] + perm + [len(perm) + 1]
return perm
return tuple(perm)

def line_to_permutations(self, nr:int):
"""Convert one line to multiple permutations
Example: (+1 -3, -2)(+4 +5)
Result: [(1, -3, 2), (4, 5)]
The number of the line is selected by line_nr.
:param nr: line number
:return: list of permutations (tuples)
"""
matches = re.findall('\(([^)]*)\)', self.line(nr))
result = []
for digits in matches:
result.append(tuple(int(d) for d in re.compile(
self.split_pattern).split(digits)))
return result


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

def fasta(self, start=0, stop=None):
def fasta(self, start:int=0, stop:int=None):
"""Generator to read FASTA formatted samples.
Reads multiple fasta sequences and yields them.
Expand Down Expand Up @@ -338,7 +360,7 @@ def fasta(self, start=0, stop=None):
# Yield final sequence
yield name, sequence

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

# noinspection PyMethodMayBeStatic
def format_list_of_integers(self, integers, joint=', '):
def format_list_of_integers(self, integers:list, joint:str=', '):
"""Join a list of integers to a string
Use the given joint.
"""
return joint.join(str(x) for x in integers)

def format_path(self, integers, backwards=False):
def format_path(self, integers:list, backwards:bool=False):
"""Join a list of integers to path of nodes.
The joint is -> by default. If the parameter
Expand All @@ -378,8 +400,8 @@ def format_path(self, integers, backwards=False):
joint = '->'
return self.format_list_of_integers(integers, joint)

def format_permutations(self, permutations, separator = '\n',
element_separator = ' '):
def format_permutations(self, permutations:list, separator:str = '\n',
element_separator:str = ' '):
entries = []
for perm in permutations:
entry = '('
Expand Down
13 changes: 10 additions & 3 deletions tests/test_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,26 @@ def test_line_to_permuation(self):
"""Show a line can be retrieved as permutation."""
self.challenge.lines = ['one', '+1 -2']
result = self.challenge.line_to_permutation(1)
self.assertEqual(result, [1, -2])
self.assertEqual((1, -2), result)

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

def test_line_to_permuation_surrounded_with_terminals(self):
"""Show a line can be retrieved as permutation."""
self.challenge.lines = ['one', '+1 -2']
result = self.challenge.line_to_permutation(1)
self.assertEqual(result, [1, -2])
self.assertEqual(result, (1, -2))

def test_line_to_permutations(self):
"""Show a line can be retrived as genome. """
self.challenge.lines = ['one', '(+1 -2)(+3 +4)']
result = self.challenge.line_to_permutations(1)
expect = [(1, -2), (3, 4)]
self.assertEqual(expect, result)

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

0 comments on commit 3ae0e40

Please sign in to comment.