Appearance
question:A company wants to ensure that their employees have unique email addresses. An email address consists of a local part and a domain part separated by '@'. For this problem, we'll assume the following rules to determine the final form of an email address: 1. Any dots ('.') in the local part should be ignored. 2. Any character after a plus sign ('+') in the local part should be ignored. For example, for the email address "[email protected]": - "user.name+spam" is the local part. - Ignoring the dots, it becomes "username". - Ignoring everything after the plus sign, it remains "username". - The domain part remains "company.com". So, "[email protected]" is effectively "[email protected]". Given a list of email addresses, compute the number of unique email addresses based on the above rules. Input The first line contains an integer n (1 ≤ n ≤ 1000), the number of email addresses. The next n lines each contain an email address. Output Print a single integer representing the number of unique email addresses. Examples Input 3 [email protected] [email protected] [email protected] Output 2 Input 2 [email protected] [email protected] Output 1 Note In the first sample: - "[email protected]" becomes "[email protected]". - "[email protected]" becomes "[email protected]". - "[email protected]" becomes "[email protected]". "[email protected]" appears twice, and "[email protected]" appears once. Therefore, the number of unique email addresses is 2. In the second sample: - "[email protected]" is unique. - "[email protected]" is effectively the same as "[email protected]". So only one unique email address exists.
answer:def num_unique_emails(n, emails): Returns the number of unique email addresses given a list of emails. :param n: Number of email addresses (integer) :param emails: List of email addresses (strings) :return: Number of unique email addresses (integer) unique_emails = set() for email in emails: local, domain = email.split('@') local = local.split('+')[0].replace('.', '') unique_email = f"{local}@{domain}" unique_emails.add(unique_email) return len(unique_emails)
question:You are given a matrix of size NxM initially filled with zeros. Your task is to process a series of update operations and then output the resulting matrix. An update operation is described by four integers: r1, c1, r2, c2, where (r1, c1) is the top-left coordinate and (r2, c2) is the bottom-right coordinate of a submatrix within the NxM matrix. For each update operation, you need to increment by 1 every element within the specified submatrix range (inclusive). Implement a function to process the list of update operations and output the final matrix. # Input - The first line of input contains two integers N and M (1 ≤ N, M ≤ 500), the dimensions of the matrix. - The second line contains an integer K (1 ≤ K ≤ 10,000), the number of update operations. - The following K lines each contain four integers r1, c1, r2, c2 (1 ≤ r1 ≤ r2 ≤ N; 1 ≤ c1 ≤ c2 ≤ M), describing an update operation. # Output - Output the final N x M matrix after processing all K update operations. # Example Input ``` 3 3 2 1 1 2 2 2 2 3 3 ``` Output ``` 1 1 0 1 2 1 0 1 1 ``` # Notes - The matrix indexing is 1-based, which means the top-left corner of the matrix is (1, 1) and the bottom-right corner is (N, M). - Ensure that your solution is efficient enough to handle the upper constraint limits.
answer:def process_updates(N, M, K, updates): # Initialize the matrix with zeros matrix = [[0] * M for _ in range(N)] # Apply each update for update in updates: r1, c1, r2, c2 = update for i in range(r1 - 1, r2): for j in range(c1 - 1, c2): matrix[i][j] += 1 return matrix
question:Given an array of N positive integers and an integer X. The task is to rearrange the array such that all elements less than or equal to X come before all elements greater than X. The relative order of the elements should be maintained within the two partitions. Example 1: Input: arr = [1, 4, 2, 10, 5, 3] X = 4 Output: [1, 4, 2, 3, 10, 5] Explanation: Elements less than or equal to 4 are 1, 4, 2, 3. Elements greater than 4 are 10, 5. The relative order is maintained. Example 2: Input: arr = [7, 9, 5, 8, 1, 6] X = 6 Output: [5, 1, 6, 7, 9, 8] Explanation: Elements less than or equal to 6 are 5, 1, 6. Elements greater than 6 are 7, 9, 8. The relative order is maintained. Your Task: You don't need to read input or print anything. Complete the function rearrangeArray(arr, X) that takes the list arr and an integer X as parameters and returns the rearranged list. Constraints: 1 <= N <= 10^5 1 <= arr[i], X <= 10^7 Expected time complexity: O(N) Expected auxiliary space: O(1)
answer:def rearrangeArray(arr, X): Rearranges the array such that all elements <= X come before all elements > X. Relative order is maintained. :param arr: List of positive integers :param X: Integer to partition the array :return: Rearranged list less_than_equal = [] greater_than = [] for num in arr: if num <= X: less_than_equal.append(num) else: greater_than.append(num) return less_than_equal + greater_than
question:You are working on a text-based adventure game, and one of the key features is a dynamic inventory system that players can interact with. The inventory supports three main operations: 1. `add item_name weight`: Adds an item to the inventory with the given name and weight. If an item with the same name already exists, its weight is updated to the new weight. 2. `remove item_name`: Removes the item with the specified name from the inventory. If the item does not exist, the operation should return `ITEM_NOT_FOUND`. 3. `print_inventory`: Outputs all items in the inventory sorted by their names in ascending order. Each item should be printed in a new line in the format `item_name weight`. Your task is to implement the inventory system. Each operation will be given on a separate line, and the system should output the results of `remove` and `print_inventory` operations as specified. **Input** The first line of input contains a single integer `n` (1 ≤ n ≤ 100), representing the number of operations. The following `n` lines each contain one operation, which can be one of the three described above. **Output** - For `remove` operations, if the item does not exist in the inventory, output `ITEM_NOT_FOUND`. - For `print_inventory` operations, output the items in the specified format. **Example** Input ``` 6 add sword 10 add shield 15 remove helmet add potion 5 print_inventory remove sword ``` Output ``` ITEM_NOT_FOUND potion 5 shield 15 ```
answer:class Inventory: def __init__(self): self.items = {} def add(self, item_name, weight): self.items[item_name] = weight def remove(self, item_name): if item_name in self.items: del self.items[item_name] else: return "ITEM_NOT_FOUND" def print_inventory(self): for item_name in sorted(self.items.keys()): print(f"{item_name} {self.items[item_name]}") def process_operations(n, operations): inventory = Inventory() result = [] for operation in operations: parts = operation.split() command = parts[0] if command == "add": item_name = parts[1] weight = int(parts[2]) inventory.add(item_name, weight) elif command == "remove": item_name = parts[1] response = inventory.remove(item_name) if response: result.append(response) elif command == "print_inventory": output = "n".join(f"{item_name} {inventory.items[item_name]}" for item_name in sorted(inventory.items.keys())) result.append(output) return result