Given a string of words and numbers. Extract the expression including: 
1. the operator: either addition or subtraction
2. the two numbers that we are operating on

Return the result of the calculation.

Example:

"Panda has 48 apples and loses 4" returns 44

"Jerry has 34 apples and gains 6" returns 40

"loses" and "gains" are the only two words describing operators.

Should be a nice little kata for you :)

Note:
No fruit debts nor bitten apples = The numbers are integers and no negatives

Difficulty: introductory

Starter code:

def calculate(string):
	

Solutions:
def calculate(s):
    x=[int(i) for i in s.split() if i.isdigit()]
    return sum(x) if 'gains' in s.split() else x[0]-x[1]

import re
def calculate(string):
    x,y=re.findall('\d+',string)
    if re.search('loses', string): s= int(x)-int(y)
    if re.search('gains', string): s= int(x)+int(y)
    return s

import re

def calculate(string):
    a, b = map(int, re.findall(r'\d+', string))
    if 'lose' in string:
        return a - b
    else:
        return a + b

import re
import operator


def calculate(s):
    match_res = re.match(r'.+\s(\d+)\s.+\s(loses|gains)\s(\d+)\Z', s.strip())
    try:
        return {
        'loses': operator.sub,
        'gains': operator.add,
        }[match_res.group(2)](int(match_res.group(1)), int(match_res.group(3)))
    except AttributeError:
        raise ValueError('string do not obey sepcific')

import re
def calculate(string):
    return eval(re.sub("[^\d+-]", "", string.replace("loses", "-").replace("gains", "+")) )

from re import findall

def calculate(s):
    op1, op2 = map(int, findall('(\d+)', s))
    return op1 + op2 if 'gains' in s else op1 - op2

from operator import add, sub


def calculate(s):
    nums = []
    op = None
    for word in s.split():
        if word.isdigit():
            nums.append(int(word))
        elif word in ('gains', 'loses'):
            op = add if word == 'gains' else sub
    return op(*nums)


calculate=lambda s: eval("".join([a for a in s.replace("loses","-").replace("gains","+") if a in "0123456789+-"]))

def calculate(string):
    numbers = [int(x) for x in string.split() if x.isdigit()]
    return numbers[0]+numbers[1] if 'gains' in string else numbers[0]-numbers[1]

import re

def calculate(string):
    x, y = [int(n) for n in re.findall(r'\d+', string)]
    return x - y + y * 2 * ('gains' in string)