You are given a list of directions in the form of a list:

goal = ["N", "S", "E", "W"]

Pretend that each direction counts for 1 step in that particular direction.

Your task is to create a function called directions, that will return a reduced list that will get you to the same point.The order of directions must be returned as N then S then E then W.

If you get back to beginning, return an empty array.

Difficulty: introductory

Starter code:

def directions(goal):
	

Solutions:
def directions(goal):
    y = goal.count("N") - goal.count("S")
    x = goal.count("E") - goal.count("W")
    
    return ["N"] * y + ["S"] * (-y) + ["E"] * x + ["W"] * (-x)

def directions(goal):
    count = lambda s: len([c for c in goal if c == s])
    ns = 1 * count("N") - 1 * count("S")
    we = 1 * count("W") - 1 * count("E")
    ns = [("N" if ns > 0 else "S")] * abs(ns)
    we = [("W" if we > 0 else "E")] * abs(we)
    return ns + we


from collections import Counter


def directions(goal):
    c = Counter(goal)
    c["N"], c["W"] = c["N"] - c["S"], c["W"] - c["E"]
    c["S"], c["E"] = -c["N"], -c["W"]
    return sorted(c.elements(), key="NSEW".index)

from collections import Counter

def directions(goal):
    c = Counter(goal)
    for a, b in ['NS', 'EW']:
        m = min(c[a], c[b])
        c[a] -= m
        c[b] -= m
    return sorted(c.elements(), key='NSEW'.find)

def directions(goal):
    MOVES = {'N': [0,1], 'S': [0,-1], 'E': [1,1], 'W': [1,-1]}
    g = [0,0]
    for d in goal: g[MOVES[d][0]] += MOVES[d][1]
    return ["SN"[g[0]>=0]] * abs(g[0]) + ["WE"[g[1]>=0]] * abs(g[1])

def directions(goal):
    y=sum(1 if d=='N' else -1 if d=='S' else 0 for d in goal)
    x=sum(1 if d=='E' else -1 if d=='W' else 0 for d in goal)
    return list(('N' if y>0 else 'S')*abs(y) + ('E' if x>0 else 'W')*abs(x))

def directions(goal):

  y = goal.count('N') - goal.count('S')
  x = goal.count('E') - goal.count('W')
  
  ns = 'N' if y > 0 else 'S'
  ew = 'E' if x > 0 else 'W'
  
  return [_ for _ in abs(y)*ns + abs(x)*ew]

def directions(goal):
    while "S" in goal and "N" in goal:
        goal.remove("N")
        goal.remove("S")
    while "W" in goal and "E" in goal:
        goal.remove("W")
        goal.remove("E")
    lookup=['N','S', 'E', 'W']
    key_func=lambda s: (lookup.index(s[0]),s) if s[0] in lookup else (len(lookup),s)
    return sorted(goal, key=key_func)

def directions(goal):
    ans=[]
    ns = goal.count('N') - goal.count('S')
    if ns>0: ans.extend(ns*['N'])
    if ns<0: ans.extend(-ns*['S'])
    ew = goal.count('E') - goal.count('W')
    if ew>0: ans.extend(ew*['E'])
    if ew<0: ans.extend(-ew*['W'])
    return ans

def directions(goal):
#     print(goal)
    a={i:goal.count(i) for i in set(goal)}
    print(a)
    b=[]
#     print(list(a.keys()) )
    if  'N' in list(a.keys()) and 'S'  in list(a.keys() ):
#         print('a.keys()',a.keys())
        if a['N']>a['S']:
            ns=a['N']-a['S']
            b.extend(ns*'N')
        elif a['N']<a['S']:
            sn=a['S']-a['N']
            b.extend(sn*'S')
        elif a['N']==a['S']:
            sn=a['S']-a['N']
        
    elif 'N' in list(a.keys()):
#         print(a['N'])   
        b.extend(a['N']*str('N'))
    elif 'S' in list(a.keys()):
#         print(a['S'])  
        b.extend(a['S']*str('S'))
    #Code goes here! :)
    if  'E' in list(a.keys()) and 'W'  in list(a.keys() ):
#         print('a.keys()',a.keys())
        if a['E']>a['W']:
            ew=a['E']-a['W']
            print(('ew',ew))
            b.extend(ew*str('E'))
        elif a['E']<a['W']:
            we=a['W']-a['E']   
            print(('we',we))
            b.extend(we*str('W'))
    elif 'W' in list(a.keys()):
        print((a['W']))   
        b.extend(a['W']*str('W'))
    elif 'E' in list(a.keys()):
        print((a['E']))       
        b.extend(a['E']*str('E'))
    return b