Mathématiques en BCPST2

.

Python en BCPST2

Oral G2E 2024

(Sujet)


## 1 Somme

def est_valide(ch):
    if ch[0] == '+' or ch[-1]=='+':
        return False
    n = len(ch)
    for i in range(n):
        if ch[i]=='+':
            if ch[i+1]=='+':
                return False
        else:
            if not ch[i].isdigit():
                return False
    return True

## 2 Evaluation

def evalue(ch):
    l = ch.split('+')
    s = 0
    for x in l:
        s = s+int(x)
    return s

## 3 Liste d'additions

def plus_grande_somme(l):
    ll = [evalue(x) for x in l]
    return max(ll)

## 4 Expressions arithmétiques

def calcule(a,b,op):
    if op == '+':
        return a+b
    if op == '-':
        return a-b
    if op == '*':
        return a*b
    if op == '/':
        return a//b

## 5 Evaluation

def evaluation(l):
    if len(l)==1: return l[0]
    else:
        a,op,b = l.pop(0), l.pop(0), l.pop(0)
        l = [calcule(a,b,op)] + l
        return evaluation(l)

def evaluation2(l):
    r=l[0]
    n = len(l)
    i=1
    while i<n:
        r = calcule(r,l[i+1],l[i])
        i=i+2
    return r

## 6 Expression postfixee

def postfix(l):
    ope = ['+','-','*','/']
    pile = []
    for x in l:
        if x in ope:
            b,a = pile.pop(),pile.pop()
            pile.append(calcule(a,b,x))
        else:
            pile.append(x)
    return pile[0]

## 7 Verification d'erreur

def postfix(l):
    ope = ['+','-','*','/']
    pile = []
    for x in l:
        if x in ope:
            if len(pile) <2: return 'ERR'
            b,a = pile.pop(),pile.pop()
            if x=='/' and b==0: return 'ERR'
            pile.append(calcule(a,b,x))
        else:
            pile.append(x)
    if len(pile)!=1: return 'ERR'
    return pile[0]

Archives

Ces documents ne concernent pas mes étudiants actuels, mais peuvent continuer d'intéresser certaines personnes.