Appearance
question:import os import json from datetime import datetime TASKS_FILE = 'tasks.json' def load_tasks(): Load tasks from the task file. pass def save_tasks(tasks): Save tasks to the task file. pass def add_task(description, due_date): Add a new task with a description and due date. Args: description (str): The description of the task. due_date (str): Due date of the task in YYYY-MM-DD format. Returns: str: A message indicating the result of the operation. >>> add_task("Task 1", "2023-12-01") 'Task added successfully' >>> add_task("Task 2", "2023-31-12") 'Invalid date format, should be YYYY-MM-DD' pass def list_tasks(): List all tasks. Returns: List[dict]: A list of tasks. >>> list_tasks() [] pass def mark_task_completed(index): Mark a task as completed. Args: index (int): The index of the task to be marked as completed. Returns: str: A message indicating the result of the operation. >>> mark_task_completed(0) 'Task marked as completed' >>> mark_task_completed(100) 'Invalid task index' pass def delete_task(index): Delete a task. Args: index (int): The index of the task to be deleted. Returns: str: A message indicating the result of the operation. >>> delete_task(0) 'Task deleted successfully' >>> delete_task(100) 'Invalid task index' pass
answer:import json import os from datetime import datetime TASKS_FILE = 'tasks.json' def load_tasks(): if os.path.exists(TASKS_FILE): with open(TASKS_FILE, 'r') as file: return json.load(file) return [] def save_tasks(tasks): with open(TASKS_FILE, 'w') as file: json.dump(tasks, file) def add_task(description, due_date): try: datetime.strptime(due_date, '%Y-%m-%d') except ValueError: return "Invalid date format, should be YYYY-MM-DD" tasks = load_tasks() tasks.append({"description": description, "due_date": due_date, "completed": False}) save_tasks(tasks) return "Task added successfully" def list_tasks(): tasks = load_tasks() return tasks def mark_task_completed(index): tasks = load_tasks() if index < 0 or index >= len(tasks): return "Invalid task index" tasks[index]["completed"] = True save_tasks(tasks) return "Task marked as completed" def delete_task(index): tasks = load_tasks() if index < 0 or index >= len(tasks): return "Invalid task index" tasks.pop(index) save_tasks(tasks) return "Task deleted successfully"
question:from typing import List, Dict def word_count(words: List[str]) -> Dict[str, int]: Takes a list of strings as input and returns a dictionary where the keys are the unique words (case insensitive) and the values are the number of times each word appears in the list. Handles edge cases such as an empty list or a list with repeated words. >>> word_count(["apple", "Apple", "banana", "BANANA", "banana", "cherry"]) {'apple': 2, 'banana': 3, 'cherry': 1} >>> word_count([]) {} >>> word_count(["apple"]) {'apple': 1} >>> word_count(["apple", "apple", "apple"]) {'apple': 3} >>> word_count(["aPpLe", "ApPlE", "BANAnA", "bAnAna", "CHERRY", "cherRY"]) {'apple': 2, 'banana': 2, 'cherry': 2}
answer:def word_count(words): Returns a dictionary where the keys are unique words (case insensitive) and the values are the number of times each word appears in the input list. word_dict = {} for word in words: word_lower = word.lower() if word_lower in word_dict: word_dict[word_lower] += 1 else: word_dict[word_lower] = 1 return word_dict
question:from typing import List def frequency_description(int_list: List[int]) -> List[str]: Takes a list of integers and returns a list of strings describing the frequency of each integer in the format "number: count". The output list is sorted by the numbers in ascending order. >>> frequency_description([4, -1, -1, 2, 4, 0, 4]) == ["-1: 2", "0: 1", "2: 1", "4: 3"] >>> frequency_description([7, 7, 7, 7, 7]) == ["7: 5"] >>> frequency_description([1, -1, 0]) == ["-1: 1", "0: 1", "1: 1"] >>> frequency_description([]) == [] >>> frequency_description([-3, 0, 2, 0, -3, 2, 2]) == ["-3: 2", "0: 2", "2: 3"] >>> frequency_description([10]) == ["10: 1"]
answer:def frequency_description(int_list): Takes a list of integers and returns a list of strings describing the frequency of each integer in the format "number: count". The output list is sorted by the numbers in ascending order. :param int_list: List of integers :return: List of strings from collections import Counter # Get the frequency count of each integer in the list frequency_count = Counter(int_list) # Create a list of "number: count" strings sorted by number return [f"{num}: {count}" for num, count in sorted(frequency_count.items())]
question:def has_pair_with_sum(lst, target): Determines if there are any two distinct elements in a given list that add up to a specified target. :param lst: List of integers. :param target: Target sum to find within the list. :return: True if a pair exists that adds up to the target, False otherwise. >>> has_pair_with_sum([10, 15, 3, 7], 17) True >>> has_pair_with_sum([1, 2, 3, 9], 8) False >>> has_pair_with_sum([], 5) False >>> has_pair_with_sum([5], 5) False >>> has_pair_with_sum([-10, 20, 10, -30, -5], 15) True >>> has_pair_with_sum([5, 5, 5, 5, 10, 0], 10) True
answer:def has_pair_with_sum(lst, target): Determines if there are any two distinct elements in a given list that add up to a specified target. :param lst: List of integers. :param target: Target sum to find within the list. :return: True if a pair exists that adds up to the target, False otherwise. seen = set() for number in lst: if target - number in seen: return True seen.add(number) return False