Appearance
question:You are given a list of N integers. Perform the following tasks: 1. Find the maximum and minimum elements of the list. 2. Compute the sum of all elements in the list. 3. Determine whether the list contains at least one pair of elements whose sum is even. Constraints * 1 ≤ N ≤ 10^5 * -10^9 ≤ elements of the list ≤ 10^9 Input * First line contains integer N, the length of the list. * Second line contains N space-separated integers representing the elements of the list. Output * First line should contain the maximum and minimum elements separated by a space. * Second line should contain the sum of the elements. * Third line should contain "YES" if there is at least one pair of elements whose sum is even, otherwise "NO". Example Input 5 1 2 3 4 5 Output 5 1 15 YES Input 4 -1 -2 -3 -4 Output -1 -4 -10 YES
answer:def process_list(numbers): Process the list of numbers for the given tasks: 1. Find the maximum and minimum elements. 2. Compute the sum of all elements. 3. Determine whether the list contains at least one pair of elements whose sum is even. max_element = max(numbers) min_element = min(numbers) total_sum = sum(numbers) even_count = sum(1 for num in numbers if num % 2 == 0) odd_count = len(numbers) - even_count sum_is_even = even_count > 1 or (even_count > 0 and odd_count > 0) result = (max_element, min_element, total_sum, "YES" if sum_is_even else "NO") return result
question:Mike is really interested in prime numbers and their properties. One of his interests is the sum of prime factors of a number. He defines the sum of prime factors as follows: For a given integer M, find all its prime factors and compute their sum. If a prime factor appears multiple times in the factorization of M, it should still only be counted once in the sum. Now, he wants you to write a program that can calculate the sum of prime factors for different numbers. Input The first line of the input contains an integer Q denoting the number of queries. The description of Q queries follows. Each of the next Q lines contains a single integer M. Output For each query, output a single line containing the sum of prime factors of the corresponding integer. Constraints 1 ≤ Q ≤ 100,000 2 ≤ M ≤ 1,000,000 Example Input: 3 12 18 45 Output: 5 5 8
answer:def sum_of_prime_factors(n): Returns the sum of prime factors of n. prime_factors = set() i = 2 while n % i == 0: prime_factors.add(i) n //= i i = 3 while i * i <= n: while n % i == 0: prime_factors.add(i) n //= i i += 2 if n > 1: prime_factors.add(n) return sum(prime_factors) def process_queries(queries): Takes a list of integers (queries) and returns a list of sums of prime factors of those integers. return [sum_of_prime_factors(m) for m in queries]
question:Given a list of strings, we define the "common character count" as the number of characters that appear in every string on the list. The same character could appear multiple times in different strings, but it needs to appear at least once in every string to be counted. Write a function that takes a list of strings and returns the common character count. Example: Input: ```python ["bella", "label", "roller"] ``` Output: 3 Explanation: The common characters among the strings are "e", "l", and "l". Each of these appears in every string in the input list. Input: ```python ["cool", "lock", "cook"] ``` Output: 2 Explanation: The common characters among the strings are "c" and "o". Each of these appears in every string in the input list. Function Signature: ```python def common_character_count(strings: List[str]) -> int: # your code here ```
answer:from typing import List from collections import Counter def common_character_count(strings: List[str]) -> int: if not strings: return 0 # Initialize with the character count of the first string common_chars = Counter(strings[0]) # Intersect the counts with each subsequent string for s in strings[1:]: common_chars &= Counter(s) # Summing the counts of common characters return sum(common_chars.values())
question:A retailer is organizing a clearance sale and offers a series of discounts on select products. Each product has a base price, and a sequence of discount percentages is applied to it sequentially. Can you write a program to calculate the final price of each product after all discounts are applied? Input Format The first line contains the number of test cases T, T lines follow. Each line contains an integer N (the number of discounts), followed by a float B (the base price of the product), and then N space-separated float values denoting the discount percentages. Output Format For each test case, print the final price of the product after applying all the discounts, rounded to two decimal places. Constraints 1 ≤ T ≤ 100 1 ≤ N ≤ 10 0 < B ≤ 10^5 0 ≤ discount percentages < 100 SAMPLE INPUT 2 1 100.0 20.0 2 200.0 10.0 25.0 SAMPLE OUTPUT 80.00 135.00 Explanation Case 1: A single discount of 20% is applied to the base price of 100.0, resulting in a final price of 80.00. Case 2: Two discounts, 10% and 25%, are applied sequentially to the base price of 200.0. The first discount reduces the price to 180.0, and the second discount further reduces it to 135.00.
answer:def final_price(num_discounts, base_price, discounts): Calculate the final price after applying a sequence of discounts. Args: num_discounts (int): The number of discounts. base_price (float): The base price of the product. discounts (list of float): The discounts to be applied. Returns: float: The final price rounded to two decimal places. final_price = base_price for discount in discounts: final_price -= (final_price * discount / 100) return round(final_price, 2) def calculate_final_prices(test_cases): Calculate the final prices for a list of test cases. Args: test_cases (list of tuples): Each tuple contains the number of discounts, base price, and a list of discounts. Returns: list of float: The final prices for each test case. results = [] for case in test_cases: num_discounts, base_price, discounts = case results.append(final_price(num_discounts, base_price, discounts)) return results