You will be given a fruit which a farmer has harvested, your job is to see if you should buy or sell.

You will be given 3 pairs of fruit. Your task is to trade your harvested fruit back into your harvested fruit via the intermediate pair, you should return a string of 3 actions.

if you have harvested apples, you would buy this fruit pair: apple_orange, if you have harvested oranges, you would sell that fruit pair.

(In other words, to go from left to right (apples to oranges) you buy, and to go from right to left you sell (oranges to apple))

e.g. 
apple_orange, orange_pear, apple_pear
1. if you have harvested apples, you would buy this fruit pair: apple_orange
2. Then you have oranges, so again you would buy this fruit pair: orange_pear
3. After you have pear, but now this time you would sell this fruit pair: apple_pear
4. Finally you are back with the apples

So your function would return a list: [“buy”,”buy”,”sell”]

If any invalid input is given, "ERROR" should be returned

Difficulty: introductory

Starter code:

def buy_or_sell(pairs, harvested_fruit):
	

Solutions:
def buy_or_sell(pairs, harvested_fruit):
    
    currentFruit = harvested_fruit
    actions = list()
    
    for pair in pairs:
    
        if currentFruit not in pair: return 'ERROR'
    
        if currentFruit == pair[0]:
        
            actions.append('buy')
            currentFruit = pair[1]
            
        else:
        
            actions.append('sell')
            currentFruit = pair[0]
            
    return actions

def buy_or_sell(pairs, fruit):
    result = []
    for pair in pairs:
        if pair[0] == fruit:
            result.append("buy")
            fruit = pair[1]
        elif pair[1] == fruit:
            result.append("sell")
            fruit = pair[0]
        else:
            return 'ERROR'
    return result

def buy_or_sell(pairs, fruit):
    res = []
    for b, s in pairs:
        if fruit == b:
            fruit = s
            res.append("buy")
        elif fruit == s:
            fruit = b
            res.append("sell")
        else:
            return "ERROR"
    return res

def buy_or_sell(pairs, fruit):
    def trade(pair):
        nonlocal fruit
        ret = ['buy', 'sell'][pair.index(fruit)]
        fruit = (set(pair) - set([fruit])).pop()
        return ret
    try:
        return [trade(pair) for pair in pairs]
    except ValueError:
        return 'ERROR'

def buy_or_sell(p, hf):
    act=[]
    for f in p:
        if hf not in f:
            return "ERROR"
        elif hf==f[0]:
            act.append("buy")
            hf=f[1]
        else:
            act.append("sell")
            hf=f[0]
    return act

def buy_or_sell(pairs, want):
    r = []
    for buy, sell in pairs:
        if want not in [buy, sell]: return 'ERROR'
        r, want = (r + ['buy'], sell) if buy == want else (r + ['sell'], buy)
            
    return r  

def buy_or_sell(pairs, harvested_fruit):
    result = []
    fruit = harvested_fruit
    for a, b in pairs:
        if fruit == a:
            result.append('buy')
            fruit = b
        elif fruit == b:
            result.append('sell')
            fruit = a
        else:
            return 'ERROR'
    if fruit != harvested_fruit:
        return 'ERROR'
    return result


def buy_or_sell(pairs,f):
    out=[]
    for pair in pairs:
        if pair[0]==f:
            f=pair[1]
            out.append("buy")
        elif pair[1]==f:
            f=pair[0]
            out.append("sell")
        else:return "ERROR"
    return out

def buy_or_sell(pairs, harvested_fruit):
    fruit = harvested_fruit
    acts = []
    try:
        for pair in pairs:
            act = pair.index(fruit)
            acts.append(["buy", "sell"][act])
            fruit = pair[1 - act]
        return acts
    except:
        return "ERROR"

def buy_or_sell(pairs, harvested_fruit):
    result= []
    for i in pairs:
        if i[0] == harvested_fruit:
            result.append("buy")
            harvested_fruit = i[1]
        elif i[1] == harvested_fruit:
            result.append("sell")
            harvested_fruit = i[0]
        else:
            return "ERROR"
    return result