Appearance
question:A company wants to monitor user activity on their website. Each activity is characterized by an event type and a timestamp. The website developers have created a list of events, where each event is represented as a string in the format "event_type timestamp". The events are sorted chronologically by their timestamp. You are given a list of events and a specific event type. Your task is to find the timestamp of the first occurrence of this event type. Implement a function `find_first_event` that takes in the following parameters: - A list of strings `events`, where each string follows the format "event_type timestamp" (1 ≤ length of events ≤ 1000). - A string `event_type` which is the type of event you need to look for. The function should return the timestamp of the first occurrence of `event_type`. If the event type does not occur in the list, return "NOT FOUND". # Example Input: ``` events = ["login 2023-01-01T10:00:00", "logout 2023-01-01T10:15:00", "login 2023-01-01T10:30:00"] event_type = "login" ``` Output: ``` 2023-01-01T10:00:00 ``` Input: ``` events = ["login 2023-01-01T10:00:00", "logout 2023-01-01T10:15:00", "login 2023-01-01T10:30:00"] event_type = "signup" ``` Output: ``` NOT FOUND ``` # Note: - Timestamps follow the ISO 8601 format: "YYYY-MM-DDTHH:MM:SS". - The function should be case-sensitive; "Login" is different from "login". You may assume that timestamps are unique for simplicity.
answer:def find_first_event(events, event_type): Finds the first occurrence of the event_type and returns its timestamp. :param events: List of strings with each event in the format "event_type timestamp". :param event_type: String indicating the event type to look for. :return: Timestamp of the first occurrence of the specified event type or "NOT FOUND". for event in events: current_event_type, timestamp = event.split() if current_event_type == event_type: return timestamp return "NOT FOUND"
question:Ori and Sein are venturing through the Forbidden Grove where they encounter another challenge. To pass through the grove, they must collect certain ancient tokens hidden within n mystical flowers. However, each flower can only be collected during specific time intervals during which a flower blooms. Each flower has an associated interval [s_i, e_i] denoting the start and end bloom times respectively. Ori wants to gather all the tokens, and to do so, he must find the maximum number of non-overlapping time intervals such that he can collect as many tokens as possible without any two intervals overlapping in time. Given the time intervals of blooming for each of the n flowers, calculate the maximum number of non-overlapping intervals Ori can select to maximize his collection of tokens. # Input - The first line contains a single integer n (1 le n le 2 cdot 10^5) — the number of flowers. - The next n lines contain two integers each, s_i and e_i (1 le s_i lt e_i le 10^9) — the blooming start and end times of the i-th flower. # Output - Print a single integer — the maximum number of non-overlapping intervals Ori can select. # Example Input ``` 5 1 3 2 5 4 6 8 10 7 9 ``` Output ``` 3 ``` Explanation Ori can select intervals [1, 3], [4, 6], and [8, 10] to maximize the number of collected tokens without overlapping intervals.
answer:def max_non_overlapping_intervals(n, intervals): Finds the maximum number of non-overlapping intervals. Parameters: n (int): The number of intervals. intervals (list of tuples): List containing the intervals as (start, end) tuples. Returns: int: The maximum number of non-overlapping intervals. # Sort intervals by their end times intervals.sort(key=lambda x: x[1]) # Initialize the count of non-overlapping intervals count = 0 # The end time of the last added interval last_end_time = 0 for interval in intervals: if interval[0] > last_end_time: # if the start time is greater than the last end time count += 1 last_end_time = interval[1] return count
question:You are working on a project that involves generating secure passwords for users. A secure password must adhere to the following rules: - It must be at least 8 characters long. - It must contain at least one uppercase letter (A-Z). - It must contain at least one lowercase letter (a-z). - It must contain at least one digit (0-9). - It must contain at least one special character from the set (!@#%^&*()_+). Write a function `is_secure_password` that takes a string `password` as input and returns `True` if the password is secure according to the above rules, otherwise returns `False`. You may assume that the input will be a string with ASCII characters only. Example Usage: ``` print(is_secure_password("Aa1!aa1a")) # Output: True print(is_secure_password("Aa1aaaaa")) # Output: False (missing special character) print(is_secure_password("aaa1!aaa")) # Output: False (missing uppercase letter) ```
answer:def is_secure_password(password): Checks if the password is secure based on the following rules: - It must be at least 8 characters long. - It must contain at least one uppercase letter (A-Z). - It must contain at least one lowercase letter (a-z). - It must contain at least one digit (0-9). - It must contain at least one special character (!@#%^&*()_+). Args: password (str): The password to check. Returns: bool: True if the password is secure, False otherwise. if len(password) < 8: return False has_upper = any(c.isupper() for c in password) has_lower = any(c.islower() for c in password) has_digit = any(c.isdigit() for c in password) has_special = any(c in '!@#%^&*()_+' for c in password) return has_upper and has_lower and has_digit and has_special
question:Alice just learned about an interesting sorting algorithm called “CyclicSort”. Unlike traditional sorting algorithms, CyclicSort requires the array elements to be within a specific range: 1. The array should contain integers in the range [1, n] where n is the number of elements in the array. 2. Each integer from the range [1, n] should appear exactly once. The algorithm sorts the array such that the integer 1 appears at the index 0, 2 at index 1 and so on, following this pattern until the end of the array. The steps of the CyclicSort algorithm are as follows: - Iterate through the list. - If the element at the current index is not at the correct position (i.e., value should be at index `value - 1`), swap it with the element at its correct position. - Repeat the above step until the entire array is sorted. Given an unsorted array of size n which contains integers in the range [1, n], sort the array using the CyclicSort algorithm and return the sorted array. # Function Signature ```python def cyclic_sort(arr: List[int]) -> List[int]: pass ``` # Input - A single list of integers `arr` (1 ≤ len(arr) ≤ 100,000) with each integer in the range [1, len(arr)]. # Output - Return the sorted list. # Example ```python arr = [3, 1, 5, 4, 2] print(cyclic_sort(arr)) # Output: [1, 2, 3, 4, 5] arr = [2, 1, 3] print(cyclic_sort(arr)) # Output: [1, 2, 3] ```
answer:def cyclic_sort(arr): Sort the array using the CyclicSort algorithm. Parameters: arr (List[int]): List of integers in the range [1, n] Returns: List[int]: Sorted list i = 0 while i < len(arr): if arr[i] != arr[arr[i] - 1]: arr[arr[i] - 1], arr[i] = arr[i], arr[arr[i] - 1] else: i += 1 return arr