1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
2
Rep Power
0

#### 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.

def find_chars_reverse(m):
row=len(m)
col=len(m[0])
output=""
for i in range(row-1,-1,-1):
for j in range(col-1,-1,-1):
if m[i][j]!=type(int):
output=output+str(m[i][j])
return output

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?
2. 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
[['a','3.14159','b',2.718]]
We'll disregard conundrums like 'two' and call it a string.
Code:
```def f(ARRAY):
'''
doctest.  python -m doctest thisfile.py

>>> f([['a','3.14159','b',2.718]])
'ba'
'''
RESULT = []
for LIST in reversed(ARRAY):
for OBJECT in reversed(LIST):
GOOD = False
if isinstance(OBJECT, str):
try:
complex(OBJECT)
except:
GOOD = True
if GOOD:
RESULT.append(OBJECT)
return ''.join(RESULT)```
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.