October 20th, 2013, 01:18 PM
Help with matrices??
I have to write a function called find_chars_reverse that, given a matrix as an input argument, must do the following things:
Scan the matrix from the bottom-right to the upper left corner in row order and,
Return a string composed by the concatenation of the strings contained in the matrix elements.
Scanning the matrix from the bottom-right to the upper left corner in row order means that, if the matrix has M rows and N columns, the function must:
First, scan the elements in row M, then those in row M-1, up to row 0; and
For each row, scan the element from the Nth to the 0th column.
Here is the code I have written. There are other functions I must write for this project that I am having the same problem with. The problem I am having is telling the program to discern between integers and strings. The program should only return strings, but the matrix contains integers which the program must bypass. By the way, the input argument is the matrix=m.
for i in range(row-1,-1,-1):
for j in range(col-1,-1,-1):
The code above works, but returns integers which have been converted to string.
I have tried numerous different ways of writing the line: if m[i][j]!=type(int) - such as: if m[i][j]==type(str):.
So, my question is how would I return only strings?
October 20th, 2013, 02:58 PM
This is a bit confusing. If a list has m items from 0 through m then it actually has m+1 items. You seem to understand because your loop indexes are correct.
The other confusion is that
'451' is a string.
I think you're saying that 'ba' should be the result of input
We'll disregard conundrums like 'two' and call it a string.
You could dispense with the "extra variable" GOOD by replacing for `GOOD = True' the code you'll use to prepare the result. I avoided that because it violates the "minimize and simplify code within try-except blocks" rule. I admit that result += 'another string' or RESULT.append(OBJECT) are also quite straightforward. Still, you might misssspell.
doctest. python -m doctest thisfile.py
RESULT = 
for LIST in reversed(ARRAY):
for OBJECT in reversed(LIST):
GOOD = False
if isinstance(OBJECT, str):
GOOD = True
[/code] are essential for python code and Makefiles!