Proposition de corrigé du midterm pour la partie programmation
Notez que beaucoup d'autres solutions que strictement ce qui est proposé ci-dessous ont pu obtenir des points (surtout pour le 20)!
Exercice 19:
def is_in_unit_disc(x: float, y: float) -> bool:
r2 = x * x + y * y
if r2 < 1:
return True
else:
return False
# plus concis:
def is_in_unit_disc(x: float, y: float) -> bool:
return x * x + y * y < 1
def simulate_one() -> bool:
x = rand()
y = rand()
if is_in_unit_disc(x, y):
return True
else:
return False
# plus concis:
def simulate_one() -> bool:
return is_in_unit_disc(x=rand(), y=rand())
def estimate_pi(num: int) -> float:
count = 0
for _ in range(num):
if simulate_one():
count += 1
p = count / num
return 4 * p
# plus concis, même si on n'a pas encore appris à écrire ceci:
def estimate_pi(num: int) -> float:
return sum(simulate_one() for _ in range(num)) / num * 4
r2 = x * x + y * y
if r2 < 1:
return True
else:
return False
# plus concis:
def is_in_unit_disc(x: float, y: float) -> bool:
return x * x + y * y < 1
def simulate_one() -> bool:
x = rand()
y = rand()
if is_in_unit_disc(x, y):
return True
else:
return False
# plus concis:
def simulate_one() -> bool:
return is_in_unit_disc(x=rand(), y=rand())
def estimate_pi(num: int) -> float:
count = 0
for _ in range(num):
if simulate_one():
count += 1
p = count / num
return 4 * p
# plus concis, même si on n'a pas encore appris à écrire ceci:
def estimate_pi(num: int) -> float:
return sum(simulate_one() for _ in range(num)) / num * 4
def count_syllables(word: str) -> int:
vowels = {"a", "e", "i", "o", "u", "y"}
count = 0
last_was_vowel = False
last_index = len(word) - 1
for i in range(len(word)):
is_vowel = word[i] in vowels
if is_vowel and not last_was_vowel and not (i == last_index and word[i] == 'e'):
count += 1
last_was_vowel = is_vowel
if count == 0:
return 1
return count
classified: Dict[int, List[str]] = {}
for word in words:
syllables = count_syllables(word)
if syllables not in classified:
classified[syllables] = []
classified[syllables].append(word)
vowels = {"a", "e", "i", "o", "u", "y"}
count = 0
last_was_vowel = False
last_index = len(word) - 1
for i in range(len(word)):
is_vowel = word[i] in vowels
if is_vowel and not last_was_vowel and not (i == last_index and word[i] == 'e'):
count += 1
last_was_vowel = is_vowel
if count == 0:
return 1
return count
classified: Dict[int, List[str]] = {}
for word in words:
syllables = count_syllables(word)
if syllables not in classified:
classified[syllables] = []
classified[syllables].append(word)
Modifié le: lundi, 14 novembre 2022, 15:31