Exercices sur les boucles

## Syracuse 0
n = int(input("Rang voulu ? "))
s = int(input("Graine ? "))

l=[s]

for _ in range(n):
    if s%2 == 0:
        s //=  2
    else:
        s *=3; s+=1
    l.append(s)
print(l)

## Syracuse 1

n = int(input("Rang voulu ? "))
s = int(input("Graine ? "))

for _ in range(n):
    if s%2==0:
        s = s//2
    else:
        s = 3*s+1
print("Le",n,'ieme terme de la suite est',s)

## Syracuse 2

s = int(input("Graine ? "))
cmpt = 0

while s != 1:
    if s%2==0:
        s = s//2
    else:
        s = 3*s+1
    cmpt = cmpt + 1
print('Le temps de vol est',cmpt)

## Syracuse 3

s = int(input("Graine ? "))

if s%2 == 0:
    print("Le temps de vol est 0")
else:
    s0 = s
    s = 3*s+1
    cmpt = 0    
    while s > s0:
        if s%2==0:
            s = s//2
        else:
            s = 3*s+1
        cmpt = cmpt + 1
    print('Le temps de vol en altitude est',cmpt)

## Syracuse 4

n = int(input("Rang voulu ? "))
s = int(input("Graine ? "))
m = s

for _ in range(n):
    if s%2==0:
        s = s//2
    else:
        s = 3*s+1
    if s>m:
        m = s
print("L'altitude maximale est",m)

## Syracuse 5

n = int(input('Graine max ? '))
m = 0
s0 = 0
for i in range(1,n+1):
    s= i
    cmpt=0
    while s != 1:
        if s%2==0:
            s = s//2
        else:
            s = 3*s+1
        cmpt = cmpt + 1
    if cmpt > m:
        m = cmpt
        s0 = i
print("Le temps de vol maximal est",m,"atteint pour",s0)

## Chaines 1

chaine = str(input("Chaine à renverser ?"))
reversed = ""

for lettre in chaine:
    reversed = lettre + reversed
    print(lettre,reversed)
print(reversed)

## Chaines 1
chaine = str(input("Chaine à renverser ?"))

print(chaine[::-1])

## Chaines 2
chaine = str(input("Chaine à tester ?"))
reversed = ""
for lettre in chaine:
    reversed = lettre + reversed

print(chaine == reversed)

## Chaine 3
chaine = str(input("Chaine à tester ?"))
car = str(input("Caractere a supprimer ?"))

supp = False

result = ""
for lettre in chaine:
    if lettre == car and not supp:
        supp = True
    else:
        result = result + lettre
if chaine == result:
    print(car,"n'est pas dans",chaine)
else:
    print(result)

## Chaine 4
chaine1 = str(input("Premiere chaine à tester ?"))
chaine2 = str(input("Seconde chaine à tester ?"))

anagramme = True

for car in chaine1:
    supp = False
    result = ""
    for lettre in chaine2:
        if lettre == car and not supp:
            supp = True
        else:
            result = result + lettre
    if chaine2 == result:
        anagramme = False
    else:
        chaine2 = result
print(anagramme and chaine2=='')