mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-07 10:50:36 +00:00
Add university content
This commit is contained in:
62
university/4.2.2.py
Normal file
62
university/4.2.2.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import random as rng
|
||||
|
||||
|
||||
def random_int(start: int, end: int) -> int:
|
||||
return rng.randint(start, end)
|
||||
|
||||
|
||||
start = 0
|
||||
end = 100
|
||||
number = random_int(start, end)
|
||||
|
||||
|
||||
def is_number(guess) -> int:
|
||||
if number == guess:
|
||||
return 0
|
||||
elif number < guess:
|
||||
return 1
|
||||
else:
|
||||
return -1
|
||||
|
||||
|
||||
def dumb_ai(start: int, end: int) -> int:
|
||||
tries = 0
|
||||
while True:
|
||||
guess = rng.randint(start, end)
|
||||
tries += 1
|
||||
if is_number(guess) == 0:
|
||||
return tries
|
||||
|
||||
|
||||
def guess_number(start: int, end: int) -> int:
|
||||
tries = 0
|
||||
while True:
|
||||
guess = (start + end) // 2
|
||||
|
||||
tries += 1
|
||||
hint = is_number(guess)
|
||||
if hint == 0:
|
||||
return tries
|
||||
elif hint == -1:
|
||||
start = guess + 1
|
||||
else:
|
||||
end = guess - 1
|
||||
|
||||
|
||||
def main(start, end) -> int:
|
||||
print(f"Number to guess: {number}")
|
||||
return guess_number(start, end)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = int(input("Number of games: "))
|
||||
tries_global = list()
|
||||
for _ in range(n):
|
||||
tries_smart = main(start, end)
|
||||
tries_dumb = dumb_ai(start, end)
|
||||
print(tries_dumb, tries_smart)
|
||||
print("*" * tries_smart)
|
||||
print("-" * tries_dumb)
|
||||
tries_global.append(tries_smart)
|
||||
|
||||
print(f"{sum(tries_global) / len(tries_global)}")
|
||||
76
university/4.2.3.py
Normal file
76
university/4.2.3.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
LOW, HIGH = 0, 100
|
||||
N_VALUES = HIGH - LOW + 1
|
||||
N_TRIALS = 1000
|
||||
|
||||
|
||||
def smart_ai(secret, low=LOW, high=HIGH):
|
||||
c = 0
|
||||
lo, hi = low, high
|
||||
while lo <= hi:
|
||||
c += 1
|
||||
mid = (lo + hi) // 2
|
||||
if mid == secret:
|
||||
return c
|
||||
elif mid < secret:
|
||||
lo = mid + 1
|
||||
else:
|
||||
hi = mid - 1
|
||||
return c
|
||||
|
||||
|
||||
def dumb_ai(secret, low=LOW, high=HIGH):
|
||||
c = 0
|
||||
remaining = list(range(low, high + 1))
|
||||
while True:
|
||||
c += 1
|
||||
g = random.choice(remaining)
|
||||
if g == secret:
|
||||
return c
|
||||
remaining.remove(g)
|
||||
|
||||
|
||||
def simulate(n_trials=N_TRIALS):
|
||||
means_smart = []
|
||||
means_dumb = []
|
||||
for secret in range(LOW, HIGH + 1):
|
||||
s_sum = 0
|
||||
d_sum = 0
|
||||
for _ in range(n_trials):
|
||||
s_sum += smart_ai(secret)
|
||||
d_sum += dumb_ai(secret)
|
||||
means_smart.append(s_sum / n_trials)
|
||||
means_dumb.append(d_sum / n_trials)
|
||||
return means_smart, means_dumb
|
||||
|
||||
|
||||
def print_text_hist(means_smart, means_dumb):
|
||||
for i, (ms, md) in enumerate(zip(means_smart, means_dumb)):
|
||||
print(f"[{i}] " + "*" * int(round(ms)))
|
||||
print(f"[{i}] " + "-" * int(round(md)))
|
||||
|
||||
|
||||
def plot_curves(means_smart, means_dumb):
|
||||
x = list(range(LOW, HIGH + 1))
|
||||
plt.figure(figsize=(10, 5))
|
||||
plt.plot(x, means_smart, label="IA intelligente (dichotomie)")
|
||||
plt.plot(x, means_dumb, label="IA aléatoire (sans remise)")
|
||||
plt.xlabel("Nombre secret")
|
||||
plt.ylabel("Nombre moyen d'essais")
|
||||
plt.title(
|
||||
f"Moyenne des essais pour chaque nombre secret ({N_TRIALS} parties/valeur)"
|
||||
)
|
||||
plt.legend()
|
||||
plt.grid(True)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
means_smart, means_dumb = simulate()
|
||||
print_text_hist(means_smart, means_dumb)
|
||||
plot_curves(means_smart, means_dumb)
|
||||
print(f"Moyenne globale IA intelligente ≈ {sum(means_smart) / N_VALUES:.2f}")
|
||||
print(f"Moyenne globale IA aléatoire ≈ {sum(means_dumb) / N_VALUES:.2f}")
|
||||
44
university/4.2.py
Normal file
44
university/4.2.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import random as rng
|
||||
|
||||
|
||||
def create_number(start: int, end: int) -> int:
|
||||
return rng.randint(start, end)
|
||||
|
||||
|
||||
def main_part(start: int, end: int, max_tries: int) -> (bool, int):
|
||||
number_to_guess = create_number(start, end)
|
||||
guess = int(input("Entrez votre nombre: "))
|
||||
tries = 1
|
||||
while guess != number_to_guess and tries < max_tries:
|
||||
if guess < number_to_guess:
|
||||
print(f"{guess} est trop petit")
|
||||
start = max(guess, start)
|
||||
else:
|
||||
print(f"{guess} est trop grand")
|
||||
end = min(guess, end)
|
||||
tries += 1
|
||||
guess = int(input(f"Entrez votre nombre [{start};{end}]: "))
|
||||
|
||||
if guess == number_to_guess:
|
||||
print(
|
||||
f"Félicitation vous avez correctement deviner le nombre {number_to_guess} au bout de {tries}"
|
||||
)
|
||||
return True, tries
|
||||
else:
|
||||
print("Perdu")
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
start = 0
|
||||
end = 100
|
||||
max_tries = 5
|
||||
again = True
|
||||
played_game = 1
|
||||
game_won = 0
|
||||
stats = []
|
||||
while again:
|
||||
game = main_part(start, end, max_tries)
|
||||
game_won += 1 if game[0] else 0
|
||||
stats.append(game[1])
|
||||
again = "y" == input("Voulez vous recommencez ? (y/n)")
|
||||
48
university/fibibi.py
Normal file
48
university/fibibi.py
Normal file
@@ -0,0 +1,48 @@
|
||||
def fibo(n: int, mem=None) -> int:
|
||||
if mem is None:
|
||||
mem = {}
|
||||
if n <= 1:
|
||||
return n
|
||||
if n in mem:
|
||||
return mem[n]
|
||||
mem[n] = fibo(n - 1, mem) + fibo(n - 2, mem)
|
||||
return mem[n]
|
||||
|
||||
|
||||
memo = {1: [1]}
|
||||
|
||||
|
||||
def syracuse(n):
|
||||
if n in memo:
|
||||
return memo[n]
|
||||
if n % 2 == 0:
|
||||
seq = [n] + syracuse(n // 2)
|
||||
else:
|
||||
seq = [n] + syracuse(3 * n + 1)
|
||||
memo[n] = seq
|
||||
return seq
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
|
||||
n = int(input("n: "))
|
||||
start = time.time()
|
||||
print(fibo(n))
|
||||
print(len(str((fibo(n)))))
|
||||
end = time.time()
|
||||
print(end - start)
|
||||
|
||||
|
||||
def fibo_fast(n: int) -> int:
|
||||
def _fd(k: int) -> tuple[int, int]:
|
||||
if k == 0:
|
||||
return (0, 1)
|
||||
a, b = _fd(k // 2)
|
||||
c = a * ((b << 1) - a)
|
||||
d = a * a + b * b
|
||||
if k & 1:
|
||||
return (d, c + d)
|
||||
return (c, d)
|
||||
|
||||
return _fd(n)[0]
|
||||
Reference in New Issue
Block a user