Appearance
question:Write a python function to analyze and redact sensitive information from a text document. As a field journalist, I often deal with documents that may contain sensitive personal information, such as phone numbers, email addresses, or social security numbers. I need a function that takes a string as input and replaces any detected sensitive information with '[REDACTED]'.Input: A string that may contain sensitive information.Output: A string with all detected sensitive information replaced with '[REDACTED]'.Example:```pythoninput_text = "Contact John at [email protected] or call him at 555-123-4567. His SSN is 123-45-6789."output_text = redact_sensitive_info(input_text)print(output_text)```Expected Output:```"Contact John at [REDACTED] or call him at [REDACTED]. His SSN is [REDACTED]."```Note: The function should be able to detect and redact email addresses, phone numbers (in the format xxx-xxx-xxxx), and social security numbers (in the format xxx-xx-xxxx).
answer:Here's the Python function to redact sensitive information from a text document:```pythonimport redef redact_sensitive_info(text): # Regular expressions to match sensitive information email_regex = r'b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b' phone_regex = r'bd{3}-d{3}-d{4}b' ssn_regex = r'bd{3}-d{2}-d{4}b' # Replace sensitive information with '[REDACTED]' redacted_text = re.sub(email_regex, '[REDACTED]', text) redacted_text = re.sub(phone_regex, '[REDACTED]', redacted_text) redacted_text = re.sub(ssn_regex, '[REDACTED]', redacted_text) return redacted_text```This function uses regular expressions to match email addresses, phone numbers, and social security numbers in the input text. The `re.sub` function is then used to replace the matched sensitive information with the string `'[REDACTED]'`.Here's how the function works:1. The regular expressions are defined to match the desired patterns: - `email_regex`: Matches email addresses in the format `[email protected]`. - `phone_regex`: Matches phone numbers in the format `xxx-xxx-xxxx`. - `ssn_regex`: Matches social security numbers in the format `xxx-xx-xxxx`.2. The `re.sub` function is used to replace the matched patterns with `'[REDACTED]'`: - `redacted_text = re.sub(email_regex, '[REDACTED]', text)`: Replaces email addresses with `'[REDACTED]'`. - `redacted_text = re.sub(phone_regex, '[REDACTED]', redacted_text)`: Replaces phone numbers with `'[REDACTED]'`. - `redacted_text = re.sub(ssn_regex, '[REDACTED]', redacted_text)`: Replaces social security numbers with `'[REDACTED]'`.3. The final `redacted_text` is returned, containing the input text with all sensitive information replaced by `'[REDACTED]'`.Note that this function assumes that the sensitive information follows the specified patterns. If the sensitive information is in a different format, you may need to modify the regular expressions accordingly.
question:Write a python function to calculate the average speed of a moving object given a list of tuples, where each tuple contains the time in seconds and the corresponding distance in meters covered at that time. The function should take this list as input and return the average speed in meters per second. Assume that the list is sorted by time and that the object starts from rest. If the list is empty or contains less than two points, the function should return 0.0.Input:- A list of tuples, e.g., `[(0, 0), (2, 10), (5, 25), (10, 45)]`Output:- A float representing the average speed.Example:```pythonaverage_speed([(0, 0), (2, 10), (5, 25), (10, 45)]) # Should return 4.5```
answer:def average_speed(points): if len(points) < 2: return 0.0 total_distance = 0 total_time = 0 prev_time, prev_distance = points[0] for time, distance in points[1:]: total_distance += distance - prev_distance total_time += time - prev_time prev_time, prev_distance = time, distance return total_distance / total_time
question:Write a python function to convert a list of military time strings into a more readable 12-hour format with AM/PM. The input will be a list of strings representing time in 24-hour format (HH:MM). The output should be a list of strings with each time converted to a 12-hour format followed by 'AM' or 'PM'. For example, given the input:```python["13:45", "00:30", "09:00", "23:59"]```The expected output should be:```python["1:45 PM", "12:30 AM", "9:00 AM", "11:59 PM"]```Please ensure that the function handles edge cases such as midnight ("00:00") and noon ("12:00") correctly.
answer:def convert_to_12hour_format(time_list): output = [] for time in time_list: hours, minutes = map(int, time.split(":")) if hours == 0: hours = 12 period = "AM" elif hours < 12: period = "AM" elif hours == 12: period = "PM" else: hours -= 12 period = "PM" output.append(f"{hours}:{minutes:02d} {period}") return output
question:Write a python function to calculate the average goals scored per game by a soccer team given a list of matches with the goals scored in each match. The function should take a list of integers as input, where each integer represents the number of goals scored by the team in a particular match. The output should be a float representing the average goals scored per game, rounded to two decimal places.Example:Input: [2, 1, 3, 0, 4]Output: 2.00Note: You can assume the list of matches is never empty.
answer:def calculate_avg_goals(matches): total_goals = sum(matches) num_games = len(matches) avg_goals = total_goals / num_games return round(avg_goals, 2)
question:Write a python function to analyze a list of stress-related scores collected from firefighters during a study. The function should take a list of integers as input, representing the stress scores recorded over a week. It should return a tuple containing three elements: the average stress score, the highest recorded stress score, and the lowest recorded stress score.Input: A list of integers where each integer represents a stress score. For example: `[70, 85, 90, 78, 88, 92, 80]`.Output: A tuple with three elements: 1. The average stress score as a float (rounded to 2 decimal places).2. The highest stress score as an integer.3. The lowest stress score as an integer.Example: Given the input `[70, 85, 90, 78, 88, 92, 80]`, the function should return `(83.29, 92, 70)`.
answer:def analyze_stress_scores(scores): total_score = sum(scores) num_scores = len(scores) average_score = round(total_score / num_scores, 2) highest_score = max(scores) lowest_score = min(scores) return (average_score, highest_score, lowest_score)
question:Write a python function to calculate the average price per square foot of a list of houses. Each house is represented by a dictionary with keys "price" (an integer, representing the price in dollars) and "square_feet" (an integer, representing the house size in square feet). The function should take a list of such dictionaries as input and return the average price per square foot as a float.Input: A list of dictionaries, where each dictionary has the keys "price" and "square_feet". For example:```pythonhouses = [ {"price": 750000, "square_feet": 1500}, {"price": 1200000, "square_feet": 2400}, {"price": 500000, "square_feet": 1000}]```Output: A float representing the average price per square foot across all the houses in the list.Example: Given the input above, the function should return `500.0`, since the total price is 2,450,000 and the total square footage is 4,900, resulting in an average of 500 dollars per square foot.
answer:def calculate_avg_price_per_sqft(houses): total_price = 0 total_sqft = 0 for house in houses: total_price += house["price"] total_sqft += house["square_feet"] if total_sqft == 0: return 0.0 else: return total_price / total_sqft