-1

I'm trying to write a function that determines if a letter is present at a certain row and column on a list of lists.

#Input:

lst2 = [['.', 'W', 'W', 'E', 'E'],
        ['.', '.', '.', '.', 'f'],
        ['A', 'A', 'A', '.', 'f']]

#Output: is_space_occupied(0, 1, lst2) should return True because 'W' is present on that spot 
        while is_space_occupied(1, 1, lst2) should return False because '.' is present on 
        that spot and not a letter.

This is the code I have so far:

def letter_on_spot(row,col,lst):
A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for row in lst:
    for col in lst:
        if col == A:
            return True
        else:
            return False

Edit: I'm getting return outside function as an error for return True and am not sure if my function works correctly

warrior4223
  • 113
  • 1
  • 1
  • 8

4 Answers4

1

There's two issues here.

First, you're looping unnecessarily; you provide the indices for the list so you only need to check if the value with those indices is contained inside the string A. No need to traverse through every element just to check one; index the list lst.

Second, col == A will always fail (unless col = 'ABCD..yz'). You compare their values when you should be checking if A contains col with the in operator.

In short, you could change your function to:

def letter_on_spot(row,col,lst):
    A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    return lst[row][col] in A

and get the wanted result -- True/False based on the contents of a given index.

Ideally, some error checking should be performed in order to now allow indices that result in IndexErrors; play around with the lists' len for that, something like this:

def letter_on_spot(row,col,lst):
    A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    if row >= len(lst) or col >= len(lst[row]): 
        return False    
    return lst[row][col] in A

where, before trying to access lst, you check if the bounds are acceptable by testing against the length of the list lst and the length of the sub-list lst[row].

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Thank you Jim this works perfectly but how could I have this function return False if the letter is not at the position or if it is a non-letter character – warrior4223 Nov 01 '16 at 20:30
  • I'd really suggest using `str.isalpha()`. The intent is arguably clearer, and there's no chance of forgetting one or more characters. – rainer Nov 01 '16 at 20:33
  • @warrior4223 If a letter is not in the position specified by `lst[row][col]` you'll get `False` returned due to the `in` check failing. If it is a non-letter character? You mean punctuation? – Dimitris Fasarakis Hilliard Nov 01 '16 at 20:33
  • @rainer I'd agree with that but I didn't want to deviate from the code he had *too* much. – Dimitris Fasarakis Hilliard Nov 01 '16 at 20:34
  • So if the input was 'is_space_occupied(5, 5, lst2)' would this function return False since that position doesn't exist on the list of lists or how could I have it do this? I'm not sure what it's currently doing in that situation – warrior4223 Nov 01 '16 at 20:36
  • @warrior4223 Right, you'd need to check against that as I specified in my last sentence. I edited my answer now to include that. – Dimitris Fasarakis Hilliard Nov 01 '16 at 20:48
0

You can use indices to access list elements:

x = [10, 11, 12, 13]
x[1] # => 11

The same works for lists of lists:

lst2 = [['.', 'W', 'W', 'E', 'E'],
        ['.', '.', '.', '.', 'f'],
        ]
lst2[2]    # => ['A', 'A', 'A', '.', 'f']
lst2[2][1] # => 'A' 

Now, all you need to do is check whether the symbol there is a character. Python provides str.isalpha() for that:

lst2[0][1].isalpha() # => True
lst2[1][1].isalpha() # => False

Note that str.isalpha() would also return True for strings with more than one character (eg. "hello".isalpha() returns True).If that is of concern, just check the string's length which you can get using len().

rainer
  • 6,769
  • 3
  • 23
  • 37
0

I am not sure what exactly is wrong with you code but a shorter alternative is the following:

def letter_on_spot(row,col,lst):
    return lst2[row][col].isalpha()
spyrostheodoridis
  • 508
  • 2
  • 8
  • 27
0

letter_on_spot(row, col, lst) takes two integers (hopefully) and a list as input

lets go over this step by step:

for row in lst:

same as saying for each smaller list in lst

for col in lst:

same as saying for each smaller list in lst so that's your first issue; we want to go through the elements of row, not lst

if col == A:

assuming we fixed the above problem, are we checking if col is the same as A, or are we checking if col is in A?

we can access the elements of lst and check if its a letter with the following:

if lst[row][col] in A:
    return True
else:
    return False
appills
  • 172
  • 1
  • 14