Skip to content
🤔prompts chat🧠
🔍
question:from typing import List, Tuple from datetime import datetime def detect_brute_force_attacks(logs: List[Tuple[str, str, str]], k: int, m: int) -> List[str]: Identifies IP addresses with `k` or more failed login attempts within any `m` minute window. Args: logs (List[Tuple[str, str, str]]): List of log entries as tuples (timestamp, IP, status). k (int): Number of failed attempts to consider as a brute force attack. m (int): Window of time in minutes within which the fails should be counted. Returns: List[str]: List of IP addresses. pass # Write your implementation here def test_single_ip_failed_attempts(): logs = [ ("2023-01-01 00:01:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:02:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:05:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:01:00", "192.168.1.2", "SUCCESS"), ("2023-01-01 00:01:30", "192.168.1.2", "FAIL"), ("2023-01-01 00:01:55", "192.168.1.2", "FAIL") ] assert detect_brute_force_attacks(logs, 3, 10) == ["192.168.1.1"] def test_no_ip_exceeds_failed_attempts(): logs = [ ("2023-01-01 00:01:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:02:00", "192.168.1.2", "FAIL"), ("2023-01-01 00:02:01", "192.168.1.3", "FAIL") ] assert detect_brute_force_attacks(logs, 2, 5) == [] def test_multiple_ips_exceed_failed_attempts(): logs = [ ("2023-01-01 00:01:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:02:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:03:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:01:00", "192.168.1.2", "FAIL"), ("2023-01-01 00:02:01", "192.168.1.2", "FAIL"), ("2023-01-01 00:03:02", "192.168.1.2", "FAIL") ] result = detect_brute_force_attacks(logs, 3, 10) assert "192.168.1.1" in result assert "192.168.1.2" in result def test_ignore_success_attempts(): logs = [ ("2023-01-01 00:01:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:02:00", "192.168.1.1", "FAIL"), ("2023-01-01 00:03:00", "192.168.1.1", "SUCCESS"), ("2023-01-01 00:03:30", "192.168.1.1", "FAIL"), ("2023-01-01 00:04:30", "192.168.1.1", "FAIL") ] assert detect_brute_force_attacks(logs, 3, 5) == ["192.168.1.1"]

answer:from datetime import datetime, timedelta from collections import defaultdict, deque def detect_brute_force_attacks(logs, k, m): Identifies IP addresses with `k` or more failed login attempts within any `m` minute window. Args: logs (list): List of log entries as tuples (timestamp, IP, status). k (int): Number of failed attempts to consider as a brute force attack. m (int): Window of time in minutes within which the fails should be counted. Returns: list: List of IP addresses. failed_attempts = defaultdict(deque) result = set() # Convert logs into tuples and sort by timestamp log_entries = [(datetime.strptime(log[0], "%Y-%m-%d %H:%M:%S"), log[1], log[2]) for log in logs] log_entries.sort() for timestamp, ip, status in log_entries: if status == "FAIL": attempt_window = failed_attempts[ip] # Add the current attempt to the queue attempt_window.append(timestamp) # Remove attempts older than the m-minute window while attempt_window and (timestamp - attempt_window[0]).total_seconds() > m * 60: attempt_window.popleft() # Check if the number of failed attempts within the window meets or exceeds k if len(attempt_window) >= k: result.add(ip) return list(result)

question:def is_beautiful_sequence(n: int, sequence: List[int]) -> str: Determines if the sequence is beautiful. A sequence is considered beautiful if the difference between consecutive integers is either 1 or -1. Parameters: n (int): Number of elements in the sequence. sequence (list of int): The sequence of integers. Returns: str: "BEAUTIFUL" if the sequence is beautiful, otherwise "NOT BEAUTIFUL". >>> is_beautiful_sequence(5, [1, 2, 3, 2, 1]) "BEAUTIFUL" >>> is_beautiful_sequence(4, [1, 3, 5, 7]) "NOT BEAUTIFUL"

answer:def is_beautiful_sequence(n, sequence): Determines if the sequence is beautiful. A sequence is considered beautiful if the difference between consecutive integers is either 1 or -1. Parameters: n (int): Number of elements in the sequence. sequence (list of int): The sequence of integers. Returns: str: "BEAUTIFUL" if the sequence is beautiful, otherwise "NOT BEAUTIFUL". for i in range(1, n): if abs(sequence[i] - sequence[i-1]) != 1: return "NOT BEAUTIFUL" return "BEAUTIFUL"

question:def max_passengers(n: int, stations: List[Tuple[int, int]]) -> int: Calculate the maximum number of passengers on the train at any point. Parameters: n (int): Number of stations. stations (list of tuple): List of tuples where each tuple contains two integers, passengers getting on and off the train at each station. Returns: int: The maximum number of passengers on the train at any point. Examples: >>> max_passengers(3, [(10, 0), (3, 5), (2, 8)]) 10 >>> max_passengers(4, [(15, 0), (0, 5), (3, 3), (7, 10)]) 15 from typing import List, Tuple def test_example1(): n = 3 stations = [(10, 0), (3, 5), (2, 8)] assert max_passengers(n, stations) == 10 def test_example2(): n = 4 stations = [(15, 0), (0, 5), (3, 3), (7, 10)] assert max_passengers(n, stations) == 15 def test_all_on_no_off(): n = 3 stations = [(5, 0), (10, 0), (15, 0)] assert max_passengers(n, stations) == 30 def test_all_off_no_on(): n = 2 stations = [(0, 5), (0, 10)] assert max_passengers(n, stations) == 0 def test_alternating_on_off(): n = 5 stations = [(5, 0), (0, 5), (10, 0), (0, 10), (20, 0)] assert max_passengers(n, stations) == 20 def test_large_numbers(): n = 4 stations = [(100, 0), (0, 100), (100, 0), (0, 100)] assert max_passengers(n, stations) == 100

answer:def max_passengers(n, stations): Calculate the maximum number of passengers on the train at any point. Parameters: n (int): Number of stations. stations (list of tuple): List of tuples where each tuple contains two integers, passengers getting on and off the train at each station. Returns: int: The maximum number of passengers on the train at any point. current_passengers = 0 max_passengers = 0 for on, off in stations: current_passengers += on current_passengers -= off max_passengers = max(max_passengers, current_passengers) return max_passengers

question:def search_in_circular_array(n: int, x: int, a: List[int]) -> int: Determines if the integer 'x' exists in the sorted circular array 'a'. Returns the index of 'x' if it exists, or -1 if it does not. >>> search_in_circular_array(5, 3, [3, 4, 5, 1, 2]) 0 >>> search_in_circular_array(6, 7, [5, 6, 7, 8, 9, 10]) 2 >>> search_in_circular_array(4, 12, [10, 11, 12, 3]) 2 >>> search_in_circular_array(4, 2, [10, 11, 12, 3]) -1

answer:def search_in_circular_array(n, x, a): Determines if the integer 'x' exists in the sorted circular array 'a'. Returns the index of 'x' if it exists, or -1 if it does not. for i in range(n): if a[i] == x: return i return -1

Released under the chat License.

has loaded