Skip to content
🤔prompts chat🧠
🔍
question:Write a function that accepts a list of dictionaries, each representing a transaction. Each dictionary has keys: "id" (a unique integer), "amount" (a float), and "type" (either "debit" or "credit"). The function should return a new dictionary summarizing the transactions, with keys: "total_debit" (sum of all debit transactions), "total_credit" (sum of all credit transactions), "balance" (total credit minus total debit). You should write a self-contained function starting with: ```python def summarize_transactions(transactions): ```

answer:def summarize_transactions(transactions): This function accepts a list of dictionaries representing transactions and returns a summary dictionary with the total debit, total credit, and balance. total_debit = 0.0 total_credit = 0.0 for transaction in transactions: if transaction["type"] == "debit": total_debit += transaction["amount"] elif transaction["type"] == "credit": total_credit += transaction["amount"] balance = total_credit - total_debit return { "total_debit": total_debit, "total_credit": total_credit, "balance": balance }

question:You are tasked with writing a function called `highest_scorer()` that determines the student with the highest score from a dictionary of student names and their corresponding scores. This exercise will help you understand basic dictionary operations and comparison techniques in Python. Your function should: 1. Take a dictionary `students_scores` as input, where: - The keys are student names (strings). - The values are their corresponding scores (integers). 2. Identify the student with the highest score. 3. Return the name of the student with the highest score. If there are multiple students with the highest score, return any one of them. Example input: ```python students_scores = { "Alice": 88, "Bob": 95, "Charlie": 90, "David": 88, "Eve": 95 } ``` In the example above, the function could return either "Bob" or "Eve" since both have the highest score of 95. The function definition should be as follows: ```python def highest_scorer(students_scores): ... ```

answer:def highest_scorer(students_scores): Returns the name of the student with the highest score. If there are multiple students with the highest score, return any one of them. if not students_scores: return None max_score = max(students_scores.values()) for student, score in students_scores.items(): if score == max_score: return student

question:Create a function named `is_anagram(s1, s2)` that takes two string arguments `s1` and `s2` and returns `True` if the strings are anagrams of each other, and `False` otherwise. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. For example, "listen" and "silent" are anagrams. Your function should: 1. Ensure that the inputs `s1` and `s2` are of type `str`. If either input is not a string, raise a `RuntimeError` with the message "Both arguments must be strings". 2. Ignore the case when comparing the characters. 3. Return `False` if the lengths of the strings are not equal. Example: ```python print(is_anagram("listen", "silent")) # True print(is_anagram("Triangle", "Integral")) # True print(is_anagram("Apple", "Pabble")) # False ``` Use the following template for your solution: ```python def is_anagram(s1, s2): # Your implementation here ```

answer:def is_anagram(s1, s2): Returns True if s1 and s2 are anagrams, False otherwise. Parameters: s1 (str): The first string s2 (str): The second string Raises: RuntimeError: If either s1 or s2 is not a string. if not isinstance(s1, str) or not isinstance(s2, str): raise RuntimeError("Both arguments must be strings") # Normalize the strings to lower case and remove white spaces for comparison s1_normalized = sorted(s1.replace(" ", "").lower()) s2_normalized = sorted(s2.replace(" ", "").lower()) return s1_normalized == s2_normalized

question:The task requires you to write a function named `find_largest_island` that identifies the largest contiguous group of `1`s in a given 2D grid of `0`s and `1`s. The function should return the size of the largest island, where an island is defined as a group of horizontally or vertically connected `1`s. Diagonal connections are not considered. # Function Signature ```python def find_largest_island(grid: List[List[int]]) -> int: pass ``` # Requirements 1. Implement a search mechanism (either DFS or BFS) to explore islands in the grid. 2. Track the size of each island encountered during the search. 3. Ensure that visited cells are marked to avoid recounting. 4. Return the size of the largest island found. # Assumptions - The grid is a list of lists containing `0`s and `1`s. - The grid may not be rectangular, i.e., rows may have different lengths. - If there are no `1`s in the grid, the function should return `0`. # Example When `find_largest_island([[0, 1, 0], [1, 1, 0], [0, 0, 1]])` is called, it should return `3`, as the largest island is formed by three connected `1`s in the middle of the grid. # Constraints - The function should handle grids up to `100x100` efficiently.

answer:from typing import List def find_largest_island(grid: List[List[int]]) -> int: if not grid: return 0 rows = len(grid) cols = len(grid[0]) def dfs(r, c): if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == 0: return 0 grid[r][c] = 0 size = 1 size += dfs(r + 1, c) size += dfs(r - 1, c) size += dfs(r, c + 1) size += dfs(r, c - 1) return size max_size = 0 for r in range(rows): for c in range(cols): if grid[r][c] == 1: max_size = max(max_size, dfs(r, c)) return max_size

Released under the chat License.

has loaded