1

I have a function that have to return True if and only if all the ints in L1 are the lengths of the strings in L2 at the corresponding positions. and the Precondition: len(L1) == len(L2).

EXAMPLE:

>>> are_lengths_of_strs([4, 0, 2], ['abcd', '', 'ef']) 
True

Below is the function:

def are_lengths_of_strs(L1, L2):
 result = True
 for i in range(len(L1)):
 if i in len(L1) != len(L2):
 result = False
 return result

It result in a error. The line if i in len(L1) != len(L2): is the wrong . Can someone help me in this line ??

Obs : I have to use !=

BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Nov 17, 2012 at 20:01
2
  • 5
    "Something rotten in the state of Danmark" Commented Nov 17, 2012 at 20:04
  • +1 to sehe's comment, please use descriptive titles for your questions. Commented Nov 17, 2012 at 20:08

3 Answers 3

1

Corrected version of your code:

def are_lengths_of_strs(L1, L2):
 result = True
 for i in range(len(L1)):
 if L1[i] != len(L2[i]):
 result = False
 return result

Note that this is not pythonic, because you don't in fact need an index here:

def are_lengths_of_strs(L1, L2):
 result = True
 for i, l in zip(L1, L2)
 if i != len(l):
 result = False
 return result

Even shorter:

def are_lengths_of_strs(L1, L2):
 return all(i == len(l)
 for i, l in zip(L1, L2))
answered Nov 17, 2012 at 20:03
Sign up to request clarification or add additional context in comments.

Comments

0

Your if condition is probably the problem:

 if i in len(L1) != len(L2):

This checks if i in len(L1), but len(L1) is an integer, which can't "contain" another number.

You have to iterate over the elements of each list:

def are_lengths_of_strs(L1, L2):
 result = True
 for i in range(len(L1)):
 if L1[i] != len(L2[i]):
 result = False
 return result

Also, get rid of result. A simple return statement will work just fine:

def are_lengths_of_strs(L1, L2):
 for i in range(len(L1)):
 if L1[i] != len(L2[i]):
 return False
 return True

If you want to get even more Pythonic, use zip:

def are_lengths_of_strs(L1, L2):
 for a, b in zip(L1, L2)
 if a != len(b):
 return False
 return True
answered Nov 17, 2012 at 20:09

Comments

0

use the built-in all() along with zip():

In [5]: all(x==len(y) for x,y in zip([4, 0, 2], ['abcd', '', 'ef']))
Out[5]: True

or if != is compulsory use any():

In [7]: not any(x != len(y) for x,y in zip([4, 0, 2], ['abcd', '', 'ef']))
Out[7]: True

or improving your code:

def are_lengths_of_strs(L1, L2):
 for x,y in zip(L1,L2):
 if x!=len(y):
 return False
 return True
In [12]: are_lengths_of_strs([4, 0, 2], ['abcd', '', 'ef'])
Out[12]: True
answered Nov 17, 2012 at 20:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.