Untitled
unknown
python
a year ago
1.1 kB
4
Indexable
def count_faults(n, logs):
"""Counts the number of times a faulty server was replaced.
Args:
n: The number of servers.
logs: An array of strings where each element is a log message in the format "<server_id> <success/error>".
Returns:
The number of times a faulty server was replaced.
"""
server_statuses = {i: [] for i in range(1, n + 1)} # Initialize dictionary to store server statuses
replacements = 0
for log in logs:
server_id, status = log.split()
server_statuses[int(server_id)].append(status)
# Check if the last 3 statuses for the server are errors
if len(server_statuses[int(server_id)]) >= 3 and all(
s == "error" for s in server_statuses[int(server_id)][-3:]
):
replacements += 1
server_statuses[int(server_id)] = [status] # Reset server status after replacement
return replacements
# Example usage
n = 2
logs = ["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]
replacements = count_faults(n, logs)
print(replacements) # Output: 1
Editor is loading...
Leave a Comment