ICC-P Corrigé
Code:
from dataclasses import dataclass
print("-------------------")
print("--- EXERCICE 2 ---")
print("-------------------")
print()
small: list[float] = [1.4, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0, 141]
large: list[float] = [1.6, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, 10.1]
### 2 (a) - 2pts
def smaller_length(list1: list[float], list2: list[float]) -> int:
l1 = len(list1)
l2 = len(list2)
if l1 < l2:
return l1
return l2
# alternative:
# return l1 if l1 < l2 else l2
def smaller_length2(list1: list[float], list2: list[float]) -> int:
return min(len(list1), len(list2))
### 2 (a) - TEST
# Décommentez pour tester
print(f"Smaller length: {smaller_length(small, large)}")
# Doit afficher: Smaller length: 10
### 2 (b) - 4pts
def count_mismatches(small: list[float], large: list[float]) -> int:
mismatches = 0
for i in range(smaller_length(small, large)):
if small[i] > large[i]:
mismatches += 1
return mismatches
def count_mismatches2(small: list[float], large: list[float]) -> int:
return sum(map(lambda v: v[0] > v[1], zip(small, large)))
### 2 (b) - TEST
# Décommentez pour tester
print(f"Nb. mismatches: {count_mismatches(small, large)}")
# Doit afficher: Nb. mismatches: 1
print()
print("-------------------")
print("--- EXERCICE 3 ---")
print("-------------------")
print()
tictactoe_board = [
["x", " ", "x"],
["x", "o", " "],
["x", "o", " "],
["o", "x", "o"],
]
### 3 (a) - 4pts
def is_data_valid(board: list[list[str]]) -> bool:
num_rows = len(board)
if num_rows < 3:
return False
num_cols = len(board[0])
if num_cols < 3:
return False
for i in range(1, num_rows):
if len(board[i]) != num_cols:
return False
return True
### 3 (a) - TEST
# Décommentez pour tester
print(f"Data valid: {is_data_valid(tictactoe_board)}")
# Doit afficher: Data valid: True
invalid_board = tictactoe_board.copy()
invalid_board.append(["x"])
print(f"Data valid: {is_data_valid(invalid_board)}")
# Doit afficher: Data valid: False
### 3 (b) - 5pts
def is_board_consistent(board: list[list[str]]) -> bool:
x_count = 0
o_count = 0
for row in board:
for cell in row:
if cell == "x":
x_count += 1
elif cell == "o":
o_count += 1
return abs(x_count - o_count) <= 1
### 3 (b) - TEST
# Décommentez pour tester
print(f"Board consistent: {is_board_consistent(tictactoe_board)}")
# Doit afficher: Board consistent: True
inconsistent_board = tictactoe_board.copy()
inconsistent_board[0] = ["x", "x", "x"]
print(f"Board consistent: {is_board_consistent(inconsistent_board)}")
# Doit afficher: Board consistent: False
### 3 (c) - 8pts
def is_winner(board: list[list[str]], player: str) -> bool:
for i in range(len(board[0])):
n = 0
for j in range(len(board)):
if board[j][i] == player:
n += 1
if n == 3:
return True
else:
n = 0
return False
### 3 (c) - TEST
# Décommentez pour tester
print(f"'x' wins: {is_winner(tictactoe_board, 'x')}")
# Doit afficher: 'x' wins: True
print(f"'o' wins: {is_winner(tictactoe_board, 'o')}")
# Doit afficher: 'o' wins: False
print()
print("-------------------")
print("--- EXERCICE 4 ---")
print("-------------------")
print()
### 4 (a) - 3pts
@dataclass
class LoanRequest:
owner: str
amount: int
requests: list[LoanRequest] = [
LoanRequest("Alice", 75),
LoanRequest("Bob", 250),
LoanRequest("Charlie", 85),
LoanRequest("Diana", 80),
LoanRequest("Eve", 100),
]
### 4 (b) - 4pts
def total_requested_amount(requests: list[LoanRequest]) -> int:
total = 0
for request in requests:
total += request.amount
return total
def total_requested_amount2(requests: list[LoanRequest]) -> int:
return sum(request.amount for request in requests)
### 4 (b) - TEST
# Décommentez pour tester
print(f"Total amount: {total_requested_amount(requests)}")
# Doit afficher: Total amount: 590
### 4 (c) - 8pts
best_total = 0
best_omitted = -1
limit = 500
for omitted in range(len(requests)):
total = 0
for i, request in enumerate(requests):
if i != omitted:
total += request.amount
# alternative:
# total = sum(p.amount for i, p in enumerate(requests) if i != omitted)
if total > best_total and total <= limit:
best_total = total
best_omitted = omitted
print(f"Total amount of selected requests: {best_total}")
print(f"Rejecting request from: {requests[best_omitted].owner}")
print("-------------------")
print("--- EXERCICE 2 ---")
print("-------------------")
print()
small: list[float] = [1.4, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0, 141]
large: list[float] = [1.6, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, 10.1]
### 2 (a) - 2pts
def smaller_length(list1: list[float], list2: list[float]) -> int:
l1 = len(list1)
l2 = len(list2)
if l1 < l2:
return l1
return l2
# alternative:
# return l1 if l1 < l2 else l2
def smaller_length2(list1: list[float], list2: list[float]) -> int:
return min(len(list1), len(list2))
### 2 (a) - TEST
# Décommentez pour tester
print(f"Smaller length: {smaller_length(small, large)}")
# Doit afficher: Smaller length: 10
### 2 (b) - 4pts
def count_mismatches(small: list[float], large: list[float]) -> int:
mismatches = 0
for i in range(smaller_length(small, large)):
if small[i] > large[i]:
mismatches += 1
return mismatches
def count_mismatches2(small: list[float], large: list[float]) -> int:
return sum(map(lambda v: v[0] > v[1], zip(small, large)))
### 2 (b) - TEST
# Décommentez pour tester
print(f"Nb. mismatches: {count_mismatches(small, large)}")
# Doit afficher: Nb. mismatches: 1
print()
print("-------------------")
print("--- EXERCICE 3 ---")
print("-------------------")
print()
tictactoe_board = [
["x", " ", "x"],
["x", "o", " "],
["x", "o", " "],
["o", "x", "o"],
]
### 3 (a) - 4pts
def is_data_valid(board: list[list[str]]) -> bool:
num_rows = len(board)
if num_rows < 3:
return False
num_cols = len(board[0])
if num_cols < 3:
return False
for i in range(1, num_rows):
if len(board[i]) != num_cols:
return False
return True
### 3 (a) - TEST
# Décommentez pour tester
print(f"Data valid: {is_data_valid(tictactoe_board)}")
# Doit afficher: Data valid: True
invalid_board = tictactoe_board.copy()
invalid_board.append(["x"])
print(f"Data valid: {is_data_valid(invalid_board)}")
# Doit afficher: Data valid: False
### 3 (b) - 5pts
def is_board_consistent(board: list[list[str]]) -> bool:
x_count = 0
o_count = 0
for row in board:
for cell in row:
if cell == "x":
x_count += 1
elif cell == "o":
o_count += 1
return abs(x_count - o_count) <= 1
### 3 (b) - TEST
# Décommentez pour tester
print(f"Board consistent: {is_board_consistent(tictactoe_board)}")
# Doit afficher: Board consistent: True
inconsistent_board = tictactoe_board.copy()
inconsistent_board[0] = ["x", "x", "x"]
print(f"Board consistent: {is_board_consistent(inconsistent_board)}")
# Doit afficher: Board consistent: False
### 3 (c) - 8pts
def is_winner(board: list[list[str]], player: str) -> bool:
for i in range(len(board[0])):
n = 0
for j in range(len(board)):
if board[j][i] == player:
n += 1
if n == 3:
return True
else:
n = 0
return False
### 3 (c) - TEST
# Décommentez pour tester
print(f"'x' wins: {is_winner(tictactoe_board, 'x')}")
# Doit afficher: 'x' wins: True
print(f"'o' wins: {is_winner(tictactoe_board, 'o')}")
# Doit afficher: 'o' wins: False
print()
print("-------------------")
print("--- EXERCICE 4 ---")
print("-------------------")
print()
### 4 (a) - 3pts
@dataclass
class LoanRequest:
owner: str
amount: int
requests: list[LoanRequest] = [
LoanRequest("Alice", 75),
LoanRequest("Bob", 250),
LoanRequest("Charlie", 85),
LoanRequest("Diana", 80),
LoanRequest("Eve", 100),
]
### 4 (b) - 4pts
def total_requested_amount(requests: list[LoanRequest]) -> int:
total = 0
for request in requests:
total += request.amount
return total
def total_requested_amount2(requests: list[LoanRequest]) -> int:
return sum(request.amount for request in requests)
### 4 (b) - TEST
# Décommentez pour tester
print(f"Total amount: {total_requested_amount(requests)}")
# Doit afficher: Total amount: 590
### 4 (c) - 8pts
best_total = 0
best_omitted = -1
limit = 500
for omitted in range(len(requests)):
total = 0
for i, request in enumerate(requests):
if i != omitted:
total += request.amount
# alternative:
# total = sum(p.amount for i, p in enumerate(requests) if i != omitted)
if total > best_total and total <= limit:
best_total = total
best_omitted = omitted
print(f"Total amount of selected requests: {best_total}")
print(f"Rejecting request from: {requests[best_omitted].owner}")
Last modified: Monday, 22 April 2024, 14:12