물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Take a Ten Minutes Walk

BEstyle 2022. 9. 26. 12:49

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 for each letter (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.


def is_valid_walk(walk):
    #determine if walk is valid
    x=0
    y=0
    
    if len(walk)!=10:
        print(len(walk))
        return False
    for letter in walk:
        if letter == 'n':
            y+=1
        elif letter == 's':
            y-=1
        elif letter == 'w':
            x-=1
        elif letter == 'e':
            x+=1
    print(x,y)
    if x==0 and y==0:
        return True
    return False

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Break camelCase  (1) 2022.09.26
Count the smiley faces!  (1) 2022.09.26
Detect Pangram  (0) 2022.09.26
Build a pile of Cubes  (0) 2022.09.26
Two Sum  (0) 2022.09.23