challenges package¶
Submodules¶
challenges.challenge module¶
Core module of challenges
This module holds the base class of all challenges.
-
class
challenges.challenge.
Challenge
[source]¶ Bases:
object
Base class of all challenges
Design concept is the Template Method Design Pattern (GOF).
Attributes:
Sample: The input of the challenge. Output: The output of the challenge Workflow:
The main method controls the overall workflow by calling the worker methods. This is the common character of all challenges. The base class controls the workflow of the derived workers.
Workers:
The worker methods need to be implemented by the inheriting class.
Read: Read the input into a list of lines. Build: Build the data model from the lines. Calc: Run the main algorithm of the challenge. Format: Create the output string required by the grader. Library:
The other methods support the implementation of the workers. They address the extraction of data from the input lines or the formatting of the output.
Sample:
The attribute sample is both used as class and as instance attribute. When the instance attribute is injected it shadows the class attribute. By this the class attribute sets a tiny but useful default.
When the challenge runner is executed with the option –klass no instance variable is injected and the sample from the class is used:
prompt> challenge MyChallenge --klass
When the runner is executed with the option –file the files content is injected:
prompt> challenge MyChallenge --file ~/Downloads/data.txt
-
br
= '\n'¶ Line breaks as expected by the most graders.
-
build
()[source]¶ Set up the model from the input lines.
This method must be implemented. Reads from self.lines. Fills self.model.
-
calc
()[source]¶ Main algorithm of the challenge.
This method must be implemented. Here the interesting stuff happens. Best practice is to delegate to functions, that are named by the algorithms used or even to other classes that implement the algorithm.
Reads from self.model. Fills self.result.
-
edge_pattern
= '^(\\d+)->(\\d+)(:(\\d+))?$'¶ Reg expression to extract edges of a graph.
With or without weight.
2->3 2->3:22A default setting used by methods that extract edges from input lines. May need adjustment for different kind of edge input formats.
-
edges
(start: int = 0, stop: int = None)[source]¶ Generator to read edges from lines.
!!! DEPRECATED !!! use lines_to_edges()
Reads a range of lines, one edge per line, and yields the edges.
By the start and stop parameters a range can be given. The stop parameter is the index behind the last line to use.
The line to start is set by the parameter start. It defaults to zero. The line to stop is set by the parameter stop. When it is not provided lines are used as long as they match the edge_pattern reg expression. The match behaviour can be adjusted by the self.edge_pattern.
-
expect
= '\n expected result\n expected result\n '¶ Holds the expected result with additional leading whitespace.
Whitespace surrounding lines is for readability. It typically needs to be stripped to get the actual expactation.
-
fasta
(start: int = 0, stop: int = None)[source]¶ Generator to read FASTA formatted samples.
Reads multiple fasta sequences and yields them.
By the start and stop parameters a range can be given. The stop parameter is the index behind the last line to use.
The line to start is set by the parameter start. It defaults to zero. The line to stop is set by the parameter stop. When it is not provided lines are used as long as they match the FASTA format. The match behaviour can be adjusted by the self.fasta_pattern.
-
fasta_pattern
= '^[\\-\\*A-Z]+$'¶ Reg expression for FASTA sequences.
Matches lines holding FASTA sequences.
-
fasta_strands
(start: int = 0, stop: int = None)[source]¶ Get the strands of a fasta read as list.
Takes the same arguments as self.fasta() and delegates to it.
-
format
()[source]¶ Format the output string.
In simple cases this method can be used as is. In other cases it needs to be reimplemented.
Reads from self.result. Fills self.output.
-
format_list_of_integers
(integers: list, joint: str = ', ')[source]¶ Join a list of integers to a string
Use the given joint.
-
format_path
(integers: list, backwards: bool = False)[source]¶ Join a list of integers to path of nodes.
The joint is -> by default. If the parameter backwards is True the joint is <-.
-
format_permutations
(permutations: list, separator: str = '\n', element_separator: str = ' ')[source]¶
-
line
(nr: int)[source]¶ Return one line by the given number.
Parameters: nr – line number Returns: line as string
-
line_to_edge
(nr: int)[source]¶ Convert one line to an edge.
Parameters: nr – line number Returns: edge (namespace: tail, head, weight) See: self._to_edges
-
line_to_edges
(nr: int)[source]¶ Convert one line to multiple edges.
1->2,3,4
Parameters: nr – line number Returns: edge (namespace: tail, head, weight) See: self._to_edges
-
line_to_floats
(nr: int)[source]¶ Split one line into a list of floats.
Parameters: nr – line number Returns: list of floats See: self._to_floats
-
line_to_integer
(nr: int)[source]¶ Return line as integer.
Parameters: nr – line number Returns: integer
-
line_to_integers
(nr: int)[source]¶ Split one line into a list of integers.
Parameters: nr – line number Returns: list of integers See: self._to_integers
-
line_to_permutation
(nr: int, terminals: bool = False)[source]¶ 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)
The number of the line is selected by nr. Input may be surrounded by a pair of round parenthesis.
Parameters: - nr – line number
- terminals – if True surrounded by 0 and n + 1
Returns: permutation
-
line_to_permutations
(nr: int)[source]¶ 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.
Parameters: nr – line number Returns: list of permutations (tuples)
-
line_to_words
(nr: int)[source]¶ Split one line into a list of words.
Parameters: nr – line number Returns: list of words See: self._to_words()
-
lines
= None¶ A list of lines that will be filled by the method read().
-
lines_to_edges
(start: int = 0, stop: int = None)[source]¶ Retrun a list of edges for range of lines.
1->2 # simple edge 1->2:22 # weighted edge 1->2,3,4 # muliple edges per line
If stop is not given all remaining lines are used.
Parameters: - start –
- stop –
Returns: list of edges (namespace: tail, head, weight)
See: self._to_edges
-
lines_to_floats
(start: int = 0, stop=None, flatten=False)[source]¶ Split a range of lines into floats
If stop is not given all remaining lines are used.
Parameters: - start – index of first line
- stop – index of line after last line
- flatten – flatten to one dimensional list
Returns: one or two dimensional list of floats
See: self._to_floats()
-
lines_to_graph
(start: int = 0, stop: int = None)[source]¶ Retrun a graph for range of lines
If stop is not given all remaining lines are usee.
Formats:
1->2 # simple edge 1->2:22 # weighted edge 1->2,3,4 # muliple edges per lineProperties:
- graph.edges:
- dict, tails as keys and list of heads as values
- graph.weights:
- dict, pairs of tail, head as keys and weight as value
Parameters: - start –
- stop –
Returns: graph, namespace with graphs properties
See: self._to_edges
-
lines_to_integers
(start: int = 0, stop=None, flatten=False)[source]¶ Split a range of lines into integers
If stop is not given all remaining lines are used.
Parameters: - start – index of first line
- stop – index of line after last line
- flatten – flatten to one dimensional list
Returns: one or two dimensional list of integers
See: self._to_integers()
-
lines_to_list
(start: int = 0, stop: int = None)[source]¶ Return a list of lines.
If stop is not given all remaining lines are used.
Parameters: - start – index of first line
- stop – index of line after last line
Returns: list of lines
-
lines_to_words
(start: int = 0, stop: int = None, flatten: bool = False)[source]¶ Split a range of lines into words.
If stop is not given all remaining lines are used.
Parameters: - start – index of first line
- stop – index of line after last line
- flatten – flatten to one dimensional list
Returns: one or two dimensional list of words
See: self._to_words()
-
main
()[source]¶ Control the workflow of the challenge.
Usually this method doesn’t need to be overwritten.
The workers share data via instance variables. The overall input is injected into self.sample. The overall output is read from self.result.
-
model
= None¶ The imported data model.
A flexible namespace object to take up any kind of data. In simple cases this may be completely overwritten, i.e by a list or dict.
-
multi_edge_pattern
= '^(\\d+)->(\\d+(,?\\s*\\d+)*)$'¶ Reg expressen to extrct edges of a graph.
Multiple edges on one line.
2->3, 4, 5A default setting used by methods that extract edges from input lines. May need adjustment for different kind of edge input formats.
-
output
= None¶ The output string.
The string representation of the resulting model as expected by the grader.
-
read
()[source]¶ Extract the input string self.sample into self.lines.
Typically this method can be used as is.
-
result
= None¶ The resulting data model.
A flexible namespace object to take up any kind of data. In simple cases this may be completely overwritten, i.e by a list or dict.
-
sample
= '\n sample\n sample\n '¶ Holds a minimal example of the input with additional whitespace.
This class variable should always be preset with a tiny sample of input. Whitespace surrounding lines is for readability. It typically needs to be stripped to get the actual sample.
-
split_pattern
= '\\s+|\\s?,\\s?'¶ Reg expression to split input lines.
Used by some of the input parsing functions. The default splits by whitespace and/or comma. If the input is separated differently like colons or semicolons it needs adjustment in the inheriting class.
-