-
Description array should always have "y" and "x"
Code def distance(array): for i in range(len(array)): if "y" in array[i] : y = abs(array[i].index("y") - i) if "x" in array[i] : x = abs(array[i].index("x") - i) return (x+y)
Preloaded Code Test Cases Failed array = [1,1,1,"y",1],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1] test.assert_equals(distance(array),3) array = [1,1,1,1,"y"],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1] test.assert_equals(distance(array),4) array = [1,1,1,1,"y",1,1],[1,1,1,1,"x",1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1] test.assert_equals(distance(array),1) array = [1,"y",1],[1,1,1],[1,1,"x"] test.assert_equals(distance(array),3)
Output:
-
Code - def distance(array):
- for i in range(len(array)):
- if "y" in array[i] : y = abs(array[i].index("y") - i)
- if "x" in array[i] : x = abs(array[i].index("x") - i)
return (x+y)- return (x+y)
Test Cases - array = [1,1,1,"y",1],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
- test.assert_equals(distance(array),3)
- array = [1,1,1,1,"y"],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
- test.assert_equals(distance(array),4)
- array = [1,1,1,1,"y",1,1],[1,1,1,1,"x",1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1]
- test.assert_equals(distance(array),1)
- array = [1,"y",1],[1,1,1],[1,1,"x"]
- test.assert_equals(distance(array),3)
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
Please sign in or sign up to leave a comment.