Here is the text of the problem, found on Codewars (https://www.codewars.com/kata/take-a-ten-minute-walk/train/python):
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.
Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).
My code worked fine for the majority of tests but failed some of them, which I will list at the end of the question. Here's my code:
def isValidWalk(walk):
#determine if walk is valid
i=0
b=0
c=0
if( len(walk)==10):
while(i<len(walk)):
if(walk[i]=='n'):
b=b+1
elif(walk[i]=='e'):
c=c+1
elif(walk[i]=='s'):
b=b-1
elif(walk[i]=='w'):
c=c-1
i=i+1
if(b==0 & c==0):
return(True)
else:
return(False)
else:
return(False)
In the beginning I was using a=math.exp(1), adding and subtracting '1' and 'e' respectively for up-down and left-right directions, I changed my approach thinking that the problem might have been an approximation but it didn't solve anything.
Running the test multiple times always results in facing a minority of failures, some lists that make it fail are:
[s, s, w, w, n, e, n, w, e, w] [e, s, s, w, n, e, n, n, e, s]
However it also failed with a similar string which contained three 'e' and one 'w' so I'm starting to think that there is a bug in the testing system.
I don't think my question is very relevant to others, unless by checking the link it's estabilished that there is a bug indeed. Otherwise I would like to know where the problem arises.
Aucun commentaire:
Enregistrer un commentaire