I am trying to write a Python function extractAlignment(S) that takes in as an i
ID: 3801426 • Letter: I
Question
I am trying to write a Python function extractAlignment(S) that takes in as an input a matrix S, of dynamic length nx by ny (these lengths are determined by strings inputted into an earlier function that I wrote to create S) and returns a vector (list in Python) that represents the optimal path in getting back to S[0][0] from the lower right corner S[nx][ny]. S is filled simply with integers.
The optimal path is determined by looking at the values above and to the left before each move, and choosing the node represented by the smallest number. Only upward and leftward movements can be performed in order to reach S[0][0], and in the case of a tie, the path to the next node MUST be randomly chosen. Any help would be greatly appreciated. Thanks!
Explanation / Answer
There are 5 general mechanisms for creating arrays:
This section will not cover means of replicating, joining, or otherwise expanding or mutating existing arrays. Nor will it cover creating object arrays or structured arrays. Both of those are covered in their own sections.
Converting Python array_like Objects to Numpy Arrays
In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).
Examples:
>>>
Intrinsic Numpy Array Creation
Numpy has built-in functions for creating arrays from scratch:
zeros(shape) will create an array filled with 0 values with the specified shape. The default dtype is float64.
>>> np.zeros((2, 3)) array([[ 0., 0., 0.], [ 0., 0., 0.]])
ones(shape) will create an array filled with 1 values. It is identical to zeros in all other respects.
arange() will create arrays with regularly incrementing values. Check the docstring for complete information on the various ways it can be used. A few examples will be given here:
>>>
Note that there are some subtleties regarding the last usage that the user should be aware of that are described in the arange docstring.
linspace() will create arrays with a specified number of elements, and spaced equally between the specified beginning and end values. For example:
>>>
The advantage of this creation function is that one can guarantee the number of elements and the starting and end point, which arange() generally will not do for arbitrary start, stop, and step values.
indices() will create a set of arrays (stack)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.