Exercice 1.¶

In [ ]:
words = []

word=input("Entrez un mot(un blanc pour quitter ):")
while word != "":
      words.append(word)
      word=input("Entrez un mot(un blanc pour quitter ):")

print(set(words))

Exercice 2.¶

In [ ]:
from random import randrange
NUM_RUNS = 1000
D_MAX = 6
## Simulate rolling two six-sided dice
# @return the total from rolling two simulated dice
def twoDice():
  # Simulate two dice
  d1 = randrange(1, D_MAX + 1)
  d2 = randrange(1, D_MAX + 1) 
  # Return the total
  return d1 + d2
In [ ]:
def main():
  # Create a dictionary of expected proportions
  expected = {2: 1/36, 3: 2/36, 4: 3/36, 5: 4/36, 6: 5/36, 7: 6/36, 8: 5/36, 9: 4/36, 10: 3/36, 11: 2/36, 12: 1/36}
  # Create a dictionary that maps from the total of two dice to the number of occurrences
  counts = {2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0}
  for i in range(NUM_RUNS):
    t = twoDice()
    counts[t] = counts[t] + 1
  
  # Display the simulated proportions and the expected proportions
  print("Total Simulated Expected")
  print("Percent Percent")
  for i in sorted(counts.keys()):
    print("%5d %11.2f %8.2f" % (i, counts[i] / NUM_RUNS * 100, expected[i] * 100))
    #print((i, counts[i] / NUM_RUNS * 100, expected[i] * 100))
In [ ]:
main()
In [ ]:
def characterCounts(s):
    # Create a new, empty dictionary
    counts = {}
    # Update the count for each character in the string
    for ch in s:
        if ch in counts:
            counts[ch] = counts[ch] + 1
        else:
            counts[ch] = 1
    # Return the result
    return counts
In [ ]:
s1 = input("Enter the first string: ")
In [ ]:
s2 = input("Enter the second string: ")
In [ ]:
# Get the character counts for each string
counts1 = characterCounts(s1)
counts2 = characterCounts(s2)
In [ ]:
# Display the result
if counts1 == counts2:
    print("Those strings are anagrams.")
else:
    print("Those strings are not anagrams.")
In [ ]:
print(counts1)
print(counts2)
In [ ]:
def palindrome(mot):
    if (mot == mot[::-1]):
        return("Ce mot est un palindrome")
    else:
        return ("Ce mot n'est pas un palindrome")


#Teste avec une lettre
print(palindrome("aliaila"))

Exercice (approximation d'une racine carée)¶

In [ ]:
def sqRoot(a, p):
    """
     cette fonction approche la racine carrée de a avec une précision p
    """
    x = a / 2
    val = True

    while val:
        b = 1 / 2 * (x + (a / x))
        error = abs((b - x) / b)
        if error <= p:
            val = False
        x = b
    return x

Exercice (jour de naissance)¶

In [ ]:
def isleap(a) :
    if a % 4 == 0:
       if a % 100 == 0 :
           if a % 400 == 0 : 
               return True
           else :
               return False
       else :
           return True 
    else :
        return False 
In [ ]:
leap(1904)
In [ ]:
def verification(n) : 
    if(len(n)!=8):
        print("attention chaine de caractere doit etre > 8")
        return False
    
    DD = int(n[0:2])
    ##print(DD)
    MM = int(n[2:4])
    ##print(MM)
    YY = int(n[4:8])
    ##print(YY)
    if 1 <= DD <= 31 :
        if 1 <= MM <= 12 :
            if DD == 31 :
                if MM in [1, 3, 5, 7, 8, 10, 12] :
                    return True
                else :
                    return False
            elif MM==2:
                if DD > 29 :
                    return False
                elif DD==29:
                    return isleap(YY)
                else :
                    return True
            else:
                return True 
        else :
            return False
    else :
        return False
In [ ]:
def jour(n):
    if verification(n) : 
       day = int(n[0:2])
       month = int(n[2:4])
       year = int(n[4:8])
       x = 0
       y = 0
       exp = 0

       if (month == 1) or (month == 2):
          x = year - 1
          y = month + 12
       else:
          x = year
          y = month
       exp = day - 1 + (5 * x // 4) - (x // 100) + (x // 400) + (13 * (y + 1) // 5)
       final = exp % 7
       dic = {0:'dimanche', 1:'lundi', 2:'mardi', 3:'mercredi', 4:'jeudi', 5:'vendredi', 6:'samedi'}
       return dic[final]
    else:
       return "vérifier la date"
In [ ]:
jour("01111986") # petit test